Oolite
Loading...
Searching...
No Matches
OOConstToJSString.m File Reference
#include "OOConstToJSString.h"
#include "OOCompassMode.tbl"
#include "OOEntityStatus.tbl"
#include "OOGalacticHyperspaceBehaviour.tbl"
#include "OOGUIScreenID.tbl"
#include "OOScanClass.tbl"
#include "OOViewID.tbl"
#include "OOShipDamageType.tbl"
#include "OOLegalStatusReason.tbl"
#include "OOLongRangeChartMode.tbl"
Include dependency graph for OOConstToJSString.m:

Go to the source code of this file.

Classes

struct  TableEntry
struct  ConstTable

Macros

#define TABLE(entries)
#define ENTRY(label, val)
#define GALACTIC_HYPERSPACE_ENTRY(label, val)
#define DIFF_STRING_ENTRY(label, string)

Typedefs

typedef struct ConstTable ConstTable

Functions

static void InitTable (JSContext *context, ConstTable *table)
void OOConstToJSStringInit (JSContext *context)
void OOConstToJSStringDestroy (void)
static int CompareEntries (const void *a, const void *b)
JSValue OOJSValueFromConstantPRIVATE (JSContext *context, NSInteger value, struct ConstTable *table)
NSUInteger OOConstantFromJSValuePRIVATE (JSContext *context, JSValueConst value, struct ConstTable *table, NSInteger defaultValue)

Variables

static BOOL sInited = NO
static BOOL sAtomsInterned = NO
static JSAtom sUndefinedAtom
static TableEntry sOOCompassModeTableEntries []
static TableEntry sOOEntityStatusTableEntries []
static TableEntry sOOGalacticHyperspaceBehaviourTableEntries []
static TableEntry sOOGUIScreenIDTableEntries []
static TableEntry sOOScanClassTableEntries []
static TableEntry sOOViewIDTableEntries []
static TableEntry sOOShipDamageTypeTableEntries []
static TableEntry sOOLegalStatusReasonTableEntries []
static TableEntry sOOLongRangeChartModeTableEntries []
ConstTable gOOCompassModeConstTable = TABLE(sOOCompassModeTableEntries)
ConstTable gOOEntityStatusConstTable = TABLE(sOOEntityStatusTableEntries)
ConstTable gOOGalacticHyperspaceBehaviourConstTable = TABLE(sOOGalacticHyperspaceBehaviourTableEntries)
ConstTable gOOGUIScreenIDConstTable = TABLE(sOOGUIScreenIDTableEntries)
ConstTable gOOScanClassConstTable = TABLE(sOOScanClassTableEntries)
ConstTable gOOViewIDConstTable = TABLE(sOOViewIDTableEntries)
ConstTable gOOShipDamageTypeConstTable = TABLE(sOOShipDamageTypeTableEntries)
ConstTable gOOLegalStatusReasonConstTable = TABLE(sOOLegalStatusReasonTableEntries)
ConstTable gOOLongRangeChartModeConstTable = TABLE(sOOLongRangeChartModeTableEntries)

Macro Definition Documentation

◆ DIFF_STRING_ENTRY

#define DIFF_STRING_ENTRY ( label,
string )
Value:
{ .value = label, .cString = string },

Definition at line 74 of file OOConstToJSString.m.

◆ ENTRY

#define ENTRY ( label,
val )
Value:
{ .value = label, .cString = #label },

Definition at line 72 of file OOConstToJSString.m.

◆ GALACTIC_HYPERSPACE_ENTRY

#define GALACTIC_HYPERSPACE_ENTRY ( label,
val )
Value:
{ .value = GALACTIC_HYPERSPACE_##label, .cString = #label },

Definition at line 73 of file OOConstToJSString.m.

◆ TABLE

#define TABLE ( entries)
Value:
{ sizeof entries / sizeof *entries, entries }

Definition at line 58 of file OOConstToJSString.m.

Typedef Documentation

◆ ConstTable

typedef struct ConstTable ConstTable

Function Documentation

◆ CompareEntries()

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

Definition at line 162 of file OOConstToJSString.m.

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}
NSInteger value

References TableEntry::value.

◆ InitTable()

void InitTable ( JSContext * context,
ConstTable * table )
static

Definition at line 174 of file OOConstToJSString.m.

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}
static int CompareEntries(const void *a, const void *b)
TableEntry * entries
NSUInteger count
const char * cString

References TableEntry::atom, CompareEntries(), ConstTable::count, TableEntry::cString, and ConstTable::entries.

Here is the call graph for this function:

◆ OOConstantFromJSValuePRIVATE()

NSUInteger OOConstantFromJSValuePRIVATE ( JSContext * context,
JSValueConst value,
struct ConstTable * table,
NSInteger defaultValue )

Definition at line 212 of file OOConstToJSString.m.

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
#define EXPECT_NOT(x)
unsigned count

References TableEntry::atom, ConstTable::count, count, ConstTable::entries, EXPECT_NOT, sInited, and TableEntry::value.

◆ OOConstToJSStringDestroy()

void OOConstToJSStringDestroy ( void )

Definition at line 155 of file OOConstToJSString.m.

156{
157 NSCAssert(sInited, @"OOConstToJSStringDestroy() called while not inited.");
158 sInited = NO;
159 // The atoms remain valid; they belong to the runtime.
160}

References sInited.

Referenced by OOJavaScriptEngine(Private)::destroyMainThreadContext.

Here is the caller graph for this function:

◆ OOConstToJSStringInit()

void OOConstToJSStringInit ( JSContext * context)

Definition at line 130 of file OOConstToJSString.m.

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}
ConstTable gOOViewIDConstTable
ConstTable gOOEntityStatusConstTable
ConstTable gOOLongRangeChartModeConstTable
ConstTable gOOCompassModeConstTable
ConstTable gOOLegalStatusReasonConstTable
static void InitTable(JSContext *context, ConstTable *table)
ConstTable gOOShipDamageTypeConstTable
ConstTable gOOScanClassConstTable
ConstTable gOOGUIScreenIDConstTable
ConstTable gOOGalacticHyperspaceBehaviourConstTable
static BOOL sAtomsInterned
static JSAtom sUndefinedAtom

References gOOCompassModeConstTable, gOOEntityStatusConstTable, gOOGalacticHyperspaceBehaviourConstTable, gOOGUIScreenIDConstTable, gOOLegalStatusReasonConstTable, gOOLongRangeChartModeConstTable, gOOScanClassConstTable, gOOShipDamageTypeConstTable, gOOViewIDConstTable, InitTable(), JS_IsInRequest, sAtomsInterned, sInited, sUndefinedAtom, and sUndefinedString.

Referenced by OOJavaScriptEngine(Private)::createMainThreadContext.

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

◆ OOJSValueFromConstantPRIVATE()

JSValue OOJSValueFromConstantPRIVATE ( JSContext * context,
NSInteger value,
struct ConstTable * table )

Definition at line 188 of file OOConstToJSString.m.

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}

References TableEntry::atom, ConstTable::count, ConstTable::entries, sInited, sUndefinedAtom, and TableEntry::value.

Referenced by OOJSValueFromCompassMode(), OOJSValueFromEntityStatus(), OOJSValueFromGalacticHyperspaceBehaviour(), OOJSValueFromGUIScreenID(), OOJSValueFromLegalStatusReason(), OOJSValueFromScanClass(), OOJSValueFromShipDamageType(), and OOJSValueFromViewID().

Here is the caller graph for this function:

Variable Documentation

◆ gOOCompassModeConstTable

ConstTable gOOCompassModeConstTable = TABLE(sOOCompassModeTableEntries)

Definition at line 116 of file OOConstToJSString.m.

◆ gOOEntityStatusConstTable

ConstTable gOOEntityStatusConstTable = TABLE(sOOEntityStatusTableEntries)

Definition at line 117 of file OOConstToJSString.m.

◆ gOOGalacticHyperspaceBehaviourConstTable

ConstTable gOOGalacticHyperspaceBehaviourConstTable = TABLE(sOOGalacticHyperspaceBehaviourTableEntries)

Definition at line 118 of file OOConstToJSString.m.

◆ gOOGUIScreenIDConstTable

ConstTable gOOGUIScreenIDConstTable = TABLE(sOOGUIScreenIDTableEntries)

Definition at line 119 of file OOConstToJSString.m.

◆ gOOLegalStatusReasonConstTable

ConstTable gOOLegalStatusReasonConstTable = TABLE(sOOLegalStatusReasonTableEntries)

Definition at line 123 of file OOConstToJSString.m.

◆ gOOLongRangeChartModeConstTable

ConstTable gOOLongRangeChartModeConstTable = TABLE(sOOLongRangeChartModeTableEntries)

Definition at line 124 of file OOConstToJSString.m.

◆ gOOScanClassConstTable

ConstTable gOOScanClassConstTable = TABLE(sOOScanClassTableEntries)

Definition at line 120 of file OOConstToJSString.m.

◆ gOOShipDamageTypeConstTable

ConstTable gOOShipDamageTypeConstTable = TABLE(sOOShipDamageTypeTableEntries)

Definition at line 122 of file OOConstToJSString.m.

◆ gOOViewIDConstTable

ConstTable gOOViewIDConstTable = TABLE(sOOViewIDTableEntries)

Definition at line 121 of file OOConstToJSString.m.

◆ sAtomsInterned

BOOL sAtomsInterned = NO
static

Definition at line 61 of file OOConstToJSString.m.

Referenced by OOConstToJSStringInit().

◆ sInited

BOOL sInited = NO
static

Definition at line 60 of file OOConstToJSString.m.

◆ sOOCompassModeTableEntries

TableEntry sOOCompassModeTableEntries[]
static
Initial value:
= {
}

Definition at line 76 of file OOConstToJSString.m.

76 {
77#include "OOCompassMode.tbl"
78};

◆ sOOEntityStatusTableEntries

TableEntry sOOEntityStatusTableEntries[]
static
Initial value:
= {
}

Definition at line 80 of file OOConstToJSString.m.

80 {
81#include "OOEntityStatus.tbl"
82};

◆ sOOGalacticHyperspaceBehaviourTableEntries

TableEntry sOOGalacticHyperspaceBehaviourTableEntries[]
static
Initial value:
= {
}

Definition at line 84 of file OOConstToJSString.m.

84 {
85#include "OOGalacticHyperspaceBehaviour.tbl"
86};

◆ sOOGUIScreenIDTableEntries

TableEntry sOOGUIScreenIDTableEntries[]
static
Initial value:
= {
}

Definition at line 88 of file OOConstToJSString.m.

88 {
89#include "OOGUIScreenID.tbl"
90};

◆ sOOLegalStatusReasonTableEntries

TableEntry sOOLegalStatusReasonTableEntries[]
static
Initial value:
= {
}

Definition at line 104 of file OOConstToJSString.m.

104 {
105#include "OOLegalStatusReason.tbl"
106};

◆ sOOLongRangeChartModeTableEntries

TableEntry sOOLongRangeChartModeTableEntries[]
static
Initial value:
= {
}

Definition at line 108 of file OOConstToJSString.m.

108 {
109#include "OOLongRangeChartMode.tbl"
110};

◆ sOOScanClassTableEntries

TableEntry sOOScanClassTableEntries[]
static
Initial value:
= {
}

Definition at line 92 of file OOConstToJSString.m.

92 {
93#include "OOScanClass.tbl"
94};

◆ sOOShipDamageTypeTableEntries

TableEntry sOOShipDamageTypeTableEntries[]
static
Initial value:
= {
}

Definition at line 100 of file OOConstToJSString.m.

100 {
101#include "OOShipDamageType.tbl"
102};

◆ sOOViewIDTableEntries

TableEntry sOOViewIDTableEntries[]
static
Initial value:
= {
}

Definition at line 96 of file OOConstToJSString.m.

96 {
97#include "OOViewID.tbl"
98};

◆ sUndefinedAtom

JSAtom sUndefinedAtom
static

Definition at line 67 of file OOConstToJSString.m.

Referenced by OOConstToJSStringInit(), and OOJSValueFromConstantPRIVATE().