Oolite 1.91.0.7644-241112-7f5034b
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 77 of file OOConstToJSString.m.

◆ ENTRY

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

Definition at line 75 of file OOConstToJSString.m.

◆ GALACTIC_HYPERSPACE_ENTRY

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

Definition at line 76 of file OOConstToJSString.m.

◆ TABLE

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

Definition at line 60 of file OOConstToJSString.m.

Typedef Documentation

◆ ConstTable

typedef struct ConstTable ConstTable

Function Documentation

◆ CompareEntries()

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

Definition at line 173 of file OOConstToJSString.m.

174{
175 const TableEntry *entA = a;
176 const TableEntry *entB = b;
177
178 if (entA->value < entB->value) return -1;
179 if (entA->value > entB->value) return 1;
180 return 0;
181}
NSInteger value

References TableEntry::value.

Referenced by InitTable().

+ Here is the caller graph for this function:

◆ InitTable()

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

Definition at line 184 of file OOConstToJSString.m.

185{
186 NSCParameterAssert(context != NULL && JS_IsInRequest(context) && table != NULL);
187
188 NSUInteger i;
189 for(i = 0; i < table->count; i++)
190 {
191 table->entries[i].jsString = JS_InternString(context, table->entries[i].cString);
192 }
193
194 qsort(table->entries, table->count, sizeof *table->entries, CompareEntries);
195}
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 232 of file OOConstToJSString.m.

233{
234 NSCAssert1(sInited, @"%s called before OOConstToJSStringInit().", __PRETTY_FUNCTION__);
235 NSCParameterAssert(context != NULL && JS_IsInRequest(context) && table != NULL);
236
237 // Quick pass: look for pointer-equal string.
238 NSUInteger i, count = table->count;
239 for(i = 0; i < count; i++)
240 {
241 if (table->entries[i].jsString == string)
242 {
243 return table->entries[i].value;
244 }
245 }
246
247
248 // Slow pass: use string comparison. This is expected to fail.
249 if (string != NULL)
250 {
251 for(i = 0; i < count; i++)
252 {
253 int32 result;
254 if (JS_CompareStrings(context, string, table->entries[i].jsString, &result) && result == 0)
255 {
256 return table->entries[i].value;
257 }
258 }
259 }
260
261 // Fail.
262 return defaultValue;
263}
static BOOL sInited
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 266 of file OOConstToJSString.m.

267{
268 if (EXPECT(JSVAL_IS_STRING(value)))
269 {
270 return OOConstantFromJSStringPRIVATE(context, JSVAL_TO_STRING(value), table, defaultValue);
271 }
272 else
273 {
274 return defaultValue;
275 }
276
277}
NSUInteger OOConstantFromJSStringPRIVATE(JSContext *context, JSString *string, struct ConstTable *table, NSInteger defaultValue)
#define EXPECT(x)

References EXPECT, and OOConstantFromJSStringPRIVATE().

Referenced by OOCompassModeFromJSValue(), OOEntityStatusFromJSValue(), OOGalacticHyperspaceBehaviourFromJSValue(), OOGUIScreenIDFromJSValue(), OOScanClassFromJSValue(), 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 165 of file OOConstToJSString.m.

166{
167 NSCAssert(sInited, @"OOConstToJSStringDestroy() called while not inited.");
168 sInited = NO;
169 // jsString pointers are now officially junk.
170}

References sInited.

◆ OOConstToJSStringInit()

void OOConstToJSStringInit ( JSContext * context)

Definition at line 144 of file OOConstToJSString.m.

145{
146 NSCAssert(!sInited, @"OOConstToJSStringInit() called while already inited.");
147 NSCParameterAssert(context != NULL && JS_IsInRequest(context));
148
149 sUndefinedString = JS_InternString(context, "UNDEFINED");
150
160
161 sInited = YES;
162}
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

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

+ Here is the call graph for this function:

◆ OOJSStringFromConstantPRIVATE()

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

Definition at line 200 of file OOConstToJSString.m.

201{
202 NSCAssert1(sInited, @"%s called before OOConstToJSStringInit().", __PRETTY_FUNCTION__);
203 NSCParameterAssert(context != NULL && JS_IsInRequest(context));
204 NSCParameterAssert(table != NULL && table->count > 0);
205
206 // Binary search.
207 NSUInteger min = 0, max = table->count - 1;
208 NSInteger current;
209 do
210 {
211 NSUInteger mid = (min + max) / 2;
212 current = table->entries[mid].value;
213 if (current < value)
214 {
215 min = mid + 1;
216 }
217 else if (current > value)
218 {
219 max = mid - 1;
220 }
221 else
222 {
223 return table->entries[mid].jsString;
224 }
225 }
226 while (min <= max);
227
228 return sUndefinedString;
229}

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

ConstTable gOOLegalStatusReasonConstTable = TABLE(sOOLegalStatusReasonTableEntries)

Definition at line 136 of file OOConstToJSString.m.

Referenced by OOConstToJSStringInit(), and OOJSStringFromLegalStatusReason().

◆ gOOLongRangeChartModeConstTable

ConstTable gOOLongRangeChartModeConstTable = TABLE(sOOLongRangeChartModeTableEntries)

Definition at line 137 of file OOConstToJSString.m.

Referenced by OOConstToJSStringInit().

◆ gOOScanClassConstTable

◆ gOOShipDamageTypeConstTable

ConstTable gOOShipDamageTypeConstTable = TABLE(sOOShipDamageTypeTableEntries)

Definition at line 135 of file OOConstToJSString.m.

Referenced by OOConstToJSStringInit(), and OOJSStringFromShipDamageType().

◆ gOOViewIDConstTable

◆ sInited

◆ sOOCompassModeTableEntries

TableEntry sOOCompassModeTableEntries[]
static
Initial value:
=
{
}

Definition at line 79 of file OOConstToJSString.m.

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

◆ sOOEntityStatusTableEntries

TableEntry sOOEntityStatusTableEntries[]
static
Initial value:
=
{
}

Definition at line 84 of file OOConstToJSString.m.

85{
86 #include "OOEntityStatus.tbl"
87};

◆ sOOGalacticHyperspaceBehaviourTableEntries

TableEntry sOOGalacticHyperspaceBehaviourTableEntries[]
static
Initial value:
=
{
}

Definition at line 89 of file OOConstToJSString.m.

90{
91 #include "OOGalacticHyperspaceBehaviour.tbl"
92};

◆ sOOGUIScreenIDTableEntries

TableEntry sOOGUIScreenIDTableEntries[]
static
Initial value:
=
{
}

Definition at line 94 of file OOConstToJSString.m.

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

◆ sOOLegalStatusReasonTableEntries

TableEntry sOOLegalStatusReasonTableEntries[]
static
Initial value:
=
{
}

Definition at line 114 of file OOConstToJSString.m.

115{
116 #include "OOLegalStatusReason.tbl"
117};

◆ sOOLongRangeChartModeTableEntries

TableEntry sOOLongRangeChartModeTableEntries[]
static
Initial value:
=
{
}

Definition at line 119 of file OOConstToJSString.m.

120{
121 #include "OOLongRangeChartMode.tbl"
122};

◆ sOOScanClassTableEntries

TableEntry sOOScanClassTableEntries[]
static
Initial value:
=
{
}

Definition at line 99 of file OOConstToJSString.m.

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

◆ sOOShipDamageTypeTableEntries

TableEntry sOOShipDamageTypeTableEntries[]
static
Initial value:
=
{
}

Definition at line 109 of file OOConstToJSString.m.

110{
111 #include "OOShipDamageType.tbl"
112};

◆ sOOViewIDTableEntries

TableEntry sOOViewIDTableEntries[]
static
Initial value:
=
{
}

Definition at line 104 of file OOConstToJSString.m.

105{
106 #include "OOViewID.tbl"
107};

◆ sUndefinedString

JSString* sUndefinedString
static

Definition at line 69 of file OOConstToJSString.m.

Referenced by OOConstToJSStringInit(), and OOJSStringFromConstantPRIVATE().