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
This commit is contained in:
Jens Ayton 2010-06-12 19:36:22 +00:00
parent a436e27ad2
commit 1edaad2aba
3 changed files with 47 additions and 33 deletions

View File

@ -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:

View File

@ -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
{

View File

@ -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;
}