Oolite
Loading...
Searching...
No Matches
OOJSClasses.m
Go to the documentation of this file.
1/*
2
3OOJSClasses.m (QuickJS-ng)
4
5JS class registration, subclass relationships, and SpiderMonkey-compatible
6own properties for native classes.
7
8Copyright (C) 2007-2013 David Taylor and Jens Ayton.
9
10This program is free software; you can redistribute it and/or
11modify it under the terms of the GNU General Public License
12as published by the Free Software Foundation; either version 2
13of the License, or (at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23MA 02110-1301, USA.
24
25*/
26
28
29// Indexed by class ID; process lifetime, as are class IDs.
30static const char* sClassNames[kOOJSMaxClassID];
31static JSClassID sSuperclasses[kOOJSMaxClassID];
32
33/* Per class: the atoms of the getter/setter properties defined on its
34 prototype by OOJSInitClass(), which SpiderMonkey reported as own
35 properties of every instance (shared permanent properties), in
36 definition order (as Object.keys() listed them) and sorted for lookup.
37 Atoms are interned for the life of the process, like class IDs.
38
39 Only the direct prototype's properties counted: for instance, a ship's
40 Ship properties are own properties, but its Entity properties are not
41 (checked against SpiderMonkey with tools/js-parity).
42*/
43typedef struct
44{
45 JSAtom* atoms;
46 JSAtom* sortedAtoms;
47 uint32_t count;
49
50static SharedPropertyTable sSharedProperties[kOOJSMaxClassID];
51
52/* Each class without exotic methods of its own gets its own, initially
53 empty, JSClassExoticMethods; the hooks are only filled in when
54 OOJSInitClass() records shared properties for it. Classes without
55 accessor properties (Script, Timer...) therefore pay nothing on property
56 lookups, which matters for Script objects: they are the with() scope of
57 every script, so all bare identifier lookups pass through them.
58*/
59static JSClassExoticMethods* sOwnExoticMethods[kOOJSMaxClassID];
60
61JSClassID OOJSRegisterClass(JSClassID* ioClassID, const JSClassDef* classDef)
62{
63 NSCParameterAssert(ioClassID != NULL && classDef != NULL);
64
65 JSRuntime* runtime = JS_GetRuntime(OOJSAcquireContext());
66 JSClassID classID = JS_NewClassID(runtime, ioClassID);
67 NSCAssert(classID < kOOJSMaxClassID, @"QuickJS class ID out of range.");
68
69 if (!JS_IsRegisteredClass(runtime, classID)) {
70 JSClassDef def = *classDef;
71 if (def.exotic == NULL) {
72 sOwnExoticMethods[classID] = calloc(1, sizeof(JSClassExoticMethods));
73 def.exotic = sOwnExoticMethods[classID];
74 }
75 JS_NewClass(runtime, classID, &def);
76 sClassNames[classID] = classDef->class_name;
77 }
78 return classID;
79}
80
81JSValue OOJSObjectPrototype(JSContext* context)
82{
83 JSValue object = JS_NewObject(context);
84 JSValue result = JS_GetPrototype(context, object);
85 JS_FreeValue(context, object);
86 return result;
87}
88
89const char* OOJSClassName(JSClassID classID)
90{
91 return (classID < kOOJSMaxClassID) ? sClassNames[classID] : NULL;
92}
93
94void OOJSRegisterSubclass(JSClassID subclass, JSClassID superclass)
95{
96 NSCParameterAssert(subclass != 0 && superclass != 0 && subclass < kOOJSMaxClassID);
97 NSCAssert(sSuperclasses[subclass] == 0 || sSuperclasses[subclass] == superclass, @"A JS class cannot be registered as a subclass of multiple classes.");
98
99 sSuperclasses[subclass] = superclass;
100}
101
102BOOL OOJSIsSubclass(JSClassID putativeSubclass, JSClassID superclass)
103{
104 while (putativeSubclass != 0 && putativeSubclass < kOOJSMaxClassID) {
105 if (putativeSubclass == superclass)
106 return YES;
107
108 putativeSubclass = sSuperclasses[putativeSubclass];
109 }
110
111 return NO;
112}
113
114static int CompareAtoms(const void* a, const void* b)
115{
116 JSAtom x = *(const JSAtom*)a, y = *(const JSAtom*)b;
117 return (x > y) - (x < y);
118}
119
120static void RecordSharedProperties(JSContext* context, JSClassID classID, const JSCFunctionListEntry* properties, size_t count)
121{
122 SharedPropertyTable* table = &sSharedProperties[classID];
123 if (table->atoms != NULL || properties == NULL)
124 return;
125
126 table->atoms = malloc(sizeof(JSAtom) * (count ? count : 1));
127 table->sortedAtoms = malloc(sizeof(JSAtom) * (count ? count : 1));
128 for (size_t i = 0; i < count; i++) {
129 if (properties[i].def_type == JS_DEF_CGETSET || properties[i].def_type == JS_DEF_CGETSET_MAGIC) {
130 JSAtom atom = OOJSIDFromString([NSString stringWithUTF8String:properties[i].name]);
131
132 // Some tables define a property twice (e.g. Ship's homeSystem); like SpiderMonkey, keep the first.
133 BOOL duplicate = NO;
134 for (uint32_t j = 0; j < table->count && !duplicate; j++)
135 duplicate = (table->atoms[j] == atom);
136 if (!duplicate)
137 table->atoms[table->count++] = atom;
138 }
139 }
140 memcpy(table->sortedAtoms, table->atoms, sizeof(JSAtom) * table->count);
141 qsort(table->sortedAtoms, table->count, sizeof(JSAtom), CompareAtoms);
142
143 JSClassExoticMethods* exotic = sOwnExoticMethods[classID];
144 if (exotic != NULL && table->count != 0) {
145 exotic->get_own_property = OOJSGetSharedPermanentOwnProperty;
146 exotic->get_own_property_names = OOJSGetSharedPermanentOwnPropertyNames;
147 }
148}
149
150static BOOL TableContainsAtom(const SharedPropertyTable* table, JSAtom atom)
151{
152 return table->count != 0 && bsearch(&atom, table->sortedAtoms, table->count, sizeof(JSAtom), CompareAtoms) != NULL;
153}
154
155/* A property is a shared permanent property of an object if its class
156 defines it on its prototype. The descriptor is the prototype's
157 getter/setter pair, so reads and writes go through the native callbacks
158 as before (a data descriptor would make QuickJS create a shadowing own
159 property on assignment).
160*/
161int OOJSGetSharedPermanentOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst object, JSAtom prop)
162{
163 JSClassID classID = JS_GetClassID(object);
164 if (classID == 0 || classID >= kOOJSMaxClassID || !TableContainsAtom(&sSharedProperties[classID], prop))
165 return 0;
166
167 JSValue prototype = JS_GetClassProto(context, classID);
168 int result = 0;
169 // The prototype holds the property in its own shape, so this does not recurse.
170 if (JS_IsObject(prototype) && !JS_IsStrictEqual(context, prototype, object))
171 result = JS_GetOwnProperty(context, desc, prototype, prop);
172 JS_FreeValue(context, prototype);
173 return result;
174}
175
176int OOJSGetSharedPermanentOwnPropertyNames(JSContext* context, JSPropertyEnum** ptab, uint32_t* plen, JSValueConst object)
177{
178 JSClassID classID = JS_GetClassID(object);
179 const SharedPropertyTable* table = (classID != 0 && classID < kOOJSMaxClassID) ? &sSharedProperties[classID] : NULL;
180 uint32_t count = (table != NULL) ? table->count : 0;
181
182 // A class's own prototype gets its properties from its shape, not from here.
183 JSValue prototype = (count != 0) ? JS_GetClassProto(context, classID) : JS_UNDEFINED;
184 if (JS_IsStrictEqual(context, prototype, object))
185 count = 0;
186 JS_FreeValue(context, prototype);
187
188 JSPropertyEnum* tab = js_malloc(context, sizeof(JSPropertyEnum) * (count ? count : 1));
189 if (tab == NULL)
190 return -1;
191
192 for (uint32_t i = 0; i < count; i++) {
193 tab[i].atom = JS_DupAtom(context, table->atoms[i]);
194 tab[i].is_enumerable = false; // Ignored; QuickJS asks get_own_property for flags.
195 }
196
197 *ptab = tab;
198 *plen = count;
199 return 0;
200}
201
202JSValueConst OOJSInitClass(JSContext* context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype,
203 JSCFunction* constructor, int constructorArgCount,
204 const JSCFunctionListEntry* properties, size_t propertyCount,
205 const JSCFunctionListEntry* methods, size_t methodCount,
206 const JSCFunctionListEntry* staticMethods, size_t staticMethodCount)
207{
208 JSValue prototype;
209 if (JS_IsUndefined(parentPrototype)) {
210 JSValue objectPrototype = OOJSObjectPrototype(context);
211 prototype = JS_NewObjectProtoClass(context, objectPrototype, classID);
212 JS_FreeValue(context, objectPrototype);
213 } else {
214 prototype = JS_NewObjectProtoClass(context, parentPrototype, classID);
215 }
216
217 if (properties != NULL) {
218 JS_SetPropertyFunctionList(context, prototype, properties, (int)propertyCount);
219 RecordSharedProperties(context, classID, properties, propertyCount);
220 }
221 if (methods != NULL)
222 JS_SetPropertyFunctionList(context, prototype, methods, (int)methodCount);
223
224 if (constructor != NULL) {
225 const char* name = OOJSClassName(classID);
226 JSValue ctor = JS_NewCFunction2(context, constructor, name, constructorArgCount, JS_CFUNC_constructor_or_func, 0);
227 JS_SetConstructor(context, ctor, prototype);
228 if (staticMethods != NULL)
229 JS_SetPropertyFunctionList(context, ctor, staticMethods, (int)staticMethodCount);
230 JS_DefinePropertyValueStr(context, global, name, ctor, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
231 }
232
233 // The calls above don't report failures; don't leave an exception for the next script to find.
234 if (JS_HasException(context))
236
237 // The context's class prototype table takes ownership, so the value stays valid as a borrowed reference.
238 JS_SetClassProto(context, classID, prototype);
239 return prototype;
240}
int OOJSGetSharedPermanentOwnPropertyNames(JSContext *context, JSPropertyEnum **ptab, uint32_t *plen, JSValueConst object)
static const char * sClassNames[kOOJSMaxClassID]
Definition OOJSClasses.m:30
JSValue OOJSObjectPrototype(JSContext *context)
Definition OOJSClasses.m:81
static JSClassID sSuperclasses[kOOJSMaxClassID]
Definition OOJSClasses.m:31
void OOJSRegisterSubclass(JSClassID subclass, JSClassID superclass)
Definition OOJSClasses.m:94
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
static JSClassExoticMethods * sOwnExoticMethods[kOOJSMaxClassID]
Definition OOJSClasses.m:59
int OOJSGetSharedPermanentOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom prop)
static void RecordSharedProperties(JSContext *context, JSClassID classID, const JSCFunctionListEntry *properties, size_t count)
const char * OOJSClassName(JSClassID classID)
Definition OOJSClasses.m:89
static BOOL TableContainsAtom(const SharedPropertyTable *table, JSAtom atom)
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
static SharedPropertyTable sSharedProperties[kOOJSMaxClassID]
Definition OOJSClasses.m:50
static int CompareAtoms(const void *a, const void *b)
BOOL OOJSIsSubclass(JSClassID putativeSubclass, JSClassID superclass)
jsid OOJSIDFromString(NSString *string)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSReportPendingException(JSContext *context)
unsigned count
float y
float x
int OOJSGetSharedPermanentOwnPropertyNames(JSContext *context, JSPropertyEnum **ptab, uint32_t *plen, JSValueConst object)
@ kOOJSMaxClassID
int OOJSGetSharedPermanentOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom prop)