Converted all unguarded oo_jsValueInContext:s to OOJSValueFromNativeObject().

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@4356 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2011-02-14 21:57:58 +00:00
parent fdd4e8b447
commit 2e788a219d
13 changed files with 73 additions and 28 deletions

View File

@ -314,11 +314,12 @@ static JSBool ConsoleGetProperty(JSContext *context, JSObject *this, jsid propID
#endif
case kConsole_shaderMode:
*value = [OOStringFromShaderSetting([UNIVERSE shaderEffectsLevel]) oo_jsValueInContext:context];
// EMMSTRAN: if still relevant, OOConstToJSString-ify.
*value = OOJSValueFromNativeObject(context, OOStringFromShaderSetting([UNIVERSE shaderEffectsLevel]));
break;
case kConsole_maximumShaderMode:
*value = [OOStringFromShaderSetting([[OOOpenGLExtensionManager sharedManager] maximumShaderSetting]) oo_jsValueInContext:context];
*value = OOJSValueFromNativeObject(context, OOStringFromShaderSetting([[OOOpenGLExtensionManager sharedManager] maximumShaderSetting]));
break;
case kConsole_reducedDetailMode:
@ -330,7 +331,7 @@ static JSBool ConsoleGetProperty(JSContext *context, JSObject *this, jsid propID
break;
case kConsole_platformDescription:
*value = [OOPlatformDescription() oo_jsValueInContext:context];
*value = OOJSValueFromNativeObject(context, OOPlatformDescription());
break;
case kConsole_pedanticMode:
@ -353,11 +354,11 @@ static JSBool ConsoleGetProperty(JSContext *context, JSObject *this, jsid propID
break;
case kConsole_glVendorString:
*value = [[[OOOpenGLExtensionManager sharedManager] vendorString] oo_jsValueInContext:context];
*value = OOJSValueFromNativeObject(context, [[OOOpenGLExtensionManager sharedManager] vendorString]);
break;
case kConsole_glRendererString:
*value = [[[OOOpenGLExtensionManager sharedManager] rendererString] oo_jsValueInContext:context];
*value = OOJSValueFromNativeObject(context, [[OOOpenGLExtensionManager sharedManager] rendererString]);
break;
case kConsole_glFixedFunctionTextureUnitCount:
@ -991,7 +992,7 @@ static JSBool ConsoleProfile(JSContext *context, uintN argc, jsval *vp)
JSBool result = PerformProfiling(context, @"profile", argc, OOJS_ARGV, NULL, NO, &profile);
if (result)
{
OOJS_SET_RVAL([[profile description] oo_jsValueInContext:context]);
OOJS_SET_RVAL(OOJSValueFromNativeObject(context, [profile description]));
}
[pool release];
@ -1019,7 +1020,7 @@ static JSBool ConsoleGetProfile(JSContext *context, uintN argc, jsval *vp)
JSBool result = PerformProfiling(context, @"getProfile", argc, OOJS_ARGV, NULL, NO, &profile);
if (result)
{
OOJS_SET_RVAL([profile oo_jsValueInContext:context]);
OOJS_SET_RVAL(OOJSValueFromNativeObject(context, profile));
}
[pool release];
@ -1071,8 +1072,8 @@ static JSBool PerformProfiling(JSContext *context, NSString *nominalFunction, ui
if (argc > 1) this = argv[1];
else
{
jsval debugConsole = [[OODebugMonitor sharedDebugMonitor] oo_jsValueInContext:context];
assert(JSVAL_IS_OBJECT(debugConsole));
jsval debugConsole = OOJSValueFromNativeObject(context, [OODebugMonitor sharedDebugMonitor]);
assert(JSVAL_IS_OBJECT(debugConsole) && !JSVAL_IS_NULL(debugConsole));
JS_GetProperty(context, JSVAL_TO_OBJECT(debugConsole), "script", &this);
}

View File

@ -2023,7 +2023,12 @@ static WormholeEntity *whole = nil;
if (function != nil)
{
JSFunctionPredicateParameter param = { context, OBJECT_TO_JSVAL(JS_GetFunctionObject([function function])), JSVAL_TO_OBJECT([self oo_jsValueInContext:context]), NO };
JSFunctionPredicateParameter param =
{
.context = context,
.function = [function functionValue],
.jsThis = OOJSObjectFromNativeObject(context, self)
};
[self scanForNearestShipWithPredicate:JSFunctionPredicate parameter:&param];
}
else

View File

@ -1006,7 +1006,7 @@ static NSDictionary* instructions(int station_id, Vector coords, float speed, fl
JSContext *context = OOJSAcquireContext();
jsval rval = JSVAL_VOID;
jsval args[] = { [ship oo_jsValueInContext:context] };
jsval args[] = { OOJSValueFromNativeObject(context, ship) };
JSBool permit = YES;
BOOL OK = [[self script] callMethod:OOJSID("permitDocking") inContext:context withArguments:args count:1 result:&rval];

View File

@ -735,7 +735,7 @@ static void UpdateProfileForFrame(OOHighResTimeValue now, OOJSProfileStackFrame
- (jsval) oo_jsValueInContext:(JSContext *)context
{
return [[self propertyListRepresentation] oo_jsValueInContext:context];
return OOJSValueFromNativeObject(context, [self propertyListRepresentation]);
}
@ -968,7 +968,7 @@ static void UpdateProfileForFrame(OOHighResTimeValue now, OOJSProfileStackFrame
- (jsval) oo_jsValueInContext:(JSContext *)context
{
return [[self propertyListRepresentation] oo_jsValueInContext:context];
return OOJSValueFromNativeObject(context, [self propertyListRepresentation]);
}

View File

@ -414,7 +414,7 @@ static JSBool EquipmentInfoGetAllEqipment(JSContext *context, JSObject *this, js
{
OOJS_NATIVE_ENTER(context)
*value = [[OOEquipmentType allEquipmentTypes] oo_jsValueInContext:context];
*value = OOJSValueFromNativeObject(context, [OOEquipmentType allEquipmentTypes]);
return YES;
OOJS_NATIVE_EXIT

View File

@ -461,7 +461,7 @@ static void RunDeferredOperations(JSContext *context)
OOJSValue *callbackObj = [operation objectForKey:@"value"];
NSString *errorString = nil;
if (!AddCallback(context, [callbackObj oo_jsValueInContext:context], trackingID, &errorString))
if (!AddCallback(context, OOJSValueFromNativeObject(context, callbackObj), trackingID, &errorString))
{
OOLogWARN(@"script.frameCallback.deferredAdd.failed", @"Deferred frame callback insertion failed: %@", errorString);
}

View File

@ -49,6 +49,7 @@ MA 02110-1301, USA.
- (NSString *) name;
- (JSFunction *) function;
- (jsval) functionValue;
// Raw evaluation. Context may not be NULL and must be in a request.
- (BOOL) evaluateWithContext:(JSContext *)context

View File

@ -160,6 +160,20 @@ MA 02110-1301, USA.
}
- (jsval) functionValue
{
if (EXPECT(_function != NULL))
{
return OBJECT_TO_JSVAL(JS_GetFunctionObject(_function));
}
else
{
return JSVAL_NULL;
}
}
- (BOOL) evaluateWithContext:(JSContext *)context
scope:(JSObject *)jsThis
argc:(uintN)argc

View File

@ -347,7 +347,7 @@ static JSBool MissionRunScreen(JSContext *context, uintN argc, jsval *vp)
}
else
{
sCallbackThis = [sCallbackScript oo_jsValueInContext:context];
sCallbackThis = OOJSValueFromNativeObject(context, sCallbackScript);
}
}

View File

@ -194,11 +194,11 @@ static JSBool PlayerGetProperty(JSContext *context, JSObject *this, jsid propID,
return JS_NewNumberValue(context, [player creditBalance], value);
case kPlayer_rank:
*value = [OODisplayRatingStringFromKillCount([player score]) oo_jsValueInContext:context];
*value = OOJSValueFromNativeObject(context, OODisplayRatingStringFromKillCount([player score]));
return YES;
case kPlayer_legalStatus:
*value = [OODisplayStringFromLegalStatus([player bounty]) oo_jsValueInContext:context];
*value = OOJSValueFromNativeObject(context, OODisplayStringFromLegalStatus([player bounty]));
return YES;
case kPlayer_alertCondition:
@ -237,7 +237,8 @@ static JSBool PlayerGetProperty(JSContext *context, JSObject *this, jsid propID,
return YES;
case kPlayer_dockingClearanceStatus:
*value = [DockingClearanceStatusToString([player getDockingClearanceStatus]) oo_jsValueInContext:context];
// EMMSTRAN: OOConstToJSString-ify this.
*value = OOJSValueFromNativeObject(context, DockingClearanceStatusToString([player getDockingClearanceStatus]));
return YES;
case kPlayer_bounty:

View File

@ -304,7 +304,7 @@ jsval GetJSSystemInfoForSystem(JSContext *context, OOGalaxyID galaxy, OOSystemID
OOJSReportWarning(context, @"Could not create system info object for galaxy %u, system %i.", galaxy, system);
}
result = info ? [info oo_jsValueInContext:context] : JSVAL_NULL;
result = OOJSValueFromNativeObject(context, info);
// Cache is not a root; we clear it in finalize if necessary.
sCachedSystemInfo = JSVAL_TO_OBJECT(result);
@ -599,14 +599,14 @@ static JSBool SystemInfoStaticFilteredSystems(JSContext *context, uintN argc, js
OOJSPauseTimeLimiter();
// Iterate over systems.
BOOL OK = YES;
BOOL OK = result != nil;
OOGalaxyID galaxy = [PLAYER currentGalaxyID];
OOSystemID system;
for (system = 0; system <= kOOMaximumSystemID; system++)
{
// NOTE: this deliberately bypasses the cache, since it's inherently unfriendly to a single-item cache.
// NOTE: this deliberately bypasses the cache, since iteration is inherently unfriendly to a single-item cache.
OOSystemInfo *info = [[[OOSystemInfo alloc] initWithGalaxy:galaxy system:system] autorelease];
jsval args[1] = { [info oo_jsValueInContext:context] };
jsval args[1] = { OOJSValueFromNativeObject(context, info) };
jsval rval = JSVAL_VOID;
OOJSResumeTimeLimiter();

View File

@ -198,8 +198,13 @@ OOINLINE jsval OOJSValueFromBOOL(int b)
Return the JavaScript value representation of an object. The default
implementation returns JSVAL_VOID.
Note that sending this to nil does not return JSVAL_NULL. For that
behaviour, use OOJSValueFromNativeObject() below.
SAFETY NOTE: if this message is sent to nil, the return value depends on
the platform and whether JS_USE_JSVAL_JSID_STRUCT_TYPES is set. If the
receiver may be nil, use OOJSValueFromNativeObject() instead.
One case where it is safe to use oo_jsValueInContext: is with objects
retrieved from Foundation collections, as they can never be nil.
Requires a request on context.
*/
@ -237,6 +242,15 @@ OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
}
/* OOJSObjectFromNativeObject()
Return a JavaScript object representation of an object, or null if passed
nil. The value is boxed if necessary.
Requires a request on context.
*/
JSObject *OOJSObjectFromNativeObject(JSContext *context, id object);
/* OOJSValue: an object whose purpose in life is to hold a JavaScript value.
This is somewhat useful for putting JavaScript objects in ObjC collections,
for instance to pass as properties to script loaders. The value is
@ -658,7 +672,7 @@ JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp);
#define OOJS_RETURN_NULL OOJS_RETURN(JSVAL_NULL)
#define OOJS_RETURN_BOOL(v) OOJS_RETURN(OOJSValueFromBOOL(v))
#define OOJS_RETURN_INT(v) OOJS_RETURN(INT_TO_JSVAL(v))
#define OOJS_RETURN_OBJECT(o) do { id o_ = (o); OOJS_RETURN(o_ ? [o_ oo_jsValueInContext:context] : JSVAL_NULL); } while (0)
#define OOJS_RETURN_OBJECT(o) OOJS_RETURN(OOJSValueFromNativeObject(context, o))
#define OOJS_RETURN_WITH_HELPER(helper, value) \
do { \

View File

@ -1347,6 +1347,15 @@ static BOOL JSNewNSDictionaryValue(JSContext *context, NSDictionary *dictionary,
@end
JSObject *OOJSObjectFromNativeObject(JSContext *context, id object)
{
jsval value = OOJSValueFromNativeObject(context, object);
JSObject *result = NULL;
if (JS_ValueToObject(context, value, &result)) return result;
return NULL;
}
@implementation OOJSValue
+ (id) valueWithJSValue:(jsval)value inContext:(JSContext *)context
@ -1967,13 +1976,13 @@ BOOL JSFunctionPredicate(Entity *entity, void *parameter)
jsval rval = JSVAL_VOID;
JSBool result = NO;
NSCParameterAssert(entity != NULL && param != NULL);
NSCParameterAssert(entity != nil && param != NULL);
NSCParameterAssert(param->context != NULL && JS_IsInRequest(param->context));
NSCParameterAssert(OOJSValueIsFunction(param->context, param->function));
if (EXPECT_NOT(param->errorFlag)) return NO;
args[0] = [entity oo_jsValueInContext:param->context];
args[0] = [entity oo_jsValueInContext:param->context]; // entity is required to be non-nil (asserted above), so oo_jsValueInContext: is safe.
OOJSStartTimeLimiter();
OOJSResumeTimeLimiter();