From 1edaad2abae4402fc4183b974825c363a9c9a96e Mon Sep 17 00:00:00 2001 From: Jens Ayton Date: Sat, 12 Jun 2010 19:36:22 +0000 Subject: [PATCH] Fixed temporary object rooting problems when converting dictionaries and arrays to JS representations. git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@3509 127b21dd-08f5-0310-b4b7-95ae10353056 --- Resources/Config/logcontrol.plist | 1 + src/Core/Scripting/OOJSScript.m | 10 ++-- src/Core/Scripting/OOJavaScriptEngine.m | 69 +++++++++++++++---------- 3 files changed, 47 insertions(+), 33 deletions(-) diff --git a/Resources/Config/logcontrol.plist b/Resources/Config/logcontrol.plist index 0d5263f7..045adad3 100644 --- a/Resources/Config/logcontrol.plist +++ b/Resources/Config/logcontrol.plist @@ -299,6 +299,7 @@ script.trace.runWorld = inherit; script.trace.plist.run = inherit; script.trace.javaScript.call = inherit; // Prints selector and parameter string on Player.call() + script.trace.javaScript.event = inherit; // Log event handler calls. script.deprecated.scriptActionOnTarget = $scriptError; // Warning not to use scriptActionOnTarget: diff --git a/src/Core/Scripting/OOJSScript.m b/src/Core/Scripting/OOJSScript.m index d7503be5..7f378e0e 100644 --- a/src/Core/Scripting/OOJSScript.m +++ b/src/Core/Scripting/OOJSScript.m @@ -305,12 +305,7 @@ static JSFunctionSpec sScriptMethods[] = - (void)runWithTarget:(Entity *)target { - OOLog(@"script.trace.js.run", @"Runing script \"%@\"", [self name]); - OOLogIndentIf(@"script.trace.js.run"); - [self doEvent:@"tickle" withArguments:[NSArray arrayWithObject:[[PlayerEntity sharedPlayer] status_string]]]; - - OOLogOutdentIf(@"script.trace.js.run"); } @@ -387,6 +382,9 @@ static JSFunctionSpec sScriptMethods[] = function = [self functionNamed:eventName context:context]; if (function != NULL) { + OOLog(@"script.trace.javaScript.event", @"Calling [%@].%@()", [self name], eventName); + OOLogIndentIf(@"script.trace.javaScript.event"); + // Push self on stack of running scripts. RunningStack stackElement = { @@ -428,6 +426,8 @@ static JSFunctionSpec sScriptMethods[] = sRunningStack = stackElement.back; JS_ClearNewbornRoots(context); + + OOLogOutdentIf(@"script.trace.javaScript.event"); } else { diff --git a/src/Core/Scripting/OOJavaScriptEngine.m b/src/Core/Scripting/OOJavaScriptEngine.m index b1786884..85020a85 100644 --- a/src/Core/Scripting/OOJavaScriptEngine.m +++ b/src/Core/Scripting/OOJavaScriptEngine.m @@ -692,20 +692,25 @@ static JSObject *JSArrayFromNSArray(JSContext *context, NSArray *array) unsigned i; unsigned count; jsval value; - BOOL OK = YES; if (array == nil) return NULL; NS_DURING result = JS_NewArrayObject(context, 0, NULL); - if (result == NULL) NS_VALUERETURN(NULL, JSObject *); - - count = [array count]; - for (i = 0; i != count; ++i) + if (result != NULL) { - value = [[array objectAtIndex:i] javaScriptValueInContext:context]; - OK = JS_SetElement(context, result, i, &value); - if (!OK) NS_VALUERETURN(NULL, JSObject *); + count = [array count]; + for (i = 0; i != count; ++i) + { + value = [[array objectAtIndex:i] javaScriptValueInContext:context]; + BOOL OK = JS_SetElement(context, result, i, &value); + + if (EXPECT_NOT(!OK)) + { + result = NULL; + break; + } + } } NS_HANDLER result = NULL; @@ -736,7 +741,7 @@ static BOOL JSNewNSArrayValue(JSContext *context, NSArray *array, jsval *value) *value = OBJECT_TO_JSVAL(object); } - JS_LeaveLocalRootScope(context); + JS_LeaveLocalRootScopeWithResult(context, *value); return OK; } @@ -758,37 +763,45 @@ static JSObject *JSObjectFromNSDictionary(JSContext *context, NSDictionary *dict NS_DURING result = JS_NewObject(context, NULL, NULL, NULL); // create object of class Object - if (result == NULL) NS_VALUERETURN(NULL, JSObject *); - - for (keyEnum = [dictionary keyEnumerator]; (key = [keyEnum nextObject]); ) + if (result != NULL) { - if ([key isKindOfClass:[NSString class]] && [key length] != 0) + for (keyEnum = [dictionary keyEnumerator]; (key = [keyEnum nextObject]); ) { - value = [[dictionary objectForKey:key] javaScriptValueInContext:context]; - if (value != JSVAL_VOID) - { - OK = JSSetNSProperty(context, result, key, &value); - if (!OK) NS_VALUERETURN(NULL, JSObject *); - } - } - else if ([key isKindOfClass:[NSNumber class]]) - { - index = [key intValue]; - if (0 < index) + if ([key isKindOfClass:[NSString class]] && [key length] != 0) { value = [[dictionary objectForKey:key] javaScriptValueInContext:context]; if (value != JSVAL_VOID) { - OK = JS_SetElement(context, (JSObject *)result, index, &value); - if (!OK) NS_VALUERETURN(NULL, JSObject *); + OK = JSSetNSProperty(context, result, key, &value); + if (EXPECT_NOT(!OK)) break; } } + else if ([key isKindOfClass:[NSNumber class]]) + { + index = [key intValue]; + if (0 < index) + { + value = [[dictionary objectForKey:key] javaScriptValueInContext:context]; + if (value != JSVAL_VOID) + { + OK = JS_SetElement(context, (JSObject *)result, index, &value); + if (EXPECT_NOT(!OK)) break; + } + } + } + + if (EXPECT_NOT(!OK)) break; } } NS_HANDLER - result = NULL; + OK = NO; NS_ENDHANDLER + if (EXPECT_NOT(!OK)) + { + result = NULL; + } + return (JSObject *)result; } @@ -814,7 +827,7 @@ static BOOL JSNewNSDictionaryValue(JSContext *context, NSDictionary *dictionary, *value = OBJECT_TO_JSVAL(object); } - JS_LeaveLocalRootScope(context); + JS_LeaveLocalRootScopeWithResult(context, *value); return OK; }