Oolite
Loading...
Searching...
No Matches
OOJSClasses.m File Reference
Include dependency graph for OOJSClasses.m:

Go to the source code of this file.

Classes

struct  SharedPropertyTable

Functions

JSClassID OOJSRegisterClass (JSClassID *ioClassID, const JSClassDef *classDef)
JSValue OOJSObjectPrototype (JSContext *context)
const char * OOJSClassName (JSClassID classID)
void OOJSRegisterSubclass (JSClassID subclass, JSClassID superclass)
BOOL OOJSIsSubclass (JSClassID putativeSubclass, JSClassID superclass)
static int CompareAtoms (const void *a, const void *b)
static void RecordSharedProperties (JSContext *context, JSClassID classID, const JSCFunctionListEntry *properties, size_t count)
static BOOL TableContainsAtom (const SharedPropertyTable *table, JSAtom atom)
int OOJSGetSharedPermanentOwnProperty (JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom prop)
int OOJSGetSharedPermanentOwnPropertyNames (JSContext *context, JSPropertyEnum **ptab, uint32_t *plen, JSValueConst object)
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)

Variables

static const char * sClassNames [kOOJSMaxClassID]
static JSClassID sSuperclasses [kOOJSMaxClassID]
static SharedPropertyTable sSharedProperties [kOOJSMaxClassID]
static JSClassExoticMethods * sOwnExoticMethods [kOOJSMaxClassID]

Function Documentation

◆ CompareAtoms()

int CompareAtoms ( const void * a,
const void * b )
static

Definition at line 114 of file OOJSClasses.m.

115{
116 JSAtom x = *(const JSAtom*)a, y = *(const JSAtom*)b;
117 return (x > y) - (x < y);
118}
float y
float x

References x, and y.

Referenced by RecordSharedProperties(), and TableContainsAtom().

Here is the caller graph for this function:

◆ OOJSClassName()

const char * OOJSClassName ( JSClassID classID)

Definition at line 89 of file OOJSClasses.m.

90{
91 return (classID < kOOJSMaxClassID) ? sClassNames[classID] : NULL;
92}
static const char * sClassNames[kOOJSMaxClassID]
Definition OOJSClasses.m:30
@ kOOJSMaxClassID

References kOOJSMaxClassID, and sClassNames.

Referenced by OOJSClassNameOfValue(), OOJSInitClass(), OOJSObjectGetterImplPRIVATE(), and OOJSObjectWrapperToString().

Here is the caller graph for this function:

◆ OOJSGetSharedPermanentOwnProperty()

int OOJSGetSharedPermanentOwnProperty ( JSContext * context,
JSPropertyDescriptor * desc,
JSValueConst object,
JSAtom prop )

Definition at line 161 of file OOJSClasses.m.

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}
static BOOL TableContainsAtom(const SharedPropertyTable *table, JSAtom atom)
static SharedPropertyTable sSharedProperties[kOOJSMaxClassID]
Definition OOJSClasses.m:50

References kOOJSMaxClassID, sSharedProperties, and TableContainsAtom().

Referenced by ManifestGetOwnProperty(), RecordSharedProperties(), and SystemInfoExoticGetOwnProperty().

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

◆ OOJSGetSharedPermanentOwnPropertyNames()

int OOJSGetSharedPermanentOwnPropertyNames ( JSContext * context,
JSPropertyEnum ** ptab,
uint32_t * plen,
JSValueConst object )

Definition at line 176 of file OOJSClasses.m.

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}
unsigned count

References SharedPropertyTable::atoms, count, SharedPropertyTable::count, kOOJSMaxClassID, and sSharedProperties.

Referenced by RecordSharedProperties().

Here is the caller graph for this function:

◆ OOJSInitClass()

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 )

Definition at line 202 of file OOJSClasses.m.

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}
JSValue OOJSObjectPrototype(JSContext *context)
Definition OOJSClasses.m:81
static void RecordSharedProperties(JSContext *context, JSClassID classID, const JSCFunctionListEntry *properties, size_t count)
const char * OOJSClassName(JSClassID classID)
Definition OOJSClasses.m:89
OOINLINE void OOJSReportPendingException(JSContext *context)

References OOJSClassName(), OOJSObjectPrototype(), OOJSReportPendingException(), and RecordSharedProperties().

Referenced by InitOOJSClock(), InitOOJSDock(), InitOOJSEntity(), InitOOJSEquipmentInfo(), InitOOJSExhaustPlume(), InitOOJSFlasher(), InitOOJSManifest(), InitOOJSMission(), InitOOJSOolite(), InitOOJSPlanet(), InitOOJSPlayer(), InitOOJSPlayerShip(), InitOOJSQuaternion(), InitOOJSScript(), InitOOJSShip(), InitOOJSShipGroup(), InitOOJSSound(), InitOOJSSoundSource(), InitOOJSStation(), InitOOJSSun(), InitOOJSSystem(), InitOOJSSystemInfo(), InitOOJSTimer(), InitOOJSVector(), InitOOJSVisualEffect(), InitOOJSWaypoint(), and InitOOJSWormhole().

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

◆ OOJSIsSubclass()

BOOL OOJSIsSubclass ( JSClassID putativeSubclass,
JSClassID superclass )

Definition at line 102 of file OOJSClasses.m.

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}
static JSClassID sSuperclasses[kOOJSMaxClassID]
Definition OOJSClasses.m:31

References kOOJSMaxClassID, and sSuperclasses.

◆ OOJSObjectPrototype()

JSValue OOJSObjectPrototype ( JSContext * context)

Definition at line 81 of file OOJSClasses.m.

82{
83 JSValue object = JS_NewObject(context);
84 JSValue result = JS_GetPrototype(context, object);
85 JS_FreeValue(context, object);
86 return result;
87}

Referenced by InitOOJSMissionVariables(), InitOOJSWorldScripts(), and OOJSInitClass().

Here is the caller graph for this function:

◆ OOJSRegisterClass()

JSClassID OOJSRegisterClass ( JSClassID * ioClassID,
const JSClassDef * classDef )

Definition at line 61 of file OOJSClasses.m.

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}
static JSClassExoticMethods * sOwnExoticMethods[kOOJSMaxClassID]
Definition OOJSClasses.m:59
OOINLINE JSContext * OOJSAcquireContext(void)

References kOOJSMaxClassID, OOJSAcquireContext(), sClassNames, and sOwnExoticMethods.

Referenced by InitOOJSClock(), InitOOJSDock(), InitOOJSEntity(), InitOOJSEquipmentInfo(), InitOOJSExhaustPlume(), InitOOJSFlasher(), InitOOJSManifest(), InitOOJSMission(), InitOOJSMissionVariables(), InitOOJSOolite(), InitOOJSPlanet(), InitOOJSPlayer(), InitOOJSPlayerShip(), InitOOJSQuaternion(), InitOOJSScript(), InitOOJSShip(), InitOOJSShipGroup(), InitOOJSSound(), InitOOJSSoundSource(), InitOOJSStation(), InitOOJSSun(), InitOOJSSystem(), InitOOJSSystemInfo(), InitOOJSTimer(), InitOOJSVector(), InitOOJSVisualEffect(), InitOOJSWaypoint(), InitOOJSWorldScripts(), and InitOOJSWormhole().

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

◆ OOJSRegisterSubclass()

void OOJSRegisterSubclass ( JSClassID subclass,
JSClassID superclass )

Definition at line 94 of file OOJSClasses.m.

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}

References kOOJSMaxClassID, and sSuperclasses.

◆ RecordSharedProperties()

void RecordSharedProperties ( JSContext * context,
JSClassID classID,
const JSCFunctionListEntry * properties,
size_t count )
static

Definition at line 120 of file OOJSClasses.m.

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}
static int CompareAtoms(const void *a, const void *b)
jsid OOJSIDFromString(NSString *string)
int OOJSGetSharedPermanentOwnPropertyNames(JSContext *context, JSPropertyEnum **ptab, uint32_t *plen, JSValueConst object)
int OOJSGetSharedPermanentOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom prop)

References SharedPropertyTable::atoms, CompareAtoms(), count, SharedPropertyTable::count, OOJSGetSharedPermanentOwnProperty(), OOJSGetSharedPermanentOwnPropertyNames(), OOJSIDFromString(), SharedPropertyTable::sortedAtoms, sOwnExoticMethods, and sSharedProperties.

Referenced by OOJSInitClass().

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

◆ TableContainsAtom()

BOOL TableContainsAtom ( const SharedPropertyTable * table,
JSAtom atom )
static

Definition at line 150 of file OOJSClasses.m.

151{
152 return table->count != 0 && bsearch(&atom, table->sortedAtoms, table->count, sizeof(JSAtom), CompareAtoms) != NULL;
153}

References CompareAtoms(), SharedPropertyTable::count, and SharedPropertyTable::sortedAtoms.

Referenced by OOJSGetSharedPermanentOwnProperty().

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

Variable Documentation

◆ sClassNames

const char* sClassNames[kOOJSMaxClassID]
static

Definition at line 30 of file OOJSClasses.m.

Referenced by OOJSClassName(), and OOJSRegisterClass().

◆ sOwnExoticMethods

JSClassExoticMethods* sOwnExoticMethods[kOOJSMaxClassID]
static

Definition at line 59 of file OOJSClasses.m.

Referenced by OOJSRegisterClass(), and RecordSharedProperties().

◆ sSharedProperties

◆ sSuperclasses

JSClassID sSuperclasses[kOOJSMaxClassID]
static

Definition at line 31 of file OOJSClasses.m.

Referenced by OOJSIsSubclass(), and OOJSRegisterSubclass().