Oolite
Loading...
Searching...
No Matches
OOConstToJSString.m
Go to the documentation of this file.
1/*
2
3OOConstToJSString.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#include "OOConstToJSString.h"
27
28/*
29 Each type of constant has its own lookup table, which is a linear list of
30 TableEntry structs plus a count.
31
32 The list is statically initialized with the constant values and C strings.
33 OOConstToJSStringInit() (through InitTable()) adds an interned JSAtom for
34 each C string. Atoms are interned once per process (they belong to the
35 runtime, which survives engine resets) and are never freed. Two atoms are
36 equal if and only if their strings are equal, so lookups from JS strings
37 compare atoms.
38
39 ConstTables are globals, which are accessed as local externs in the inlines
40 in the header. All the advantages of globals with most of the advantages
41 of encapsulation, sorta.
42
43 -- Ahruman 2011-01-15
44*/
45
46typedef struct
47{
48 NSInteger value;
49 const char* cString;
50 JSAtom atom;
52
53typedef struct ConstTable {
54 NSUInteger count;
57
58#define TABLE(entries) { sizeof entries / sizeof *entries, entries }
59
60static BOOL sInited = NO;
61static BOOL sAtomsInterned = NO;
62
63/*
64 The interned string "UNDEFINED", returned by OOJSValueFromConstantPRIVATE()
65 if passed a bogus constant value.
66*/
67static JSAtom sUndefinedAtom;
68
69/*
70 Initialize table contents (with C strings, see above) from table files.
71*/
72#define ENTRY(label, val) { .value = label, .cString = #label },
73#define GALACTIC_HYPERSPACE_ENTRY(label, val) { .value = GALACTIC_HYPERSPACE_##label, .cString = #label },
74#define DIFF_STRING_ENTRY(label, string) { .value = label, .cString = string },
75
77#include "OOCompassMode.tbl"
78};
79
81#include "OOEntityStatus.tbl"
82};
83
85#include "OOGalacticHyperspaceBehaviour.tbl"
86};
87
89#include "OOGUIScreenID.tbl"
90};
91
93#include "OOScanClass.tbl"
94};
95
97#include "OOViewID.tbl"
98};
99
101#include "OOShipDamageType.tbl"
102};
103
105#include "OOLegalStatusReason.tbl"
106};
107
109#include "OOLongRangeChartMode.tbl"
110};
111
112#undef ENTRY
113#undef GALACTIC_HYPERSPACE_ENTRY
114#undef DIFF_STRING_ENTRY
115
125
126static void InitTable(JSContext* context, ConstTable* table);
127
128// MARK: Initialization
129
130void OOConstToJSStringInit(JSContext* context)
131{
132 NSCAssert(!sInited, @"OOConstToJSStringInit() called while already inited.");
133 NSCParameterAssert(context != NULL);
134
135 // Atoms survive engine resets, so they only need interning once.
136 if (!sAtomsInterned) {
137 sUndefinedAtom = JS_NewAtom(context, "UNDEFINED");
138
148
149 sAtomsInterned = YES;
150 }
151
152 sInited = YES;
153}
154
156{
157 NSCAssert(sInited, @"OOConstToJSStringDestroy() called while not inited.");
158 sInited = NO;
159 // The atoms remain valid; they belong to the runtime.
160}
161
162static int CompareEntries(const void* a, const void* b)
163{
164 const TableEntry* entA = a;
165 const TableEntry* entB = b;
166
167 if (entA->value < entB->value)
168 return -1;
169 if (entA->value > entB->value)
170 return 1;
171 return 0;
172}
173
174static void InitTable(JSContext* context, ConstTable* table)
175{
176 NSCParameterAssert(context != NULL && table != NULL);
177
178 NSUInteger i;
179 for (i = 0; i < table->count; i++) {
180 table->entries[i].atom = JS_NewAtom(context, table->entries[i].cString);
181 }
182
183 qsort(table->entries, table->count, sizeof *table->entries, CompareEntries);
184}
185
186// MARK: Lookup
187
188JSValue OOJSValueFromConstantPRIVATE(JSContext* context, NSInteger value, struct ConstTable* table)
189{
190 NSCAssert1(sInited, @"%s called before OOConstToJSStringInit().", __PRETTY_FUNCTION__);
191 NSCParameterAssert(context != NULL);
192 NSCParameterAssert(table != NULL && table->count > 0);
193
194 // Binary search. (Signed bounds: the SpiderMonkey version underflowed for values below the first entry.)
195 NSInteger min = 0, max = (NSInteger)table->count - 1;
196 NSInteger current;
197 while (min <= max) {
198 NSInteger mid = (min + max) / 2;
199 current = table->entries[mid].value;
200 if (current < value) {
201 min = mid + 1;
202 } else if (current > value) {
203 max = mid - 1;
204 } else {
205 return JS_AtomToString(context, table->entries[mid].atom);
206 }
207 }
208
209 return JS_AtomToString(context, sUndefinedAtom);
210}
211
212NSUInteger OOConstantFromJSValuePRIVATE(JSContext* context, JSValueConst value, struct ConstTable* table, NSInteger defaultValue)
213{
214 NSCAssert1(sInited, @"%s called before OOConstToJSStringInit().", __PRETTY_FUNCTION__);
215 NSCParameterAssert(context != NULL && table != NULL);
216
217 if (EXPECT_NOT(!JS_IsString(value)))
218 return defaultValue;
219
220 // Interning a string yields the same atom as the table entry with the same contents.
221 JSAtom atom = JS_ValueToAtom(context, value);
222 if (EXPECT_NOT(atom == JS_ATOM_NULL)) {
223 JS_FreeValue(context, JS_GetException(context));
224 return defaultValue;
225 }
226
227 NSInteger result = defaultValue;
228 NSUInteger i, count = table->count;
229 for (i = 0; i < count; i++) {
230 if (table->entries[i].atom == atom) {
231 result = table->entries[i].value;
232 break;
233 }
234 }
235
236 JS_FreeAtom(context, atom);
237 return result;
238}
static BOOL sInited
Definition OOCPUInfo.m:56
ConstTable gOOViewIDConstTable
ConstTable gOOEntityStatusConstTable
static TableEntry sOOEntityStatusTableEntries[]
static TableEntry sOOGalacticHyperspaceBehaviourTableEntries[]
void OOConstToJSStringDestroy(void)
static int CompareEntries(const void *a, const void *b)
ConstTable gOOLongRangeChartModeConstTable
ConstTable gOOCompassModeConstTable
static TableEntry sOOViewIDTableEntries[]
ConstTable gOOLegalStatusReasonConstTable
void OOConstToJSStringInit(JSContext *context)
static TableEntry sOOScanClassTableEntries[]
static void InitTable(JSContext *context, ConstTable *table)
ConstTable gOOShipDamageTypeConstTable
ConstTable gOOScanClassConstTable
#define TABLE(entries)
ConstTable gOOGUIScreenIDConstTable
static TableEntry sOOCompassModeTableEntries[]
ConstTable gOOGalacticHyperspaceBehaviourConstTable
static TableEntry sOOLongRangeChartModeTableEntries[]
static TableEntry sOOShipDamageTypeTableEntries[]
static TableEntry sOOGUIScreenIDTableEntries[]
static TableEntry sOOLegalStatusReasonTableEntries[]
NSUInteger OOConstantFromJSValuePRIVATE(JSContext *context, jsval value, struct ConstTable *table, NSInteger defaultValue)
#define EXPECT_NOT(x)
unsigned count
static BOOL sAtomsInterned
JSValue OOJSValueFromConstantPRIVATE(JSContext *context, NSInteger value, struct ConstTable *table)
static JSAtom sUndefinedAtom
TableEntry * entries
NSUInteger count
NSInteger value
const char * cString