Oolite
Loading...
Searching...
No Matches
OOJSFunction.m
Go to the documentation of this file.
1/*
2
3OOJSFunction.m
4
5
6JavaScript support for Oolite
7Copyright (C) 2007-2013 David Taylor and Jens Ayton.
8
9This program is free software; you can redistribute it and/or
10modify it under the terms of the GNU General Public License
11as published by the Free Software Foundation; either version 2
12of the License, or (at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22MA 02110-1301, USA.
23
24*/
25
26#import "OOJSFunction.h"
28#import "OOJSScript.h"
29
30@implementation OOJSFunction
31
32- (id)initWithFunction:(JSFunction*)function context:(JSContext*)context
33{
34 NSParameterAssert(context != NULL);
35
36 if (function == NULL) {
37 [self release];
38 return nil;
39 }
40
41 if ((self = [super init])) {
42 _function = function;
43 OOJSAddGCObjectRoot(context, (JSObject**)&_function, "OOJSFunction._function");
44 _name = [OOStringFromJSString(context, JS_GetFunctionId(function)) retain];
45
46 [[NSNotificationCenter defaultCenter] addObserver:self
47 selector:@selector(deleteJSValue)
48 name:kOOJavaScriptEngineWillResetNotification
50 }
51
52 return self;
53}
54
55- (id)initWithName:(NSString*)name
56 scope:(JSObject*)scope
57 code:(NSString*)code
58 argumentCount:(NSUInteger)argCount
59 argumentNames:(const char**)argNames
60 fileName:(NSString*)fileName
61 lineNumber:(NSUInteger)lineNumber
62 context:(JSContext*)context
63{
64 BOOL OK = YES;
65 BOOL releaseContext = NO;
66 jschar* buffer = NULL;
67 size_t length = 0;
68 JSFunction* function;
69
70 if (context == NULL) {
71 context = OOJSAcquireContext();
72 releaseContext = YES;
73 }
74 if (scope == NULL)
76
77 if (code == nil || (argCount > 0 && argNames == NULL))
78 OK = NO;
79
80 if (OK) {
81 // jschar and unichar are both defined to be 16-bit elements.
82 assert(sizeof(jschar) == sizeof(unichar));
83
84 length = [code length];
85 buffer = malloc(sizeof(jschar) * length);
86 if (buffer == NULL)
87 OK = NO;
88 }
89
90 if (OK) {
91 assert(argCount < UINT32_MAX);
92
93 [code getCharacters:buffer];
94
95 function = JS_CompileUCFunction(context, scope, [name UTF8String], (uint32_t)argCount, argNames, buffer, length, [fileName UTF8String], (uint32_t)lineNumber);
96 if (function == NULL)
97 OK = NO;
98
99 free(buffer);
100 }
101
102 if (OK) {
103 self = [self initWithFunction:function context:context];
104 } else {
105 DESTROY(self);
106 }
107
108 if (releaseContext)
109 OOJSRelinquishContext(context);
110
111 return self;
112}
113
115{
116 if (_function != NULL) {
117 JSContext* context = OOJSAcquireContext();
118 JS_RemoveObjectRoot(context, (JSObject**)&_function);
119 OOJSRelinquishContext(context);
120
121 _function = NULL;
122 [[NSNotificationCenter defaultCenter] removeObserver:self
123 name:kOOJavaScriptEngineWillResetNotification
125 }
126}
127
128- (void)dealloc
129{
130 [self deleteJSValue];
131 DESTROY(_name);
132
133 [super dealloc];
134}
135
137{
138 NSString* name = [self name];
139 if (name == nil)
140 name = @"<anonymous>";
141 return [NSString stringWithFormat:@"%@()", name];
142}
143
144- (NSString*)name
145{
146 return _name;
147}
148
149- (JSFunction*)function
150{
151 return _function;
152}
153
154- (jsval)functionValue
155{
156 if (EXPECT(_function != NULL)) {
157 return OBJECT_TO_JSVAL(JS_GetFunctionObject(_function));
158 } else {
159 return JSVAL_NULL;
160 }
161}
162
163- (BOOL)evaluateWithContext:(JSContext*)context
164 scope:(JSObject*)jsThis
165 argc:(uintN)argc
166 argv:(jsval*)argv
167 result:(jsval*)result
168{
171 BOOL OK = JS_CallFunction(context, jsThis, _function, argc, argv, result);
173 [OOJSScript popScript:nil];
174
175 return OK;
176}
177
178// Semi-raw evaluation shared by convenience methods below.
179- (BOOL)evaluateWithContext:(JSContext*)context
180 scope:(id)jsThis
181 arguments:(NSArray*)arguments
182 result:(jsval*)result
183{
184 NSUInteger i, argc = [arguments count];
185 assert(argc < UINT32_MAX);
186 jsval argv[argc];
187
188 for (i = 0; i < argc; i++) {
189 argv[i] = [[arguments objectAtIndex:i] oo_jsValueInContext:context];
190 OOJSAddGCValueRoot(context, &argv[i], "OOJSFunction argv");
191 }
192
193 JSObject* scopeObj = NULL;
194 BOOL OK = YES;
195 if (jsThis != nil)
196 OK = JS_ValueToObject(context, [jsThis oo_jsValueInContext:context], &scopeObj);
197 if (OK)
198 OK = [self evaluateWithContext:context
199 scope:scopeObj
200 argc:(uint32_t)argc
201 argv:argv
202 result:result];
203
204 for (i = 0; i < argc; i++) {
205 JS_RemoveValueRoot(context, &argv[i]);
206 }
207
208 return OK;
209}
210
211- (id)evaluateWithContext:(JSContext*)context
212 scope:(id)jsThis
213 arguments:(NSArray*)arguments
214{
215 jsval result;
216 BOOL OK = [self evaluateWithContext:context
217 scope:jsThis
218 arguments:arguments
219 result:&result];
220 if (!OK)
221 return nil;
222
223 return OOJSNativeObjectFromJSValue(context, result);
224}
225
226- (BOOL)evaluatePredicateWithContext:(JSContext*)context
227 scope:(id)jsThis
228 arguments:(NSArray*)arguments
229{
230 jsval result;
231 BOOL OK = [self evaluateWithContext:context
232 scope:jsThis
233 arguments:arguments
234 result:&result];
235 JSBool retval = NO;
236 if (OK)
237 OK = JS_ValueToBoolean(context, result, &retval);
238 return OK && retval;
239}
240
241@end
#define DESTROY(x)
#define EXPECT(x)
#define OOJSStopTimeLimiter()
#define OOJSStartTimeLimiter()
id OOJSNativeObjectFromJSValue(JSContext *context, jsval value)
OOINLINE JSContext * OOJSAcquireContext(void)
#define OOJSAddGCObjectRoot(context, root, name)
OOINLINE void OOJSRelinquishContext(JSContext *context)
#define OOJSAddGCValueRoot(context, root, name)
return nil
NSString * descriptionComponents()
JSFunction * function()
NSString * _name
JSFunction * _function
jsval functionValue()
BOOL evaluateWithContext:scope:argc:argv:result:(JSContext *context,[scope] JSObject *jsThis,[argc] uintN argc,[argv] jsval *argv,[result] jsval *result)
NSString * name()
void pushScript:(OOJSScript *script)
Definition OOJSScript.m:480
void popScript:(OOJSScript *script)
Definition OOJSScript.m:493
OOJavaScriptEngine * sharedEngine()