This repository was archived by the owner on Jun 20, 2019. It is now read-only.
forked from phoboslab/JavaScriptCore-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSContext.h
More file actions
133 lines (113 loc) · 6.08 KB
/
JSContext.h
File metadata and controls
133 lines (113 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef JSContext_h
#define JSContext_h
#include <JavaScriptCore/JavaScript.h>
#if JSC_OBJC_API_ENABLED
@class JSVirtualMachine, JSValue;
// An instance of JSContext represents a JavaScript execution environment. All
// JavaScript execution takes place within a context.
// JSContext is also used to manage the life-cycle of objects within the
// JavaScript virtual machine. Every instance of JSValue is associated with a
// JSContext via a strong reference. The JSValue will keep the JSContext it
// references alive so long as the JSValue remains alive. When all of the JSValues
// that reference a particular JSContext have been deallocated the JSContext
// will be deallocated unless it has been previously retained.
#ifndef JSC_OBJC_API_AVAILABLE_MAC_OS_X_1080
NS_CLASS_AVAILABLE(10_9, 7_0)
#else
OBJC_VISIBLE
#endif
@interface JSContext : NSObject
// Create a JSContext.
- (id)init;
// Create a JSContext in the specified virtual machine.
- (id)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine;
// Evaluate a string of JavaScript code.
- (JSValue *)evaluateScript:(NSString *)script;
// This method retrieves the global object of the JavaScript execution context.
// Instances of JSContext originating from WebKit will return a reference to the
// WindowProxy object.
- (JSValue *)globalObject;
// This method may be called from within an Objective-C block or method invoked
// as a callback from JavaScript to retrieve the callback's context. Outside of
// a callback from JavaScript this method will return nil.
+ (JSContext *)currentContext;
// This method may be called from within an Objective-C block or method invoked
// as a callback from JavaScript to retrieve the callback's this value. Outside
// of a callback from JavaScript this method will return nil.
+ (JSValue *)currentThis;
// This method may be called from within an Objective-C block or method invoked
// as a callback from JavaScript to retrieve the callback's arguments, objects
// in the returned array are instances of JSValue. Outside of a callback from
// JavaScript this method will return nil.
+ (NSArray *)currentArguments;
// The "exception" property may be used to throw an exception to JavaScript.
// Before a callback is made from JavaScript to an Objective-C block or method,
// the prior value of the exception property will be preserved and the property
// will be set to nil. After the callback has completed the new value of the
// exception property will be read, and prior value restored. If the new value
// of exception is not nil, the callback will result in that value being thrown.
// This property may also be used to check for uncaught exceptions arising from
// API function calls (since the default behaviour of "exceptionHandler" is to
// assign an uncaught exception to this property).
// If a JSValue originating from a different JSVirtualMachine than this context
// is assigned to this property, an Objective-C exception will be raised.
@property(retain) JSValue *exception;
// If a call to an API function results in an uncaught JavaScript exception, the
// "exceptionHandler" block will be invoked. The default implementation for the
// exception handler will store the exception to the exception property on
// context. As a consequence the default behaviour is for unhandled exceptions
// occurring within a callback from JavaScript to be rethrown upon return.
// Setting this value to nil will result in all uncaught exceptions thrown from
// the API being silently consumed.
@property(copy) void(^exceptionHandler)(JSContext *context, JSValue *exception);
// All instances of JSContext are associated with a single JSVirtualMachine. The
// virtual machine provides an "object space" or set of execution resources.
@property(readonly, retain) JSVirtualMachine *virtualMachine;
@end
// Instances of JSContext implement the following methods in order to enable
// support for subscript access by key and index, for example:
//
// JSContext *context;
// JSValue *v = context[@"X"]; // Get value for "X" from the global object.
// context[@"Y"] = v; // Assign 'v' to "Y" on the global object.
//
// An object key passed as a subscript will be converted to a JavaScript value,
// and then the value converted to a string used to resolve a property of the
// global object.
@interface JSContext(SubscriptSupport)
- (JSValue *)objectForKeyedSubscript:(id)key;
- (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key;
@end
// These functions are for bridging between the C API and the Objective-C API.
@interface JSContext(JSContextRefSupport)
// Creates a JSContext, wrapping its C API counterpart.
+ (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)jsGlobalContextRef;
// Returns the C API counterpart wrapped by a JSContext.
- (JSGlobalContextRef)JSGlobalContextRef;
@end
#endif
#endif // JSContext_h