32- (id)initWithFunction:(JSValueConst)function context:(JSContext*)context
34 NSParameterAssert(context != NULL);
36 if (!JS_IsFunction(context,
function)) {
41 if ((
self = [super init])) {
42 _function = JS_DupValue(context,
function);
45 JSValue nameValue = JS_GetPropertyStr(context,
function,
"name");
46 if (JS_IsException(nameValue)) {
47 JS_FreeValue(context, JS_GetException(context));
50 if ([
name length] != 0)
51 _name = [name retain];
53 JS_FreeValue(context, nameValue);
56 selector:@selector(deleteJSValue)
57 name:kOOJavaScriptEngineWillResetNotification
64- (id)initWithName:(NSString*)name
65 scope:(JSValueConst)scope
67 argumentCount:(NSUInteger)argCount
68 argumentNames:(const
char**)argNames
69 fileName:(NSString*)fileName
70 lineNumber:(NSUInteger)lineNumber
71 context:(JSContext*)context
74 BOOL releaseContext = NO;
75 JSValue
function = JS_UNDEFINED;
77 if (context == NULL) {
89 if (code ==
nil || (argCount > 0 && argNames == NULL))
93 assert(argCount < INT_MAX && lineNumber < INT_MAX);
99 NSMutableString* source = [
NSMutableString stringWithString:@"(function("];
101 for (i = 0; i < argCount; i++) {
103 [
source appendString:@","];
106 [
source appendFormat:@"){%@\n})", code];
108 NSData* utf8 = [
source dataUsingEncoding:NSUTF8StringEncoding];
110 [
buffer appendBytes:"" length:1];
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
118 function = JS_Eval2(context, [buffer bytes], [utf8 length], &options);
119 if (JS_IsException(
function)) {
121 function = JS_UNDEFINED;
123 }
else if (!JS_IsFunction(context,
function)) {
130 JS_DefinePropertyValueStr(context,
function,
"name", [
name oo_jsValueInContext:context], JS_PROP_CONFIGURABLE);
138 JS_FreeValue(context,
function);
202- (BOOL)evaluateWithContext:(JSContext*)context
203 scope:(JSValueConst)jsThis
205 argv:(JSValueConst*)argv
206 result:(JSValue*)result
208 NSParameterAssert(result != NULL);
211 *result = JS_UNDEFINED;
216 JSValue global = JS_UNDEFINED;
217 if (!JS_IsObject(jsThis)) {
218 global = JS_GetGlobalObject(context);
224 JSValue rval = JS_Call(context,
_function, jsThis, argc, argv);
228 JS_FreeValue(context, global);
230 if (JS_IsException(rval)) {
231 *result = JS_UNDEFINED;
239- (BOOL)evaluateWithContext:(JSContext*)context
241 arguments:(NSArray*)arguments
242 result:(JSValue*)result
245 assert(argc < INT_MAX);
246 JSValue argv[
argc + 1];
248 for (i = 0; i < argc; i++) {
249 argv[
i] = [[
arguments objectAtIndex:i] oo_jsValueInContext:context];
252 JSValue scopeObj = JS_UNDEFINED;
255 JSValue thisValue = [
jsThis oo_jsValueInContext:context];
257 if (!JS_IsNull(thisValue) && !JS_IsUndefined(thisValue)) {
258 scopeObj = JS_ToObject(context, thisValue);
259 if (JS_IsException(scopeObj)) {
260 scopeObj = JS_UNDEFINED;
264 JS_FreeValue(context, thisValue);
276 if (JS_HasException(context))
278 *result = JS_UNDEFINED;
281 JS_FreeValue(context, scopeObj);
282 for (i = 0; i < argc; i++) {
283 JS_FreeValue(context, argv[i]);