Oolite
Loading...
Searching...
No Matches
OOJSFunction.h
Go to the documentation of this file.
1/*
2
3OOJSFunction.h
4
5Object encapsulating a runnable JavaScript function.
6
7
8JavaScript support for Oolite
9Copyright (C) 2007-2013 David Taylor and Jens Ayton.
10
11This program is free software; you can redistribute it and/or
12modify it under the terms of the GNU General Public License
13as published by the Free Software Foundation; either version 2
14of the License, or (at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19GNU General Public License for more details.
20
21You should have received a copy of the GNU General Public License
22along with this program; if not, write to the Free Software
23Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24MA 02110-1301, USA.
25
26*/
27
28#import "OOFoundation.h"
29#include <quickjs.h>
30
31@interface OOJSFunction : NSObject {
32@private
33 JSValue _function; // Owned; zero-filled (not an object) when invalid.
34 NSString* _name;
35}
36
37// function must be a function value; the OOJSFunction takes its own reference.
38- (id)initWithFunction:(JSValueConst)function context:(JSContext*)context;
39- (id)initWithName:(NSString*)name
40 scope:(JSValueConst)scope // Ignored; QuickJS compiles functions in the global scope. Pass JS_UNDEFINED.
41 code:(NSString*)code // Function body.
42 argumentCount:(NSUInteger)argCount
43 argumentNames:(const char**)argNames
44 fileName:(NSString*)fileName
45 lineNumber:(NSUInteger)lineNumber
46 context:(JSContext*)context; // may be NULL.
47
48- (NSString*)name;
49// Borrowed; valid while the receiver is alive and the engine is not reset. JS_NULL if invalid.
50- (JSValueConst)function;
51// Owned (caller must free). JS_NULL if invalid.
52- (JSValue)functionValue;
53
54// Raw evaluation. *result is owned by the caller on success. On failure, any exception is left pending (not reported).
55- (BOOL)evaluateWithContext:(JSContext*)context
56 scope:(JSValueConst)jsThis
57 argc:(int)argc
58 argv:(JSValueConst*)argv
59 result:(JSValue*)result;
60
61// Object-wrapper evaluation.
62- (id)evaluateWithContext:(JSContext*)context
63 scope:(id)jsThis
64 arguments:(NSArray*)arguments;
65
66// As above, but converts result to a boolean.
67- (BOOL)evaluatePredicateWithContext:(JSContext*)context
68 scope:(id)jsThis
69 arguments:(NSArray*)arguments;
70
71@end
JSFunction * function()
NSString * _name
JSFunction * _function
jsval functionValue()
NSString * name()