Oolite
Loading...
Searching...
No Matches
OOJSFunction Class Reference

#include <OOJSFunction.h>

Inheritance diagram for OOJSFunction:
Collaboration diagram for OOJSFunction:

Instance Methods

(id) - initWithFunction:context:
(id) - initWithName:scope:code:argumentCount:argumentNames:fileName:lineNumber:context:
(NSString *) - name
(JSFunction *) - function
(jsval) - functionValue
(BOOL) - evaluateWithContext:scope:argc:argv:result:
(id) - evaluateWithContext:scope:arguments:
(BOOL) - evaluatePredicateWithContext:scope:arguments:
(id) - initWithFunction:context:
(id) - initWithName:scope:code:argumentCount:argumentNames:fileName:lineNumber:context:
(NSString *) - name
(JSValueConst) - function
(JSValue) - functionValue
(BOOL) - evaluateWithContext:scope:argc:argv:result:
(id) - evaluateWithContext:scope:arguments:
(BOOL) - evaluatePredicateWithContext:scope:arguments:
(void) - deleteJSValue [implementation]
(void) - dealloc [implementation]
(NSString *) - descriptionComponents [implementation]
(BOOL) - evaluateWithContext:scope:arguments:result: [implementation]
(BOOL) - isValid [implementation]
(BOOL) - evaluateWithContext:scope:arguments:result: [implementation]

Private Attributes

JSFunction * _function
NSString * _name
JSValue _function

Detailed Description

Definition at line 31 of file OOJSFunction.h.

Method Documentation

◆ dealloc

- (void) dealloc
implementation

Definition at line 128 of file OOJSFunction.m.

129{
130 [self deleteJSValue];
131 DESTROY(_name);
132
133 [super dealloc];
134}
#define DESTROY(x)
NSString * _name

References _name, dealloc, deleteJSValue, and DESTROY.

Referenced by dealloc.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ deleteJSValue

- (void) deleteJSValue
implementation

Definition at line 114 of file OOJSFunction.m.

115{
116 if (_function != NULL) {
117 JSContext* context = OOJSAcquireContext();
118 JS_RemoveObjectRoot(context, (JSObject**)&_function);
119 OOJSRelinquishContext(context);
120
121 _function = NULL;
122 [[NSNotificationCenter defaultCenter] removeObserver:self
123 name:kOOJavaScriptEngineWillResetNotification
125 }
126}
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
JSFunction * _function
OOJavaScriptEngine * sharedEngine()

References _function, deleteJSValue, OOJSAcquireContext(), OOJSRelinquishContext(), and OOJavaScriptEngine::sharedEngine.

Referenced by dealloc, and deleteJSValue.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Definition at line 136 of file OOJSFunction.m.

137{
138 NSString* name = [self name];
139 if (name == nil)
140 name = @"<anonymous>";
141 return [NSString stringWithFormat:@"%@()", name];
142}
return nil
NSString * name()

References descriptionComponents, name, and nil.

Referenced by descriptionComponents.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ evaluatePredicateWithContext:scope:arguments: [1/2]

- (BOOL) evaluatePredicateWithContext: (JSContext*) context
scope: (id) jsThis
arguments: (NSArray*) arguments 

Definition at line 306 of file OOJSFunction.m.

306 :(JSContext*)context
307 scope:(id)jsThis
308 arguments:(NSArray*)arguments
309{
310 JSValue result;
311 BOOL OK = [self evaluateWithContext:context
312 scope:jsThis
313 arguments:arguments
314 result:&result];
315 int retval = NO;
316 if (OK) {
317 retval = JS_ToBool(context, result);
318 JS_FreeValue(context, result);
319 if (retval < 0) {
321 OK = NO;
322 }
323 }
324 return OK && retval;
325}
OOINLINE void OOJSReportPendingException(JSContext *context)

References evaluateWithContext:scope:arguments:result:, and OOJSReportPendingException().

Referenced by OORegExpMatcher::string:matchesExpression:flags:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ evaluatePredicateWithContext:scope:arguments: [2/2]

- (BOOL) evaluatePredicateWithContext: (JSContext *) context
scope: (id) jsThis
arguments: (NSArray *) arguments 

◆ evaluateWithContext:scope:argc:argv:result: [1/2]

- (BOOL) evaluateWithContext: (JSContext*) context
scope: (JSObject*) jsThis
argc: (uintN) argc
argv: (jsval*) argv
result: (jsval*) result 

Definition at line 202 of file OOJSFunction.m.

202 :(JSContext*)context
203 scope:(JSValueConst)jsThis
204 argc:(int)argc
205 argv:(JSValueConst*)argv
206 result:(JSValue*)result
207{
208 NSParameterAssert(result != NULL);
209
210 if (EXPECT_NOT(![self isValid])) {
211 *result = JS_UNDEFINED;
212 return NO;
213 }
214
215 // As with JS_CallFunction(), a missing this means the global object.
216 JSValue global = JS_UNDEFINED;
217 if (!JS_IsObject(jsThis)) {
218 global = JS_GetGlobalObject(context);
219 jsThis = global;
220 }
221
224 JSValue rval = JS_Call(context, _function, jsThis, argc, argv);
226 [OOJSScript popScript:nil];
227
228 JS_FreeValue(context, global);
229
230 if (JS_IsException(rval)) {
231 *result = JS_UNDEFINED;
232 return NO;
233 }
234 *result = rval;
235 return YES;
236}
#define EXPECT_NOT(x)
#define OOJSStopTimeLimiter()
#define OOJSStartTimeLimiter()
void pushScript:(OOJSScript *script)
Definition OOJSScript.m:480
void popScript:(OOJSScript *script)
Definition OOJSScript.m:493
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque

References _function, EXPECT_NOT, isValid, OOJSStartTimeLimiter, OOJSStopTimeLimiter, OOJSScript::popScript:, and OOJSScript::pushScript:.

Referenced by evaluateWithContext:scope:arguments:result:, and evaluateWithContext:scope:arguments:result:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ evaluateWithContext:scope:argc:argv:result: [2/2]

- (BOOL) evaluateWithContext: (JSContext*) context
scope: (JSValueConst) jsThis
argc: (int) argc
argv: (JSValueConst*) argv
result: (JSValue*) result 

Definition at line 202 of file OOJSFunction.m.

202 :(JSContext*)context
203 scope:(JSValueConst)jsThis
204 argc:(int)argc
205 argv:(JSValueConst*)argv
206 result:(JSValue*)result
207{
208 NSParameterAssert(result != NULL);
209
210 if (EXPECT_NOT(![self isValid])) {
211 *result = JS_UNDEFINED;
212 return NO;
213 }
214
215 // As with JS_CallFunction(), a missing this means the global object.
216 JSValue global = JS_UNDEFINED;
217 if (!JS_IsObject(jsThis)) {
218 global = JS_GetGlobalObject(context);
219 jsThis = global;
220 }
221
224 JSValue rval = JS_Call(context, _function, jsThis, argc, argv);
226 [OOJSScript popScript:nil];
227
228 JS_FreeValue(context, global);
229
230 if (JS_IsException(rval)) {
231 *result = JS_UNDEFINED;
232 return NO;
233 }
234 *result = rval;
235 return YES;
236}

◆ evaluateWithContext:scope:arguments: [1/2]

- (id) evaluateWithContext: (JSContext*) context
scope: (id) jsThis
arguments: (NSArray*) arguments 

Definition at line 289 of file OOJSFunction.m.

289 :(JSContext*)context
290 scope:(id)jsThis
291 arguments:(NSArray*)arguments
292{
293 JSValue result;
294 BOOL OK = [self evaluateWithContext:context
295 scope:jsThis
296 arguments:arguments
297 result:&result];
298 if (!OK)
299 return nil;
300
301 id object = OOJSNativeObjectFromJSValue(context, result);
302 JS_FreeValue(context, result);
303 return object;
304}
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)

References evaluateWithContext:scope:arguments:result:, nil, and OOJSNativeObjectFromJSValue().

Here is the call graph for this function:

◆ evaluateWithContext:scope:arguments: [2/2]

- (id) evaluateWithContext: (JSContext *) context
scope: (id) jsThis
arguments: (NSArray *) arguments 

◆ evaluateWithContext:scope:arguments:result: [1/2]

- (BOOL) evaluateWithContext: (JSContext*) context
scope: (id) jsThis
arguments: (NSArray*) arguments
result: (jsval*) result 
implementation

Definition at line 179 of file OOJSFunction.m.

179 :(JSContext*)context
180 scope:(id)jsThis
181 arguments:(NSArray*)arguments
182 result:(jsval*)result
183{
184 NSUInteger i, argc = [arguments count];
185 assert(argc < UINT32_MAX);
186 jsval argv[argc];
187
188 for (i = 0; i < argc; i++) {
189 argv[i] = [[arguments objectAtIndex:i] oo_jsValueInContext:context];
190 OOJSAddGCValueRoot(context, &argv[i], "OOJSFunction argv");
191 }
192
193 JSObject* scopeObj = NULL;
194 BOOL OK = YES;
195 if (jsThis != nil)
196 OK = JS_ValueToObject(context, [jsThis oo_jsValueInContext:context], &scopeObj);
197 if (OK)
198 OK = [self evaluateWithContext:context
199 scope:scopeObj
200 argc:(uint32_t)argc
201 argv:argv
202 result:result];
203
204 for (i = 0; i < argc; i++) {
205 JS_RemoveValueRoot(context, &argv[i]);
206 }
207
208 return OK;
209}
#define OOJSAddGCValueRoot(context, root, name)

References evaluateWithContext:scope:argc:argv:result:, nil, and OOJSAddGCValueRoot.

Referenced by evaluatePredicateWithContext:scope:arguments:, and evaluateWithContext:scope:arguments:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ evaluateWithContext:scope:arguments:result: [2/2]

- (BOOL) evaluateWithContext: (JSContext*) context
scope: (id) jsThis
arguments: (NSArray*) arguments
result: (JSValue*) result 
implementation

Definition at line 239 of file OOJSFunction.m.

239 :(JSContext*)context
240 scope:(id)jsThis
241 arguments:(NSArray*)arguments
242 result:(JSValue*)result
243{
244 NSUInteger i, argc = [arguments count];
245 assert(argc < INT_MAX);
246 JSValue argv[argc + 1]; // + 1 avoids a zero-length array.
247
248 for (i = 0; i < argc; i++) {
249 argv[i] = [[arguments objectAtIndex:i] oo_jsValueInContext:context];
250 }
251
252 JSValue scopeObj = JS_UNDEFINED;
253 BOOL OK = YES;
254 if (jsThis != nil) {
255 JSValue thisValue = [jsThis oo_jsValueInContext:context];
256 // As with JS_ValueToObject(), null and undefined give no object (the global object is used).
257 if (!JS_IsNull(thisValue) && !JS_IsUndefined(thisValue)) {
258 scopeObj = JS_ToObject(context, thisValue);
259 if (JS_IsException(scopeObj)) {
260 scopeObj = JS_UNDEFINED;
261 OK = NO;
262 }
263 }
264 JS_FreeValue(context, thisValue);
265 }
266 if (OK)
267 OK = [self evaluateWithContext:context
268 scope:scopeObj
269 argc:(int)argc
270 argv:argv
271 result:result];
272 if (!OK) {
273 /* SpiderMonkey reported uncaught exceptions automatically when
274 control returned from JS; QuickJS leaves them pending.
275 */
276 if (JS_HasException(context))
278 *result = JS_UNDEFINED;
279 }
280
281 JS_FreeValue(context, scopeObj);
282 for (i = 0; i < argc; i++) {
283 JS_FreeValue(context, argv[i]);
284 }
285
286 return OK;
287}

References evaluateWithContext:scope:argc:argv:result:, nil, and OOJSReportPendingException().

Here is the call graph for this function:

◆ function [1/2]

- (JSValueConst) function

Definition at line 188 of file OOJSFunction.m.

189{
190 return [self isValid] ? _function : JS_NULL;
191}

References _function, function, and isValid.

Referenced by function.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ function [2/2]

- (JSValueConst) function

References function.

Here is the call graph for this function:

◆ functionValue [1/2]

- (JSValue) functionValue

Definition at line 193 of file OOJSFunction.m.

194{
195 if (EXPECT([self isValid])) {
196 return JS_DupValue(OOJSAcquireContext(), _function);
197 } else {
198 return JS_NULL;
199 }
200}
#define EXPECT(x)

References _function, EXPECT, functionValue, isValid, and OOJSAcquireContext().

Referenced by functionValue.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ functionValue [2/2]

- (JSValue) functionValue

References functionValue.

Here is the call graph for this function:

◆ initWithFunction:context: [1/2]

- (id) initWithFunction: (JSFunction*) function
context: (JSContext*) context 

Definition at line 32 of file OOJSFunction.m.

32 :(JSValueConst)function context:(JSContext*)context
33{
34 NSParameterAssert(context != NULL);
35
36 if (!JS_IsFunction(context, function)) {
37 [self release];
38 return nil;
39 }
40
41 if ((self = [super init])) {
42 _function = JS_DupValue(context, function);
43
44 // QuickJS has no JS_GetFunctionId(); use the function's name property.
45 JSValue nameValue = JS_GetPropertyStr(context, function, "name");
46 if (JS_IsException(nameValue)) {
47 JS_FreeValue(context, JS_GetException(context));
48 } else {
49 NSString* name = OOStringFromJSValue(context, nameValue);
50 if ([name length] != 0)
51 _name = [name retain];
52 }
53 JS_FreeValue(context, nameValue);
54
55 [[NSNotificationCenter defaultCenter] addObserver:self
56 selector:@selector(deleteJSValue)
57 name:kOOJavaScriptEngineWillResetNotification
59 }
60
61 return self;
62}
NSString * OOStringFromJSValue(JSContext *context, jsval value)

References _function, _name, name, nil, OOStringFromJSValue(), and OOJavaScriptEngine::sharedEngine.

Referenced by initWithName:scope:code:argumentCount:argumentNames:fileName:lineNumber:context:.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initWithFunction:context: [2/2]

- (id) initWithFunction: (JSValueConst) function
context: (JSContext*) context 

Definition at line 32 of file OOJSFunction.m.

32 :(JSValueConst)function context:(JSContext*)context
33{
34 NSParameterAssert(context != NULL);
35
36 if (!JS_IsFunction(context, function)) {
37 [self release];
38 return nil;
39 }
40
41 if ((self = [super init])) {
42 _function = JS_DupValue(context, function);
43
44 // QuickJS has no JS_GetFunctionId(); use the function's name property.
45 JSValue nameValue = JS_GetPropertyStr(context, function, "name");
46 if (JS_IsException(nameValue)) {
47 JS_FreeValue(context, JS_GetException(context));
48 } else {
49 NSString* name = OOStringFromJSValue(context, nameValue);
50 if ([name length] != 0)
51 _name = [name retain];
52 }
53 JS_FreeValue(context, nameValue);
54
55 [[NSNotificationCenter defaultCenter] addObserver:self
56 selector:@selector(deleteJSValue)
57 name:kOOJavaScriptEngineWillResetNotification
59 }
60
61 return self;
62}

◆ initWithName:scope:code:argumentCount:argumentNames:fileName:lineNumber:context: [1/2]

- (id) initWithName: (NSString*) name
scope: (JSObject*) scope
code: (NSString*) code
argumentCount: (NSUInteger) argCount
argumentNames: (const char**) argNames
fileName: (NSString*) fileName
lineNumber: (NSUInteger) lineNumber
context: (JSContext*) context 

Definition at line 64 of file OOJSFunction.m.

64 :(NSString*)name
65 scope:(JSValueConst)scope
66 code:(NSString*)code
67 argumentCount:(NSUInteger)argCount
68 argumentNames:(const char**)argNames
69 fileName:(NSString*)fileName
70 lineNumber:(NSUInteger)lineNumber
71 context:(JSContext*)context
72{
73 BOOL OK = YES;
74 BOOL releaseContext = NO;
75 JSValue function = JS_UNDEFINED;
76
77 if (context == NULL) {
78 context = OOJSAcquireContext();
79 releaseContext = YES;
80 }
81
82 /* SpiderMonkey's JS_CompileUCFunction() could compile the function in an
83 arbitrary scope object. QuickJS has no equivalent, so the function is
84 always compiled in the global scope. (The only caller passed NULL,
85 meaning the global object, anyway.)
86 */
87 (void)scope;
88
89 if (code == nil || (argCount > 0 && argNames == NULL))
90 OK = NO;
91
92 if (OK) {
93 assert(argCount < INT_MAX && lineNumber < INT_MAX);
94
95 /* QuickJS has no API to compile a function from a body and argument
96 names, so evaluate a function expression. The wrapper is kept on
97 the first line so that line numbers in the body are unchanged.
98 */
99 NSMutableString* source = [NSMutableString stringWithString:@"(function("];
100 NSUInteger i;
101 for (i = 0; i < argCount; i++) {
102 if (i != 0)
103 [source appendString:@","];
104 [source appendString:[NSString stringWithUTF8String:argNames[i]]];
105 }
106 [source appendFormat:@"){%@\n})", code];
107
108 NSData* utf8 = [source dataUsingEncoding:NSUTF8StringEncoding];
109 NSMutableData* buffer = [NSMutableData dataWithData:utf8];
110 [buffer appendBytes:"" length:1]; // JS_Eval2() requires a NUL-terminated buffer.
111
112 JSEvalOptions options = {
113 .version = JS_EVAL_OPTIONS_VERSION,
114 .eval_flags = JS_EVAL_TYPE_GLOBAL,
115 .filename = [fileName UTF8String] ?: "<anonymous>",
116 .line_num = (int)lineNumber
117 };
118 function = JS_Eval2(context, [buffer bytes], [utf8 length], &options);
119 if (JS_IsException(function)) {
121 function = JS_UNDEFINED;
122 OK = NO;
123 } else if (!JS_IsFunction(context, function)) {
124 OK = NO;
125 }
126 }
127
128 if (OK && name != nil) {
129 // Function expression names are read-only but configurable; redefine it.
130 JS_DefinePropertyValueStr(context, function, "name", [name oo_jsValueInContext:context], JS_PROP_CONFIGURABLE);
131 }
132
133 if (OK) {
134 self = [self initWithFunction:function context:context];
135 } else {
136 DESTROY(self);
137 }
138 JS_FreeValue(context, function);
139
140 if (releaseContext)
141 OOJSRelinquishContext(context);
142
143 return self;
144}

References DESTROY, initWithFunction:context:, int(), name, nil, OOJSAcquireContext(), OOJSRelinquishContext(), and OOJSReportPendingException().

Here is the call graph for this function:

◆ initWithName:scope:code:argumentCount:argumentNames:fileName:lineNumber:context: [2/2]

- (id) initWithName: (NSString*) name
scope: (JSValueConst) scope
code: (NSString*) code
argumentCount: (NSUInteger) argCount
argumentNames: (const char**) argNames
fileName: (NSString*) fileName
lineNumber: (NSUInteger) lineNumber
context: (JSContext*) context 

Definition at line 64 of file OOJSFunction.m.

64 :(NSString*)name
65 scope:(JSValueConst)scope
66 code:(NSString*)code
67 argumentCount:(NSUInteger)argCount
68 argumentNames:(const char**)argNames
69 fileName:(NSString*)fileName
70 lineNumber:(NSUInteger)lineNumber
71 context:(JSContext*)context
72{
73 BOOL OK = YES;
74 BOOL releaseContext = NO;
75 JSValue function = JS_UNDEFINED;
76
77 if (context == NULL) {
78 context = OOJSAcquireContext();
79 releaseContext = YES;
80 }
81
82 /* SpiderMonkey's JS_CompileUCFunction() could compile the function in an
83 arbitrary scope object. QuickJS has no equivalent, so the function is
84 always compiled in the global scope. (The only caller passed NULL,
85 meaning the global object, anyway.)
86 */
87 (void)scope;
88
89 if (code == nil || (argCount > 0 && argNames == NULL))
90 OK = NO;
91
92 if (OK) {
93 assert(argCount < INT_MAX && lineNumber < INT_MAX);
94
95 /* QuickJS has no API to compile a function from a body and argument
96 names, so evaluate a function expression. The wrapper is kept on
97 the first line so that line numbers in the body are unchanged.
98 */
99 NSMutableString* source = [NSMutableString stringWithString:@"(function("];
100 NSUInteger i;
101 for (i = 0; i < argCount; i++) {
102 if (i != 0)
103 [source appendString:@","];
104 [source appendString:[NSString stringWithUTF8String:argNames[i]]];
105 }
106 [source appendFormat:@"){%@\n})", code];
107
108 NSData* utf8 = [source dataUsingEncoding:NSUTF8StringEncoding];
109 NSMutableData* buffer = [NSMutableData dataWithData:utf8];
110 [buffer appendBytes:"" length:1]; // JS_Eval2() requires a NUL-terminated buffer.
111
112 JSEvalOptions options = {
113 .version = JS_EVAL_OPTIONS_VERSION,
114 .eval_flags = JS_EVAL_TYPE_GLOBAL,
115 .filename = [fileName UTF8String] ?: "<anonymous>",
116 .line_num = (int)lineNumber
117 };
118 function = JS_Eval2(context, [buffer bytes], [utf8 length], &options);
119 if (JS_IsException(function)) {
121 function = JS_UNDEFINED;
122 OK = NO;
123 } else if (!JS_IsFunction(context, function)) {
124 OK = NO;
125 }
126 }
127
128 if (OK && name != nil) {
129 // Function expression names are read-only but configurable; redefine it.
130 JS_DefinePropertyValueStr(context, function, "name", [name oo_jsValueInContext:context], JS_PROP_CONFIGURABLE);
131 }
132
133 if (OK) {
134 self = [self initWithFunction:function context:context];
135 } else {
136 DESTROY(self);
137 }
138 JS_FreeValue(context, function);
139
140 if (releaseContext)
141 OOJSRelinquishContext(context);
142
143 return self;
144}

◆ isValid

- (BOOL) isValid
implementation

Definition at line 182 of file OOJSFunction.m.

183{
184 // _function is zero-filled (the integer 0) until set, and JS_NULL after reset.
185 return JS_IsObject(_function);
186}

References _function, and isValid.

Referenced by evaluateWithContext:scope:argc:argv:result:, function, functionValue, and isValid.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ name [1/2]

- (NSString *) name

Definition at line 177 of file OOJSFunction.m.

178{
179 return _name;
180}

References _name, and name.

Referenced by descriptionComponents, initWithFunction:context:, initWithName:scope:code:argumentCount:argumentNames:fileName:lineNumber:context:, and name.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ name [2/2]

- (NSString *) name

References name.

Here is the call graph for this function:

Member Data Documentation

◆ _function [1/2]

- (JSFunction*) _function
private

◆ _function [2/2]

- (JSValue) _function
private

Definition at line 33 of file OOJSFunction.h.

◆ _name

- (NSString *) _name
private

Definition at line 34 of file OOJSFunction.h.

Referenced by dealloc, initWithFunction:context:, and name.


The documentation for this class was generated from the following files: