2 * Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <Foundation/Foundation.h>
29 #import <WebKit/WebScriptObject.h>
37 #include "interpreter.h"
40 #include "runtime_object.h"
42 #define LOG(formatAndArgs...) { \
43 fprintf (stderr, "%s: ", __PRETTY_FUNCTION__); \
44 fprintf(stderr, formatAndArgs); \
47 @interface MySecondInterface : NSObject
56 @implementation MySecondInterface
61 doubleValue = 666.666;
67 @interface MyFirstInterface : NSObject
70 MySecondInterface *mySecondInterface;
76 - (void)setInt: (int)anInt;
77 - (MySecondInterface *)getMySecondInterface;
78 - (void)logMessage:(NSString *)message;
79 - (void)setJSObject:(id)jsobject;
82 @implementation MyFirstInterface
84 + (NSString *)webScriptNameForSelector:(SEL)aSelector
86 if (aSelector == @selector(logMessage:))
88 if (aSelector == @selector(logMessages:))
89 return @"logMessages";
90 if (aSelector == @selector(logMessage:prefix:))
91 return @"logMessageWithPrefix";
95 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
100 + (BOOL)isKeyExcludedFromWebScript:(const char *)name
106 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args;
108 NSLog (@"Call to undefined method %@", name);
109 NSLog (@"%d args\n", [args count]);
111 for (i = 0; i < [args count]; i++) {
112 NSLog (@"%d: %@\n", i, [args objectAtIndex:i]);
119 - (id)valueForUndefinedKey:(NSString *)key
121 NSLog (@"%s: key = %@", __PRETTY_FUNCTION__, key);
126 - (void)setValue:(id)value forUndefinedKey:(NSString *)key
128 NSLog (@"%s: key = %@", __PRETTY_FUNCTION__, key);
134 mySecondInterface = [[MySecondInterface alloc] init];
141 [mySecondInterface release];
147 LOG ("myInt = %d\n", myInt);
151 - (void)setInt: (int)anInt
153 LOG ("anInt = %d\n", anInt);
157 - (NSString *)getString
162 - (MySecondInterface *)getMySecondInterface
165 return mySecondInterface;
168 - (void)logMessage:(NSString *)message
170 printf ("%s\n", [message lossyCString]);
173 - (void)logMessages:(id)messages
175 int i, count = [[messages valueForKey:@"length"] intValue];
176 for (i = 0; i < count; i++)
177 printf ("%s\n", [[messages webScriptValueAtIndex:i] lossyCString]);
180 - (void)logMessage:(NSString *)message prefix:(NSString *)prefix
182 printf ("%s:%s\n", [prefix lossyCString], [message lossyCString]);
185 - (void)setJSObject:(id)jso
187 [jsobject autorelease];
188 jsobject = [jso retain];
191 - (void)callJSObject:(int)arg1 :(int)arg2
193 id foo1 = [jsobject callWebScriptMethod:@"call" withArguments:[NSArray arrayWithObjects:jsobject, [NSNumber numberWithInt:arg1], [NSNumber numberWithInt:arg2], nil]];
194 printf ("foo (via call) = %s\n", [[foo1 description] lossyCString] );
195 id foo2 = [jsobject callWebScriptMethod:@"apply" withArguments:[NSArray arrayWithObjects:jsobject, [NSArray arrayWithObjects:[NSNumber numberWithInt:arg1], [NSNumber numberWithInt:arg2], nil], nil]];
196 printf ("foo (via apply) = %s\n", [[foo2 description] lossyCString] );
203 using namespace KJS::Bindings;
205 class GlobalImp : public ObjectImp {
207 virtual UString className() const { return "global"; }
210 #define BufferSize 200000
211 static char code[BufferSize];
213 const char *readJavaScriptFromFile (const char *file)
215 FILE *f = fopen(file, "r");
217 fprintf(stderr, "Error opening %s.\n", file);
221 int num = fread(code, 1, BufferSize, f);
223 if(num >= BufferSize)
224 fprintf(stderr, "Warning: File may have been too long.\n");
231 int main(int argc, char **argv)
233 // expecting a filename
235 fprintf(stderr, "You have to specify at least one filename\n");
241 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
245 // create interpreter w/ global object
246 Object global(new GlobalImp());
248 interp.setGlobalObject(global);
249 ExecState *exec = interp.globalExec();
251 MyFirstInterface *myInterface = [[MyFirstInterface alloc] init];
253 global.put(exec, Identifier("myInterface"), Instance::createRuntimeObject(Instance::ObjectiveCLanguage, (void *)myInterface));
255 for (int i = 1; i < argc; i++) {
256 const char *code = readJavaScriptFromFile(argv[i]);
260 Completion comp(interp.evaluate(code));
262 if (comp.complType() == Throw) {
263 Value exVal = comp.value();
264 char *msg = exVal.toString(exec).ascii();
266 if (exVal.type() == ObjectType) {
267 Value lineVal = Object::dynamicCast(exVal).get(exec,Identifier("line"));
268 if (lineVal.type() == NumberType)
269 lineno = int(lineVal.toNumber(exec));
272 fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
274 fprintf(stderr,"Exception: %s\n",msg);
277 else if (comp.complType() == ReturnValue) {
278 char *msg = comp.value().toString(interp.globalExec()).ascii();
279 fprintf(stderr,"Return value: %s\n",msg);
284 [myInterface release];
286 } // end block, so that Interpreter and global get deleted