Optimized JS-to-native string converters to avoid up to three unnecessary class method calls. Informally deprecated stringWithJavaScriptString:, stringWithJavaScriptValue:inContext: and stringOrNilWithJavaScriptValue:inContext: in favour of OOStringFromJSString(), OOStringFromJSValueEvenIfNull() and OOStringFromJSValue() (formerly OOJSValToNSString()).

git-svn-id: http://svn.berlios.de/svnroot/repos/oolite-linux/trunk@4011 127b21dd-08f5-0310-b4b7-95ae10353056
This commit is contained in:
Jens Ayton 2011-01-05 22:13:17 +00:00
parent fa6b634793
commit 8589c5a13e
18 changed files with 163 additions and 148 deletions

View File

@ -386,7 +386,7 @@ static JSBool ConsoleSetProperty(OOJS_PROP_ARGS)
break;
#endif
case kConsole_shaderMode:
sValue = OOJSValToNSString(context, *value);
sValue = OOStringFromJSValue(context, *value);
if (sValue != nil)
{
OOJS_BEGIN_FULL_NATIVE(context)
@ -605,8 +605,8 @@ static JSBool ConsoleConsoleMessage(OOJS_NATIVE_ARGS)
return NO;
}
colorKey = OOJSValToNSString(context,OOJS_ARG(0));
message = OOJSValToNSString(context,OOJS_ARG(1));
colorKey = OOStringFromJSValue(context,OOJS_ARG(0));
message = OOStringFromJSValue(context,OOJS_ARG(1));
if (4 <= argc)
{
@ -764,7 +764,7 @@ static JSBool ConsoleIsExecutableJavaScript(OOJS_NATIVE_ARGS)
OOJSPauseTimeLimiter();
#if OO_NEW_JS
// FIXME: this must be possible using just JSAPI functions.
NSString *string = OOJSValToNSString(context, OOJS_ARG(1));
NSString *string = OOStringFromJSValue(context, OOJS_ARG(1));
NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
result = JS_BufferIsCompilableUnit(context, target, [stringData bytes], [stringData length]);
#else
@ -801,7 +801,7 @@ static JSBool ConsoleSetDisplayMessagesInClass(OOJS_NATIVE_ARGS)
NSString *messageClass = nil;
JSBool flag;
messageClass = OOJSValToNSString(context, OOJS_ARG(0));
messageClass = OOStringFromJSValue(context, OOJS_ARG(0));
if (messageClass != nil && JS_ValueToBoolean(context, OOJS_ARG(1), &flag))
{
OOLogSetDisplayMessagesInClass(messageClass, flag);

View File

@ -96,7 +96,7 @@ BOOL OOJSCallObjCObjectMethod(JSContext *context, id object, NSString *oo_jsClas
[PLAYER setScriptTarget:object];
}
selectorString = OOJSValToNSString(context,argv[0]);
selectorString = OOStringFromJSValue(context,argv[0]);
// Join all parameters together with spaces.
if (1 < argc && [selectorString hasSuffix:@":"])

View File

@ -162,7 +162,7 @@ OOEquipmentType *JSValueToEquipmentType(JSContext *context, jsval value)
}
}
NSString *string = OOJSValToNSString(context, value);
NSString *string = OOStringFromJSValue(context, value);
if (string != nil) return [OOEquipmentType equipmentTypeWithIdentifier:string];
return nil;
@ -444,7 +444,7 @@ static JSBool EquipmentInfoStaticInfoForKey(OOJS_NATIVE_ARGS)
NSString *key = nil;
key = OOJSValToNSString(context, OOJS_ARG(0));
key = OOStringFromJSValue(context, OOJS_ARG(0));
if (key == nil)
{
OOJSReportBadArguments(context, @"EquipmentInfo", @"infoForKey", argc, OOJS_ARGV, nil, @"string");

View File

@ -223,11 +223,11 @@ static JSBool GlobalLog(OOJS_NATIVE_ARGS)
if (argc < 2)
{
messageClass = kOOLogDebugMessage;
message = OOJSValToNSString(context, OOJS_ARG(0));
message = OOStringFromJSValue(context, OOJS_ARG(0));
}
else
{
messageClass = OOJSValToNSString(context, OOJS_ARG(0));
messageClass = OOStringFromJSValue(context, OOJS_ARG(0));
message = [NSString concatenationOfStringsFromJavaScriptValues:OOJS_ARGV + 1 count:argc - 1 separator:@", " inContext:context];
}
@ -255,7 +255,7 @@ static JSBool GlobalExpandDescription(OOJS_NATIVE_ARGS)
NSString *string = nil;
NSDictionary *overrides = nil;
string = OOJSValToNSString(context, OOJS_ARG(0));
string = OOStringFromJSValue(context, OOJS_ARG(0));
if (string == nil)
{
OOJSReportBadArguments(context, nil, @"expandDescription", argc, OOJS_ARGV, nil, @"string");
@ -282,7 +282,7 @@ static JSBool GlobalExpandMissionText(OOJS_NATIVE_ARGS)
NSMutableString *mString = nil;
NSDictionary *overrides = nil;
string = OOJSValToNSString(context, OOJS_ARG(0));
string = OOStringFromJSValue(context, OOJS_ARG(0));
if (string == nil)
{
OOJSReportBadArguments(context, nil, @"expandMissionText", argc, OOJS_ARGV, nil, @"string");
@ -315,7 +315,7 @@ static JSBool GlobalDisplayNameForCommodity(OOJS_NATIVE_ARGS)
NSString *string = nil;
string = OOJSValToNSString(context,OOJS_ARG(0));
string = OOStringFromJSValue(context,OOJS_ARG(0));
if (string == nil)
{
OOJSReportBadArguments(context, nil, @"displayNameForCommodity", argc, OOJS_ARGV, nil, @"string");
@ -366,7 +366,7 @@ static JSBool GlobalSetScreenBackground(OOJS_NATIVE_ARGS)
{
OOJS_NATIVE_ENTER(context)
NSString *value = OOJSValToNSString(context, OOJS_ARG(0));
NSString *value = OOStringFromJSValue(context, OOJS_ARG(0));
PlayerEntity *player = OOPlayerForScripting();
BOOL result = NO;
@ -389,7 +389,7 @@ static JSBool GlobalSetScreenOverlay(OOJS_NATIVE_ARGS)
OOJS_NATIVE_ENTER(context)
BOOL result = NO;
NSString *value = OOJSValToNSString(context, OOJS_ARG(0));
NSString *value = OOStringFromJSValue(context, OOJS_ARG(0));
if ([UNIVERSE viewDirection] == VIEW_GUI_DISPLAY)
{
@ -415,7 +415,7 @@ static JSBool GlobalTakeSnapShot(OOJS_NATIVE_ARGS)
if (argc > 0)
{
value = OOJSValToNSString(context, OOJS_ARG(0));
value = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(value == nil || [value rangeOfCharacterFromSet:[allowedChars invertedSet]].location != NSNotFound))
{
OOJSReportBadArguments(context, nil, @"takeSnapShot", argc, OOJS_ARGV, nil, @"alphanumeric string");

View File

@ -196,7 +196,7 @@ static JSBool MissionAddMessageText(OOJS_NATIVE_ARGS)
// Found "FIXME: warning if no mission screen running.",,,
// However: used routinely by the Constrictor mission in F7, without mission screens.
text = OOJSValToNSString(context, OOJS_ARG(0));
text = OOStringFromJSValue(context, OOJS_ARG(0));
[player addLiteralMissionText:text];
OOJS_RETURN_VOID;
@ -227,7 +227,7 @@ static JSBool MissionSetInstructionsInternal(OOJS_NATIVE_ARGS, BOOL isKey)
NSString *text = nil;
NSString *missionKey = nil;
text = OOJSValToNSString(context, OOJS_ARG(0));
text = OOStringFromJSValue(context, OOJS_ARG(0));
if (argc > 1)
{
@ -265,7 +265,7 @@ OOINLINE NSString *GetParameterString(JSContext *context, JSObject *object, cons
jsval value = JSVAL_NULL;
if (JS_GetProperty(context, object, key, &value))
{
return OOJSValToNSString(context, value);
return OOStringFromJSValue(context, value);
}
return nil;
}
@ -321,7 +321,7 @@ static JSBool MissionRunScreen(OOJS_NATIVE_ARGS)
// Apply settings.
if (JS_GetProperty(context, params, "title", &value))
{
[player setMissionTitle:OOJSValToNSString(context, value)];
[player setMissionTitle:OOStringFromJSValue(context, value)];
}
else
{
@ -345,7 +345,7 @@ static JSBool MissionRunScreen(OOJS_NATIVE_ARGS)
{
OOJSReportWarning(context, @"Mission.runScreen: model will not be displayed while in flight.");
}
[player showShipModel:OOJSValToNSString(context, value)];
[player showShipModel:OOStringFromJSValue(context, value)];
}
// Start the mission screen.

View File

@ -156,7 +156,7 @@ static JSBool MissionVariablesSetProperty(OOJS_PROP_ARGS)
return NO;
}
NSString *objValue = OOJSValToNSString(context, *value);
NSString *objValue = OOStringFromJSValue(context, *value);
if ([objValue isKindOfClass:[NSNull class]]) objValue = nil;
[player setMissionVariable:objValue forKey:key];

View File

@ -189,7 +189,7 @@ static JSBool PlanetSetProperty(OOJS_PROP_ARGS)
case kPlanet_texture:
// all error messages are self contained
sValue = OOJSValToNSString(context, *value);
sValue = OOStringFromJSValue(context, *value);
if ([planet isKindOfClass:[OOPlanetEntity class]])
{

View File

@ -338,7 +338,7 @@ static JSBool PlayerCommsMessage(OOJS_NATIVE_ARGS)
double time = 4.5;
BOOL gotTime = YES;
message = OOJSValToNSString(context, OOJS_ARG(0));
message = OOStringFromJSValue(context, OOJS_ARG(0));
if (argc > 1) gotTime = JS_ValueToNumber(context, OOJS_ARG(1), &time);
if (message == nil || !gotTime)
{
@ -362,7 +362,7 @@ static JSBool PlayerConsoleMessage(OOJS_NATIVE_ARGS)
double time = 3.0;
BOOL gotTime = YES;
message = OOJSValToNSString(context, OOJS_ARG(0));
message = OOStringFromJSValue(context, OOJS_ARG(0));
if (argc > 1) gotTime = JS_ValueToNumber(context, OOJS_ARG(1), &time);
if (message == nil || !gotTime)
{
@ -431,7 +431,7 @@ static JSBool PlayerAddMessageToArrivalReport(OOJS_NATIVE_ARGS)
NSString *report = nil;
report = OOJSValToNSString(context, OOJS_ARG(0));
report = OOStringFromJSValue(context, OOJS_ARG(0));
if (report == nil)
{
OOJSReportBadArguments(context, @"Player", @"addMessageToArrivalReport", argc, OOJS_ARGV, nil, @"arrival message");

View File

@ -377,7 +377,7 @@ static JSBool PlayerShipSetProperty(OOJS_PROP_ARGS)
break;
case kPlayerShip_galacticHyperspaceBehaviour:
sValue = OOJSValToNSString(context,*value);
sValue = OOStringFromJSValue(context,*value);
if (sValue != nil)
{
ghBehaviour = StringToGalacticHyperspaceBehaviour(sValue);
@ -429,7 +429,7 @@ static JSBool PlayerShipSetProperty(OOJS_PROP_ARGS)
break;
case kPlayerShip_hud:
sValue = OOJSValToNSString(context, *value);
sValue = OOStringFromJSValue(context, *value);
if (sValue != nil)
{
if ([player switchHudTo:sValue])
@ -517,14 +517,14 @@ static JSBool PlayerShipUseSpecialCargo(OOJS_NATIVE_ARGS)
PlayerEntity *player = OOPlayerShipForScripting();
NSString *name = nil;
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(name == nil))
{
OOJSReportBadArguments(context, @"PlayerShip", @"useSpecialCargo", argc, OOJS_ARGV, nil, @"special cargo description");
return NO;
}
[player useSpecialCargo:OOJSValToNSString(context, OOJS_ARG(0))];
[player useSpecialCargo:OOStringFromJSValue(context, OOJS_ARG(0))];
OOJS_RETURN_VOID;
OOJS_NATIVE_EXIT

View File

@ -777,7 +777,7 @@ static JSBool ShipSetProperty(OOJS_PROP_ARGS)
}
else
{
sValue = OOJSValToNSString(context,*value);
sValue = OOStringFromJSValue(context,*value);
if (sValue != nil)
{
[entity setName:sValue];
@ -793,7 +793,7 @@ static JSBool ShipSetProperty(OOJS_PROP_ARGS)
}
else
{
sValue = OOJSValToNSString(context,*value);
sValue = OOStringFromJSValue(context,*value);
if (sValue != nil)
{
[entity setDisplayName:sValue];
@ -809,7 +809,7 @@ static JSBool ShipSetProperty(OOJS_PROP_ARGS)
}
else
{
sValue = OOJSValToNSString(context,*value);
sValue = OOStringFromJSValue(context,*value);
if (sValue != nil)
{
[entity setPrimaryRole:sValue];
@ -825,7 +825,7 @@ static JSBool ShipSetProperty(OOJS_PROP_ARGS)
}
else
{
sValue = OOJSValToNSString(context,*value);
sValue = OOStringFromJSValue(context,*value);
if (sValue != nil)
{
[[entity getAI] setState:sValue];
@ -1049,7 +1049,7 @@ static JSBool ShipSetScript(OOJS_NATIVE_ARGS)
NSString *name = nil;
GET_THIS_SHIP(thisEnt);
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(name == nil))
{
OOJSReportBadArguments(context, @"Ship", @"setScript", argc, OOJS_ARGV, nil, @"script name");
@ -1077,7 +1077,7 @@ static JSBool ShipSetAI(OOJS_NATIVE_ARGS)
NSString *name = nil;
GET_THIS_SHIP(thisEnt);
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(name == nil))
{
OOJSReportBadArguments(context, @"Ship", @"setAI", argc, OOJS_ARGV, nil, @"AI name");
@ -1105,7 +1105,7 @@ static JSBool ShipSwitchAI(OOJS_NATIVE_ARGS)
NSString *name = nil;
GET_THIS_SHIP(thisEnt);
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(name == nil))
{
OOJSReportBadArguments(context, @"Ship", @"switchAI", argc, OOJS_ARGV, nil, @"AI name");
@ -1143,7 +1143,7 @@ static JSBool ShipExitAI(OOJS_NATIVE_ARGS)
if (argc > 0)
{
message = OOJSValToNSString(context, OOJS_ARG(0));
message = OOStringFromJSValue(context, OOJS_ARG(0));
}
if (![thisAI hasSuspendedStateMachines])
@ -1169,7 +1169,7 @@ static JSBool ShipReactToAIMessage(OOJS_NATIVE_ARGS)
NSString *message = nil;
GET_THIS_SHIP(thisEnt);
message = OOJSValToNSString(context, OOJS_ARG(0));
message = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(message == nil))
{
OOJSReportBadArguments(context, @"Ship", @"reactToAIMessage", argc, OOJS_ARGV, nil, @"string");
@ -1197,7 +1197,7 @@ static JSBool ShipSendAIMessage(OOJS_NATIVE_ARGS)
NSString *message = nil;
GET_THIS_SHIP(thisEnt);
message = OOJSValToNSString(context, OOJS_ARG(0));
message = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(message == nil))
{
OOJSReportBadArguments(context, @"Ship", @"sendAIMessage", argc, OOJS_ARGV, nil, @"string");
@ -1258,7 +1258,7 @@ static JSBool ShipHasRole(OOJS_NATIVE_ARGS)
GET_THIS_SHIP(thisEnt);
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(role == nil))
{
OOJSReportBadArguments(context, @"Ship", @"hasRole", argc, OOJS_ARGV, nil, @"role");
@ -1281,7 +1281,7 @@ static JSBool ShipEjectItem(OOJS_NATIVE_ARGS)
GET_THIS_SHIP(thisEnt);
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(role == nil))
{
OOJSReportBadArguments(context, @"Ship", @"ejectItem", argc, OOJS_ARGV, nil, @"role");
@ -1304,7 +1304,7 @@ static JSBool ShipEjectSpecificItem(OOJS_NATIVE_ARGS)
GET_THIS_SHIP(thisEnt);
itemKey = OOJSValToNSString(context, OOJS_ARG(0));
itemKey = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(itemKey == nil))
{
OOJSReportBadArguments(context, @"Ship", @"ejectSpecificItem", argc, OOJS_ARGV, nil, @"ship key");
@ -1352,7 +1352,7 @@ static JSBool ShipSpawn(OOJS_NATIVE_ARGS)
GET_THIS_SHIP(thisEnt);
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (argc > 1) gotCount = JS_ValueToInt32(context, OOJS_ARG(1), &count);
if (EXPECT_NOT(role == nil || !gotCount || count < 1 || count > 64))
{
@ -1463,7 +1463,7 @@ static JSBool ShipCommsMessage(OOJS_NATIVE_ARGS)
GET_THIS_SHIP(thisEnt);
message = OOJSValToNSString(context, OOJS_ARG(0));
message = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(message == nil)|| ( argc > 1 && EXPECT_NOT(!JSVAL_IS_OBJECT(OOJS_ARG(1)) || !JSShipGetShipEntity(context, JSVAL_TO_OBJECT(OOJS_ARG(1)), &target))))
{
OOJSReportBadArguments(context, @"Ship", @"commsMessage", argc, OOJS_ARGV, nil, @"message and optional target");
@ -1533,7 +1533,7 @@ static JSBool ShipAddPassenger(OOJS_NATIVE_ARGS)
if (argc == 5)
{
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(name == nil || JSVAL_IS_INT(OOJS_ARG(0))))
{
OOJSReportBadArguments(context, @"Ship", @"addPassenger", argc, OOJS_ARGV, nil, @"name:string");
@ -1583,7 +1583,7 @@ static JSBool ShipAwardContract(OOJS_NATIVE_ARGS)
if (OK && argc == 6)
{
key = OOJSValToNSString(context, OOJS_ARG(1));
key = OOStringFromJSValue(context, OOJS_ARG(1));
if (EXPECT_NOT(key == nil || !JSVAL_IS_STRING(OOJS_ARG(1))))
{
OOJSReportBadArguments(context, @"Ship", @"awardContract", argc, OOJS_ARGV, nil, @"commodity identifier:string");
@ -1835,7 +1835,7 @@ static JSBool ShipRemovePassenger(OOJS_NATIVE_ARGS)
return NO;
}
key = OOJSValToNSString(context, OOJS_ARG(0));
key = OOStringFromJSValue(context, OOJS_ARG(0));
OK = [thisEnt passengerCount] > 0 && [key length] > 0;
if (OK)
@ -1902,7 +1902,7 @@ static JSBool ShipSetEquipmentStatus(OOJS_NATIVE_ARGS)
return NO;
}
status = OOJSValToNSString(context, OOJS_ARG(1));
status = OOStringFromJSValue(context, OOJS_ARG(1));
if (EXPECT_NOT(status == nil))
{
OOJSReportBadArguments(context, @"Ship", @"setEquipmentStatus", argc - 1, OOJS_ARGV + 1, nil, @"equipment status");
@ -2026,7 +2026,7 @@ static JSBool ShipFireMissile(OOJS_NATIVE_ARGS)
GET_THIS_SHIP(thisEnt);
if (argc > 0) result = [thisEnt fireMissileWithIdentifier:OOJSValToNSString(context, OOJS_ARG(0)) andTarget:[thisEnt primaryTarget]];
if (argc > 0) result = [thisEnt fireMissileWithIdentifier:OOStringFromJSValue(context, OOJS_ARG(0)) andTarget:[thisEnt primaryTarget]];
else result = [thisEnt fireMissile];
OOJS_RETURN_OBJECT(result);
@ -2047,7 +2047,7 @@ static JSBool ShipSetCargo(OOJS_NATIVE_ARGS)
GET_THIS_SHIP(thisEnt);
cargoType = OOJSValToNSString(context, OOJS_ARG(0));
cargoType = OOStringFromJSValue(context, OOJS_ARG(0));
if (argc > 1) gotCount = JS_ValueToInt32(context, OOJS_ARG(1), &count);
if (EXPECT_NOT(cargoType == nil || !gotCount || count < 1))
{

View File

@ -113,7 +113,7 @@ OOSound *SoundFromJSValue(JSContext *context, jsval value)
OOJSPauseTimeLimiter();
if (JSVAL_IS_STRING(value))
{
return GetNamedSound(OOJSValToNSString(context, value));
return GetNamedSound(OOStringFromJSValue(context, value));
}
else
{
@ -181,7 +181,7 @@ static JSBool SoundStaticLoad(OOJS_NATIVE_ARGS)
NSString *name = nil;
OOSound *sound = nil;
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (name == nil)
{
OOJSReportBadArguments(context, @"Sound", @"load", argc, OOJS_ARGV, nil, @"string");
@ -206,7 +206,7 @@ static JSBool SoundStaticPlayMusic(OOJS_NATIVE_ARGS)
NSString *name = nil;
JSBool loop = NO;
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (name == nil)
{
OOJSReportBadArguments(context, @"Sound", @"playMusic", 1, OOJS_ARGV, nil, @"string");
@ -239,7 +239,7 @@ static JSBool SoundStaticStopMusic(OOJS_NATIVE_ARGS)
if (argc > 0)
{
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(name == nil))
{
OOJSReportBadArguments(context, @"Sound", @"stopMusic", argc, OOJS_ARGV, nil, @"string or no argument");

View File

@ -177,9 +177,7 @@ static JSBool SoundSourceSetProperty(OOJS_PROP_ARGS)
switch (OOJS_PROPID_INT)
{
case kSoundSource_sound:
OOJSPauseTimeLimiter();
[soundSource setSound:SoundFromJSValue(context, *value)];
OOJSResumeTimeLimiter();
OK = YES;
break;
@ -293,9 +291,7 @@ static JSBool SoundSourcePlaySound(OOJS_NATIVE_ARGS)
if (EXPECT_NOT(!JSSoundSourceGetSoundSource(context, OOJS_THIS, &thisv))) return NO;
OOJSPauseTimeLimiter();
sound = SoundFromJSValue(context, OOJS_ARG(0));
OOJSResumeTimeLimiter();
if (sound == nil)
{

View File

@ -361,7 +361,7 @@ static JSBool StationLaunchShipWithRole(OOJS_NATIVE_ARGS)
if (argc > 1) JS_ValueToBoolean(context, OOJS_ARG(1), &abortAllDockings);
NSString *shipRole = OOJSValToNSString(context, OOJS_ARG(0));
NSString *shipRole = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(shipRole == nil))
{
OOJSReportBadArguments(context, @"Station", @"launchShipWithRole", argc, OOJS_ARGV, nil, @"shipRole");

View File

@ -356,7 +356,7 @@ static JSBool SystemSetProperty(OOJS_PROP_ARGS)
switch (OOJS_PROPID_INT)
{
case kSystem_name:
stringValue = OOJSValToNSString(context, *value);
stringValue = OOStringFromJSValue(context, *value);
if (stringValue != nil)
{
[UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_NAME value:stringValue];
@ -365,7 +365,7 @@ static JSBool SystemSetProperty(OOJS_PROP_ARGS)
break;
case kSystem_description:
stringValue = OOJSValToNSString(context, *value);
stringValue = OOStringFromJSValue(context, *value);
if (stringValue != nil)
{
[UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_DESCRIPTION value:stringValue];
@ -374,7 +374,7 @@ static JSBool SystemSetProperty(OOJS_PROP_ARGS)
break;
case kSystem_inhabitantsDescription:
stringValue = OOJSValToNSString(context, *value);
stringValue = OOStringFromJSValue(context, *value);
if (stringValue != nil)
{
[UNIVERSE setSystemDataForGalaxy:galaxy planet:system key:KEY_INHABITANTS value:stringValue];
@ -464,7 +464,7 @@ static JSBool SystemAddPlanet(OOJS_NATIVE_ARGS)
NSString *key = nil;
OOPlanetEntity *planet = nil;
key = OOJSValToNSString(context, OOJS_ARG(0));
key = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(key == nil))
{
OOJSReportBadArguments(context, @"System", @"addPlanet", argc, OOJS_ARGV, @"Expected planet key, got", nil);
@ -490,7 +490,7 @@ static JSBool SystemAddMoon(OOJS_NATIVE_ARGS)
NSString *key = nil;
OOPlanetEntity *planet = nil;
key = OOJSValToNSString(context, OOJS_ARG(0));
key = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(key == nil))
{
OOJSReportBadArguments(context, @"System", @"addMoon", argc, OOJS_ARGV, @"Expected planet key, got", nil);
@ -531,7 +531,7 @@ static JSBool SystemCountShipsWithPrimaryRole(OOJS_NATIVE_ARGS)
double range = -1;
unsigned result;
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(role == nil))
{
OOJSReportBadArguments(context, @"System", @"countShipsWithPrimaryRole", argc, OOJS_ARGV, nil, @"role");
@ -563,7 +563,7 @@ static JSBool SystemCountShipsWithRole(OOJS_NATIVE_ARGS)
double range = -1;
unsigned result;
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(role == nil))
{
OOJSReportBadArguments(context, @"System", @"countShipsWithRole", argc, OOJS_ARGV, nil, @"role");
@ -595,7 +595,7 @@ static JSBool SystemShipsWithPrimaryRole(OOJS_NATIVE_ARGS)
double range = -1;
NSArray *result = nil;
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(role == nil))
{
OOJSReportBadArguments(context, @"System", @"shipsWithPrimaryRole", argc, OOJS_ARGV, nil, @"role and optional reference entity and range");
@ -628,7 +628,7 @@ static JSBool SystemShipsWithRole(OOJS_NATIVE_ARGS)
double range = -1;
NSArray *result = nil;
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(role == nil))
{
OOJSReportBadArguments(context, @"System", @"shipsWithRole", argc, OOJS_ARGV, nil, @"role and optional reference entity and range");
@ -662,7 +662,7 @@ static JSBool SystemCountEntitiesWithScanClass(OOJS_NATIVE_ARGS)
double range = -1;
unsigned result;
scString = OOJSValToNSString(context, OOJS_ARG(0));
scString = OOStringFromJSValue(context, OOJS_ARG(0));
if (scString == nil)
{
OOJSReportBadArguments(context, @"System", @"countEntitiesWithScanClass", argc, OOJS_ARGV, nil, @"scan class and optional reference entity and range");
@ -702,7 +702,7 @@ static JSBool SystemEntitiesWithScanClass(OOJS_NATIVE_ARGS)
double range = -1;
NSArray *result = nil;
scString = OOJSValToNSString(context, OOJS_ARG(0));
scString = OOStringFromJSValue(context, OOJS_ARG(0));
if (scString == nil)
{
OOJSReportBadArguments(context, @"System", @"entitiesWithScanClass", argc, OOJS_ARGV, nil, @"scan class and optional reference entity and range");
@ -806,7 +806,7 @@ static JSBool SystemLegacyAddShips(OOJS_NATIVE_ARGS)
NSString *role = nil;
int32 count;
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(role == nil ||
!JS_ValueToInt32(context, OOJS_ARG(1), &count) ||
argc < 2 ||
@ -835,7 +835,7 @@ static JSBool SystemLegacyAddSystemShips(OOJS_NATIVE_ARGS)
NSString *role = nil;
int32 count;
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(role == nil ||
!JS_ValueToInt32(context, OOJS_ARG(1), &count) ||
count < 1 || 64 < count ||
@ -868,8 +868,8 @@ static JSBool SystemLegacyAddShipsAt(OOJS_NATIVE_ARGS)
NSString *coordScheme = nil;
NSString *arg = nil;
role = OOJSValToNSString(context, OOJS_ARG(0));
coordScheme = OOJSValToNSString(context, OOJS_ARG(2));
role = OOStringFromJSValue(context, OOJS_ARG(0));
coordScheme = OOStringFromJSValue(context, OOJS_ARG(2));
if (EXPECT_NOT(role == nil ||
!JS_ValueToInt32(context, OOJS_ARG(1), &count) ||
count < 1 || 64 < count ||
@ -904,8 +904,8 @@ static JSBool SystemLegacyAddShipsAtPrecisely(OOJS_NATIVE_ARGS)
NSString *coordScheme = nil;
NSString *arg = nil;
role = OOJSValToNSString(context, OOJS_ARG(0));
coordScheme = OOJSValToNSString(context, OOJS_ARG(2));
role = OOStringFromJSValue(context, OOJS_ARG(0));
coordScheme = OOStringFromJSValue(context, OOJS_ARG(2));
if (EXPECT_NOT(role == nil ||
!JS_ValueToInt32(context, OOJS_ARG(1), &count) ||
count < 1 || 64 < count ||
@ -942,8 +942,8 @@ static JSBool SystemLegacyAddShipsWithinRadius(OOJS_NATIVE_ARGS)
NSString *arg = nil;
uintN consumed = 0;
role = OOJSValToNSString(context, OOJS_ARG(0));
coordScheme = OOJSValToNSString(context, OOJS_ARG(2));
role = OOStringFromJSValue(context, OOJS_ARG(0));
coordScheme = OOStringFromJSValue(context, OOJS_ARG(2));
if (EXPECT_NOT(role == nil ||
!JS_ValueToInt32(context, OOJS_ARG(1), &count) ||
count < 1 || 64 < count ||
@ -975,7 +975,7 @@ static JSBool SystemLegacySpawnShip(OOJS_NATIVE_ARGS)
NSString *key = nil;
OOPlayerForScripting(); // For backwards-compatibility
key = OOJSValToNSString(context, OOJS_ARG(0));
key = OOStringFromJSValue(context, OOJS_ARG(0));
if (key == nil)
{
OOJSReportBadArguments(context, @"System", @"legacy_addShipWithinRadius", argc, OOJS_ARGV, nil, @"ship key");
@ -1021,7 +1021,7 @@ static JSBool SystemStaticSystemIDForName(OOJS_NATIVE_ARGS)
NSString *name = nil;
unsigned result;
name = OOJSValToNSString(context, OOJS_ARG(0));
name = OOStringFromJSValue(context, OOJS_ARG(0));
if (name == nil)
{
OOJSReportBadArguments(context, @"System", @"systemIDForName", argc, OOJS_ARGV, nil, @"string");
@ -1086,7 +1086,7 @@ static JSBool SystemAddShipsOrGroup(OOJS_NATIVE_ARGS, BOOL isGroup)
NSString *func = isGroup ? @"addGroup" : @"addShips";
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (role == nil)
{
OOJSReportError(context, @"System.%@(): role not defined.", func);
@ -1153,7 +1153,7 @@ static JSBool SystemAddShipsOrGroupToRoute(OOJS_NATIVE_ARGS, BOOL isGroup)
NSString *func = isGroup ? @"addGroup" : @"addShips";
role = OOJSValToNSString(context, OOJS_ARG(0));
role = OOStringFromJSValue(context, OOJS_ARG(0));
if (role == nil)
{
OOJSReportError(context, @"System.%@(): role not defined.", func);
@ -1177,7 +1177,7 @@ static JSBool SystemAddShipsOrGroupToRoute(OOJS_NATIVE_ARGS, BOOL isGroup)
if (argc > 3 && !JSVAL_IS_NULL(OOJS_ARG(3)))
{
route = OOJSValToNSString(context, OOJS_ARG(3));
route = OOStringFromJSValue(context, OOJS_ARG(3));
if (!JSVAL_IS_STRING(OOJS_ARG(3)) || route == nil || [routes rangeOfString:[NSString stringWithFormat:@" %@",route] options:NSCaseInsensitiveSearch].length !=3)
{
OOJSReportError(context, @"System.%@(): expected %@, got '%@'.", func, @"route string", [NSString stringWithJavaScriptValue:OOJS_ARG(3) inContext:context]);

View File

@ -435,7 +435,7 @@ static JSBool SystemInfoSetProperty(OOJS_PROP_ARGS)
NSString *key = [NSString stringWithJavaScriptString:OOJS_PROPID_STRING];
OOSystemInfo *info = OOJSNativeObjectOfClassFromJSObject(context, this, [OOSystemInfo class]);
[info setValue:OOJSValToNSString(context, *value) forKey:key];
[info setValue:OOStringFromJSValue(context, *value) forKey:key];
}
return YES;
@ -507,7 +507,7 @@ static JSBool SystemInfoRouteToSystem(OOJS_NATIVE_ARGS)
if (argc >= 2)
{
routeType = StringToRouteType(OOJSValToNSString(context, OOJS_ARG(1)));
routeType = StringToRouteType(OOStringFromJSValue(context, OOJS_ARG(1)));
}
OOJS_BEGIN_FULL_NATIVE(context)

View File

@ -951,7 +951,7 @@ static JSBool VectorToCoordinateSystem(OOJS_NATIVE_ARGS)
if (EXPECT_NOT(!GetThisVector(context, OOJS_THIS, &thisv, @"toCoordinateSystem"))) return NO;
coordScheme = OOJSValToNSString(context, OOJS_ARG(0));
coordScheme = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(coordScheme == nil ||
argc < 1 ))
{
@ -980,7 +980,7 @@ static JSBool VectorFromCoordinateSystem(OOJS_NATIVE_ARGS)
if (EXPECT_NOT(!GetThisVector(context, OOJS_THIS, &thisv, @"fromCoordinateSystem"))) return NO;
coordScheme = OOJSValToNSString(context, OOJS_ARG(0));
coordScheme = OOStringFromJSValue(context, OOJS_ARG(0));
if (EXPECT_NOT(coordScheme == nil ||
argc < 1 ))
{

View File

@ -204,43 +204,44 @@ BOOL OOJSDefineProperty(JSContext *context, JSObject *object, NSString *name, js
@end
/* OOJSObjectWrapperFinalize
Finalizer for JS classes whose private storage is a retained object
reference (generally an OOWeakReference, but doesn't have to be).
/**** String utilities ****/
// Convert a JSString to an NSString.
NSString *OOStringFromJSString(JSString *string);
/* Convert an arbitrary JS object to an NSString, calling JS_ValueToString.
OOStringFromJSValue() returns nil if value is null or undefined,
OOStringFromJSValueEvenIfNull() returns "null" or "undefined".
*/
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this);
NSString *OOStringFromJSValue(JSContext *context, jsval value);
NSString *OOStringFromJSValueEvenIfNull(JSContext *context, jsval value);
@interface NSString (OOJavaScriptExtensions)
// Convert a JSString to an NSString.
+ (id)stringWithJavaScriptString:(JSString *)string;
// Convert an arbitrary JS object to an NSString, using JS_ValueToString.
+ (id) stringWithJavaScriptValue:(jsval)value inContext:(JSContext *)context;
+ (id)stringOrNilWithJavaScriptValue:(jsval)value inContext:(JSContext *)context;
// For diagnostic messages; produces things like @"(42, true, "a string", an object description)".
+ (id)stringWithJavaScriptParameters:(jsval *)params count:(uintN)count inContext:(JSContext *)context;
+ (id) stringWithJavaScriptParameters:(jsval *)params count:(uintN)count inContext:(JSContext *)context;
// Concatenate sequence of arbitrary JS objects into string.
+ (id)concatenationOfStringsFromJavaScriptValues:(jsval *)values count:(size_t)count separator:(NSString *)separator inContext:(JSContext *)context;
+ (id) concatenationOfStringsFromJavaScriptValues:(jsval *)values count:(size_t)count separator:(NSString *)separator inContext:(JSContext *)context;
// Add escape codes for string so that it's a valid JavaScript literal (if you put "" or '' around it).
- (NSString *)escapedForJavaScriptLiteral;
- (NSString *) escapedForJavaScriptLiteral;
// Wrapper for OOStringFromJSString(). DEPRECATED
+ (id) stringWithJavaScriptString:(JSString *)string;
// Wrapper for OOStringFromJSValueEvenIfNull(). DEPRECATED
+ (id) stringWithJavaScriptValue:(jsval)value inContext:(JSContext *)context;
// Wrapper for OOStringFromJSValue(). DEPRECATED
+ (id) stringOrNilWithJavaScriptValue:(jsval)value inContext:(JSContext *)context;
@end
OOINLINE NSString *OOJSValToNSString(JSContext *context, jsval value) ALWAYS_INLINE_FUNC;
OOINLINE NSString *OOJSValToNSString(JSContext *context, jsval value)
{
return [NSString stringOrNilWithJavaScriptValue:value inContext:context];
}
// OOEntityFilterPredicate wrapping a JavaScript function.
typedef struct
{
@ -278,6 +279,14 @@ OOINLINE JSClass *OOJSGetClass(JSContext *cx, JSObject *obj)
}
/* OOJSObjectWrapperFinalize
Finalizer for JS classes whose private storage is a retained object
reference (generally an OOWeakReference, but doesn't have to be).
*/
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this);
/* OOJSDictionaryFromStringTable(context, value);
Treat an arbitrary JavaScript object as a dictionary mapping strings to

View File

@ -1178,20 +1178,14 @@ static BOOL JSNewNSDictionaryValue(JSContext *context, NSDictionary *dictionary,
@end
@implementation NSString (OOJavaScriptExtensions)
// Convert a JSString to an NSString.
+ (id) stringWithJavaScriptString:(JSString *)string
NSString *OOStringFromJSString(JSString *string)
{
OOJS_PROFILE_ENTER
jschar *chars = NULL;
size_t length;
if (EXPECT_NOT(string == NULL)) return nil;
if (string == NULL) return nil;
chars = JS_GetStringChars(string);
length = JS_GetStringLength(string);
jschar *chars = JS_GetStringChars(string);
size_t length = JS_GetStringLength(string);
return [NSString stringWithCharacters:chars length:length];
@ -1199,46 +1193,53 @@ static BOOL JSNewNSDictionaryValue(JSContext *context, NSDictionary *dictionary,
}
+ (id) stringWithJavaScriptValue:(jsval)value inContext:(JSContext *)context
NSString *OOStringFromJSValueEvenIfNull(JSContext *context, jsval value)
{
OOJS_PROFILE_ENTER
// In some cases we didn't test the original stringWith... function for nil, causing difficult
// to track crashes. We now have two similar functions: stringWith... which never returns nil and
// stringOrNilWith... (alias OOJSValToNSString) which can return nil and is used in most cases.
if (JSVAL_IS_VOID(value)) return @"undefined";
if (JSVAL_IS_NULL(value)) return @"null";
return [self stringOrNilWithJavaScriptValue:value inContext:context];
NSCParameterAssert(context != NULL && JS_IsInRequest(context));
JSString *string = JS_ValueToString(context, value); // Calls the value's toString method if needed.
return [NSString stringWithJavaScriptString:string];
OOJS_PROFILE_EXIT
}
+ (id) stringOrNilWithJavaScriptValue:(jsval)value inContext:(JSContext *)context
NSString *OOStringFromJSValue(JSContext *context, jsval value)
{
OOJS_PROFILE_ENTER
JSString *string = NULL;
BOOL tempCtxt = NO;
if (JSVAL_IS_NULL(value) || JSVAL_IS_VOID(value)) return nil;
if (context == NULL)
if (EXPECT(!JSVAL_IS_NULL(value) && !JSVAL_IS_VOID(value)))
{
context = [[OOJavaScriptEngine sharedEngine] acquireContext];
tempCtxt = YES;
return OOStringFromJSValueEvenIfNull(context, value);
}
string = JS_ValueToString(context, value); // Calls the value's toString method if needed.
if (tempCtxt) [[OOJavaScriptEngine sharedEngine] releaseContext:context];
return [NSString stringWithJavaScriptString:string];
return nil;
OOJS_PROFILE_EXIT
}
@implementation NSString (OOJavaScriptExtensions)
+ (id) stringWithJavaScriptString:(JSString *)string
{
return OOStringFromJSString(string);
}
+ (id) stringOrNilWithJavaScriptValue:(jsval)value inContext:(JSContext *)context
{
return OOStringFromJSValue(context, value);
}
+ (id) stringWithJavaScriptValue:(jsval)value inContext:(JSContext *)context
{
return OOStringFromJSValueEvenIfNull(context, value);
}
+ (id) stringWithJavaScriptParameters:(jsval *)params count:(uintN)count inContext:(JSContext *)context
{
OOJS_PROFILE_ENTER
@ -1418,13 +1419,22 @@ static BOOL JSNewNSDictionaryValue(JSContext *context, NSDictionary *dictionary,
// For use in debugger
const char *JSValueToStrDbg(jsval val)
{
return [OOJSValToNSString(NULL, val) UTF8String];
OOJavaScriptEngine *jsEngine = [OOJavaScriptEngine sharedEngine];
JSContext *context = [jsEngine acquireContext];
JS_BeginRequest(context);
const char *result = [[NSString stringWithJavaScriptValue:val inContext:context] UTF8String];
JS_EndRequest(context);
[jsEngine releaseContext:context];
return result;
}
const char *JSObjectToStrDbg(JSObject *obj)
{
return [OOJSValToNSString(NULL, OBJECT_TO_JSVAL(obj)) UTF8String];
return JSValueToStrDbg(OBJECT_TO_JSVAL(obj));
}
@ -1730,7 +1740,7 @@ BOOL OOJSObjectGetterImpl(JSContext *context, JSObject *object, JSClass *require
JSClass *actualClass = OOJSGetClass(context, object);
if (EXPECT_NOT(!OOJSIsSubclass(actualClass, requiredJSClass)))
{
OOJSReportError(context, @"Native method expected %s, got %@.", requiredJSClass->name, OOJSValToNSString(context, OBJECT_TO_JSVAL(object)));
OOJSReportError(context, @"Native method expected %s, got %@.", requiredJSClass->name, OOStringFromJSValue(context, OBJECT_TO_JSVAL(object)));
return NO;
}
NSCAssert(actualClass->flags & JSCLASS_HAS_PRIVATE, @"Native object accessor requires JS class with private storage.");
@ -1811,7 +1821,7 @@ NSDictionary *OOJSDictionaryFromStringTable(JSContext *context, jsval tableValue
if (objKey != nil && !JSVAL_IS_VOID(value))
{
// Note: we want nulls and undefines included, so not OOJSValToNSString().
// Note: we want nulls and undefines included, so not OOStringFromJSValue().
objValue = [NSString stringWithJavaScriptValue:value inContext:context];
if (objValue != nil)
@ -1847,7 +1857,7 @@ id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
}
if (JSVAL_IS_STRING(value))
{
return OOJSValToNSString(context, value);
return OOStringFromJSValue(context, value);
}
if (JSVAL_IS_OBJECT(value))
{