Oolite
Loading...
Searching...
No Matches
OOJSScript Class Reference

#include <OOJSScript.h>

Inheritance diagram for OOJSScript:
Collaboration diagram for OOJSScript:

Instance Methods

(id) - initWithPath:properties:
(BOOL) - callMethod:inContext:withArguments:count:result:
(id) - propertyWithID:inContext:
(BOOL) - setProperty:withID:inContext:
(BOOL) - defineProperty:withID:inContext:
(id) - propertyNamed:
(BOOL) - setProperty:named:
(BOOL) - defineProperty:named:
(id) - initWithPath:properties:
(BOOL) - callMethod:inContext:withArguments:count:result:
(id) - propertyWithID:inContext:
(BOOL) - setProperty:withID:inContext:
(BOOL) - defineProperty:withID:inContext:
(id) - propertyNamed:
(BOOL) - setProperty:named:
(BOOL) - defineProperty:named:
(id) - initWithPath:
(NSString *) - name
(NSString *) - version
(NSString *) - displayName
(JSValueConst) - scriptObject
(BOOL) - callMethodNamed:withArguments:count:result:
(void) - dealloc [implementation]
(NSString *) - oo_jsClassName [implementation]
(NSString *) - descriptionComponents [implementation]
(void) - javaScriptEngineWillReset: [implementation]
(id) - weakRetain [implementation]
(void) - weakRefDied: [implementation]
(NSString *) - scriptDescription [implementation]
(void) - runWithTarget: [implementation]
(jsval) - oo_jsValueInContext: [implementation]
(NSString *) - scriptNameFromPath: [implementation]
(NSString *) - scriptNameFromPath: [implementation]
(NSDictionary *) - defaultPropertiesFromPath: [implementation]
(NSDictionary *) - defaultPropertiesFromPath: [implementation]
(void) - checkForTickle: [implementation]
(BOOL) - loadScriptWithContext: [implementation]
(NSString *) - stringPropertyNamed:inContext: [implementation]
Instance Methods inherited from OOScript
(BOOL) - requiresTickle
Instance Methods inherited from <OOWeakReferenceSupport>
(id) - OO_RETURNS_RETAINED

Class Methods

(id) + scriptWithPath:properties:
(OOJSScript *) + currentlyRunningScript
(NSArray *) + scriptStack
(void) + pushScript:
(void) + popScript:
(id) + scriptWithPath:properties:
(OOJSScript *) + currentlyRunningScript
(NSArray *) + scriptStack
(void) + pushScript:
(void) + popScript:
(id) + scriptWithPath:
(OOJSScript *) + currentlyRunningScript
Class Methods inherited from OOScript
(NSArray *) + worldScriptsAtPath:
(NSArray *) + scriptsFromFileNamed:
(NSArray *) + scriptsFromList:
(NSArray *) + scriptsFromFileAtPath:
(id) + jsScriptFromFileNamed:properties:
(id) + jsAIScriptFromFileNamed:properties:

Private Attributes

JSObject * _jsSelf
NSString * name
NSString * description
NSString * version
NSString * filePath
OOWeakReference * weakSelf
JSValue _jsSelf
NSString * _name
NSString * _version
NSString * _path

Detailed Description

Definition at line 30 of file OOJSScript.h.

Method Documentation

◆ callMethod:inContext:withArguments:count:result: [1/2]

- (BOOL) callMethod: (JSAtom) methodID
inContext: (JSContext*) context
withArguments: (JSValueConst*) argv
count: (int) argc
result: (JSValue*) outResult 

Reimplemented from OOScript.

Definition at line 328 of file OOJSScript.m.

328 :(JSAtom)methodID
329 inContext:(JSContext*)context
330 withArguments:(JSValueConst*)argv
331 count:(int)argc
332 result:(JSValue*)outResult
333{
334 NSParameterAssert(name != NULL && (argv != NULL || argc == 0) && context != NULL);
335 if (outResult != NULL)
336 *outResult = JS_UNDEFINED;
337 if (!JS_IsObject(_jsSelf))
338 return NO;
339
340 BOOL OK = NO;
341
342 JSValue method = JS_GetProperty(context, _jsSelf, methodID);
343 if (EXPECT_NOT(JS_IsException(method))) {
345 return NO;
346 }
347
348 if (EXPECT(!JS_IsUndefined(method))) {
349#ifndef NDEBUG
350 if (JS_HasException(context)) {
351 OOLog(@"script.internalBug", @"Exception pending on context before calling method in %s, clearing. This is an internal error, please report it.", __PRETTY_FUNCTION__);
352 JS_FreeValue(context, JS_GetException(context));
353 }
354
355 OOLog(@"script.javaScript.call", @"Calling [%@].%@()", [self name], OOStringFromJSID(methodID));
356 OOLogIndentIf(@"script.javaScript.call");
357#endif
358
359 // Push self on stack of running scripts.
360 RunningStack stackElement = {
361 .back = sRunningStack,
362 .current = self
363 };
364 sRunningStack = &stackElement;
365
366 // Call the method.
368 JSValue result = JS_Call(context, method, _jsSelf, argc, argv);
370
371 if (JS_IsException(result)) {
373 OK = NO;
374 } else {
375 OK = YES;
376 if (outResult != NULL)
377 *outResult = result;
378 else
379 JS_FreeValue(context, result);
380 }
381
382 // Pop running scripts stack
383 sRunningStack = stackElement.back;
384
385#ifndef NDEBUG
386 OOLogOutdentIf(@"script.javaScript.call");
387#endif
388 }
389
390 JS_FreeValue(context, method);
391 return OK;
392}
#define EXPECT_NOT(x)
#define EXPECT(x)
#define OOJSStopTimeLimiter()
#define OOJSStartTimeLimiter()
static RunningStack * sRunningStack
Definition OOJSScript.m:56
struct RunningStack RunningStack
Definition OOJSScript.m:49
NSString * OOStringFromJSID(jsid propID)
OOINLINE void OOJSReportPendingException(JSContext *context)
#define OOLogOutdentIf(class)
Definition OOLogging.h:112
#define OOLog(class, format,...)
Definition OOLogging.h:84
#define OOLogIndentIf(class)
Definition OOLogging.h:107
unsigned count
JSObject * _jsSelf
Definition OOJSScript.h:32
NSString * name
Definition OOJSScript.m:108
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
OOJSScript * current
Definition OOJSScript.m:52
RunningStack * back
Definition OOJSScript.m:51

◆ callMethod:inContext:withArguments:count:result: [2/2]

- (BOOL) callMethod: (jsid) methodID
inContext: (JSContext*) context
withArguments: (jsval*) argv
count: (intN) argc
result: (jsval*) outResult 

Reimplemented from OOScript.

Definition at line 328 of file OOJSScript.m.

328 :(JSAtom)methodID
329 inContext:(JSContext*)context
330 withArguments:(JSValueConst*)argv
331 count:(int)argc
332 result:(JSValue*)outResult
333{
334 NSParameterAssert(name != NULL && (argv != NULL || argc == 0) && context != NULL);
335 if (outResult != NULL)
336 *outResult = JS_UNDEFINED;
337 if (!JS_IsObject(_jsSelf))
338 return NO;
339
340 BOOL OK = NO;
341
342 JSValue method = JS_GetProperty(context, _jsSelf, methodID);
343 if (EXPECT_NOT(JS_IsException(method))) {
345 return NO;
346 }
347
348 if (EXPECT(!JS_IsUndefined(method))) {
349#ifndef NDEBUG
350 if (JS_HasException(context)) {
351 OOLog(@"script.internalBug", @"Exception pending on context before calling method in %s, clearing. This is an internal error, please report it.", __PRETTY_FUNCTION__);
352 JS_FreeValue(context, JS_GetException(context));
353 }
354
355 OOLog(@"script.javaScript.call", @"Calling [%@].%@()", [self name], OOStringFromJSID(methodID));
356 OOLogIndentIf(@"script.javaScript.call");
357#endif
358
359 // Push self on stack of running scripts.
360 RunningStack stackElement = {
361 .back = sRunningStack,
362 .current = self
363 };
364 sRunningStack = &stackElement;
365
366 // Call the method.
368 JSValue result = JS_Call(context, method, _jsSelf, argc, argv);
370
371 if (JS_IsException(result)) {
373 OK = NO;
374 } else {
375 OK = YES;
376 if (outResult != NULL)
377 *outResult = result;
378 else
379 JS_FreeValue(context, result);
380 }
381
382 // Pop running scripts stack
383 sRunningStack = stackElement.back;
384
385#ifndef NDEBUG
386 OOLogOutdentIf(@"script.javaScript.call");
387#endif
388 }
389
390 JS_FreeValue(context, method);
391 return OK;
392}

References _jsSelf, RunningStack::back, EXPECT, EXPECT_NOT, name, OOJSReportPendingException(), OOJSStartTimeLimiter, OOJSStopTimeLimiter, OOLog, OOLogIndentIf, OOLogOutdentIf, OOStringFromJSID(), and sRunningStack.

Referenced by StationEntity::acceptDockingClearanceRequestFrom:, PlayerEntity::activatePrimableEquipment:withMode:, PlayerEntity::adjustPriceByScriptForEqKey:withCurrent:, DockEntity::allowsLaunchingOf:, ShipEntity::behaviour_scripted_ai:, DockEntity::canAcceptShipForDocking:, Universe::canInstantiateShip:, StationEntity::dockingInstructionsForShip:, OOCharacter::doScriptEvent:, OOVisualEffectEntity::doScriptEvent:, ShipEntity::doScriptEvent:inContext:withArguments:count:, ShipEntity::equipmentValidToAdd:whileLoading:inContext:, OOShipRegistry(OODataLoader)::loadDemoShips, and Universe::shipsForSaleForSystem:withTL:atTime:.

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

◆ callMethodNamed:withArguments:count:result:

- (BOOL) callMethodNamed: (NSString*) methodName
withArguments: (JSValueConst*) argv
count: (int) argc
result: (JSValue*) outResult 

Definition at line 130 of file OOJSScript.m.

130 :(NSString*)methodName
131 withArguments:(JSValueConst*)argv
132 count:(int)argc
133 result:(JSValue*)outResult
134{
135 JSContext* context = OOJSAcquireContext();
136 BOOL OK = NO;
137
138 JSValue method = JS_GetPropertyStr(context, _jsSelf, [methodName UTF8String]);
139 if (JS_IsFunction(context, method)) {
140 RunningStack stackElement = {
141 .back = sRunningStack,
142 .current = self
143 };
144 sRunningStack = &stackElement;
145
147 forObject:_jsSelf
148 argc:argc
149 argv:argv
150 result:outResult];
151
152 sRunningStack = stackElement.back;
153 } else if (JS_IsException(method)) {
155 }
156 JS_FreeValue(context, method);
157
158 OOJSRelinquishContext(context);
159 return OK;
160}
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSRelinquishContext(JSContext *context)
BOOL callJSFunction:forObject:argc:argv:result:(jsval function,[forObject] JSObject *jsThis,[argc] uintN argc,[argv] jsval *argv,[result] jsval *outResult)
OOJavaScriptEngine * sharedEngine()

References _jsSelf, RunningStack::back, OOJavaScriptEngine::callJSFunction:forObject:argc:argv:result:, OOJSAcquireContext(), OOJSRelinquishContext(), OOJSReportPendingException(), OOJavaScriptEngine::sharedEngine, and sRunningStack.

Here is the call graph for this function:

◆ checkForTickle:

- (void) checkForTickle: (JSContext*) context
implementation

Provided by category OOJSScript(OOPrivate).

Definition at line 585 of file OOJSScript.m.

585 :(JSContext*)context
586{
587 JSValue tickle = JS_GetPropertyStr(context, _jsSelf, "tickle");
588 if (JS_IsException(tickle)) {
589 JS_FreeValue(context, JS_GetException(context));
590 } else if (!JS_IsUndefined(tickle)) {
591 OOJSReportWarning(context, @"Script %@ appears to use the tickle() event handler, which is no longer supported.", [self name]);
592 }
593 JS_FreeValue(context, tickle);
594}
void OOJSReportWarning(JSContext *context, NSString *format,...)

Referenced by initWithPath:properties:.

Here is the caller graph for this function:

◆ currentlyRunningScript [1/3]

+ (OOJSScript *) currentlyRunningScript

Definition at line 101 of file OOJSScript.m.

102{
103 if (sRunningStack == NULL)
104 return nil;
105 return sRunningStack->current;
106}
return nil

References currentlyRunningScript, nil, and sRunningStack.

Referenced by currentlyRunningScript, OOJSGuiScreenKeyDefinition::init, OOJSInterfaceDefinition::init, OOJSPopulatorDefinition::init, OOJSTimer::initWithDelay:interval:context:function:this:, OOJSTimer(Private)::initWithDelay:interval:context:function:this:, LogProblem(), LogProblem(), MissionRunScreen(), MissionRunScreen(), MissionSetInstructionsInternal(), MissionSetInstructionsInternal(), ReportJSError(), OOSystemInfo::setValue:forKey:, SystemInfoSetPropertyMethod(), SystemInfoSetPropertyMethod(), SystemInfoStaticSetInterstellarProperty(), SystemInfoStaticSetInterstellarProperty(), SystemSetProperty(), and SystemSetProperty().

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

◆ currentlyRunningScript [2/3]

+ (OOJSScript *) currentlyRunningScript

References currentlyRunningScript.

Here is the call graph for this function:

◆ currentlyRunningScript [3/3]

+ (OOJSScript *) currentlyRunningScript

References currentlyRunningScript.

Here is the call graph for this function:

◆ dealloc

- (void) dealloc
implementation

Definition at line 256 of file OOJSScript.m.

257{
258 [[NSNotificationCenter defaultCenter] removeObserver:self
259 name:kOOJavaScriptEngineWillResetNotification
261
262 DESTROY(name);
266
267 if (_jsSelf != NULL) {
268 JSContext* context = OOJSAcquireContext();
269
270 OOJSObjectWrapperFinalize(context, _jsSelf); // Release weakref to self
271 JS_RemoveObjectRoot(context, &_jsSelf); // Unroot jsSelf
272
273 OOJSRelinquishContext(context);
274 }
275
276 [weakSelf weakRefDrop];
277
278 [super dealloc];
279}
#define DESTROY(x)
void OOJSObjectWrapperFinalize(JSContext *context, JSObject *this)
NSString * version
Definition OOJSScript.m:113
NSString * filePath
Definition OOJSScript.h:37
NSString * description
Definition OOJSScript.h:35

References _jsSelf, dealloc, description, DESTROY, filePath, name, OOJSAcquireContext(), OOJSObjectWrapperFinalize(), OOJSRelinquishContext(), OOJavaScriptEngine::sharedEngine, version, and OOWeakReference::weakRefDrop.

Referenced by dealloc.

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

◆ defaultPropertiesFromPath: [1/2]

- (NSDictionary *) defaultPropertiesFromPath: (NSString*) path
implementation

Provided by category OOJSScript(OOPrivate).

Definition at line 554 of file OOJSScript.m.

554 :(NSString*)path
555{
556 // remove file name, remove OXP subfolder, add manifest.plist
557 NSString* manifestPath = [[[path stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"manifest.plist"];
558 NSDictionary* manifest = OODictionaryFromFile(manifestPath);
559 NSMutableDictionary* properties = [NSMutableDictionary dictionaryWithCapacity:3];
560 /* __oolite.tmp.* is allocated for OXPs without manifests. Its
561 * values are meaningless and shouldn't be used here */
562 if (manifest != nil && ![[manifest oo_stringForKey:kOOManifestIdentifier] hasPrefix:@"__oolite.tmp."]) {
563 if ([manifest objectForKey:kOOManifestVersion] != nil) {
564 [properties setObject:[manifest oo_stringForKey:kOOManifestVersion] forKey:@"version"];
565 }
566 if ([manifest objectForKey:kOOManifestIdentifier] != nil) {
567 // used for system info
568 [properties setObject:[manifest oo_stringForKey:kOOManifestIdentifier] forKey:kLocalManifestProperty];
569 }
570 if ([manifest objectForKey:kOOManifestAuthor] != nil) {
571 [properties setObject:[manifest oo_stringForKey:kOOManifestAuthor] forKey:@"author"];
572 }
573 if ([manifest objectForKey:kOOManifestLicense] != nil) {
574 [properties setObject:[manifest oo_stringForKey:kOOManifestLicense] forKey:@"license"];
575 }
576 }
577 return properties;
578}
static NSString *const kOOManifestLicense
static NSString *const kOOManifestIdentifier
static NSString *const kOOManifestVersion
static NSString *const kOOManifestAuthor
NSDictionary * OODictionaryFromFile(NSString *path)

Referenced by initWithPath:properties:.

Here is the caller graph for this function:

◆ defaultPropertiesFromPath: [2/2]

- (NSDictionary *) defaultPropertiesFromPath: (NSString *) path
implementation

Provided by category OOJSScript(OOPrivate).

◆ defineProperty:named: [1/2]

- (BOOL) defineProperty: (id) value
named: (NSString*) name 

Definition at line 459 of file OOJSScript.m.

459 :(id)value named:(NSString*)propName
460{
461 if (value == nil || propName == nil)
462 return NO;
463 if (!JS_IsObject(_jsSelf))
464 return NO;
465
466 JSContext* context = OOJSAcquireContext();
467 BOOL result = [self defineProperty:value withID:OOJSIDFromString(propName) inContext:context];
468 OOJSRelinquishContext(context);
469
470 return result;
471}

References _jsSelf, defineProperty:withID:inContext:, nil, OOJSAcquireContext(), and OOJSRelinquishContext().

Referenced by initWithPath:properties:.

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

◆ defineProperty:named: [2/2]

- (BOOL) defineProperty: (id) value
named: (NSString *) name 

◆ defineProperty:withID:inContext: [1/2]

- (BOOL) defineProperty: (id) value
withID: (JSAtom) propID
inContext: (JSContext*) context 

Definition at line 421 of file OOJSScript.m.

421 :(id)value withID:(JSAtom)propID inContext:(JSContext*)context
422{
423 NSParameterAssert(context != NULL);
424 if (!JS_IsObject(_jsSelf))
425 return NO;
426
427 // JS_DefinePropertyValue() takes ownership of the value.
428 return JS_DefinePropertyValue(context, _jsSelf, propID, OOJSValueFromNativeObject(context, value), OOJS_PROP_READONLY) >= 0;
429}
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
#define OOJS_PROP_READONLY

◆ defineProperty:withID:inContext: [2/2]

- (BOOL) defineProperty: (id) value
withID: (jsid) propID
inContext: (JSContext*) context 

Definition at line 421 of file OOJSScript.m.

421 :(id)value withID:(JSAtom)propID inContext:(JSContext*)context
422{
423 NSParameterAssert(context != NULL);
424 if (!JS_IsObject(_jsSelf))
425 return NO;
426
427 // JS_DefinePropertyValue() takes ownership of the value.
428 return JS_DefinePropertyValue(context, _jsSelf, propID, OOJSValueFromNativeObject(context, value), OOJS_PROP_READONLY) >= 0;
429}

References _jsSelf, OOJS_PROP_READONLY, and OOJSValueFromNativeObject().

Referenced by defineProperty:named:.

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

◆ descriptionComponents

- (NSString *) descriptionComponents
implementation

Reimplemented from OOScript.

Definition at line 286 of file OOJSScript.m.

287{
288 if (_jsSelf != NULL)
289 return [super descriptionComponents];
290 else
291 return @"invalid script";
292}

References _jsSelf, and descriptionComponents.

Referenced by descriptionComponents.

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

◆ displayName

- (NSString *) displayName

Reimplemented from OOScript.

Definition at line 118 of file OOJSScript.m.

119{
120 if (_version != nil)
121 return [NSString stringWithFormat:@"%@ %@", _name, _version];
122 return _name;
123}
NSString * _version
Definition OOJSScript.h:35
NSString * _name
Definition OOJSScript.h:34

References _name, _version, displayName, and nil.

Referenced by displayName, initWithPath:properties:, LogProblem(), and version.

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

◆ initWithPath:

- (id) initWithPath: (NSString*) path

Definition at line 55 of file OOJSScript.m.

55 :(NSString*)path
56{
57 if (!(self = [super init]))
58 return nil;
59
60 JSContext* context = OOJSAcquireContext();
61 _path = [path copy];
62
63 /* The JS object refers back to us without retaining, as the
64 SpiderMonkey version did through a weak reference; -dealloc clears it.
65 */
66 _jsSelf = JS_NewObjectClass(context, sScriptClassID);
67 if (JS_IsException(_jsSelf)) {
69 _jsSelf = JS_UNDEFINED;
70 [self release];
71 return nil;
72 }
73 JS_SetOpaque(_jsSelf, self);
74
75 if (![self loadScriptWithContext:context]) {
76 OOLog(@"script.javaScript.load.failed", @"***** Error loading JavaScript script %@ -- could not run script", path);
77 [self release];
78 self = nil;
79 }
80
81 OOJSRelinquishContext(context);
82 return self;
83}
static JSClassID sScriptClassID
Definition OOJSScript.m:53
NSString * _path
Definition OOJSScript.h:36

References _jsSelf, _path, nil, OOJSAcquireContext(), OOJSRelinquishContext(), OOJSReportPendingException(), OOLog, and sScriptClassID.

Here is the call graph for this function:

◆ initWithPath:properties: [1/2]

- (id) initWithPath: (NSString*) path
properties: (NSDictionary*) properties 

Definition at line 94 of file OOJSScript.m.

94 :(NSString*)path properties:(NSDictionary*)properties
95{
96 JSContext* context = NULL;
97 NSString* problem = nil; // Acts as error flag.
98 NSString* key = nil;
99 id property = nil;
100
101 self = [super init];
102 if (self == nil)
103 problem = @"allocation failure";
104 else {
105 context = OOJSAcquireContext();
106
107 if (JS_HasException(context)) {
108 JS_FreeValue(context, JS_GetException(context));
109 OOLogERR(@"script.javaScript.load.waitingException", @"Prior to loading script %@, there was a pending JavaScript exception, which has been cleared. This is an internal error, please report it.", path);
110 }
111
112 // Set up JS object
113 if (!problem) {
114 _jsSelf = JS_NewObjectClass(context, (int)sScriptClassID);
115 if (JS_IsException(_jsSelf)) {
116 JS_FreeValue(context, JS_GetException(context));
117 _jsSelf = JS_UNDEFINED;
118 problem = @"allocation failure";
119 }
120 }
121
122 if (!problem) {
123 JS_SetOpaque(_jsSelf, OOConsumeReference([self weakRetain]));
124 }
125
126 // Push self on stack of running scripts.
127 RunningStack stackElement = {
128 .back = sRunningStack,
129 .current = self
130 };
131 sRunningStack = &stackElement;
132
133 filePath = [path retain];
134
135 // Set default properties from manifest.plist
136 NSDictionary* defaultProperties = [self defaultPropertiesFromPath:path];
137 foreachkey(key, defaultProperties)
138 {
139 if ([key isKindOfClass:[NSString class]]) {
140 property = [defaultProperties objectForKey:key];
141 if ([key isEqualToString:kLocalManifestProperty]) {
142 // this must not be editable
143 [self defineProperty:property named:key];
144 } else {
145 // can be overwritten by script itself
146 [self setProperty:property named:key];
147 }
148 }
149 }
150
151 // Set properties. (read-only)
152 if (!problem && properties != nil) {
153 foreachkey(key, properties)
154 {
155 if ([key isKindOfClass:[NSString class]]) {
156 property = [properties objectForKey:key];
157 [self defineProperty:property named:key];
158 }
159 }
160 }
161
162 /* Set initial name (in case of script error during initial run).
163 The "name" ivar is not set here, so the property can be fetched from JS
164 if we fail during setup. However, the "name" ivar is set later so that
165 the script object can't be renamed after the initial run. This could
166 probably also be achieved by fiddling with JS property attributes.
167 */
168 JSAtom nameID = OOJSID("name");
169 [self setProperty:[self scriptNameFromPath:path] withID:nameID inContext:context];
170
171 // Run the script (allowing it to set up the properties we need, as well as setting up those event handlers)
172 if (!problem) {
173 OOLog(@"script.javaScript.willLoad", @"About to load JavaScript %@", path);
174 OOLogIndentIf(@"script.javaScript.willLoad");
175
176 if (!RunScriptWithName(context, path, _jsSelf, &problem)) {
177 if (problem == nil)
178 problem = @"could not run script";
179 }
180 }
181
182 sRunningStack = stackElement.back;
183
184 if (!problem) {
185 [self checkForTickle:context];
186
187 // Get display attributes from script
188 DESTROY(name);
189 name = [StrippedName([[self propertyWithID:nameID inContext:context] description]) copy];
190 if (name == nil) {
191 name = [[self scriptNameFromPath:path] retain];
192 [self setProperty:name withID:nameID inContext:context];
193 }
194
195 version = [[[self propertyWithID:OOJSID("version") inContext:context] description] copy];
196 description = [[[self propertyWithID:OOJSID("description") inContext:context] description] copy];
197
198 OOLog(@"script.javaScript.load.success", @"Loaded JavaScript: %@ -- %@", [self displayName], description ? description : (NSString*)@"(no description)");
199 }
200
201 OOLogOutdentIf(@"script.javaScript.willLoad");
202
203 DESTROY(filePath); // Only used for error reporting during startup.
204 }
205
206 if (problem) {
207 OOLog(@"script.javaScript.load.failed", @"***** Error loading JavaScript script %@ -- %@", path, problem);
208 if (context != NULL && JS_HasException(context))
210 DESTROY(self);
211 }
212
213 OOJSRelinquishContext(context);
214
215 if (self != nil) {
216 [[NSNotificationCenter defaultCenter] addObserver:self
217 selector:@selector(javaScriptEngineWillReset:)
218 name:kOOJavaScriptEngineWillResetNotification
220 }
221
222 return self;
223}
#define foreachkey(VAR, DICT)
id OOConsumeReference(id OO_NS_CONSUMED value)
#define OOJSID(str)
Definition OOJSPropID.h:42
static NSString *const kLocalManifestProperty
Definition OOJSScript.h:28
#define OOLogERR(class, format,...)
Definition OOLogging.h:125
static BOOL RunScriptWithName(JSContext *context, NSString *path, JSValueConst object, NSString **outErrorMessage)
Definition OOJSScript.m:652
NSString * displayName()
Definition OOJSScript.m:118

References _jsSelf, RunningStack::back, checkForTickle:, defaultPropertiesFromPath:, defineProperty:named:, description, DESTROY, displayName, filePath, foreachkey, kLocalManifestProperty, name, nil, OOConsumeReference(), OOJSAcquireContext(), OOJSID, OOJSRelinquishContext(), OOJSReportPendingException(), OOLog, OOLogERR, OOLogIndentIf, OOLogOutdentIf, propertyWithID:inContext:, RunScriptWithName(), scriptNameFromPath:, setProperty:named:, setProperty:withID:inContext:, OOJavaScriptEngine::sharedEngine, sRunningStack, sScriptClassID, version, and weakRetain.

Here is the call graph for this function:

◆ initWithPath:properties: [2/2]

- (id) initWithPath: (NSString *) path
properties: (NSDictionary *) properties 

◆ javaScriptEngineWillReset:

- (void) javaScriptEngineWillReset: (NSNotification*) notification
implementation

Definition at line 294 of file OOJSScript.m.

294 :(NSNotification*)notification
295{
296 // All scripts become invalid when the JS engine resets.
297 if (_jsSelf != NULL) {
298 _jsSelf = NULL;
299 JSContext* context = OOJSAcquireContext();
300 JS_RemoveObjectRoot(context, &_jsSelf);
301 OOJSRelinquishContext(context);
302 }
303}

References _jsSelf, OOJSAcquireContext(), and OOJSRelinquishContext().

Here is the call graph for this function:

◆ loadScriptWithContext:

- (BOOL) loadScriptWithContext: (JSContext*) context
implementation

Provided by category OOJSScript(Private).

Definition at line 166 of file OOJSScript.m.

166 :(JSContext*)context
167{
168 NSString* source = [NSString stringWithContentsOfFile:_path encoding:NSUTF8StringEncoding error:NULL];
169 if (source == nil) {
170 OOLog(@"script.javaScript.load.failed", @"***** Error loading JavaScript script %@ -- could not load file", _path);
171 return NO;
172 }
173
174 // Initial name, in case of script error during initial run.
175 _name = [[[_path lastPathComponent] stringByDeletingPathExtension] retain];
176 JS_SetPropertyStr(context, _jsSelf, "name", OOJSValueFromNativeObject(context, _name));
177
178 RunningStack stackElement = {
179 .back = sRunningStack,
180 .current = self
181 };
182 sRunningStack = &stackElement;
183
184 // Run the script (allowing it to set up the properties we need, as well as setting up those event handlers)
185 NSData* wrapped = [OOJSWrapScriptSource(source) dataUsingEncoding:NSUTF8StringEncoding];
186 NSMutableData* terminated = [NSMutableData dataWithData:wrapped];
187 [terminated appendBytes:"" length:1]; // JS_Eval requires a terminating NUL.
188
190 JSValue result = JS_EvalThis(context, _jsSelf, [terminated bytes], [wrapped length], [_path UTF8String], JS_EVAL_TYPE_GLOBAL);
192
193 BOOL OK = !JS_IsException(result);
194 if (OK)
195 JS_FreeValue(context, result);
196 else
198
199 sRunningStack = stackElement.back;
200
201 if (OK) {
202 // The script can't be renamed after the initial run.
203 NSString* name = [self stringPropertyNamed:"name" inContext:context];
204 if (name != nil) {
205 [_name release];
206 _name = [name retain];
207 }
208 _version = [[self stringPropertyNamed:"version" inContext:context] retain];
209 }
210
211 return OK;
212}
#define kOOJSLongTimeLimit
#define OOJSStartTimeLimiterWithTimeLimit(limit)

◆ name

- (NSString *) name

Reimplemented from OOScript.

References name.

◆ oo_jsClassName

- (NSString *) oo_jsClassName
implementation

Definition at line 281 of file OOJSScript.m.

282{
283 return @"Script";
284}

References oo_jsClassName.

Referenced by oo_jsClassName.

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

◆ oo_jsValueInContext:

- (JSValue) oo_jsValueInContext: (JSContext*) context
implementation

Definition at line 492 of file OOJSScript.m.

492 :(JSContext*)context
493{
494 if (_jsSelf == NULL)
495 return JSVAL_VOID;
496 return OBJECT_TO_JSVAL(_jsSelf);
497}

References _jsSelf.

◆ popScript: [1/2]

+ (void) popScript: (OOJSScript*) script

Definition at line 493 of file OOJSScript.m.

493 :(OOJSScript*)script
494{
495 RunningStack* element = NULL;
496
497 assert(sRunningStack->current == script);
498
499 element = sRunningStack;
501 free(element);
502}

References sRunningStack.

Referenced by OOJSFunction::evaluateWithContext:scope:argc:argv:result:, MissionRunCallback(), MissionRunCallback(), OOJSGuiScreenKeyDefinition::runCallback:, OOJSInterfaceDefinition::runCallback:, OOJSPopulatorDefinition::runCallback:, and OOJSTimer::timerFired.

Here is the caller graph for this function:

◆ popScript: [2/2]

+ (void) popScript: (OOJSScript *) script

◆ propertyNamed: [1/2]

- (id) propertyNamed: (NSString*) name

Definition at line 431 of file OOJSScript.m.

431 :(NSString*)propName
432{
433 if (propName == nil)
434 return nil;
435 if (!JS_IsObject(_jsSelf))
436 return nil;
437
438 JSContext* context = OOJSAcquireContext();
439 id result = [self propertyWithID:OOJSIDFromString(propName) inContext:context];
440 OOJSRelinquishContext(context);
441
442 return result;
443}

References _jsSelf, nil, OOJSAcquireContext(), OOJSRelinquishContext(), and propertyWithID:inContext:.

Referenced by OOSystemInfo::setValue:forKey:, SystemInfoSetPropertyMethod(), SystemInfoSetPropertyMethod(), SystemInfoStaticSetInterstellarProperty(), SystemInfoStaticSetInterstellarProperty(), SystemSetProperty(), and SystemSetProperty().

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

◆ propertyNamed: [2/2]

- (id) propertyNamed: (NSString *) name

◆ propertyWithID:inContext: [1/2]

- (id) propertyWithID: (JSAtom) propID
inContext: (JSContext*) context 

Definition at line 394 of file OOJSScript.m.

394 :(JSAtom)propID inContext:(JSContext*)context
395{
396 NSParameterAssert(context != NULL);
397 if (!JS_IsObject(_jsSelf))
398 return nil;
399
400 JSValue jsValue = JS_GetProperty(context, _jsSelf, propID);
401 if (JS_IsException(jsValue)) {
402 JS_FreeValue(context, JS_GetException(context));
403 return nil;
404 }
405
406 id result = OOJSNativeObjectFromJSValue(context, jsValue);
407 JS_FreeValue(context, jsValue);
408 return result;
409}
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)

◆ propertyWithID:inContext: [2/2]

- (id) propertyWithID: (jsid) propID
inContext: (JSContext*) context 

Definition at line 394 of file OOJSScript.m.

394 :(JSAtom)propID inContext:(JSContext*)context
395{
396 NSParameterAssert(context != NULL);
397 if (!JS_IsObject(_jsSelf))
398 return nil;
399
400 JSValue jsValue = JS_GetProperty(context, _jsSelf, propID);
401 if (JS_IsException(jsValue)) {
402 JS_FreeValue(context, JS_GetException(context));
403 return nil;
404 }
405
406 id result = OOJSNativeObjectFromJSValue(context, jsValue);
407 JS_FreeValue(context, jsValue);
408 return result;
409}

References _jsSelf, nil, and OOJSNativeObjectFromJSValue().

Referenced by initWithPath:properties:, and propertyNamed:.

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

◆ pushScript: [1/2]

+ (void) pushScript: (OOJSScript*) script

Definition at line 480 of file OOJSScript.m.

480 :(OOJSScript*)script
481{
482 RunningStack* element = NULL;
483
484 element = malloc(sizeof *element);
485 if (element == NULL)
486 exit(EXIT_FAILURE);
487
488 element->back = sRunningStack;
489 element->current = script;
490 sRunningStack = element;
491}

References RunningStack::back, RunningStack::current, and sRunningStack.

Referenced by OOJSFunction::evaluateWithContext:scope:argc:argv:result:, MissionRunCallback(), MissionRunCallback(), OOJSGuiScreenKeyDefinition::runCallback:, OOJSInterfaceDefinition::runCallback:, OOJSPopulatorDefinition::runCallback:, and OOJSTimer::timerFired.

Here is the caller graph for this function:

◆ pushScript: [2/2]

+ (void) pushScript: (OOJSScript *) script

◆ runWithTarget:

- (void) runWithTarget: (Entity*) target
implementation

Reimplemented from OOScript.

Definition at line 353 of file OOJSScript.m.

353 :(Entity*)target
354{
355}

◆ scriptDescription

- (NSString *) scriptDescription
implementation

Reimplemented from OOScript.

Definition at line 343 of file OOJSScript.m.

344{
345 return description;
346}

References description, and scriptDescription.

Referenced by scriptDescription.

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

◆ scriptNameFromPath: [1/2]

- (NSString *) scriptNameFromPath: (NSString*) path
implementation

Provided by category OOJSScript(OOPrivate).

Definition at line 522 of file OOJSScript.m.

522 :(NSString*)path
523{
524 NSString* lastComponent = nil;
525 NSString* truncatedPath = nil;
526 NSString* theName = nil;
527
528 if (path == nil)
529 theName = [NSString stringWithFormat:@"%p", self];
530 else {
531 lastComponent = [path lastPathComponent];
532 if (![lastComponent hasPrefix:@"script."])
533 theName = lastComponent;
534 else {
535 truncatedPath = [path stringByDeletingLastPathComponent];
536 if (NSOrderedSame == [[truncatedPath lastPathComponent] caseInsensitiveCompare:@"Config"]) {
537 truncatedPath = [truncatedPath stringByDeletingLastPathComponent];
538 }
539 if (NSOrderedSame == [[truncatedPath pathExtension] caseInsensitiveCompare:@"oxp"]) {
540 truncatedPath = [truncatedPath stringByDeletingPathExtension];
541 }
542
543 lastComponent = [truncatedPath lastPathComponent];
544 theName = lastComponent;
545 }
546 }
547
548 if (0 == [theName length])
549 theName = path;
550
551 return StrippedName([theName stringByAppendingString:@".anon-script"]);
552}
static NSString * StrippedName(NSString *string)
Definition OOJSScript.m:754

Referenced by initWithPath:properties:.

Here is the caller graph for this function:

◆ scriptNameFromPath: [2/2]

- (NSString *) scriptNameFromPath: (NSString *) path
implementation

Provided by category OOJSScript(OOPrivate).

◆ scriptObject

- (JSValueConst) scriptObject

Definition at line 125 of file OOJSScript.m.

126{
127 return _jsSelf;
128}

References _jsSelf, and scriptObject.

Referenced by scriptObject, and version.

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

◆ scriptStack [1/2]

+ (NSArray *) scriptStack

Definition at line 283 of file OOJSScript.m.

284{
285 NSMutableArray* result = nil;
286
287 result = [NSMutableArray array];
289 return result;
290}
static void AddStackToArrayReversed(NSMutableArray *array, RunningStack *stack)
Definition OOJSScript.m:635

References AddStackToArrayReversed(), nil, scriptStack, and sRunningStack.

Referenced by scriptStack.

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

◆ scriptStack [2/2]

+ (NSArray *) scriptStack

References scriptStack.

Here is the call graph for this function:

◆ scriptWithPath:

+ (id) scriptWithPath: (NSString*) path

Definition at line 50 of file OOJSScript.m.

50 :(NSString*)path
51{
52 return [[[self alloc] initWithPath:path] autorelease];
53}

Referenced by RunBehaviourTests(), RunErrorTests(), RunScopeTests(), and RunTimeLimiterTests().

Here is the caller graph for this function:

◆ scriptWithPath:properties: [1/2]

+ (id) scriptWithPath: (NSString*) path
properties: (NSDictionary*) properties 

Definition at line 89 of file OOJSScript.m.

89 :(NSString*)path properties:(NSDictionary*)properties
90{
91 return [[[self alloc] initWithPath:path properties:properties] autorelease];
92}

Referenced by OOScript::jsAIScriptFromFileNamed:properties:, OOScript::jsScriptFromFileNamed:properties:, OOCheckJSSyntaxVerifierStage::run, OOScript::scriptsFromFileAtPath:, and OOScript::worldScriptsAtPath:.

Here is the caller graph for this function:

◆ scriptWithPath:properties: [2/2]

+ (id) scriptWithPath: (NSString *) path
properties: (NSDictionary *) properties 

◆ setProperty:named: [1/2]

- (BOOL) setProperty: (id) value
named: (NSString*) name 

Definition at line 445 of file OOJSScript.m.

445 :(id)value named:(NSString*)propName
446{
447 if (value == nil || propName == nil)
448 return NO;
449 if (!JS_IsObject(_jsSelf))
450 return NO;
451
452 JSContext* context = OOJSAcquireContext();
453 BOOL result = [self setProperty:value withID:OOJSIDFromString(propName) inContext:context];
454 OOJSRelinquishContext(context);
455
456 return result;
457}

References _jsSelf, nil, OOJSAcquireContext(), OOJSRelinquishContext(), and setProperty:withID:inContext:.

Referenced by initWithPath:properties:.

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

◆ setProperty:named: [2/2]

- (BOOL) setProperty: (id) value
named: (NSString *) name 

◆ setProperty:withID:inContext: [1/2]

- (BOOL) setProperty: (id) value
withID: (JSAtom) propID
inContext: (JSContext*) context 

Definition at line 411 of file OOJSScript.m.

411 :(id)value withID:(JSAtom)propID inContext:(JSContext*)context
412{
413 NSParameterAssert(context != NULL);
414 if (!JS_IsObject(_jsSelf))
415 return NO;
416
417 // JS_SetProperty() takes ownership of the value.
418 return JS_SetProperty(context, _jsSelf, propID, OOJSValueFromNativeObject(context, value)) >= 0;
419}

◆ setProperty:withID:inContext: [2/2]

- (BOOL) setProperty: (id) value
withID: (jsid) propID
inContext: (JSContext*) context 

Definition at line 411 of file OOJSScript.m.

411 :(id)value withID:(JSAtom)propID inContext:(JSContext*)context
412{
413 NSParameterAssert(context != NULL);
414 if (!JS_IsObject(_jsSelf))
415 return NO;
416
417 // JS_SetProperty() takes ownership of the value.
418 return JS_SetProperty(context, _jsSelf, propID, OOJSValueFromNativeObject(context, value)) >= 0;
419}

References _jsSelf, and OOJSValueFromNativeObject().

Referenced by initWithPath:properties:, and setProperty:named:.

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

◆ stringPropertyNamed:inContext:

- (NSString *) stringPropertyNamed: (const char*) name
inContext: (JSContext*) context 
implementation

Provided by category OOJSScript(Private).

Definition at line 214 of file OOJSScript.m.

214 :(const char*)name inContext:(JSContext*)context
215{
216 JSValue value = JS_GetPropertyStr(context, _jsSelf, name);
217 NSString* result = JS_IsString(value) ? OOStringFromJSValue(context, value) : nil;
218 JS_FreeValue(context, value);
219 return result;
220}
NSString * OOStringFromJSValue(JSContext *context, jsval value)

Referenced by OOJSScript(Private)::loadScriptWithContext:.

Here is the caller graph for this function:

◆ version

- (NSString *) version

Reimplemented from OOScript.

References displayName, scriptObject, and version.

Here is the call graph for this function:

◆ weakRefDied:

- (void) weakRefDied: (OOWeakReference*) weakRef
implementation

Reimplemented from <OOWeakReferenceSupport>.

Definition at line 328 of file OOJSScript.m.

328 :(OOWeakReference*)weakRef
329{
330 if (weakRef == weakSelf)
331 weakSelf = nil;
332}
OOWeakReference * weakSelf
Definition OOJSScript.h:39

References nil, and weakSelf.

◆ weakRetain

- (id) weakRetain
implementation

Definition at line 321 of file OOJSScript.m.

322{
323 if (weakSelf == nil)
325 return [weakSelf retain];
326}
id weakRefWithObject:(id< OOWeakReferenceSupport > object)

References nil, OOWeakReference::weakRefWithObject:, weakRetain, and weakSelf.

Referenced by OOJSGuiScreenKeyDefinition::init, OOJSInterfaceDefinition::init, OOJSPopulatorDefinition::init, OOJSTimer::initWithDelay:interval:context:function:this:, OOJSTimer(Private)::initWithDelay:interval:context:function:this:, initWithPath:properties:, LogProblem(), ReportJSError(), and weakRetain.

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

Member Data Documentation

◆ _jsSelf [1/2]

◆ _jsSelf [2/2]

- (JSValue) _jsSelf
private

Definition at line 32 of file OOJSScript.h.

◆ _name

- (NSString*) _name
private

Definition at line 34 of file OOJSScript.h.

Referenced by displayName.

◆ _path

- (NSString*) _path
private

Definition at line 36 of file OOJSScript.h.

Referenced by initWithPath:.

◆ _version

- (NSString*) _version
private

Definition at line 35 of file OOJSScript.h.

Referenced by displayName.

◆ description

- (NSString *) description
private

Definition at line 35 of file OOJSScript.h.

Referenced by dealloc, initWithPath:properties:, and scriptDescription.

◆ filePath

- (NSString *) filePath
private

Definition at line 37 of file OOJSScript.h.

Referenced by dealloc, and initWithPath:properties:.

◆ name

- (NSString *) name
private

◆ version

- (NSString *) version
private

Definition at line 113 of file OOJSScript.m.

114{
115 return _version;
116}

Referenced by dealloc, initWithPath:properties:, and version.

◆ weakSelf

- (OOWeakReference *) weakSelf
private

Definition at line 39 of file OOJSScript.h.

Referenced by weakRefDied:, and weakRetain.


The documentation for this class was generated from the following files: