33- (id) initWithFunction:(JSFunction *)function context:(JSContext *)context
35 NSParameterAssert(context != NULL);
43 if ((
self = [super init]))
47 _name = [OOStringFromJSString(context, JS_GetFunctionId(function)) retain];
49 [[NSNotificationCenter defaultCenter] addObserver:self
50 selector:@selector(deleteJSValue)
51 name:kOOJavaScriptEngineWillResetNotification
59- (id) initWithName:(NSString *)name
60 scope:(JSObject *)scope
62 argumentCount:(NSUInteger)argCount
63 argumentNames:(const
char **)argNames
64 fileName:(NSString *)fileName
65 lineNumber:(NSUInteger)lineNumber
66 context:(JSContext *)context
69 BOOL releaseContext = NO;
70 jschar *buffer = NULL;
81 if (code ==
nil || (argCount > 0 && argNames == NULL)) OK = NO;
86 assert(
sizeof(jschar) ==
sizeof(unichar));
88 length = [code length];
89 buffer = malloc(
sizeof(jschar) * length);
90 if (buffer == NULL) OK = NO;
95 assert(argCount < UINT32_MAX);
97 [code getCharacters:buffer];
99 function = JS_CompileUCFunction(context, scope, [
name UTF8String], (uint32_t)argCount, argNames, buffer, length, [fileName UTF8String], (uint32_t)lineNumber);
100 if (
function == NULL) OK = NO;
107 self = [
self initWithFunction:function context:context];
125 JS_RemoveObjectRoot(context, (JSObject **)&
_function);
129 [[NSNotificationCenter defaultCenter] removeObserver:self
130 name:kOOJavaScriptEngineWillResetNotification
138 [
self deleteJSValue];
147 NSString *
name = [
self name];
149 return [NSString stringWithFormat:@"%@()", name];
169 return OBJECT_TO_JSVAL(JS_GetFunctionObject(
_function));
179- (BOOL) evaluateWithContext:(JSContext *)context
180 scope:(JSObject *)jsThis
183 result:(jsval *)result
187 BOOL OK = JS_CallFunction(context, jsThis,
_function, argc, argv, result);
195- (BOOL) evaluateWithContext:(JSContext *)context
197 arguments:(NSArray *)arguments
198 result:(jsval *)result
200 NSUInteger i, argc = [arguments count];
201 assert(argc < UINT32_MAX);
204 for (i = 0; i < argc; i++)
206 argv[i] = [[arguments objectAtIndex:i] oo_jsValueInContext:context];
210 JSObject *scopeObj = NULL;
212 if (jsThis !=
nil) OK = JS_ValueToObject(context, [jsThis oo_jsValueInContext:context], &scopeObj);
213 if (OK) OK = [
self evaluateWithContext:context
219 for (i = 0; i < argc; i++)
221 JS_RemoveValueRoot(context, &argv[i]);
228- (id) evaluateWithContext:(JSContext *)context
230 arguments:(NSArray *)arguments
233 BOOL OK = [
self evaluateWithContext:context
243- (BOOL) evaluatePredicateWithContext:(JSContext *)context
245 arguments:(NSArray *)arguments
248 BOOL OK = [
self evaluateWithContext:context
253 if (OK) OK = JS_ValueToBoolean(context, result, &retval);
#define OOJSStopTimeLimiter()
#define OOJSStartTimeLimiter()
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
OOINLINE JSContext * OOJSAcquireContext(void)
#define OOJSAddGCObjectRoot(context, root, name)
OOINLINE void OOJSRelinquishContext(JSContext *context)
#define OOJSAddGCValueRoot(context, root, name)
NSString * descriptionComponents()
void pushScript:(OOJSScript *script)
void popScript:(OOJSScript *script)
JSObject * globalObject()
OOJavaScriptEngine * sharedEngine()