Oolite
Loading...
Searching...
No Matches
OOJSWorldScripts.m
Go to the documentation of this file.
1/*
2
3OOJSWorldScripts.m
4
5
6Oolite
7Copyright (C) 2004-2013 Giles C Williams and contributors
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSWorldScripts.h"
27#import "OOJSPlayer.h"
29#import "PlayerEntity.h"
30
31/* SpiderMonkey called the WorldScripts class's getProperty hook for every
32 property access and its enumerate hook defined each script's name on
33 demand. QuickJS has no such hooks; instead, the class's exotic
34 get_own_property and get_own_property_names methods make every world
35 script appear as an enumerable, read-only own property.
36*/
37static int WorldScriptsGetOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst object, JSAtom propID);
38static int WorldScriptsGetOwnPropertyNames(JSContext* context, JSPropertyEnum** outTable, uint32_t* outCount, JSValueConst object);
39
40static JSClassExoticMethods sWorldScriptsExoticMethods = {
41 .get_own_property = WorldScriptsGetOwnProperty,
42 .get_own_property_names = WorldScriptsGetOwnPropertyNames,
43};
44
45static JSClassID sWorldScriptsClassID;
46static JSClassDef sWorldScriptsClass = {
47 .class_name = "WorldScripts",
49};
50
51void InitOOJSWorldScripts(JSContext* context, JSValueConst global)
52{
54
55 // As with JS_DefineObject() with a NULL prototype, the prototype is Object.prototype.
56 JSValue objectPrototype = OOJSObjectPrototype(context);
57 JSValue worldScripts = JS_NewObjectProtoClass(context, objectPrototype, sWorldScriptsClassID);
58 JS_FreeValue(context, objectPrototype);
59 JS_DefinePropertyValueStr(context, global, "worldScripts", worldScripts, OOJS_PROP_READONLY);
60}
61
62// Name of a world script for a string property ID, or nil for other IDs (symbols and integers).
63static NSString* ScriptNameForPropertyID(JSContext* context, JSAtom propID)
64{
65 NSString* result = nil;
66 JSValue idValue = JS_AtomToValue(context, propID);
67 if (JS_IsString(idValue)) {
68 result = OOStringFromJSValue(context, idValue);
69 }
70 JS_FreeValue(context, idValue);
71 return result;
72}
73
74static int WorldScriptsGetOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst object, JSAtom propID)
75{
77
78 @try {
80 NSString* scriptName = nil;
81 id script = nil;
82
83 scriptName = ScriptNameForPropertyID(context, propID);
84
85 if (scriptName != nil) {
86 script = [[player worldScriptsByName] objectForKey:scriptName];
87 if (script != nil) {
88 if (desc != NULL) {
89 /* If script is an OOJSScript, this should return a JS Script
90 object. For other OOScript subclasses, it will return
91 JSVAL_NULL. If no script exists, the value will be
92 JSVAL_VOID.
93 */
94 desc->flags = OOJS_PROP_READONLY;
95 desc->value = [script oo_jsValueInContext:context];
96 desc->getter = JS_UNDEFINED;
97 desc->setter = JS_UNDEFINED;
98 }
99 return YES;
100 }
101 }
102
103 return NO;
104 }
105 @catch (id exception) {
106 OOJSReportWrappedException(context, exception);
107 return -1;
108 }
109
111}
112
113static int WorldScriptsGetOwnPropertyNames(JSContext* context, JSPropertyEnum** outTable, uint32_t* outCount, JSValueConst object)
114{
116
117 /* In order to support enumeration of world scripts (e.g.,
118 for (name in worldScripts) { ... }), report each name.
119 QuickJS frees the table and its atoms.
120 */
121 @try {
122 NSArray* names = [OOPlayerForScripting() worldScriptNames];
123 uint32_t i, count = (uint32_t)[names count];
124
125 JSPropertyEnum* table = js_malloc(context, sizeof *table * MAX(count, 1U));
126 if (table == NULL)
127 return -1;
128
129 for (i = 0; i < count; i++) {
130 table[i].is_enumerable = true;
131 table[i].atom = JS_NewAtom(context, [[names objectAtIndex:i] UTF8String]);
132 if (table[i].atom == JS_ATOM_NULL) {
133 JS_FreePropertyEnum(context, table, i);
134 return -1;
135 }
136 }
137
138 *outTable = table;
139 *outCount = count;
140 return 0;
141 }
142 @catch (id exception) {
143 OOJSReportWrappedException(context, exception);
144 return -1;
145 }
146
148}
#define OOJS_PROFILE_EXIT
#define OOJS_PROFILE_ENTER
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
void InitOOJSWorldScripts(JSContext *context, JSObject *global)
static JSClass sWorldScriptsClass
NSString * OOStringFromJSValue(JSContext *context, jsval value)
#define OOJS_PROP_READONLY
void OOJSReportWrappedException(JSContext *context, id exception)
#define MAX(A, B)
Definition OOMaths.h:94
unsigned count
return nil
static int WorldScriptsGetOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom propID)
static JSClassExoticMethods sWorldScriptsExoticMethods
static NSString * ScriptNameForPropertyID(JSContext *context, JSAtom propID)
static int WorldScriptsGetOwnPropertyNames(JSContext *context, JSPropertyEnum **outTable, uint32_t *outCount, JSValueConst object)
static JSClassID sWorldScriptsClassID
JSValue OOJSObjectPrototype(JSContext *context)
Definition OOJSClasses.m:81
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
NSDictionary * worldScriptsByName()
NSArray * worldScriptNames()