Oolite
Loading...
Searching...
No Matches
OOJSManifest.m File Reference
Include dependency graph for OOJSManifest.m:

Go to the source code of this file.

Classes

class  OOManifest

Enumerations

enum  { kManifest_list }

Functions

static JSValue ManifestComment (OOJS_NATIVE_ARGS)
static JSValue ManifestSetComment (OOJS_NATIVE_ARGS)
static JSValue ManifestShortComment (OOJS_NATIVE_ARGS)
static JSValue ManifestSetShortComment (OOJS_NATIVE_ARGS)
static JSValue ManifestGetProperty (OOJS_GETTER_ARGS)
static int ManifestGetOwnProperty (JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom propID)
static int ManifestDeleteProperty (JSContext *context, JSValueConst object, JSAtom propID)
static int ManifestSetPropertyExotic (JSContext *context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst receiver, int flags)
void InitOOJSManifest (JSContext *context, JSValueConst global)
static NSString * NameForPropertyID (JSContext *context, JSAtom propID)
static BOOL ManifestSetProperty (JSContext *context, JSValueConst this, NSString *key, JSValueConst value)

Variables

static JSClassExoticMethods sManifestExoticMethods
static JSClassID sManifestClassID
static JSClassDef sManifestClass
static const JSCFunctionListEntry sManifestProperties []
static const JSCFunctionListEntry sManifestMethods []

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
kManifest_list 

Definition at line 71 of file OOJSManifest.m.

71 {
72 kManifest_list // manifest list, array of commodities: name, unit, quantity, displayName - read-only
73};
@ kManifest_list

Function Documentation

◆ InitOOJSManifest()

void InitOOJSManifest ( JSContext * context,
JSValueConst global )

Definition at line 118 of file OOJSManifest.m.

119{
121 JSValueConst manifestPrototype = OOJSInitClass(context, global, sManifestClassID, JS_UNDEFINED, OOJSUnconstructableConstruct, 0,
124
125 // Create manifest object as a property of the player.ship object. (Its private value is NULL.)
126 JSValue playerShip = JSPlayerShipObject(); // Owned.
127 if (JS_IsObject(playerShip)) {
128 JSValue shipManifest = JS_NewObjectProtoClass(context, manifestPrototype, sManifestClassID);
129 if (JS_DefinePropertyValueStr(context, playerShip, "manifest", shipManifest, OOJS_PROP_READONLY) < 0) {
131 }
132 } else if (JS_IsException(playerShip)) {
134 playerShip = JS_UNDEFINED;
135 }
136 JS_FreeValue(context, playerShip);
137
138 // Also define manifest object as a property of the global object.
139 // Wait, what? Why? Oh well, too late now. Deprecate for EMMSTRAN? -- Ahruman 2011-02-10
140 JS_DefinePropertyValueStr(context, global, "manifest", JS_NewObjectProtoClass(context, manifestPrototype, sManifestClassID), OOJS_PROP_READONLY);
141}
static JSFunctionSpec sManifestMethods[]
static JSClass sManifestClass
static JSPropertySpec sManifestProperties[]
JSObject * JSPlayerShipObject(void)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READONLY
OOINLINE void OOJSReportPendingException(JSContext *context)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
static JSClassID sManifestClassID
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
JSValueConst OOJSInitClass(JSContext *context, JSValueConst global, JSClassID classID, JSValueConst parentPrototype, JSCFunction *constructor, int constructorArgCount, const JSCFunctionListEntry *properties, size_t propertyCount, const JSCFunctionListEntry *methods, size_t methodCount, const JSCFunctionListEntry *staticMethods, size_t staticMethodCount)
#define OOJS_COUNT(array)

References JSPlayerShipObject(), OOJS_COUNT, OOJS_PROP_READONLY, OOJSBasicPrivateObjectConverter(), OOJSInitClass(), OOJSRegisterClass(), OOJSRegisterObjectConverter(), OOJSReportPendingException(), OOJSUnconstructableConstruct(), sManifestClass, sManifestClassID, sManifestMethods, and sManifestProperties.

Here is the call graph for this function:

◆ ManifestComment()

JSValue ManifestComment ( OOJS_NATIVE_ARGS )
static

Definition at line 309 of file OOJSManifest.m.

310{
311 OOJS_NATIVE_ENTER(context)
312
313 OOCommodityType good = nil;
314 NSString* information = nil;
315
316 if (argc > 0) {
317 good = OOStringFromJSValue(context, OOJS_ARGV[0]);
318 }
319 if (good == nil) {
320 OOJSReportBadArguments(context, @"Manifest", @"comment", MIN(argc, 1), OOJS_ARGV, nil, @"good");
321 return JS_EXCEPTION;
322 }
323
324 information = [[PLAYER shipCommodityData] commentForGood:good];
325
326 OOJS_RETURN_OBJECT(information);
327
329}
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_RETURN_OBJECT(o)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
#define OOJS_ARGV
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define MIN(A, B)
Definition OOMaths.h:91
return nil
NSString * OOCommodityType
Definition OOTypes.h:97

References MIN, nil, OOJS_ARGV, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_OBJECT, OOJSReportBadArguments(), and OOStringFromJSValue().

Here is the call graph for this function:

◆ ManifestDeleteProperty()

int ManifestDeleteProperty ( JSContext * context,
JSValueConst object,
JSAtom propID )
static

Definition at line 290 of file OOJSManifest.m.

291{
293
294 @try {
295 // As before, deleting is setting to undefined.
296 NSString* key = NameForPropertyID(context, propID);
297 if (key == nil)
298 return YES;
299 return ManifestSetProperty(context, object, key, JS_UNDEFINED) ? YES : -1;
300 }
301 @catch (id exception) {
302 OOJSReportWrappedException(context, exception);
303 return -1;
304 }
305
307}
#define OOJS_PROFILE_EXIT
#define OOJS_PROFILE_ENTER
static JSBool ManifestSetProperty(JSContext *context, JSObject *this, jsid propID, JSBool strict, jsval *value)
void OOJSReportWrappedException(JSContext *context, id exception)
static NSString * NameForPropertyID(JSContext *context, JSAtom propID)

References ManifestSetProperty(), NameForPropertyID(), nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, and OOJSReportWrappedException().

Here is the call graph for this function:

◆ ManifestGetOwnProperty()

int ManifestGetOwnProperty ( JSContext * context,
JSPropertyDescriptor * desc,
JSValueConst object,
JSAtom propID )
static

Definition at line 177 of file OOJSManifest.m.

178{
180
181 @try {
183
184 /* 'list' property is hard-coded
185 * others map to the commodity keys in trade-goods.plist
186 * compatible-ish with 1.80 and earlier except that
187 * alienItems and similar aliases don't work */
188 NSString* key = NameForPropertyID(context, propID);
189 if (key != nil && [[UNIVERSE commodities] goodDefined:key]) {
190 if (desc != NULL) {
191 desc->flags = JS_PROP_C_W_E;
192 desc->value = JS_NewInt32(context, [entity cargoQuantityForType:key]);
193 desc->getter = JS_UNDEFINED;
194 desc->setter = JS_UNDEFINED;
195 }
196 return YES;
197 }
198 // As SpiderMonkey did, report "list" (a shared permanent prototype property) as an own property.
199 return OOJSGetSharedPermanentOwnProperty(context, desc, object, propID);
200 }
201 @catch (id exception) {
202 OOJSReportWrappedException(context, exception);
203 return -1;
204 }
205
207}
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
int OOJSGetSharedPermanentOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom prop)
#define UNIVERSE
Definition Universe.h:827

References NameForPropertyID(), nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, OOJSGetSharedPermanentOwnProperty(), OOJSReportWrappedException(), OOPlayerForScripting(), and UNIVERSE.

Here is the call graph for this function:

◆ ManifestGetProperty()

JSValue ManifestGetProperty ( OOJS_GETTER_ARGS )
static

Definition at line 155 of file OOJSManifest.m.

156{
157 OOJS_NATIVE_ENTER(context)
158
159 id result = nil;
161
162 switch (magic) {
163 case kManifest_list:
164 result = [entity cargoListForScripting];
165 break;
166
167 default:
169 return JS_EXCEPTION;
170 }
171
172 return OOJSValueFromNativeObject(context, result);
173
175}
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
NSArray * cargoListForScripting()

References PlayerEntity::cargoListForScripting, kManifest_list, nil, OOJS_COUNT, OOJS_GETTER_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJSReportBadPropertySelector(), OOJSValueFromNativeObject(), OOPlayerForScripting(), and sManifestProperties.

Here is the call graph for this function:

◆ ManifestSetComment()

JSValue ManifestSetComment ( OOJS_NATIVE_ARGS )
static

Definition at line 331 of file OOJSManifest.m.

332{
333 OOJS_NATIVE_ENTER(context)
334
335 BOOL OK;
336 OOCommodityType good = nil;
337 NSString* information = nil;
338
339 if (argc > 1) {
340 good = OOStringFromJSValue(context, OOJS_ARGV[0]);
341 information = OOStringFromJSValue(context, OOJS_ARGV[1]);
342 }
343 if (good == nil || information == nil) {
344 OOJSReportBadArguments(context, @"Manifest", @"setComment", MIN(argc, 2), OOJS_ARGV, nil, @"good and information text");
345 return JS_EXCEPTION;
346 }
347
348 OK = [[PLAYER shipCommodityData] setComment:information forGood:good];
349
351
353}
#define OOJS_RETURN_BOOL(v)

References MIN, nil, OOJS_ARGV, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_BOOL, OOJSReportBadArguments(), and OOStringFromJSValue().

Here is the call graph for this function:

◆ ManifestSetProperty()

BOOL ManifestSetProperty ( JSContext * context,
JSValueConst this,
NSString * key,
JSValueConst value )
static

Definition at line 210 of file OOJSManifest.m.

211{
213 int32_t iValue;
214
215 OOMassUnit unit = [[UNIVERSE commodityMarket] massUnitForGood:key];
216 // we can always change gold, platinum & gem-stones quantities, even with special cargo
217 if (unit == UNITS_TONS && [entity specialCargo]) {
218 OOJSReportWarning(context, @"PlayerShip.manifest['foo'] - cannot modify cargo tonnage when Special Cargo is in use.");
219 return YES;
220 }
221
222 if (OOJSValueToInt32(context, value, &iValue)) {
223 if (iValue < 0)
224 iValue = 0;
225 [entity setCargoQuantityForType:key amount:iValue];
226 } else {
227 // OOJSReportBadPropertyValue() takes a tinyid; this is the same message for a named property.
228 if (JS_HasException(context))
229 JS_FreeValue(context, JS_GetException(context));
230 OOJSReportError(context, @"Cannot set property %@ of instance of %@ to invalid value %@.", key, OOJSClassNameOfValue(context, this), OOJSDescribeValue(context, value, YES));
231 return NO;
232 }
233 return YES;
234}
void OOJSReportWarning(JSContext *context, NSString *format,...)
OOINLINE BOOL OOJSValueToInt32(JSContext *context, jsval value, int32_t *outValue)
NSString * OOJSDescribeValue(JSContext *context, jsval value, BOOL abbreviateObjects)
void OOJSReportError(JSContext *context, NSString *format,...)
OOMassUnit
Definition OOTypes.h:111
@ UNITS_TONS
Definition OOTypes.h:112
NSString * OOJSClassNameOfValue(JSContext *context, JSValueConst value)

References OOJSClassNameOfValue(), OOJSDescribeValue(), OOJSReportError(), OOJSReportWarning(), OOJSValueToInt32(), OOPlayerForScripting(), PlayerEntity::setCargoQuantityForType:amount:, and UNITS_TONS.

Referenced by ManifestDeleteProperty(), ManifestDeleteProperty(), and ManifestSetPropertyExotic().

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

◆ ManifestSetPropertyExotic()

int ManifestSetPropertyExotic ( JSContext * context,
JSValueConst object,
JSAtom propID,
JSValueConst value,
JSValueConst receiver,
int flags )
static

Definition at line 236 of file OOJSManifest.m.

237{
239
240 @try {
241 NSString* key = NameForPropertyID(context, propID);
242 if (key == nil) {
243 // Not handled by the SpiderMonkey hook; store as an ordinary property.
244 return JS_DefinePropertyValue(context, receiver, propID, JS_DupValue(context, value), JS_PROP_C_W_E);
245 }
246
247 // A read-only property inherited from the prototype (list) can't be assigned.
248 JSValue prototype = JS_GetPrototype(context, object);
249 if (JS_IsException(prototype))
250 return -1;
251 JSPropertyDescriptor protoDesc;
252 int found = JS_IsObject(prototype) ? JS_GetOwnProperty(context, &protoDesc, prototype, propID) : 0;
253 JS_FreeValue(context, prototype);
254 if (found < 0)
255 return -1;
256 if (found) {
257 BOOL readOnly = (protoDesc.flags & JS_PROP_GETSET) ? JS_IsUndefined(protoDesc.setter) : !(protoDesc.flags & JS_PROP_WRITABLE);
258 JS_FreeValue(context, protoDesc.value);
259 JS_FreeValue(context, protoDesc.getter);
260 JS_FreeValue(context, protoDesc.setter);
261 if (readOnly) {
262 if (flags & JS_PROP_THROW) {
263 OOJSReportError(context, @"Cannot set read-only property %@ of instance of %@.", key, OOJSClassNameOfValue(context, receiver));
264 return -1;
265 }
266 return NO;
267 }
268 }
269
270 if (!ManifestSetProperty(context, receiver, key, value))
271 return -1;
272
273 /* SpiderMonkey also stored the value in a slot. For commodities, the
274 get hook overrode it; for other names, it was what later reads
275 returned.
276 */
277 if (![[UNIVERSE commodities] goodDefined:key]) {
278 return JS_DefinePropertyValue(context, receiver, propID, JS_DupValue(context, value), JS_PROP_C_W_E);
279 }
280 return YES;
281 }
282 @catch (id exception) {
283 OOJSReportWrappedException(context, exception);
284 return -1;
285 }
286
288}

References ManifestSetProperty(), NameForPropertyID(), nil, OOJS_PROFILE_ENTER, OOJS_PROFILE_EXIT, OOJSClassNameOfValue(), OOJSReportError(), OOJSReportWrappedException(), and UNIVERSE.

Here is the call graph for this function:

◆ ManifestSetShortComment()

JSValue ManifestSetShortComment ( OOJS_NATIVE_ARGS )
static

Definition at line 377 of file OOJSManifest.m.

378{
379 OOJS_NATIVE_ENTER(context)
380
381 BOOL OK;
382 OOCommodityType good = nil;
383 NSString* information = nil;
384
385 if (argc > 1) {
386 good = OOStringFromJSValue(context, OOJS_ARGV[0]);
387 information = OOStringFromJSValue(context, OOJS_ARGV[1]);
388 }
389 if (good == nil || information == nil) {
390 OOJSReportBadArguments(context, @"Manifest", @"setShortComment", MIN(argc, 2), OOJS_ARGV, nil, @"good and information text");
391 return JS_EXCEPTION;
392 }
393
394 OK = [[PLAYER shipCommodityData] setShortComment:information forGood:good];
395
397
399}

References MIN, nil, OOJS_ARGV, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_BOOL, OOJSReportBadArguments(), and OOStringFromJSValue().

Here is the call graph for this function:

◆ ManifestShortComment()

JSValue ManifestShortComment ( OOJS_NATIVE_ARGS )
static

Definition at line 355 of file OOJSManifest.m.

356{
357 OOJS_NATIVE_ENTER(context)
358
359 OOCommodityType good = nil;
360 NSString* information = nil;
361
362 if (argc > 0) {
363 good = OOStringFromJSValue(context, OOJS_ARGV[0]);
364 }
365 if (good == nil) {
366 OOJSReportBadArguments(context, @"Manifest", @"shortComment", MIN(argc, 1), OOJS_ARGV, nil, @"good");
367 return JS_EXCEPTION;
368 }
369
370 information = [[PLAYER shipCommodityData] shortCommentForGood:good];
371
372 OOJS_RETURN_OBJECT(information);
373
375}

References MIN, nil, OOJS_ARGV, OOJS_NATIVE_ARGS, OOJS_NATIVE_ENTER, OOJS_NATIVE_EXIT, OOJS_RETURN_OBJECT, OOJSReportBadArguments(), and OOStringFromJSValue().

Here is the call graph for this function:

◆ NameForPropertyID()

NSString * NameForPropertyID ( JSContext * context,
JSAtom propID )
static

Definition at line 144 of file OOJSManifest.m.

145{
146 NSString* result = nil;
147 JSValue idValue = JS_AtomToValue(context, propID);
148 if (JS_IsString(idValue)) {
149 result = OOStringFromJSValue(context, idValue);
150 }
151 JS_FreeValue(context, idValue);
152 return result;
153}

References nil, and OOStringFromJSValue().

Referenced by ManifestDeleteProperty(), ManifestGetOwnProperty(), and ManifestSetPropertyExotic().

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

Variable Documentation

◆ sManifestClass

JSClassDef sManifestClass
static
Initial value:
= {
.class_name = "Manifest",
}
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
static JSClassExoticMethods sManifestExoticMethods

Definition at line 65 of file OOJSManifest.m.

65 {
66 .class_name = "Manifest",
67 .finalizer = OOJSObjectWrapperFinalize,
68 .exotic = &sManifestExoticMethods,
69};

Referenced by OOManifest::oo_jsValueInContext:.

◆ sManifestClassID

JSClassID sManifestClassID
static

Definition at line 64 of file OOJSManifest.m.

Referenced by InitOOJSManifest().

◆ sManifestExoticMethods

JSClassExoticMethods sManifestExoticMethods
static
Initial value:
= {
.get_own_property = ManifestGetOwnProperty,
.get_own_property_names = OOJSGetSharedPermanentOwnPropertyNames,
.delete_property = ManifestDeleteProperty,
.set_property = ManifestSetPropertyExotic,
}
static JSBool ManifestDeleteProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static int ManifestSetPropertyExotic(JSContext *context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst receiver, int flags)
static int ManifestGetOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom propID)
int OOJSGetSharedPermanentOwnPropertyNames(JSContext *context, JSPropertyEnum **ptab, uint32_t *plen, JSValueConst object)

Definition at line 57 of file OOJSManifest.m.

57 {
58 .get_own_property = ManifestGetOwnProperty,
59 .get_own_property_names = OOJSGetSharedPermanentOwnPropertyNames,
60 .delete_property = ManifestDeleteProperty,
61 .set_property = ManifestSetPropertyExotic,
62};

◆ sManifestMethods

const JSCFunctionListEntry sManifestMethods[]
static
Initial value:
= {
JS_CFUNC_DEF("shortComment", 1, ManifestShortComment),
JS_CFUNC_DEF("setShortComment", 2, ManifestSetShortComment),
JS_CFUNC_DEF("comment", 1, ManifestComment),
JS_CFUNC_DEF("setComment", 2, ManifestSetComment),
}
static JSBool ManifestShortComment(JSContext *context, uintN argc, jsval *vp)
static JSBool ManifestSetShortComment(JSContext *context, uintN argc, jsval *vp)
static JSBool ManifestComment(JSContext *context, uintN argc, jsval *vp)
static JSBool ManifestSetComment(JSContext *context, uintN argc, jsval *vp)

Definition at line 80 of file OOJSManifest.m.

80 {
81 // JS name Function min args
82 JS_CFUNC_DEF("shortComment", 1, ManifestShortComment),
83 JS_CFUNC_DEF("setShortComment", 2, ManifestSetShortComment),
84 JS_CFUNC_DEF("comment", 1, ManifestComment),
85 JS_CFUNC_DEF("setComment", 2, ManifestSetComment),
86};

◆ sManifestProperties

const JSCFunctionListEntry sManifestProperties[]
static
Initial value:
= {
}
static JSBool ManifestGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
#define OOJS_PROPERTY_RO(name, getter, magic)

Definition at line 75 of file OOJSManifest.m.

75 {
76 // JS name Getter ID
78};