Oolite
Loading...
Searching...
No Matches
OOJSMissionVariables.m
Go to the documentation of this file.
1/*
2
3OOJSMissionVariables.h
4
5JavaScript mission variables object.
6
7
8Oolite
9Copyright (C) 2004-2013 Giles C Williams and contributors
10
11This program is free software; you can redistribute it and/or
12modify it under the terms of the GNU General Public License
13as published by the Free Software Foundation; either version 2
14of the License, or (at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24MA 02110-1301, USA.
25
26*/
27
29#import "OOIsNumberLiteral.h"
31
32#import "OOJSPlayer.h"
33
34/* SpiderMonkey called the MissionVariables class's get, set and delete hooks
35 for every property access, and its enumerate hook for for-in. QuickJS
36 has no such hooks; the class's exotic methods provide the same view
37 instead: every mission variable appears as an enumerable own property,
38 assignments and deletions go to the player's mission variables, and
39 reading a missing (non-"_") name gives null, as before. Names starting
40 with "_" are not mission variables; reading them falls back to the
41 prototype (Object.prototype) and assigning them is an error. Properties
42 of Object.prototype (such as toString) are found there unless a mission
43 variable of the same name exists.
44*/
45
46// Property name for a string property ID, or nil for other IDs (symbols).
47static NSString* NameForPropertyID(JSContext* context, JSAtom propID)
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}
58
59static NSString* KeyForPropertyName(NSString* name)
60{
61 if (name == nil || [name hasPrefix:@"_"])
62 return nil;
63 return [@"mission_" stringByAppendingString:name];
64}
65
66static int MissionVariablesGetOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst object, JSAtom propID);
67static int MissionVariablesGetOwnPropertyNames(JSContext* context, JSPropertyEnum** outTable, uint32_t* outCount, JSValueConst object);
68static int MissionVariablesDeleteProperty(JSContext* context, JSValueConst object, JSAtom propID);
69static int MissionVariablesDefineOwnProperty(JSContext* context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst getter, JSValueConst setter, int flags);
70static JSValue MissionVariablesGetProperty(JSContext* context, JSValueConst object, JSAtom propID, JSValueConst receiver);
71static int MissionVariablesSetProperty(JSContext* context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst receiver, int flags);
72
73#ifdef OO_DEBUG_MONITOR_ENABLED
74static id MissionVariablesConverter(JSContext* context, JSValueConst object);
75#endif
76
77static JSClassExoticMethods sMissionVariablesExoticMethods = {
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};
85
86static JSClassID sMissionVariablesClassID;
87static JSClassDef sMissionVariablesClass = {
88 .class_name = "MissionVariables",
90};
91
92void InitOOJSMissionVariables(JSContext* context, JSValueConst global)
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}
107
108#ifdef OO_DEBUG_MONITOR_ENABLED
109static id MissionVariablesConverter(JSContext* context, JSValueConst object)
110{
111 return [PLAYER missionVariables];
112}
113#endif
114
115// Owned value of a mission variable, or JS_UNDEFINED if it doesn't exist.
116static JSValue ValueForMissionVariable(JSContext* context, NSString* key)
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}
133
134static int MissionVariablesGetOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst object, JSAtom propID)
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}
164
165static int MissionVariablesGetOwnPropertyNames(JSContext* context, JSPropertyEnum** outTable, uint32_t* outCount, JSValueConst object)
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}
209
210static int MissionVariablesDeleteProperty(JSContext* context, JSValueConst object, JSAtom propID)
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}
231
232static JSValue MissionVariablesGetProperty(JSContext* context, JSValueConst object, JSAtom propID, JSValueConst receiver)
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}
267
268static BOOL SetMissionVariable(JSContext* context, JSAtom propID, JSValueConst value)
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}
288
289static int MissionVariablesSetProperty(JSContext* context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst receiver, int flags)
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}
303
304static int MissionVariablesDefineOwnProperty(JSContext* context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst getter, JSValueConst setter, int flags)
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}
BOOL OOIsNumberLiteral(NSString *string, BOOL allowSpaces)
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
void InitOOJSMissionVariables(JSContext *context, JSObject *global)
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 JSClass sMissionVariablesClass
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
NSString * OOStringFromJSID(jsid propID)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
#define OOJS_PROP_READONLY
void OOJSReportError(JSContext *context, NSString *format,...)
void OOJSReportWrappedException(JSContext *context, id exception)
#define MAX(A, B)
Definition OOMaths.h:94
unsigned count
return nil
static int MissionVariablesGetOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom propID)
static JSValue ValueForMissionVariable(JSContext *context, NSString *key)
static JSClassExoticMethods sMissionVariablesExoticMethods
static NSString * NameForPropertyID(JSContext *context, JSAtom propID)
static NSString * KeyForPropertyName(NSString *name)
static JSClassID sMissionVariablesClassID
static int MissionVariablesDefineOwnProperty(JSContext *context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst getter, JSValueConst setter, int flags)
static BOOL SetMissionVariable(JSContext *context, JSAtom propID, JSValueConst value)
static int MissionVariablesGetOwnPropertyNames(JSContext *context, JSPropertyEnum **outTable, uint32_t *outCount, JSValueConst object)
JSValue OOJSObjectPrototype(JSContext *context)
Definition OOJSClasses.m:81
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
NSString * missionVariableForKey:(NSString *key)
void setMissionVariable:forKey:(NSString *value,[forKey] NSString *key)