/platform/external/webkit/WebCore/bridge/testbindings.mm

https://github.com/aharish/totoro-gb-opensource-update2 · Objective C++ · 287 lines · 191 code · 50 blank · 46 comment · 23 complexity · 0f858b0e25200b5fa7767da673e24661 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  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.
  12. *
  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.
  24. */
  25. #include "config.h"
  26. #include "Bridge.h"
  27. #include <Foundation/Foundation.h>
  28. #include "JSObject.h"
  29. #include "JSValue.h"
  30. #import <WebKit/WebScriptObject.h>
  31. #include "interpreter.h"
  32. #include "runtime_object.h"
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include "types.h"
  36. #define LOG(formatAndArgs...) { \
  37. fprintf (stderr, "%s: ", __PRETTY_FUNCTION__); \
  38. fprintf(stderr, formatAndArgs); \
  39. }
  40. @interface MySecondInterface : NSObject
  41. {
  42. double doubleValue;
  43. }
  44. - init;
  45. @end
  46. @implementation MySecondInterface
  47. - init
  48. {
  49. LOG ("\n");
  50. doubleValue = 666.666;
  51. return self;
  52. }
  53. @end
  54. @interface MyFirstInterface : NSObject
  55. {
  56. int myInt;
  57. MySecondInterface *mySecondInterface;
  58. id jsobject;
  59. NSString *string;
  60. }
  61. - (int)getInt;
  62. - (void)setInt: (int)anInt;
  63. - (MySecondInterface *)getMySecondInterface;
  64. - (void)logMessage:(NSString *)message;
  65. - (void)setJSObject:(id)jsobject;
  66. @end
  67. @implementation MyFirstInterface
  68. + (NSString *)webScriptNameForSelector:(SEL)aSelector
  69. {
  70. if (aSelector == @selector(logMessage:))
  71. return @"logMessage";
  72. if (aSelector == @selector(logMessages:))
  73. return @"logMessages";
  74. if (aSelector == @selector(logMessage:prefix:))
  75. return @"logMessageWithPrefix";
  76. return nil;
  77. }
  78. + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
  79. {
  80. return NO;
  81. }
  82. + (BOOL)isKeyExcludedFromWebScript:(const char *)name
  83. {
  84. return NO;
  85. }
  86. /*
  87. - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
  88. {
  89. NSLog (@"Call to undefined method %@", name);
  90. NSLog (@"%d args\n", [args count]);
  91. int i;
  92. for (i = 0; i < [args count]; i++) {
  93. NSLog (@"%d: %@\n", i, [args objectAtIndex:i]);
  94. }
  95. return @"success";
  96. }
  97. */
  98. /*
  99. - (id)valueForUndefinedKey:(NSString *)key
  100. {
  101. NSLog (@"%s: key = %@", __PRETTY_FUNCTION__, key);
  102. return @"aValue";
  103. }
  104. */
  105. - (void)setValue:(id)value forUndefinedKey:(NSString *)key
  106. {
  107. NSLog (@"%s: key = %@", __PRETTY_FUNCTION__, key);
  108. }
  109. - init
  110. {
  111. LOG ("\n");
  112. mySecondInterface = [[MySecondInterface alloc] init];
  113. return self;
  114. }
  115. - (void)dealloc
  116. {
  117. LOG ("\n");
  118. [mySecondInterface release];
  119. [super dealloc];
  120. }
  121. - (int)getInt
  122. {
  123. LOG ("myInt = %d\n", myInt);
  124. return myInt;
  125. }
  126. - (void)setInt: (int)anInt
  127. {
  128. LOG ("anInt = %d\n", anInt);
  129. myInt = anInt;
  130. }
  131. - (NSString *)getString
  132. {
  133. return string;
  134. }
  135. - (MySecondInterface *)getMySecondInterface
  136. {
  137. LOG ("\n");
  138. return mySecondInterface;
  139. }
  140. - (void)logMessage:(NSString *)message
  141. {
  142. printf ("%s\n", [message lossyCString]);
  143. }
  144. - (void)logMessages:(id)messages
  145. {
  146. int i, count = [[messages valueForKey:@"length"] intValue];
  147. for (i = 0; i < count; i++)
  148. printf ("%s\n", [[messages webScriptValueAtIndex:i] lossyCString]);
  149. }
  150. - (void)logMessage:(NSString *)message prefix:(NSString *)prefix
  151. {
  152. printf ("%s:%s\n", [prefix lossyCString], [message lossyCString]);
  153. }
  154. - (void)setJSObject:(id)jso
  155. {
  156. [jsobject autorelease];
  157. jsobject = [jso retain];
  158. }
  159. - (void)callJSObject:(int)arg1 :(int)arg2
  160. {
  161. id foo1 = [jsobject callWebScriptMethod:@"call" withArguments:[NSArray arrayWithObjects:jsobject, [NSNumber numberWithInt:arg1], [NSNumber numberWithInt:arg2], nil]];
  162. printf ("foo (via call) = %s\n", [[foo1 description] lossyCString] );
  163. id foo2 = [jsobject callWebScriptMethod:@"apply" withArguments:[NSArray arrayWithObjects:jsobject, [NSArray arrayWithObjects:[NSNumber numberWithInt:arg1], [NSNumber numberWithInt:arg2], nil], nil]];
  164. printf ("foo (via apply) = %s\n", [[foo2 description] lossyCString] );
  165. }
  166. @end
  167. using namespace JSC;
  168. using namespace JSC::Bindings;
  169. class GlobalImp : public ObjectImp {
  170. public:
  171. virtual UString className() const { return "global"; }
  172. };
  173. #define BufferSize 200000
  174. static char code[BufferSize];
  175. const char *readJavaScriptFromFile (const char *file)
  176. {
  177. FILE *f = fopen(file, "r");
  178. if (!f) {
  179. fprintf(stderr, "Error opening %s.\n", file);
  180. return 0;
  181. }
  182. int num = fread(code, 1, BufferSize, f);
  183. code[num] = '\0';
  184. if(num >= BufferSize)
  185. fprintf(stderr, "Warning: File may have been too long.\n");
  186. fclose(f);
  187. return code;
  188. }
  189. int main(int argc, char **argv)
  190. {
  191. // expecting a filename
  192. if (argc < 2) {
  193. fprintf(stderr, "You have to specify at least one filename\n");
  194. return -1;
  195. }
  196. bool ret = true;
  197. {
  198. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  199. JSLock lock;
  200. // create interpreter w/ global object
  201. Object global(new GlobalImp());
  202. Interpreter interp;
  203. interp.setGlobalObject(global);
  204. ExecState *exec = interp.globalExec();
  205. MyFirstInterface *myInterface = [[MyFirstInterface alloc] init];
  206. global.put(exec, Identifier("myInterface"), Instance::createRuntimeObject(Instance::ObjectiveCLanguage, (void *)myInterface));
  207. for (int i = 1; i < argc; i++) {
  208. const char *code = readJavaScriptFromFile(argv[i]);
  209. if (code) {
  210. // run
  211. Completion comp(interp.evaluate(code));
  212. if (comp.complType() == Throw) {
  213. Value exVal = comp.value();
  214. char *msg = exVal.toString(exec).ascii();
  215. int lineno = -1;
  216. if (exVal.type() == ObjectType) {
  217. Value lineVal = Object::dynamicCast(exVal).get(exec,Identifier("line"));
  218. if (lineVal.type() == NumberType)
  219. lineno = int(lineVal.toNumber(exec));
  220. }
  221. if (lineno != -1)
  222. fprintf(stderr,"Exception, line %d: %s\n",lineno,msg);
  223. else
  224. fprintf(stderr,"Exception: %s\n",msg);
  225. ret = false;
  226. }
  227. else if (comp.complType() == ReturnValue) {
  228. char *msg = comp.value().toString(interp.globalExec()).ascii();
  229. fprintf(stderr,"Return value: %s\n",msg);
  230. }
  231. }
  232. }
  233. [myInterface release];
  234. [pool drain];
  235. } // end block, so that Interpreter and global get deleted
  236. return ret ? 0 : 3;
  237. }