Oolite
Loading...
Searching...
No Matches
OOJSManifest.m
Go to the documentation of this file.
1/*
2
3OOJSManifest.m
4
5Oolite
6Copyright (C) 2004-2013 Giles C Williams and contributors
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
20Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21MA 02110-1301, USA.
22
23*/
24
25#import "OOJSManifest.h"
26#import "OOIsNumberLiteral.h"
27#import "OOJSPlayer.h"
28#import "OOJSPlayerShip.h"
30#import "PlayerEntity.h"
33#import "Universe.h"
34
35static JSValue ManifestComment(OOJS_NATIVE_ARGS);
39
41
42/* SpiderMonkey called the Manifest class's get, set and delete hooks for
43 every property; string-named properties map to cargo quantities. QuickJS
44 has no such hooks, so the class's exotic methods provide the same view:
45 each defined commodity appears as an own property whose value is the
46 cargo quantity, assignments to any other name set the cargo quantity as
47 before and are also stored as an ordinary property (as the SpiderMonkey
48 object kept them in a slot), and delete sets the quantity to undefined,
49 which is an error, as before. The tinyid property list stays on the
50 prototype, and is reported as an own property as SpiderMonkey did for
51 shared permanent properties (commodities are not enumerated, as before).
52*/
53static int ManifestGetOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst object, JSAtom propID);
54static int ManifestDeleteProperty(JSContext* context, JSValueConst object, JSAtom propID);
55static int ManifestSetPropertyExotic(JSContext* context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst receiver, int flags);
56
57static JSClassExoticMethods sManifestExoticMethods = {
58 .get_own_property = ManifestGetOwnProperty,
59 .get_own_property_names = OOJSGetSharedPermanentOwnPropertyNames,
60 .delete_property = ManifestDeleteProperty,
61 .set_property = ManifestSetPropertyExotic,
62};
63
64static JSClassID sManifestClassID;
65static JSClassDef sManifestClass = {
66 .class_name = "Manifest",
67 .finalizer = OOJSObjectWrapperFinalize,
68 .exotic = &sManifestExoticMethods,
69};
70
71enum {
72 kManifest_list // manifest list, array of commodities: name, unit, quantity, displayName - read-only
73};
74
75static const JSCFunctionListEntry sManifestProperties[] = {
76 // JS name Getter ID
78};
79
80static const JSCFunctionListEntry sManifestMethods[] = {
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};
87
88// Helper class wrapped by JS Manifest objects
89@interface OOManifest : NSObject
90@end
91
92@implementation OOManifest
93
94- (void)dealloc
95{
96 [super dealloc];
97}
98
99- (NSString*)oo_jsClassName
100{
101 return @"Manifest";
102}
103
104- (JSValue)oo_jsValueInContext:(JSContext*)context
105{
106 JSValue jsSelf = JS_NewObjectClass(context, sManifestClassID);
107 if (JS_IsException(jsSelf)) {
108 JS_FreeValue(context, JS_GetException(context));
109 return JS_NULL;
110 }
111 JS_SetOpaque(jsSelf, [self retain]);
112
113 return jsSelf;
114}
115
116@end
117
118void InitOOJSManifest(JSContext* context, JSValueConst global)
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}
142
143// Name for a string property ID, or nil for other IDs (symbols and integers, which SpiderMonkey's hooks ignored).
144static NSString* NameForPropertyID(JSContext* context, JSAtom propID)
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}
154
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}
176
177static int ManifestGetOwnProperty(JSContext* context, JSPropertyDescriptor* desc, JSValueConst object, JSAtom propID)
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}
208
209// Returns NO with an exception pending on failure.
210static BOOL ManifestSetProperty(JSContext* context, JSValueConst this, NSString* key, JSValueConst value)
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}
235
236static int ManifestSetPropertyExotic(JSContext* context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst receiver, int flags)
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}
289
290static int ManifestDeleteProperty(JSContext* context, JSValueConst object, JSAtom propID)
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}
308
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}
330
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}
354
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}
376
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}
#define OOJS_PROFILE_EXIT
#define OOJS_NATIVE_ENTER(cx)
#define OOJS_NATIVE_EXIT
#define OOJS_PROFILE_ENTER
static JSBool ManifestShortComment(JSContext *context, uintN argc, jsval *vp)
static JSBool ManifestDeleteProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSFunctionSpec sManifestMethods[]
static JSBool ManifestGetProperty(JSContext *context, JSObject *this, jsid propID, jsval *value)
static JSBool ManifestSetShortComment(JSContext *context, uintN argc, jsval *vp)
static JSClass sManifestClass
static JSBool ManifestComment(JSContext *context, uintN argc, jsval *vp)
static JSPropertySpec sManifestProperties[]
@ kManifest_list
static JSBool ManifestSetComment(JSContext *context, uintN argc, jsval *vp)
JSObject * JSPlayerShipObject(void)
PlayerEntity * OOPlayerForScripting(void)
Definition OOJSPlayer.m:175
void OOJSReportWarning(JSContext *context, NSString *format,...)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJS_RETURN_OBJECT(o)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
#define OOJS_RETURN_BOOL(v)
OOINLINE BOOL OOJSValueToInt32(JSContext *context, jsval value, int32_t *outValue)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
#define OOJS_PROP_READONLY
NSString * OOJSDescribeValue(JSContext *context, jsval value, BOOL abbreviateObjects)
OOINLINE void OOJSReportPendingException(JSContext *context)
void OOJSReportError(JSContext *context, NSString *format,...)
#define OOJS_ARGV
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
void OOJSReportWrappedException(JSContext *context, id exception)
#define MIN(A, B)
Definition OOMaths.h:91
return nil
NSString * OOCommodityType
Definition OOTypes.h:97
OOMassUnit
Definition OOTypes.h:111
@ UNITS_TONS
Definition OOTypes.h:112
static JSValue ManifestSetShortComment(OOJS_NATIVE_ARGS)
static NSString * NameForPropertyID(JSContext *context, JSAtom propID)
static BOOL ManifestSetProperty(JSContext *context, JSValueConst this, NSString *key, JSValueConst value)
static int ManifestSetPropertyExotic(JSContext *context, JSValueConst object, JSAtom propID, JSValueConst value, JSValueConst receiver, int flags)
static JSValue ManifestShortComment(OOJS_NATIVE_ARGS)
static JSValue ManifestGetProperty(OOJS_GETTER_ARGS)
static JSValue ManifestComment(OOJS_NATIVE_ARGS)
static JSClassExoticMethods sManifestExoticMethods
static JSValue ManifestSetComment(OOJS_NATIVE_ARGS)
static JSClassID sManifestClassID
static int ManifestGetOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom propID)
static int ManifestDeleteProperty(JSContext *context, JSValueConst object, JSAtom propID)
void InitOOJSManifest(JSContext *context, JSValueConst global)
int OOJSGetSharedPermanentOwnPropertyNames(JSContext *context, JSPropertyEnum **ptab, uint32_t *plen, JSValueConst object)
NSString * OOJSClassNameOfValue(JSContext *context, JSValueConst value)
JSClassID OOJSRegisterClass(JSClassID *ioClassID, const JSClassDef *classDef)
Definition OOJSClasses.m:61
int OOJSGetSharedPermanentOwnProperty(JSContext *context, JSPropertyDescriptor *desc, JSValueConst object, JSAtom prop)
#define OOJS_PROPERTY_RO(name, getter, magic)
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_GETTER_ARGS
#define OOJS_NATIVE_ARGS
#define OOJS_COUNT(array)
#define UNIVERSE
Definition Universe.h:827
NSString * oo_jsClassName()
OOCargoQuantity setCargoQuantityForType:amount:(OOCommodityType type,[amount] OOCargoQuantity amount)
NSArray * cargoListForScripting()