Oolite
Loading...
Searching...
No Matches
OOJavaScriptEngine.h
Go to the documentation of this file.
1/*
2
3OOJavaScriptEngine.h
4
5JavaScript support for Oolite
6Copyright (C) 2007-2013 David Taylor and Jens Ayton.
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#pragma once
26
27#import "OOFoundation.h"
28#import "PlayerEntity.h"
30#import "Universe.h"
31#include <jsapi.h>
32
33#define OOJSENGINE_MONITOR_SUPPORT OO_DEBUG_MONITOR_ENABLED
34
35#import "OOJSPropID.h"
36
37@protocol OOJavaScriptEngineMonitor;
38
39@interface OOJavaScriptEngine : NSObject {
40@private
41 JSRuntime* _runtime;
42 JSObject* _globalObject;
44
45 JSClass* _objectClass;
46 JSClass* _stringClass;
47 JSClass* _arrayClass;
48 JSClass* _numberClass;
49 JSClass* _booleanClass;
50
51#ifdef OO_DEBUG_MONITOR_ENABLED
52 BOOL _dumpStackForErrors;
53 BOOL _dumpStackForWarnings;
54#endif
55#if OOJSENGINE_MONITOR_SUPPORT
56 id<OOJavaScriptEngineMonitor> _monitor;
57#endif
58}
59
60+ (OOJavaScriptEngine*)sharedEngine;
61
62- (JSObject*)globalObject;
63
64- (void)runMissionCallback;
65
66/* Tear down context and global object and rebuild them from scratch. This
67 invalidates -globalObject and the main thread context.
68*/
69- (BOOL)reset;
70
71// Call a JS function, setting up new contexts as necessary. Caller is responsible for ensuring the jsval passed really is a function.
72- (BOOL)callJSFunction:(jsval)function
73 forObject:(JSObject*)jsThis
74 argc:(uintN)argc
75 argv:(jsval*)argv
76 result:(jsval*)outResult;
77
78- (void)removeGCObjectRoot:(JSObject**)rootPtr;
79- (void)removeGCValueRoot:(jsval*)rootPtr;
80
81- (void)garbageCollectionOpportunity:(BOOL)force;
82
83- (BOOL)showErrorLocations;
84- (void)setShowErrorLocations:(BOOL)value;
85
86- (JSClass*)objectClass;
87- (JSClass*)stringClass;
88- (JSClass*)arrayClass;
89- (JSClass*)numberClass;
90- (JSClass*)booleanClass;
91
92#ifdef OO_DEBUG_MONITOR_ENABLED
93- (BOOL)dumpStackForErrors;
94- (void)setDumpStackForErrors:(BOOL)value;
95
96- (BOOL)dumpStackForWarnings;
97- (void)setDumpStackForWarnings:(BOOL)value;
98
99// Install handler for JS "debugger" statment.
100- (void)enableDebuggerStatement;
101#endif
102
103@end
104
105#if !JS_THREADSAFE
106#define JS_IsInRequest(context) (((void)(context)), YES)
107#define JS_BeginRequest(context) \
108 do { \
109 } while (0)
110#define JS_EndRequest(context) \
111 do { \
112 } while (0)
113#endif
114
115// Get the main thread's JS context, and begin a request on it.
117{
118 extern JSContext* gOOJSMainThreadContext;
119 NSCAssert(gOOJSMainThreadContext != NULL, @"Attempt to use JavaScript context before JavaScript engine is initialized.");
122}
123
124// End a request on the main thread's context.
125OOINLINE void OOJSRelinquishContext(JSContext* context)
126{
127#ifdef OO_DEBUG_MONITOR_ENABLED
128 extern JSContext* gOOJSMainThreadContext;
129 NSCParameterAssert(context == gOOJSMainThreadContext && JS_IsInRequest(context));
130#endif
131 JS_EndRequest(context);
132}
133
134// Notifications sent when JavaScript engine is reset.
135extern NSString* const kOOJavaScriptEngineWillResetNotification;
136extern NSString* const kOOJavaScriptEngineDidResetNotification;
137
138/* Engine-neutral names, also defined by the QuickJS header, so code outside
139 src/Core/Scripting can be built against either engine. With QuickJS,
140 values are reference counted and OOJSFreeValue() must be called on owned
141 values; with SpiderMonkey it does nothing.
142*/
143typedef jsval OOJSVal;
144#define OOJSFreeValue(context, value) \
145 do { \
146 (void)(context); \
147 (void)(value); \
148 } while (0)
149#define OOJSUndefinedValue() JSVAL_VOID
150#define OOJSNullValue() JSVAL_NULL
152{
153 return JSVAL_IS_VOID(value);
154}
155OOINLINE jsval OOJSValueFromDouble(JSContext* context, double value)
156{
157 jsval result = JSVAL_VOID;
158 JS_NewNumberValue(context, value, &result);
159 return result;
160}
161// Truthiness of value; NO if conversion fails.
162OOINLINE BOOL OOJSValueToBOOL(JSContext* context, jsval value)
163{
164 JSBool result;
165 if (!JS_ValueToBoolean(context, value, &result))
166 return NO;
167 return result;
168}
169OOINLINE BOOL OOJSValueToInt32(JSContext* context, jsval value, int32_t* outValue)
170{
171 return JS_ValueToInt32(context, value, outValue);
172}
173OOINLINE void OOJSReportPendingException(JSContext* context)
174{
175 JS_ReportPendingException(context);
176}
177
178/* Error and warning reporters.
179
180 Note that after reporting an error in a JavaScript callback, the caller
181 must return NO to signal an error.
182*/
183void OOJSReportError(JSContext* context, NSString* format, ...);
184void OOJSReportErrorWithArguments(JSContext* context, NSString* format, va_list args);
185void OOJSReportErrorForCaller(JSContext* context, NSString* scriptClass, NSString* function, NSString* format, ...);
186
187void OOJSReportWarning(JSContext* context, NSString* format, ...);
188void OOJSReportWarningWithArguments(JSContext* context, NSString* format, va_list args);
189void OOJSReportWarningForCaller(JSContext* context, NSString* scriptClass, NSString* function, NSString* format, ...);
190
191void OOJSReportBadPropertySelector(JSContext* context, JSObject* thisObj, jsid propID, JSPropertySpec* propertySpec);
192void OOJSReportBadPropertyValue(JSContext* context, JSObject* thisObj, jsid propID, JSPropertySpec* propertySpec, jsval value);
193void OOJSReportBadArguments(JSContext* context, NSString* scriptClass, NSString* function, uintN argc, jsval* argv, NSString* message, NSString* expectedArgsDescription);
194
195/* OOJSSetWarningOrErrorStackSkip()
196
197 Indicate that the direct call site is not relevant for error handler.
198 Currently, if non-zero, no call site information is provided.
199 Ideally, we'd stack crawl instead.
200*/
201void OOJSSetWarningOrErrorStackSkip(unsigned skip);
202
203/* OOJSArgumentListGetNumber()
204
205 Get a single number from an argument list. The optional outConsumed
206 argument can be used to find out how many parameters were used (currently,
207 this will be 0 on failure, otherwise 1).
208
209 On failure, it will return NO and raise an error. If the caller is a JS
210 callback, it must return NO to signal an error.
211*/
212BOOL OOJSArgumentListGetNumber(JSContext* context, NSString* scriptClass, NSString* function, uintN argc, jsval* argv, double* outNumber, uintN* outConsumed);
213
214/* OOJSArgumentListGetNumberNoError()
215
216 Like OOJSArgumentListGetNumber(), but does not report an error on failure.
217*/
218BOOL OOJSArgumentListGetNumberNoError(JSContext* context, uintN argc, jsval* argv, double* outNumber, uintN* outConsumed);
219
220// Typed as int rather than BOOL to work with more general expressions such as bitfield tests.
223{
224 return BOOLEAN_TO_JSVAL(b != NO);
225}
226
227@interface NSObject (OOJavaScript)
228
229/* -oo_jsValueInContext:
230
231 Return the JavaScript value representation of an object. The default
232 implementation returns JSVAL_VOID.
233
234 SAFETY NOTE: if this message is sent to nil, the return value depends on
235 the platform and whether JS_USE_JSVAL_JSID_STRUCT_TYPES is set. If the
236 receiver may be nil, use OOJSValueFromNativeObject() instead.
237
238 One case where it is safe to use oo_jsValueInContext: is with objects
239 retrieved from Foundation collections, as they can never be nil.
240
241 Requires a request on context.
242*/
243- (jsval)oo_jsValueInContext:(JSContext*)context;
244
245/* -oo_jsDescription
246 -oo_jsDescriptionWithClassName:
247 -oo_jsClassName
248
249 See comments for -descriptionComponents in OOFoundation.h.
250*/
251- (NSString*)oo_jsDescription;
252- (NSString*)oo_jsDescriptionWithClassName:(NSString*)className;
253- (NSString*)oo_jsClassName;
254
255/* oo_clearJSSelf:
256 This is called by OOJSObjectWrapperFinalize() when a JS object wrapper is
257 collected. The default implementation does nothing.
258*/
259- (void)oo_clearJSSelf:(JSObject*)selfVal;
260
261@end
262
263/* OOJSValueFromNativeObject()
264 Return a JavaScript value representation of an object, or null if passed
265 nil.
266
267 Requires a request on context.
268*/
269OOINLINE jsval OOJSValueFromNativeObject(JSContext* context, id object)
270{
271 if (object != nil)
272 return [object oo_jsValueInContext:context];
273 return JSVAL_NULL;
274}
275
276/* OOJSObjectFromNativeObject()
277 Return a JavaScript object representation of an object, or null if passed
278 nil. The value is boxed if necessary.
279
280 Requires a request on context.
281*/
282JSObject* OOJSObjectFromNativeObject(JSContext* context, id object);
283
284/* OOJSValue: an object whose purpose in life is to hold a JavaScript value.
285 This is somewhat useful for putting JavaScript objects in ObjC collections,
286 for instance to pass as properties to script loaders. The value is
287 GC rooted for the lifetime of the OOJSValue.
288
289 All methods take a context parameter, which must either be nil or a context
290 in a request.
291*/
292@interface OOJSValue : NSObject {
293 jsval _val;
294}
295
296+ (id)valueWithJSValue:(jsval)value inContext:(JSContext*)context;
297+ (id)valueWithJSObject:(JSObject*)object inContext:(JSContext*)context;
298
299- (id)initWithJSValue:(jsval)value inContext:(JSContext*)context;
300- (id)initWithJSObject:(JSObject*)object inContext:(JSContext*)context;
301
302@end
303
304/**** String utilities ****/
305
306/* OOJSSTR(const char * [literal])
307
308 Create and cache a jsval referring to an interned string literal.
309*/
310#define OOJSSTR(str) ({ static jsval strCache; static BOOL inited; if (EXPECT_NOT(!inited)) OOJSStrLiteralCachePRIVATE(""str, &strCache, &inited); strCache; })
311void OOJSStrLiteralCachePRIVATE(const char* string, jsval* strCache, BOOL* inited);
312
313// Convert a JSString to an NSString.
314NSString* OOStringFromJSString(JSContext* context, JSString* string);
315
316/* Convert an arbitrary JS object to an NSString, calling JS_ValueToString.
317 OOStringFromJSValue() returns nil if value is null or undefined,
318 OOStringFromJSValueEvenIfNull() returns "null" or "undefined".
319*/
320NSString* OOStringFromJSValue(JSContext* context, jsval value);
321NSString* OOStringFromJSValueEvenIfNull(JSContext* context, jsval value);
322
323/* OOStringFromJSPropertyIDAndSpec(context, propID, propertySpec)
324
325 Returns the name of a property given either a name or a tinyid. (Intended
326 for error reporting inside JSPropertyOps.)
327*/
328NSString* OOStringFromJSPropertyIDAndSpec(JSContext* context, jsid propID, JSPropertySpec* propertySpec);
329
330/* Describe a value for debugging or error reporting. Strings are quoted,
331 escaped and limited in length. Functions are described as "function foo"
332 (or just "function" if they're anonymous). Up to four elements of arrays
333 are included, followed by total count of there are more than four.
334 If abbreviateObjects, the description "[object Object]" is replaced with
335 "{...}", which may or may not be clearer depending on context.
336*/
337NSString* OOJSDescribeValue(JSContext* context, jsval value, BOOL abbreviateObjects);
338
339// Convert a jsid to an NSString.
340NSString* OOStringFromJSID(jsid propID);
341
342// Convert an NSString to a jsid.
343jsid OOJSIDFromString(NSString* string);
344
345@interface NSString (OOJavaScriptExtensions)
346
347// For diagnostic messages; produces things like @"(42, true, "a string", an object description)".
348+ (NSString*)stringWithJavaScriptParameters:(jsval*)params count:(uintN)count inContext:(JSContext*)context;
349
350// Concatenate sequence of arbitrary JS objects into string.
351+ (NSString*)concatenationOfStringsFromJavaScriptValues:(jsval*)values count:(size_t)count separator:(NSString*)separator inContext:(JSContext*)context;
352
353// Add escape codes for string so that it's a valid JavaScript literal (if you put "" or '' around it).
354- (NSString*)escapedForJavaScriptLiteral;
355
356@end
357
358// OOEntityFilterPredicate wrapping a JavaScript function.
359typedef struct
360{
361 JSContext* context;
362 jsval function; // Caller is responsible for ensuring this is a function object (using OOJSValueIsFunction()).
363 JSObject* jsThis;
364 BOOL errorFlag; // Set if a JS exception occurs. The
365 // exception will have been reported.
366 // This also supresses further filtering.
368BOOL JSFunctionPredicate(Entity* entity, void* parameter);
369
370// YES for ships and (normal) planets. Parameter: ignored.
371BOOL JSEntityIsJavaScriptVisiblePredicate(Entity* entity, void* parameter);
372
373// YES for ships other than sub-entities and menu-display ships, and planets other than atmospheres and menu miniatures. Parameter: ignored.
374BOOL JSEntityIsJavaScriptSearchablePredicate(Entity* entity, void* parameter);
375
376// YES for menu-display ships. Parameter: ignored
377BOOL JSEntityIsDemoShipPredicate(Entity* entity, void* parameter);
378
379// These require a request on context.
380id OOJSNativeObjectFromJSValue(JSContext* context, jsval value);
381id OOJSNativeObjectFromJSObject(JSContext* context, JSObject* object);
382id OOJSNativeObjectOfClassFromJSValue(JSContext* context, jsval value, Class requiredClass);
383id OOJSNativeObjectOfClassFromJSObject(JSContext* context, JSObject* object, Class requiredClass);
384
385OOINLINE JSClass* OOJSGetClass(JSContext* cx, JSObject* obj) ALWAYS_INLINE_FUNC;
386OOINLINE JSClass* OOJSGetClass(JSContext* cx, JSObject* obj)
387{
388#if JS_THREADSAFE
389 return JS_GetClass(cx, obj);
390#else
391 return JS_GetClass(obj);
392#endif
393}
394
395/* OOJSValueIsFunction(context, value)
396
397 Test whether a jsval is a function object. The main tripping point here
398 is that JSVAL_IS_OBJECT() is true for JSVAL_NULL, but JS_ObjectIsFunction()
399 crashes if passed null.
400*/
401OOINLINE BOOL OOJSValueIsFunction(JSContext* context, jsval value)
402{
403 return JSVAL_IS_OBJECT(value) && !JSVAL_IS_NULL(value) && JS_ObjectIsFunction(context, JSVAL_TO_OBJECT(value));
404}
405
406/* OOJSValueIsArray(context, value)
407
408 Test whether a jsval is an array object. The main tripping point here
409 is that JSVAL_IS_OBJECT() is true for JSVAL_NULL, but JS_IsArrayObject()
410 crashes if passed null.
411
412 Also, it should be called JS_ObjectIsArray() for consistency.
413*/
414OOINLINE BOOL OOJSValueIsArray(JSContext* context, jsval value)
415{
416 return JSVAL_IS_OBJECT(value) && !JSVAL_IS_NULL(value) && JS_IsArrayObject(context, JSVAL_TO_OBJECT(value));
417}
418
419/* OOJSDictionaryFromJSValue(context, value)
420 OOJSDictionaryFromJSObject(context, object)
421
422 Converts a JavaScript value to a dictionary by calling
423 OOJSNativeObjectFromJSValue() on each of its values.
424
425 Only enumerable own (i.e., not inherited) properties with string keys are
426 included.
427
428 Requires a request on context.
429*/
430NSDictionary* OOJSDictionaryFromJSValue(JSContext* context, jsval value);
431NSDictionary* OOJSDictionaryFromJSObject(JSContext* context, JSObject* object);
432
433/* OOJSDictionaryFromStringTable(context, value)
434
435 Treat an arbitrary JavaScript object as a dictionary mapping strings to
436 strings, and convert to a corresponding NSDictionary. The values are
437 converted to strings using JS_ValueToString().
438
439 Only enumerable own (i.e., not inherited) properties with string keys are
440 included.
441
442 Requires a request on context.
443*/
444NSDictionary* OOJSDictionaryFromStringTable(JSContext* context, jsval value);
445
446/*
447 DEFINE_JS_OBJECT_GETTER()
448 Defines a helper to extract Objective-C objects from the private field of
449 JS objects, with runtime type checking. The generated accessor requires
450 a request on context. Weakrefs are automatically unpacked.
451
452 Types which extend other types, such as entity subtypes, must register
453 their relationships with OOJSRegisterSubclass() below.
454
455 The signature of the generator is:
456 BOOL <name>(JSContext *context, JSObject *inObject, <class>** outObject)
457 If it returns NO, inObject is of the wrong class and an error has been
458 raised. Otherwise, outObject is either a native object of the specified
459 class (or a subclass) or nil.
460*/
461#ifndef NDEBUG
462#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME) \
463 static BOOL NAME(JSContext* context, JSObject* inObject, OBJCCLASSNAME** outObject) GCC_ATTR((unused)); \
464 static BOOL NAME(JSContext* context, JSObject* inObject, OBJCCLASSNAME** outObject) \
465 { \
466 NSCParameterAssert(outObject != NULL); \
467 static Class cls = Nil; \
468 if (EXPECT_NOT(cls == Nil)) \
469 cls = [OBJCCLASSNAME class]; \
470 return OOJSObjectGetterImplPRIVATE(context, inObject, JSCLASS, cls, #NAME, (id*)outObject); \
471 }
472#else
473#define DEFINE_JS_OBJECT_GETTER(NAME, JSCLASS, JSPROTO, OBJCCLASSNAME) \
474 OOINLINE BOOL NAME(JSContext* context, JSObject* inObject, OBJCCLASSNAME** outObject) \
475 { \
476 return OOJSObjectGetterImplPRIVATE(context, inObject, JSCLASS, (id*)outObject); \
477 }
478#endif
479
480// For DEFINE_JS_OBJECT_GETTER()'s use.
481#ifndef NDEBUG
482BOOL OOJSObjectGetterImplPRIVATE(JSContext* context, JSObject* object, JSClass* requiredJSClass, Class requiredObjCClass, const char* name, id* outObject);
483#else
484BOOL OOJSObjectGetterImplPRIVATE(JSContext* context, JSObject* object, JSClass* requiredJSClass, id* outObject);
485#endif
486
487/*
488 Subclass relationships.
489
490 JSAPI doesn't have a concept of subclassing, as JavaScript doesn't have a
491 concept of classes, but Oolite reflects part of its class hierarchy as
492 related JSClasses whose prototypes inherit each other. For instance,
493 JS Entity methods work on JS Ships. In order for this to work,
494 OOJSEntityGetEntity() must be able to know that Ship is a subclass of
495 Entity. This is done using OOJSIsSubclass().
496
497 void OOJSRegisterSubclass(JSClass *subclass, JSClass *superclass)
498 Register subclass as a subclass of superclass. Subclass must not previously
499 have been registered as a subclass of any class (i.e., single inheritance
500 is required).
501
502 BOOL OOJSIsSubclass(JSClass *putativeSubclass, JSClass *superclass)
503 Test whether putativeSubclass is a equal to superclass or a registered
504 subclass of superclass, recursively.
505*/
506void OOJSRegisterSubclass(JSClass* subclass, JSClass* superclass);
507BOOL OOJSIsSubclass(JSClass* putativeSubclass, JSClass* superclass);
508OOINLINE BOOL OOJSIsMemberOfSubclass(JSContext* context, JSObject* object, JSClass* superclass)
509{
510 return OOJSIsSubclass(OOJSGetClass(context, object), superclass);
511}
512
513/* Support for OOJSNativeObjectFromJSValue() family
514
515 OOJSClassConverterCallback specifies the prototype for a callback function
516 which converts a JavaScript object to an Objective-C object.
517
518 OOJSBasicPrivateObjectConverter() is a OOJSClassConverterCallback which
519 returns the JS object's private storage value. It automatically unpacks
520 OOWeakReferences if relevant.
521
522 OOJSRegisterObjectConverter() registers a callback for a specific JS class.
523 It is not automatically propagated to subclasses.
524*/
525typedef id (*OOJSClassConverterCallback)(JSContext* context, JSObject* object);
526id OOJSBasicPrivateObjectConverter(JSContext* context, JSObject* object);
527
528void OOJSRegisterObjectConverter(JSClass* theClass, OOJSClassConverterCallback converter);
529
530/* JS root handling
531
532 The name parameter to JS_AddNamed*Root is assigned with no overhead, not
533 copied, but the strings serve no purpose in a release build so we may as
534 well strip them out.
535
536 In debug builds, this will deliberately cause an error if name is not a
537 string literal.
538*/
539#ifdef NDEBUG
540#define OOJSAddGCValueRoot(context, root, name) JS_AddValueRoot((context), (root))
541#define OOJSAddGCStringRoot(context, root, name) JS_AddStringRoot((context), (root))
542#define OOJSAddGCObjectRoot(context, root, name) JS_AddObjectRoot((context), (root))
543#define OOJSAddGCThingRoot(context, root, name) JS_AddGCThingRoot((context), (root))
544#else
545#define OOJSAddGCValueRoot(context, root, name) JS_AddNamedValueRoot((context), (root), "" name)
546#define OOJSAddGCStringRoot(context, root, name) JS_AddNamedStringRoot((context), (root), "" name)
547#define OOJSAddGCObjectRoot(context, root, name) JS_AddNamedObjectRoot((context), (root), "" name)
548#define OOJSAddGCThingRoot(context, root, name) JS_AddNamedGCThingRoot((context), (root), "" name)
549#endif
550
551#if OOJSENGINE_MONITOR_SUPPORT
552
553/* Protocol for debugging "monitor" object.
554 The monitor is an object -- in Oolite, or via Distributed Objects -- which
555 is provided with debugging information by the OOJavaScriptEngine.
556*/
557
558@protocol OOJavaScriptEngineMonitor <NSObject>
559
560// Sent for JS errors or warnings.
561- (oneway void)jsEngine:(in byref OOJavaScriptEngine*)engine
562 context:(in JSContext*)context
563 error:(in JSErrorReport*)errorReport
564 stackSkip:(in unsigned)stackSkip
565 showingLocation:(in BOOL)showLocation
566 withMessage:(in NSString*)message;
567
568// Sent for JS log messages. Note: messageClass will be nil if Log() is used rather than LogWithClass().
569- (oneway void)jsEngine:(in byref OOJavaScriptEngine*)engine
570 context:(in JSContext*)context
571 logMessage:(in NSString*)message
572 ofClass:(in NSString*)messageClass;
573
574@end
575
576@interface OOJavaScriptEngine (OOMonitorSupport)
577
578- (void)setMonitor:(id<OOJavaScriptEngineMonitor>)monitor;
579
580@end
581
582#endif
583
585
586/* See comments on time limiter in OOJSEngineTimeManagement.h.
587 */
588void OOJSPauseTimeLimiter(void);
589void OOJSResumeTimeLimiter(void);
590
591/* OOJSDumpStack()
592 Write JavaScript stack to log.
593
594 OOJSDescribeLocation()
595 Get script and line number for a stack frame.
596
597 OOJSMarkConsoleEvalLocation()
598 Specify that a given stack frame identifies eval()ed code from the debug
599 console, so that matching locations can be described specially by
600 OOJSDescribeLocation().
601*/
602#if OOLITE_DEBUG || defined(OO_DEBUG_MONITOR_ENABLED)
603void OOJSDumpStack(JSContext* context);
604
605NSString* OOJSDescribeLocation(JSContext* context, JSStackFrame* stackFrame);
606void OOJSMarkConsoleEvalLocation(JSContext* context, JSStackFrame* stackFrame);
607#else
608#define OOJSDumpStack(cx) \
609 do { \
610 } while (0)
611#define OOJSDescribeLocation(cx, frame) \
612 do { \
613 } while (0)
614#define OOJSMarkConsoleEvalLocation(cx, frame) \
615 do { \
616 } while (0)
617#endif
618
619/***** Reusable JS callbacks ****/
620
621/* OOJSUnconstructableConstruct
622
623 Constructor callback for pseudo-classes which can't be constructed.
624*/
625JSBool OOJSUnconstructableConstruct(JSContext* context, uintN argc, jsval* vp);
626
627/* OOJSObjectWrapperFinalize
628
629 Finalizer for JS classes whose private storage is a retained object
630 reference (generally an OOWeakReference, but doesn't have to be).
631*/
632void OOJSObjectWrapperFinalize(JSContext* context, JSObject* this);
633
634/* OOJSObjectWrapperToString
635
636 Implementation of toString() for JS classes whose private storage is an
637 Objective-C object reference (generally an OOWeakReference).
638
639 Calls -oo_jsDescription and, if that fails, -description.
640*/
641JSBool OOJSObjectWrapperToString(JSContext* context, uintN argc, jsval* vp);
642
643/***** Appropriate flags for host-defined read/write and read-only properties *****/
644
645// Slot-based (defined with JS_Define{Property/Object/Function}() and no callbacks)
646#define OOJS_PROP_READWRITE (JSPROP_PERMANENT | JSPROP_ENUMERATE)
647#define OOJS_PROP_READONLY (JSPROP_PERMANENT | JSPROP_ENUMERATE | JSPROP_READONLY)
648
649// Non-enumerable properties
650#define OOJS_PROP_HIDDEN_READWRITE (JSPROP_PERMANENT)
651#define OOJS_PROP_HIDDEN_READONLY (JSPROP_PERMANENT | JSPROP_READONLY)
652
653// Methods should be non-enumerable
654#define OOJS_METHOD_READONLY OOJS_PROP_HIDDEN_READONLY
655
656// Callback-based (includes all properties specified in JSPropertySpecs)
657#define OOJS_PROP_READWRITE_CB (OOJS_PROP_READWRITE | JSPROP_SHARED)
658#define OOJS_PROP_READONLY_CB (OOJS_PROP_READONLY | JSPROP_SHARED)
659
660#define OOJS_PROP_HIDDEN_READWRITE_CB (OOJS_PROP_HIDDEN_READWRITE | JSPROP_SHARED)
661#define OOJS_PROP_HIDDEN_READONLY_CB (OOJS_PROP_HIDDEN_READONLY | JSPROP_SHARED)
662
663/***** Helpers for native callbacks. *****/
664#define OOJS_THIS JS_THIS_OBJECT(context, vp)
665#define OOJS_ARGV JS_ARGV(context, vp)
666#define OOJS_RVAL JS_RVAL(context, vp)
667#define OOJS_SET_RVAL(v) JS_SET_RVAL(context, vp, v)
668
669#define OOJS_RETURN(v) \
670 do { \
671 OOJS_SET_RVAL(v); \
672 return YES; \
673 } while (0)
674#define OOJS_RETURN_JSOBJECT(o) OOJS_RETURN(OBJECT_TO_JSVAL(o))
675#define OOJS_RETURN_VOID OOJS_RETURN(JSVAL_VOID)
676#define OOJS_RETURN_NULL OOJS_RETURN(JSVAL_NULL)
677#define OOJS_RETURN_BOOL(v) OOJS_RETURN(OOJSValueFromBOOL(v))
678#define OOJS_RETURN_INT(v) OOJS_RETURN(INT_TO_JSVAL(v))
679#define OOJS_RETURN_OBJECT(o) OOJS_RETURN(OOJSValueFromNativeObject(context, o))
680
681#define OOJS_RETURN_WITH_HELPER(helper, value) \
682 do { \
683 jsval jsresult; \
684 BOOL OK = helper(context, value, &jsresult); \
685 JS_SET_RVAL(context, vp, jsresult); \
686 return OK; \
687 } while (0)
688
689#define OOJS_RETURN_VECTOR(value) OOJS_RETURN_WITH_HELPER(VectorToJSValue, value)
690#define OOJS_RETURN_HPVECTOR(value) OOJS_RETURN_WITH_HELPER(HPVectorToJSValue, value)
691#define OOJS_RETURN_QUATERNION(value) OOJS_RETURN_WITH_HELPER(QuaternionToJSValue, value)
692#define OOJS_RETURN_DOUBLE(value) OOJS_RETURN_WITH_HELPER(JS_NewNumberValue, value)
#define ALWAYS_INLINE_FUNC
#define INLINE_CONST_FUNC
#define OOINLINE
void OOJSPauseTimeLimiter(void)
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
BOOL JSEntityIsDemoShipPredicate(Entity *entity, void *parameter)
BOOL OOJSArgumentListGetNumberNoError(JSContext *context, uintN argc, jsval *argv, double *outNumber, uintN *outConsumed)
void OOJSReportWarning(JSContext *context, NSString *format,...)
#define OOJSMarkConsoleEvalLocation(cx, frame)
BOOL OOJSArgumentListGetNumber(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, double *outNumber, uintN *outConsumed)
JSBool OOJSObjectWrapperToString(JSContext *context, uintN argc, jsval *vp)
BOOL OOJSObjectGetterImplPRIVATE(JSContext *context, JSObject *object, JSClass *requiredJSClass, Class requiredObjCClass, const char *name, id *outObject)
BOOL JSEntityIsJavaScriptSearchablePredicate(Entity *entity, void *parameter)
OOINLINE jsval OOJSValueFromDouble(JSContext *context, double value)
NSString *const kOOJavaScriptEngineWillResetNotification
jsval OOJSVal
void OOJSReportWarningWithArguments(JSContext *context, NSString *format, va_list args)
NSString *const kOOJavaScriptEngineDidResetNotification
NSString * OOStringFromJSPropertyIDAndSpec(JSContext *context, jsid propID, JSPropertySpec *propertySpec)
#define JS_IsInRequest(context)
OOINLINE BOOL OOJSValueIsArray(JSContext *context, jsval value)
void OOJSRegisterObjectConverter(JSClass *theClass, OOJSClassConverterCallback converter)
void OOJSSetWarningOrErrorStackSkip(unsigned skip)
BOOL JSFunctionPredicate(Entity *entity, void *parameter)
id OOJSNativeObjectFromJSObject(JSContext *context, JSObject *object)
OOINLINE BOOL OOJSValueToBOOL(JSContext *context, jsval value)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
#define OOJSDumpStack(cx)
void OOJSReportBadPropertySelector(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec)
OOINLINE BOOL OOJSIsMemberOfSubclass(JSContext *context, JSObject *object, JSClass *superclass)
NSDictionary * OOJSDictionaryFromJSValue(JSContext *context, jsval value)
void OOJSReportWarningForCaller(JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)
void OOJSReportErrorForCaller(JSContext *context, NSString *scriptClass, NSString *function, NSString *format,...)
OOINLINE BOOL OOJSValueToInt32(JSContext *context, jsval value, int32_t *outValue)
NSDictionary * OOJSDictionaryFromJSObject(JSContext *context, JSObject *object)
id OOJSNativeObjectOfClassFromJSValue(JSContext *context, jsval value, Class requiredClass)
JSBool OOJSUnconstructableConstruct(JSContext *context, uintN argc, jsval *vp)
OOINLINE BOOL OOJSValueIsFunction(JSContext *context, jsval value)
void OOJSRegisterSubclass(JSClass *subclass, JSClass *superclass)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSReportPendingException(JSContext *context)
#define OOJSDescribeLocation(cx, frame)
void OOJSReportError(JSContext *context, NSString *format,...)
OOINLINE jsval OOJSValueFromBOOL(int b) INLINE_CONST_FUNC
#define JS_BeginRequest(context)
OOINLINE void OOJSRelinquishContext(JSContext *context)
id(* OOJSClassConverterCallback)(JSContext *context, JSObject *object)
void OOJSReportBadPropertyValue(JSContext *context, JSObject *thisObj, jsid propID, JSPropertySpec *propertySpec, jsval value)
id OOJSBasicPrivateObjectConverter(JSContext *context, JSObject *object)
void OOJSReportBadArguments(JSContext *context, NSString *scriptClass, NSString *function, uintN argc, jsval *argv, NSString *message, NSString *expectedArgsDescription)
#define JS_EndRequest(context)
NSDictionary * OOJSDictionaryFromStringTable(JSContext *context, jsval value)
void OOJSReportErrorWithArguments(JSContext *context, NSString *format, va_list args)
OOINLINE BOOL OOJSValueIsUndefined(jsval value)
void OOJSResumeTimeLimiter(void)
BOOL JSEntityIsJavaScriptVisiblePredicate(Entity *entity, void *parameter)
BOOL OOJSIsSubclass(JSClass *putativeSubclass, JSClass *superclass)
NSString * OOStringFromJSString(JSContext *context, JSString *string)
OOINLINE JSClass * OOJSGetClass(JSContext *cx, JSObject *obj) ALWAYS_INLINE_FUNC
void OOJSStrLiteralCachePRIVATE(const char *string, jsval *strCache, BOOL *inited)
id OOJSNativeObjectOfClassFromJSObject(JSContext *context, JSObject *object, Class requiredClass)
JSContext * gOOJSMainThreadContext
return nil
OOINLINE JSValue OOJSValueFromNativeObject(JSContext *context, id object)
JSValue OOJSObjectFromNativeObject(JSContext *context, id object)
NSString * OOStringFromJSID(JSAtom propID)
JSAtom OOJSIDFromString(NSString *string)
NSString * OOStringFromJSValueEvenIfNull(JSContext *context, JSValueConst value)
NSString * OOJSDescribeValue(JSContext *context, JSValueConst value, BOOL abbreviateObjects)
NSString * OOStringFromJSValue(JSContext *context, JSValueConst value)