Oolite 1.91.0.7646-241128-10e222e
Loading...
Searching...
No Matches
OOJSMissionVariables.m File Reference
+ Include dependency graph for OOJSMissionVariables.m:

Go to the source code of this file.

Functions

static NSString * KeyForPropertyID (JSContext *context, jsid propID)
 
static JSBool MissionVariablesDeleteProperty (JSContext *context, JSObject *this, jsid propID, jsval *value)
 
static JSBool MissionVariablesGetProperty (JSContext *context, JSObject *this, jsid propID, jsval *value)
 
static JSBool MissionVariablesSetProperty (JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
 
static JSBool MissionVariablesEnumerate (JSContext *context, JSObject *object, JSIterateOp enumOp, jsval *state, jsid *idp)
 
static id MissionVariablesConverter (JSContext *context, JSObject *object)
 
void InitOOJSMissionVariables (JSContext *context, JSObject *global)
 

Variables

static JSClass sMissionVariablesClass
 

Function Documentation

◆ InitOOJSMissionVariables()

void InitOOJSMissionVariables ( JSContext * context,
JSObject * global )

Definition at line 71 of file OOJSMissionVariables.m.

72{
73 JS_DefineObject(context, global, "missionVariables", &sMissionVariablesClass, NULL, OOJS_PROP_READONLY);
74
75#ifndef NDEBUG
76 // Allow callObjC() on missionVariables to call methods on the mission variables dictionary.
78#endif
79}
static id MissionVariablesConverter(JSContext *context, JSObject *object)
static JSClass sMissionVariablesClass
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
#define OOJS_PROP_READONLY

References MissionVariablesConverter(), OOJS_PROP_READONLY, OOJSRegisterObjectConverter(), and sMissionVariablesClass.

+ Here is the call graph for this function:

◆ KeyForPropertyID()

static NSString * KeyForPropertyID ( JSContext * context,
jsid propID )
static

Definition at line 35 of file OOJSMissionVariables.m.

36{
37 NSCParameterAssert(JSID_IS_STRING(propID));
38
39 NSString *key = OOStringFromJSString(context, JSID_TO_STRING(propID));
40 if ([key hasPrefix:@"_"]) return nil;
41 return [@"mission_" stringByAppendingString:key];
42}
NSString * OOStringFromJSString(JSContext *context, JSString *string)
return nil

References nil, and OOStringFromJSString().

Referenced by MissionVariablesDeleteProperty(), MissionVariablesGetProperty(), and MissionVariablesSetProperty().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MissionVariablesConverter()

static id MissionVariablesConverter ( JSContext * context,
JSObject * object )
static

Definition at line 83 of file OOJSMissionVariables.m.

84{
85 return [PLAYER missionVariables];
86}

Referenced by InitOOJSMissionVariables().

+ Here is the caller graph for this function:

◆ MissionVariablesDeleteProperty()

static JSBool MissionVariablesDeleteProperty ( JSContext * context,
JSObject * this,
jsid propID,
jsval * value )
static

Definition at line 90 of file OOJSMissionVariables.m.

91{
92 OOJS_NATIVE_ENTER(context)
93
95
96 if (JSID_IS_STRING(propID))
97 {
98 NSString *key = KeyForPropertyID(context, propID);
99 [player setMissionVariable:nil forKey:key];
100 }
101 return YES;
102
104}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
static NSString * KeyForPropertyID(JSContext *context, jsid propID)
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:191
void setMissionVariable:forKey:(NSString *value,[forKey] NSString *key)

References KeyForPropertyID(), OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOPlayerForScripting(), and PlayerEntity::setMissionVariable:forKey:.

+ Here is the call graph for this function:

◆ MissionVariablesEnumerate()

static JSBool MissionVariablesEnumerate ( JSContext * context,
JSObject * object,
JSIterateOp enumOp,
jsval * state,
jsid * idp )
static

Definition at line 162 of file OOJSMissionVariables.m.

163{
164 OOJS_NATIVE_ENTER(context)
165
166 NSEnumerator *enumerator = nil;
167
168 switch (enumOp)
169 {
170 case JSENUMERATE_INIT:
171 case JSENUMERATE_INIT_ALL: // For ES5 Object.getOwnPropertyNames(). Since we have no non-enumerable properties, this is the same as _INIT.
172 {
173 // -allKeys implicitly makes a copy, which is good since the enumerating code might mutate.
174 NSArray *mvars = [[PLAYER missionVariables] allKeys];
175 enumerator = [[mvars objectEnumerator] retain];
176 *state = PRIVATE_TO_JSVAL(enumerator);
177
178 NSUInteger count = [mvars count];
179 assert(count <= INT32_MAX);
180 if (idp != NULL) *idp = INT_TO_JSID((uint32_t)count);
181 return YES;
182 }
183
184 case JSENUMERATE_NEXT:
185 {
186 enumerator = JSVAL_TO_PRIVATE(*state);
187 for (;;)
188 {
189 NSString *next = [enumerator nextObject];
190 if (next == nil) break;
191 if (![next hasPrefix:@"mission_"]) continue; // Skip mission instructions, which aren't visible through missionVariables.
192
193 next = [next substringFromIndex:8]; // Cut off "mission_".
194
195 jsval val = [next oo_jsValueInContext:context];
196 return JS_ValueToId(context, val, idp);
197 }
198
199 // If we got here, we've hit the end of the enumerator.
200 *state = JSVAL_NULL;
201 // Fall through.
202 }
203
204 case JSENUMERATE_DESTROY:
205 {
206 if (enumerator == nil && JSVAL_IS_DOUBLE(*state))
207 {
208 enumerator = JSVAL_TO_PRIVATE(*state);
209 }
210 [enumerator release];
211
212 if (idp != NULL) *idp = JSID_VOID;
213 return YES;
214 }
215 }
216
218}
unsigned count

References count, nil, OOJS_NATIVE_ENTER, and OOJS_NATIVE_EXIT.

◆ MissionVariablesGetProperty()

static JSBool MissionVariablesGetProperty ( JSContext * context,
JSObject * this,
jsid propID,
jsval * value )
static

Definition at line 107 of file OOJSMissionVariables.m.

108{
109 OOJS_NATIVE_ENTER(context)
110
112
113 if (JSID_IS_STRING(propID))
114 {
115 NSString *key = KeyForPropertyID(context, propID);
116 if (key == nil) return YES;
117
118 id mvar = [player missionVariableForKey:key];
119
120 if ([mvar isKindOfClass:[NSString class]]) // Currently there should only be strings, but we may want to change this.
121 {
122 if (OOIsNumberLiteral(mvar, YES))
123 {
124 return JS_NewNumberValue(context, [mvar doubleValue], value);
125 }
126 }
127
128 *value = OOJSValueFromNativeObject(context, mvar);
129 }
130 return YES;
131
133}
BOOL OOIsNumberLiteral(NSString *string, BOOL allowSpaces)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
NSString * missionVariableForKey:(NSString *key)

References KeyForPropertyID(), PlayerEntity::missionVariableForKey:, nil, OOIsNumberLiteral(), OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSValueFromNativeObject(), and OOPlayerForScripting().

+ Here is the call graph for this function:

◆ MissionVariablesSetProperty()

static JSBool MissionVariablesSetProperty ( JSContext * context,
JSObject * this,
jsid propID,
JSBool strict,
jsval * value )
static

Definition at line 136 of file OOJSMissionVariables.m.

137{
138 OOJS_NATIVE_ENTER(context)
139
141
142 if (JSID_IS_STRING(propID))
143 {
144 NSString *key = KeyForPropertyID(context, propID);
145 if (key == nil)
146 {
147 OOJSReportError(context, @"Invalid mission variable name \"%@\".", [OOStringFromJSID(propID) escapedForJavaScriptLiteral]);
148 return NO;
149 }
150
151 NSString *objValue = OOStringFromJSValue(context, *value);
152
153 if ([objValue isKindOfClass:[NSNull class]]) objValue = nil;
154 [player setMissionVariable:objValue forKey:key];
155 }
156 return YES;
157
159}
NSString * OOStringFromJSID(jsid propID)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
void OOJSReportError(JSContext *context, NSString *format,...)

References KeyForPropertyID(), nil, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSReportError(), OOPlayerForScripting(), OOStringFromJSID(), OOStringFromJSValue(), and PlayerEntity::setMissionVariable:forKey:.

+ Here is the call graph for this function:

Variable Documentation

◆ sMissionVariablesClass

JSClass sMissionVariablesClass
static
Initial value:
=
{
"MissionVariables",
JSCLASS_NEW_ENUMERATE,
JS_PropertyStub,
(JSEnumerateOp)MissionVariablesEnumerate,
JS_ResolveStub,
JS_ConvertStub,
JS_FinalizeStub
}
static JSBool MissionVariablesGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool MissionVariablesSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
static JSBool MissionVariablesEnumerate(JSContext *context, JSObject *object, JSIterateOp enumOp, jsval *state, jsid *idp)
static JSBool MissionVariablesDeleteProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)

Definition at line 55 of file OOJSMissionVariables.m.

56{
57 "MissionVariables",
58 JSCLASS_NEW_ENUMERATE,
59
60 JS_PropertyStub,
64 (JSEnumerateOp)MissionVariablesEnumerate,
65 JS_ResolveStub,
66 JS_ConvertStub,
67 JS_FinalizeStub
68};

Referenced by InitOOJSMissionVariables().