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()) replaces the C strings with
34 interned JSStrings (hence the void * type). These are subsequently
35 constant and no other strings with the same value should occur, i.e. they
36 should be comparable with pointer equality. Because I'm paranoid, we fall
37 back to full string comparison if this fails.
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 JSString* jsString;
52
53typedef struct ConstTable {
54 NSUInteger count;
57
58#define TABLE(entries) { sizeof entries / sizeof *entries, entries }
59
60static BOOL sInited = NO;
61
62/*
63 The interned string "UNDEFINED", returned by OOJSStringFromConstantPRIVATE()
64 if passed a bogus constant value.
65*/
66static JSString* sUndefinedString;
67
68/*
69 Initialize table contents (with C strings, see above) from table files.
70*/
71#define ENTRY(label, val) { .value = label, .cString = #label },
72#define GALACTIC_HYPERSPACE_ENTRY(label, val) { .value = GALACTIC_HYPERSPACE_##label, .cString = #label },
73#define DIFF_STRING_ENTRY(label, string) { .value = label, .cString = string },
74
76#include "OOCompassMode.tbl"
77};
78
80#include "OOEntityStatus.tbl"
81};
82
84#include "OOGalacticHyperspaceBehaviour.tbl"
85};
86
88#include "OOGUIScreenID.tbl"
89};
90
92#include "OOScanClass.tbl"
93};
94
96#include "OOViewID.tbl"
97};
98
100#include "OOShipDamageType.tbl"
101};
102
104#include "OOLegalStatusReason.tbl"
105};
106
108#include "OOLongRangeChartMode.tbl"
109};
110
111#undef ENTRY
112#undef GALACTIC_HYPERSPACE_ENTRY
113#undef DIFF_STRING_ENTRY
114
124
125static void InitTable(JSContext* context, ConstTable* table);
126
127// MARK: Initialization
128
129void OOConstToJSStringInit(JSContext* context)
130{
131 NSCAssert(!sInited, @"OOConstToJSStringInit() called while already inited.");
132 NSCParameterAssert(context != NULL && JS_IsInRequest(context));
133
134 sUndefinedString = JS_InternString(context, "UNDEFINED");
135
145
146 sInited = YES;
147}
148
150{
151 NSCAssert(sInited, @"OOConstToJSStringDestroy() called while not inited.");
152 sInited = NO;
153 // jsString pointers are now officially junk.
154}
155
156static int CompareEntries(const void* a, const void* b)
157{
158 const TableEntry* entA = a;
159 const TableEntry* entB = b;
160
161 if (entA->value < entB->value)
162 return -1;
163 if (entA->value > entB->value)
164 return 1;
165 return 0;
166}
167
168static void InitTable(JSContext* context, ConstTable* table)
169{
170 NSCParameterAssert(context != NULL && JS_IsInRequest(context) && table != NULL);
171
172 NSUInteger i;
173 for (i = 0; i < table->count; i++) {
174 table->entries[i].jsString = JS_InternString(context, table->entries[i].cString);
175 }
176
177 qsort(table->entries, table->count, sizeof *table->entries, CompareEntries);
178}
179
180// MARK: Lookup
181
182JSString* OOJSStringFromConstantPRIVATE(JSContext* context, NSInteger value, struct ConstTable* table)
183{
184 NSCAssert1(sInited, @"%s called before OOConstToJSStringInit().", __PRETTY_FUNCTION__);
185 NSCParameterAssert(context != NULL && JS_IsInRequest(context));
186 NSCParameterAssert(table != NULL && table->count > 0);
187
188 // Binary search.
189 NSUInteger min = 0, max = table->count - 1;
190 NSInteger current;
191 do {
192 NSUInteger mid = (min + max) / 2;
193 current = table->entries[mid].value;
194 if (current < value) {
195 min = mid + 1;
196 } else if (current > value) {
197 max = mid - 1;
198 } else {
199 return table->entries[mid].jsString;
200 }
201 } while (min <= max);
202
203 return sUndefinedString;
204}
205
206NSUInteger OOConstantFromJSStringPRIVATE(JSContext* context, JSString* string, struct ConstTable* table, NSInteger defaultValue)
207{
208 NSCAssert1(sInited, @"%s called before OOConstToJSStringInit().", __PRETTY_FUNCTION__);
209 NSCParameterAssert(context != NULL && JS_IsInRequest(context) && table != NULL);
210
211 // Quick pass: look for pointer-equal string.
212 NSUInteger i, count = table->count;
213 for (i = 0; i < count; i++) {
214 if (table->entries[i].jsString == string) {
215 return table->entries[i].value;
216 }
217 }
218
219 // Slow pass: use string comparison. This is expected to fail.
220 if (string != NULL) {
221 for (i = 0; i < count; i++) {
222 int32 result;
223 if (JS_CompareStrings(context, string, table->entries[i].jsString, &result) && result == 0) {
224 return table->entries[i].value;
225 }
226 }
227 }
228
229 // Fail.
230 return defaultValue;
231}
232
233NSUInteger OOConstantFromJSValuePRIVATE(JSContext* context, jsval value, struct ConstTable* table, NSInteger defaultValue)
234{
235 if (EXPECT(JSVAL_IS_STRING(value))) {
236 return OOConstantFromJSStringPRIVATE(context, JSVAL_TO_STRING(value), table, defaultValue);
237 } else {
238 return defaultValue;
239 }
240}
static BOOL sInited
Definition OOCPUInfo.m:56
NSUInteger OOConstantFromJSStringPRIVATE(JSContext *context, JSString *string, struct ConstTable *table, NSInteger defaultValue)
ConstTable gOOViewIDConstTable
ConstTable gOOEntityStatusConstTable
static TableEntry sOOEntityStatusTableEntries[]
static TableEntry sOOGalacticHyperspaceBehaviourTableEntries[]
void OOConstToJSStringDestroy(void)
static int CompareEntries(const void *a, const void *b)
static JSString * sUndefinedString
JSString * OOJSStringFromConstantPRIVATE(JSContext *context, NSInteger value, struct ConstTable *table)
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(x)
#define JS_IsInRequest(context)
unsigned count
TableEntry * entries
NSUInteger count
NSInteger value
const char * cString
JSString * jsString