Oolite
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 * NameForPropertyID (JSContext *context, JSAtom propID)
static NSString * KeyForPropertyName (NSString *name)
static int MissionVariablesGetOwnProperty (JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom propID)
static int MissionVariablesGetOwnPropertyNames (JSContext *context, JSPropertyEnum **outTable, uint32_t *outCount, JSValueConst object)
static int MissionVariablesDeleteProperty (JSContext *context, JSValueConst object, JSAtom propID)
static int MissionVariablesDefineOwnProperty (JSContext *context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst getter, JSValueConst setter, int flags)
static JSValue MissionVariablesGetProperty (JSContext *context, JSValueConst object, JSAtom propID, JSValueConst receiver)
static int MissionVariablesSetProperty (JSContext *context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst receiver, int flags)
void InitOOJSMissionVariables (JSContext *context, JSValueConst global)
static JSValue ValueForMissionVariable (JSContext *context, NSString *key)
static BOOL SetMissionVariable (JSContext *context, JSAtom propID, JSValueConst value)

Variables

static JSClassExoticMethods sMissionVariablesExoticMethods
static JSClassID sMissionVariablesClassID
static JSClassDef sMissionVariablesClass

Function Documentation

◆ InitOOJSMissionVariables()

void InitOOJSMissionVariables ( JSContext * context,
JSValueConst global )

Definition at line 92 of file OOJSMissionVariables.m.

93{
95
96 // As with JS_DefineObject() with a NULL prototype, the prototype is Object.prototype.
97 JSValue objectPrototype = OOJSObjectPrototype(context);
98 JSValue missionVariables = JS_NewObjectProtoClass(context, objectPrototype, sMissionVariablesClassID);
99 JS_FreeValue(context, objectPrototype);
100 JS_DefinePropertyValueStr(context, global, "missionVariables", missionVariables, OOJS_PROP_READONLY);
101
102#ifdef OO_DEBUG_MONITOR_ENABLED
103 // Allow callObjC() on missionVariables to call methods on the mission variables dictionary.
104 OOJSRegisterObjectConverter(sMissionVariablesClassID, MissionVariablesConverter);
105#endif
106}
static JSClass sMissionVariablesClass
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
#define OOJS_PROP_READONLY
static JSClassID sMissionVariablesClassID
JSValue OOJSObjectPrototype(JSContext *context)
Definition OOJSClasses.m:81
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61

References OOJS_PROP_READONLY, OOJSObjectPrototype(), OOJSRegisterClass(), OOJSRegisterObjectConverter(), sMissionVariablesClass, and sMissionVariablesClassID.

Here is the call graph for this function:

◆ KeyForPropertyName()

NSString * KeyForPropertyName ( NSString * name)
static

Definition at line 59 of file OOJSMissionVariables.m.

60{
61 if (name == nil || [name hasPrefix:@"_"])
62 return nil;
63 return [@"mission_" stringByAppendingString:name];
64}
return nil

References nil.

Referenced by MissionVariablesDeleteProperty(), MissionVariablesGetOwnProperty(), MissionVariablesGetProperty(), and SetMissionVariable().

Here is the caller graph for this function:

◆ MissionVariablesDefineOwnProperty()

int MissionVariablesDefineOwnProperty ( JSContext * context,
JSValueConst object,
JSAtom propID,
JSValueConst value,
JSValueConst getter,
JSValueConst setter,
int flags )
static

Definition at line 304 of file OOJSMissionVariables.m.

305{
306 /* Object.defineProperty() and friends. Mission variables can't be
307 accessors; a defined value is stored like an assignment.
308 */
309 @try {
310 if (flags & (JS_PROP_HAS_GET | JS_PROP_HAS_SET)) {
311 OOJSReportError(context, @"Invalid mission variable name \"%@\".", [OOStringFromJSID(propID) escapedForJavaScriptLiteral]);
312 return -1;
313 }
314 if (!(flags & JS_PROP_HAS_VALUE))
315 return YES;
316 return SetMissionVariable(context, propID, value) ? YES : -1;
317 }
318 @catch (id exception) {
319 OOJSReportWrappedException(context, exception);
320 return -1;
321 }
322}
NSString * OOStringFromJSID(jsid propID)
void OOJSReportError(JSContext *context, NSString *format,...)
void OOJSReportWrappedException(JSContext *context, id exception)
static BOOL SetMissionVariable(JSContext *context, JSAtom propID, JSValueConst value)

References OOJSReportError(), OOJSReportWrappedException(), OOStringFromJSID(), and SetMissionVariable().

Here is the call graph for this function:

◆ MissionVariablesDeleteProperty()

int MissionVariablesDeleteProperty ( JSContext * context,
JSValueConst object,
JSAtom propID )
static

Definition at line 210 of file OOJSMissionVariables.m.

211{
213
214 @try {
216
217 NSString* name = NameForPropertyID(context, propID);
218 if (name != nil) {
219 NSString* key = KeyForPropertyName(name);
220 [player setMissionVariable:nil forKey:key];
221 }
222 return YES;
223 }
224 @catch (id exception) {
225 OOJSReportWrappedException(context, exception);
226 return -1;
227 }
228
230}
#define OOJS_PROFILE_EXIT
#define OOJS_PROFILE_ENTER
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
static NSString * NameForPropertyID(JSContext *context, JSAtom propID)
static NSString * KeyForPropertyName(NSString *name)
void setMissionVariable:forKey:(NSString *value,[forKey] NSString *key)

References KeyForPropertyName(), NameForPropertyID(), nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, OOJSReportWrappedException(), OOPlayerForScripting(), and PlayerEntity::setMissionVariable:forKey:.

Here is the call graph for this function:

◆ MissionVariablesGetOwnProperty()

int MissionVariablesGetOwnProperty ( JSContext * context,
JSPropertyDescriptor * desc,
JSValueConst object,
JSAtom propID )
static

Definition at line 134 of file OOJSMissionVariables.m.

135{
137
138 @try {
139 NSString* key = KeyForPropertyName(NameForPropertyID(context, propID));
140 if (key == nil)
141 return NO;
142
143 JSValue value = ValueForMissionVariable(context, key);
144 if (JS_IsUndefined(value))
145 return NO;
146
147 if (desc != NULL) {
148 desc->flags = JS_PROP_C_W_E;
149 desc->value = value;
150 desc->getter = JS_UNDEFINED;
151 desc->setter = JS_UNDEFINED;
152 } else {
153 JS_FreeValue(context, value);
154 }
155 return YES;
156 }
157 @catch (id exception) {
158 OOJSReportWrappedException(context, exception);
159 return -1;
160 }
161
163}
static JSValue ValueForMissionVariable(JSContext *context, NSString *key)

References KeyForPropertyName(), NameForPropertyID(), nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, OOJSReportWrappedException(), and ValueForMissionVariable().

Here is the call graph for this function:

◆ MissionVariablesGetOwnPropertyNames()

int MissionVariablesGetOwnPropertyNames ( JSContext * context,
JSPropertyEnum ** outTable,
uint32_t * outCount,
JSValueConst object )
static

Definition at line 165 of file OOJSMissionVariables.m.

166{
168
169 @try {
170 // -allKeys implicitly makes a copy, which is good since the enumerating code might mutate.
171 NSArray* mvars = [[PLAYER missionVariables] allKeys];
172 NSUInteger count = [mvars count];
173 assert(count <= INT32_MAX);
174
175 JSPropertyEnum* table = js_malloc(context, sizeof *table * MAX(count, (NSUInteger)1));
176 if (table == NULL)
177 return -1;
178
179 uint32_t used = 0;
180 NSString* next = nil;
181 foreach (next, mvars) {
182 if (![next hasPrefix:@"mission_"])
183 continue; // Skip mission instructions, which aren't visible through missionVariables.
184
185 next = [next substringFromIndex:8]; // Cut off "mission_".
186
187 JSAtom atom = JS_NewAtom(context, [next UTF8String]);
188 if (atom == JS_ATOM_NULL) {
189 JS_FreePropertyEnum(context, table, used);
190 return -1;
191 }
192 table[used].is_enumerable = true;
193 table[used].atom = atom;
194 used++;
195 }
196
197 // QuickJS frees the table and its atoms.
198 *outTable = table;
199 *outCount = used;
200 return 0;
201 }
202 @catch (id exception) {
203 OOJSReportWrappedException(context, exception);
204 return -1;
205 }
206
208}
#define MAX(A, B)
Definition OOMaths.h:94
unsigned count

References count, MAX, nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, and OOJSReportWrappedException().

Here is the call graph for this function:

◆ MissionVariablesGetProperty()

JSValue MissionVariablesGetProperty ( JSContext * context,
JSValueConst object,
JSAtom propID,
JSValueConst receiver )
static

Definition at line 232 of file OOJSMissionVariables.m.

233{
234 OOJS_NATIVE_ENTER(context)
235
236 NSString* key = KeyForPropertyName(NameForPropertyID(context, propID));
237 if (key != nil) {
238 JSValue value = ValueForMissionVariable(context, key);
239 if (!JS_IsUndefined(value))
240 return value;
241 }
242
243 // Not a mission variable; look in the prototype chain.
244 JSValue prototype = JS_GetPrototype(context, object);
245 if (JS_IsException(prototype))
246 return JS_EXCEPTION;
247
248 JSValue result = JS_UNDEFINED;
249 if (JS_IsObject(prototype)) {
250 int found = (key != nil) ? JS_HasProperty(context, prototype, propID) : YES;
251 if (found < 0) {
252 result = JS_EXCEPTION;
253 } else if (found) {
254 result = JS_GetProperty(context, prototype, propID);
255 } else {
256 // Missing mission variables read as null (OOJSValueFromNativeObject(nil)).
257 result = JS_NULL;
258 }
259 } else if (key != nil) {
260 result = JS_NULL;
261 }
262 JS_FreeValue(context, prototype);
263 return result;
264
266}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT

References KeyForPropertyName(), NameForPropertyID(), nil, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, and ValueForMissionVariable().

Here is the call graph for this function:

◆ MissionVariablesSetProperty()

int MissionVariablesSetProperty ( JSContext * context,
JSValueConst object,
JSAtom propID,
JSValueConst value,
JSValueConst receiver,
int flags )
static

Definition at line 289 of file OOJSMissionVariables.m.

290{
292
293 @try {
294 return SetMissionVariable(context, propID, value) ? YES : -1;
295 }
296 @catch (id exception) {
297 OOJSReportWrappedException(context, exception);
298 return -1;
299 }
300
302}

References OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, OOJSReportWrappedException(), and SetMissionVariable().

Here is the call graph for this function:

◆ NameForPropertyID()

NSString * NameForPropertyID ( JSContext * context,
JSAtom propID )
static

Definition at line 47 of file OOJSMissionVariables.m.

48{
49 NSString* result = nil;
50 JSValue idValue = JS_AtomToValue(context, propID);
51 // SpiderMonkey's hooks ignored integer IDs; QuickJS uses integer atoms for names like "0".
52 if (JS_IsString(idValue)) {
53 result = OOStringFromJSValue(context, idValue);
54 }
55 JS_FreeValue(context, idValue);
56 return result;
57}
NSString * OOStringFromJSValue(JSContext *context, jsval value)

References nil, and OOStringFromJSValue().

Referenced by MissionVariablesDeleteProperty(), MissionVariablesGetOwnProperty(), MissionVariablesGetProperty(), and SetMissionVariable().

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

◆ SetMissionVariable()

BOOL SetMissionVariable ( JSContext * context,
JSAtom propID,
JSValueConst value )
static

Definition at line 268 of file OOJSMissionVariables.m.

269{
271
272 NSString* name = NameForPropertyID(context, propID);
273 if (name != nil) {
274 NSString* key = KeyForPropertyName(name);
275 if (key == nil) {
276 OOJSReportError(context, @"Invalid mission variable name \"%@\".", [name escapedForJavaScriptLiteral]);
277 return NO;
278 }
279
280 NSString* objValue = OOStringFromJSValue(context, value);
281
282 if ([objValue isKindOfClass:[NSNull class]])
283 objValue = nil;
284 [player setMissionVariable:objValue forKey:key];
285 }
286 return YES;
287}

References KeyForPropertyName(), NameForPropertyID(), nil, OOJSReportError(), OOPlayerForScripting(), OOStringFromJSValue(), and PlayerEntity::setMissionVariable:forKey:.

Referenced by MissionVariablesDefineOwnProperty(), and MissionVariablesSetProperty().

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

◆ ValueForMissionVariable()

JSValue ValueForMissionVariable ( JSContext * context,
NSString * key )
static

Definition at line 116 of file OOJSMissionVariables.m.

117{
119 id mvar = [player missionVariableForKey:key];
120
121 if (mvar == nil)
122 return JS_UNDEFINED;
123
124 if ([mvar isKindOfClass:[NSString class]]) // Currently there should only be strings, but we may want to change this.
125 {
126 if (OOIsNumberLiteral(mvar, YES)) {
127 return JS_NewNumber(context, [mvar doubleValue]);
128 }
129 }
130
131 return OOJSValueFromNativeObject(context, mvar);
132}
BOOL OOIsNumberLiteral(NSString *string, BOOL allowSpaces)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
NSString * missionVariableForKey:(NSString *key)

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

Referenced by MissionVariablesGetOwnProperty(), and MissionVariablesGetProperty().

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

Variable Documentation

◆ sMissionVariablesClass

JSClassDef sMissionVariablesClass
static
Initial value:
= {
.class_name = "MissionVariables",
}
static JSClassExoticMethods sMissionVariablesExoticMethods

Definition at line 87 of file OOJSMissionVariables.m.

87 {
88 .class_name = "MissionVariables",
90};

◆ sMissionVariablesClassID

JSClassID sMissionVariablesClassID
static

Definition at line 86 of file OOJSMissionVariables.m.

Referenced by InitOOJSMissionVariables().

◆ sMissionVariablesExoticMethods

JSClassExoticMethods sMissionVariablesExoticMethods
static
Initial value:
= {
.get_own_property = MissionVariablesGetOwnProperty,
.get_own_property_names = MissionVariablesGetOwnPropertyNames,
.delete_property = MissionVariablesDeleteProperty,
.define_own_property = MissionVariablesDefineOwnProperty,
.get_property = MissionVariablesGetProperty,
.set_property = MissionVariablesSetProperty,
}
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 MissionVariablesDeleteProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static int MissionVariablesGetOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom propID)
static int MissionVariablesDefineOwnProperty(JSContext *context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst getter, JSValueConst setter, int flags)
static int MissionVariablesGetOwnPropertyNames(JSContext *context, JSPropertyEnum **outTable, uint32_t *outCount, JSValueConst object)

Definition at line 77 of file OOJSMissionVariables.m.

77 {
78 .get_own_property = MissionVariablesGetOwnProperty,
79 .get_own_property_names = MissionVariablesGetOwnPropertyNames,
80 .delete_property = MissionVariablesDeleteProperty,
81 .define_own_property = MissionVariablesDefineOwnProperty,
82 .get_property = MissionVariablesGetProperty,
83 .set_property = MissionVariablesSetProperty,
84};