Oolite
Loading...
Searching...
No Matches
OOJSScript.m
Go to the documentation of this file.
1/*
2
3OOJSScript.m (QuickJS-ng proof of concept)
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 "OOJSScript.h"
26
27typedef struct RunningStack RunningStack;
28struct RunningStack {
30 OOJSScript* current;
31};
32
34
35static JSClassID sScriptClassID;
36
37static JSClassDef sScriptClass = {
38 .class_name = "Script",
39};
40
41@interface OOJSScript (Private)
42
43- (BOOL)loadScriptWithContext:(JSContext*)context;
44- (NSString*)stringPropertyNamed:(const char*)name inContext:(JSContext*)context;
45
46@end
47
48@implementation OOJSScript
49
50+ (id)scriptWithPath:(NSString*)path
51{
52 return [[[self alloc] initWithPath:path] autorelease];
53}
54
55- (id)initWithPath:(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}
84
85- (void)dealloc
86{
87 if (JS_IsObject(_jsSelf)) {
88 JSContext* context = OOJSAcquireContext();
89 JS_SetOpaque(_jsSelf, NULL);
90 JS_FreeValue(context, _jsSelf);
91 OOJSRelinquishContext(context);
92 }
93
94 [_name release];
95 [_version release];
96 [_path release];
97
98 [super dealloc];
99}
100
102{
103 if (sRunningStack == NULL)
104 return nil;
105 return sRunningStack->current;
106}
107
108- (NSString*)name
109{
110 return _name;
111}
112
113- (NSString*)version
114{
115 return _version;
116}
117
118- (NSString*)displayName
119{
120 if (_version != nil)
121 return [NSString stringWithFormat:@"%@ %@", _name, _version];
122 return _name;
123}
124
125- (JSValueConst)scriptObject
126{
127 return _jsSelf;
128}
129
130- (BOOL)callMethodNamed:(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}
161
162@end
163
164@implementation OOJSScript (Private)
165
166- (BOOL)loadScriptWithContext:(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}
213
214- (NSString*)stringPropertyNamed:(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}
221
222@end
223
224void InitOOJSScript(JSContext* context, JSValueConst global)
225{
226 JSRuntime* runtime = JS_GetRuntime(context);
227 JS_NewClassID(runtime, &sScriptClassID);
228 JS_NewClass(runtime, sScriptClassID, &sScriptClass);
229}
#define OOJSStopTimeLimiter()
#define kOOJSLongTimeLimit
#define OOJSStartTimeLimiterWithTimeLimit(limit)
static RunningStack * sRunningStack
Definition OOJSScript.m:56
struct RunningStack RunningStack
Definition OOJSScript.m:49
static JSClass sScriptClass
Definition OOJSScript.m:71
OOINLINE jsval OOJSValueFromNativeObject(JSContext *context, id object)
NSString * OOStringFromJSValue(JSContext *context, jsval value)
OOINLINE JSContext * OOJSAcquireContext(void)
OOINLINE void OOJSReportPendingException(JSContext *context)
OOINLINE void OOJSRelinquishContext(JSContext *context)
#define OOLog(class, format,...)
Definition OOLogging.h:84
return nil
static JSClassID sScriptClassID
Definition OOJSScript.m:53
static RunningStack * sRunningStack
Definition OOJSScript.m:33
void InitOOJSScript(JSContext *context, JSValueConst global)
Definition OOJSScript.m:224
NSString * _path
Definition OOJSScript.h:36
NSString * version
Definition OOJSScript.m:113
NSString * displayName()
Definition OOJSScript.m:118
NSString * _version
Definition OOJSScript.h:35
JSValueConst scriptObject()
Definition OOJSScript.m:125
OOJSScript * currentlyRunningScript()
Definition OOJSScript.m:101
JSObject * _jsSelf
Definition OOJSScript.h:32
NSString * stringPropertyNamed:inContext:(const char *name,[inContext] JSContext *context)
Definition OOJSScript.m:214
NSString * name
Definition OOJSScript.m:108
NSString * _name
Definition OOJSScript.h:34
BOOL callJSFunction:forObject:argc:argv:result:(jsval function,[forObject] JSObject *jsThis,[argc] uintN argc,[argv] jsval *argv,[result] jsval *outResult)
OOJavaScriptEngine * sharedEngine()
OOJSScript * current
Definition OOJSScript.m:52
RunningStack * back
Definition OOJSScript.m:51