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)
JSString * OOJSStringFromConstantPRIVATE (JSContext *context, NSInteger value, struct ConstTable *table)
NSUInteger OOConstantFromJSStringPRIVATE (JSContext *context, JSString *string, struct ConstTable *table, NSInteger defaultValue)
NSUInteger OOConstantFromJSValuePRIVATE (JSContext *context, jsval value, struct ConstTable *table, NSInteger defaultValue)

Variables

static BOOL sInited = NO
static JSString * sUndefinedString
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 73 of file OOConstToJSString.m.

◆ ENTRY

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

Definition at line 71 of file OOConstToJSString.m.

◆ GALACTIC_HYPERSPACE_ENTRY

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

Definition at line 72 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 156 of file OOConstToJSString.m.

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

References TableEntry::value.

Referenced by InitTable().

Here is the caller graph for this function:

◆ InitTable()

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

Definition at line 168 of file OOConstToJSString.m.

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

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

Referenced by OOConstToJSStringInit().

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

◆ OOConstantFromJSStringPRIVATE()

NSUInteger OOConstantFromJSStringPRIVATE ( JSContext * context,
JSString * string,
struct ConstTable * table,
NSInteger defaultValue )

Definition at line 206 of file OOConstToJSString.m.

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}
static BOOL sInited
Definition OOCPUInfo.m:56
unsigned count

References ConstTable::count, count, ConstTable::entries, JS_IsInRequest, TableEntry::jsString, sInited, and TableEntry::value.

Referenced by OOCompassModeFromJSString(), OOConstantFromJSValuePRIVATE(), OOEntityStatusFromJSString(), OOGalacticHyperspaceBehaviourFromJSString(), OOGUIScreenIDFromJSString(), OOScanClassFromJSString(), and OOViewIDFromJSString().

Here is the caller graph for this function:

◆ OOConstantFromJSValuePRIVATE()

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

Definition at line 233 of file OOConstToJSString.m.

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}
NSUInteger OOConstantFromJSStringPRIVATE(JSContext *context, JSString *string, struct ConstTable *table, NSInteger defaultValue)
#define EXPECT(x)

References EXPECT, and OOConstantFromJSStringPRIVATE().

Referenced by OOCompassModeFromJSValue(), OOCompassModeFromJSValue(), OOEntityStatusFromJSValue(), OOEntityStatusFromJSValue(), OOGalacticHyperspaceBehaviourFromJSValue(), OOGalacticHyperspaceBehaviourFromJSValue(), OOGUIScreenIDFromJSValue(), OOGUIScreenIDFromJSValue(), OOScanClassFromJSValue(), OOScanClassFromJSValue(), OOViewIDFromJSValue(), and OOViewIDFromJSValue().

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

◆ OOConstToJSStringDestroy()

void OOConstToJSStringDestroy ( void )

Definition at line 149 of file OOConstToJSString.m.

150{
151 NSCAssert(sInited, @"OOConstToJSStringDestroy() called while not inited.");
152 sInited = NO;
153 // jsString pointers are now officially junk.
154}

◆ OOConstToJSStringInit()

void OOConstToJSStringInit ( JSContext * context)

Definition at line 129 of file OOConstToJSString.m.

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

◆ OOJSStringFromConstantPRIVATE()

JSString * OOJSStringFromConstantPRIVATE ( JSContext * context,
NSInteger value,
struct ConstTable * table )

Definition at line 182 of file OOConstToJSString.m.

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}

References ConstTable::count, ConstTable::entries, JS_IsInRequest, TableEntry::jsString, sInited, sUndefinedString, and TableEntry::value.

Referenced by OOJSStringFromCompassMode(), OOJSStringFromEntityStatus(), OOJSStringFromGalacticHyperspaceBehaviour(), OOJSStringFromGUIScreenID(), OOJSStringFromLegalStatusReason(), OOJSStringFromScanClass(), OOJSStringFromShipDamageType(), and OOJSStringFromViewID().

Here is the caller graph for this function:

Variable Documentation

◆ gOOCompassModeConstTable

◆ gOOEntityStatusConstTable

◆ gOOGalacticHyperspaceBehaviourConstTable

◆ gOOGUIScreenIDConstTable

◆ gOOLegalStatusReasonConstTable

◆ gOOLongRangeChartModeConstTable

ConstTable gOOLongRangeChartModeConstTable = TABLE(sOOLongRangeChartModeTableEntries)

Definition at line 123 of file OOConstToJSString.m.

Referenced by OOConstToJSStringInit().

◆ gOOScanClassConstTable

◆ gOOShipDamageTypeConstTable

◆ gOOViewIDConstTable

◆ sInited

BOOL sInited = NO
static

Definition at line 60 of file OOConstToJSString.m.

◆ sOOCompassModeTableEntries

TableEntry sOOCompassModeTableEntries[]
static
Initial value:
= {
}

Definition at line 75 of file OOConstToJSString.m.

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

◆ sOOEntityStatusTableEntries

TableEntry sOOEntityStatusTableEntries[]
static
Initial value:
= {
}

Definition at line 79 of file OOConstToJSString.m.

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

◆ sOOGalacticHyperspaceBehaviourTableEntries

TableEntry sOOGalacticHyperspaceBehaviourTableEntries[]
static
Initial value:
= {
}

Definition at line 83 of file OOConstToJSString.m.

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

◆ sOOGUIScreenIDTableEntries

TableEntry sOOGUIScreenIDTableEntries[]
static
Initial value:
= {
}

Definition at line 87 of file OOConstToJSString.m.

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

◆ sOOLegalStatusReasonTableEntries

TableEntry sOOLegalStatusReasonTableEntries[]
static
Initial value:
= {
}

Definition at line 103 of file OOConstToJSString.m.

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

◆ sOOLongRangeChartModeTableEntries

TableEntry sOOLongRangeChartModeTableEntries[]
static
Initial value:
= {
}

Definition at line 107 of file OOConstToJSString.m.

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

◆ sOOScanClassTableEntries

TableEntry sOOScanClassTableEntries[]
static
Initial value:
= {
}

Definition at line 91 of file OOConstToJSString.m.

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

◆ sOOShipDamageTypeTableEntries

TableEntry sOOShipDamageTypeTableEntries[]
static
Initial value:
= {
}

Definition at line 99 of file OOConstToJSString.m.

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

◆ sOOViewIDTableEntries

TableEntry sOOViewIDTableEntries[]
static
Initial value:
= {
}

Definition at line 95 of file OOConstToJSString.m.

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

◆ sUndefinedString

JSString* sUndefinedString
static

Definition at line 66 of file OOConstToJSString.m.

Referenced by OOConstToJSStringInit(), and OOJSStringFromConstantPRIVATE().