2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <NGScripting/NGScriptLanguage.h>
25 @interface NGJavaScriptLanguage : NGScriptLanguage
32 #include "NGJavaScriptObjectMappingContext.h"
33 #include "NGJavaScriptContext.h"
34 #include "NGJavaScriptShadow.h"
35 #include "NGJavaScriptRuntime.h"
36 #include "NSObject+JS.h"
39 @implementation NGJavaScriptLanguage
41 - (id)initWithLanguage:(NSString *)_language {
42 if ((self = [super initWithLanguage:_language])) {
43 NSLog(@"%@", [NGJavaScriptRuntime javaScriptImplementationVersion]);
45 self->rootctx = [[NGJavaScriptObjectMappingContext alloc] init];
46 [[self->rootctx jsContext] loadStandardClasses];
47 [self->rootctx pushContext];
52 [self->rootctx popContext];
53 [self->rootctx release];
57 - (NSString *)language {
61 - (id)evaluateScript:(NSString *)_script onObject:(id)_object
62 source:(NSString *)_source line:(unsigned)_line
64 NGJavaScriptObjectMappingContext *mctx;
70 const char *srcname = NULL;
72 mctx = [NGJavaScriptObjectMappingContext activeObjectMappingContext];
73 NSAssert(mctx, @"no javascript mapping context is active !");
75 jso = [mctx handleForObject:_object];
76 NSAssert(jso, @"missing JS object ..");
78 cx = [[mctx jsContext] handle];
80 if ([_source hasPrefix:@"file://"])
81 _source = [_source substringFromIndex:7];
83 srcname = [_source cString];
86 NSLog(@"%s:%i eval script on objc=0x%08X js=0x%08X cx=0x%08X",
87 [_source cString], _line,
91 res = JS_EvaluateScript(cx, jso,
93 [_script cStringLength],
94 srcname, /* source file */
95 _line, /* line number */
99 return [mctx objectForJSValue:&lastValue];
101 if ([(e = [[mctx jsContext] lastError]) retain]) {
103 [[mctx jsContext] clearLastError];
104 [[mctx jsContext] collectGarbage];
105 //[[mctx jsContext] collectGarbage];
106 //[[mctx jsContext] collectGarbage];
114 ui = [NSDictionary dictionaryWithObjectsAndKeys:
116 _object, @"objectHandler",
117 [NGJavaScriptContext jsContextForHandle:cx],
121 e = [[NSException alloc] initWithName:@"JavaScriptEvalException"
122 reason:@"couldn't evaluate script"
131 - (id)callFunction:(NSString *)_func onObject:(id)_object {
132 NGJavaScriptObjectMappingContext *mctx;
137 mctx = [NGJavaScriptObjectMappingContext activeObjectMappingContext];
138 NSAssert(mctx, @"no javascript mapping context is active !");
140 jso = [mctx handleForObject:_object];
141 NSAssert(jso, @"missing JS object ..");
143 ret = JS_CallFunctionName([[mctx jsContext] handle],
146 0 /* argc */, NULL /* argv */,
149 return [mctx objectForJSValue:&result];
151 NSLog(@"%s: couldn't run function %@", __PRETTY_FUNCTION__, _func);
155 - (id)callFunction:(NSString *)_func
156 withArgument:(id)_arg0
159 NGJavaScriptObjectMappingContext *mctx;
165 mctx = [NGJavaScriptObjectMappingContext activeObjectMappingContext];
166 NSAssert(mctx, @"no javascript mapping context is active !");
168 jso = [mctx handleForObject:_object];
169 NSAssert(jso, @"missing JS object ..");
171 if (![mctx jsValue:&(argv[0]) forObject:_arg0]) {
172 NSLog(@"%s: couldn't get value for first function argument ..",
173 __PRETTY_FUNCTION__);
177 ret = JS_CallFunctionName([[mctx jsContext] handle],
183 return [mctx objectForJSValue:&result];
185 NSLog(@"%s: couldn't run function %@", __PRETTY_FUNCTION__, _func);
188 - (id)callFunction:(NSString *)_func
189 withArgument:(id)_arg0
190 withArgument:(id)_arg1
193 NGJavaScriptObjectMappingContext *mctx;
199 mctx = [NGJavaScriptObjectMappingContext activeObjectMappingContext];
200 NSAssert(mctx, @"no javascript mapping context is active !");
202 jso = [mctx handleForObject:_object];
203 NSAssert(jso, @"missing JS object ..");
205 if (![mctx jsValue:&(argv[0]) forObject:_arg0]) {
206 NSLog(@"%s: couldn't get value for first function argument ..",
207 __PRETTY_FUNCTION__);
211 if (![mctx jsValue:&(argv[1]) forObject:_arg1]) {
212 NSLog(@"%s: couldn't get value for second function argument ..",
213 __PRETTY_FUNCTION__);
217 ret = JS_CallFunctionName([[mctx jsContext] handle],
223 return [mctx objectForJSValue:&result];
225 NSLog(@"%s: couldn't run function %@", __PRETTY_FUNCTION__, _func);
231 - (BOOL)object:(id)_object hasFunctionNamed:(NSString *)_name {
232 return [_object hasFunctionNamed:_name];
237 - (id)createShadowForMaster:(id)_master {
238 NGJavaScriptShadow *shadow;
241 /* no shadow without a master ... */
244 if ((shadow = [[NGJavaScriptShadow alloc] init]) == nil) {
245 NSLog(@"%s: could not create shadow for master %@",
246 __PRETTY_FUNCTION__, _master);
250 [shadow setMasterObject:_master];
252 return shadow; /* returns a retained object */
257 - (NGObjectMappingContext *)createMappingContext {
258 NGJavaScriptObjectMappingContext *ctx;
260 ctx = [[NGJavaScriptObjectMappingContext alloc] init];
261 [[ctx jsContext] loadStandardClasses];
265 @end /* NGJavaScriptLanguage */