ObjCController.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (C) 2007 Apple 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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #import "config.h"
  29. #import "ObjCController.h"
  30. // Avoid compile error in DOMPrivate.h.
  31. @class NSFont;
  32. #import <JavaScriptCore/JavaScriptCore.h>
  33. #import <WebKit/DOMAbstractView.h>
  34. #import <WebKit/DOMPrivate.h>
  35. #import <WebKit/WebScriptObject.h>
  36. #import <WebKit/WebView.h>
  37. #import <pthread.h>
  38. #import <wtf/Assertions.h>
  39. // Remove this once hasWebScriptKey has been made public.
  40. @interface WebScriptObject (StagedForPublic)
  41. - (BOOL)hasWebScriptKey:(NSString *)name;
  42. @end
  43. static void* runJavaScriptThread(void* arg)
  44. {
  45. JSGlobalContextRef ctx = JSGlobalContextCreate(0);
  46. JSStringRef scriptRef = JSStringCreateWithUTF8CString("'Hello World!'");
  47. JSValueRef exception = 0;
  48. JSEvaluateScript(ctx, scriptRef, 0, 0, 1, &exception);
  49. ASSERT(!exception);
  50. JSGlobalContextRelease(ctx);
  51. JSStringRelease(scriptRef);
  52. return 0;
  53. }
  54. @implementation ObjCController
  55. + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
  56. {
  57. if (0
  58. || aSelector == @selector(classNameOf:)
  59. || aSelector == @selector(isObject:instanceOf:)
  60. || aSelector == @selector(objectOfClass:)
  61. || aSelector == @selector(arrayOfString)
  62. || aSelector == @selector(identityIsEqual::)
  63. || aSelector == @selector(longLongRoundTrip:)
  64. || aSelector == @selector(unsignedLongLongRoundTrip:)
  65. || aSelector == @selector(testWrapperRoundTripping:)
  66. || aSelector == @selector(accessStoredWebScriptObject)
  67. || aSelector == @selector(storeWebScriptObject:)
  68. || aSelector == @selector(testValueForKey)
  69. || aSelector == @selector(testHasWebScriptKey:)
  70. || aSelector == @selector(testArray)
  71. || aSelector == @selector(setSelectElement:selectedIndex:allowingMultiple:)
  72. )
  73. return NO;
  74. return YES;
  75. }
  76. + (NSString *)webScriptNameForSelector:(SEL)aSelector
  77. {
  78. if (aSelector == @selector(classNameOf:))
  79. return @"className";
  80. if (aSelector == @selector(isObject:instanceOf:))
  81. return @"isObjectInstanceOf";
  82. if (aSelector == @selector(objectOfClass:))
  83. return @"objectOfClass";
  84. if (aSelector == @selector(arrayOfString))
  85. return @"arrayOfString";
  86. if (aSelector == @selector(identityIsEqual::))
  87. return @"identityIsEqual";
  88. if (aSelector == @selector(longLongRoundTrip:))
  89. return @"longLongRoundTrip";
  90. if (aSelector == @selector(unsignedLongLongRoundTrip:))
  91. return @"unsignedLongLongRoundTrip";
  92. if (aSelector == @selector(testWrapperRoundTripping:))
  93. return @"testWrapperRoundTripping";
  94. if (aSelector == @selector(storeWebScriptObject:))
  95. return @"storeWebScriptObject";
  96. if (aSelector == @selector(testValueForKey))
  97. return @"testValueForKey";
  98. if (aSelector == @selector(testHasWebScriptKey:))
  99. return @"testHasWebScriptKey";
  100. if (aSelector == @selector(testArray))
  101. return @"testArray";
  102. if (aSelector == @selector(setSelectElement:selectedIndex:allowingMultiple:))
  103. return @"setSelectElementSelectedIndexAllowingMultiple";
  104. return nil;
  105. }
  106. - (BOOL)isObject:(id)object instanceOf:(NSString *)aClass
  107. {
  108. if (!object)
  109. return [aClass isEqualToString:@"nil"];
  110. return [object isKindOfClass:NSClassFromString(aClass)];
  111. }
  112. - (NSString *)classNameOf:(id)object
  113. {
  114. if (!object)
  115. return @"nil";
  116. return NSStringFromClass([object class]);
  117. }
  118. - (id)objectOfClass:(NSString *)aClass
  119. {
  120. if ([aClass isEqualToString:@"NSNull"])
  121. return [NSNull null];
  122. if ([aClass isEqualToString:@"WebUndefined"])
  123. return [WebUndefined undefined];
  124. if ([aClass isEqualToString:@"NSCFBoolean"])
  125. return [NSNumber numberWithBool:true];
  126. if ([aClass isEqualToString:@"NSCFNumber"])
  127. return [NSNumber numberWithInt:1];
  128. if ([aClass isEqualToString:@"NSCFString"])
  129. return @"";
  130. if ([aClass isEqualToString:@"WebScriptObject"])
  131. return self;
  132. if ([aClass isEqualToString:@"NSArray"])
  133. return [NSArray array];
  134. return nil;
  135. }
  136. - (NSArray *)arrayOfString
  137. {
  138. NSString *strings[3];
  139. strings[0] = @"one";
  140. strings[1] = @"two";
  141. strings[2] = @"three";
  142. NSArray *array = [NSArray arrayWithObjects:strings count:3];
  143. return array;
  144. }
  145. - (BOOL)identityIsEqual:(WebScriptObject *)a :(WebScriptObject *)b
  146. {
  147. if ([a isKindOfClass:[NSString class]] && [b isKindOfClass:[NSString class]])
  148. return [(NSString *)a isEqualToString:(NSString *)b];
  149. return a == b;
  150. }
  151. - (long long)longLongRoundTrip:(long long)num
  152. {
  153. return num;
  154. }
  155. - (unsigned long long)unsignedLongLongRoundTrip:(unsigned long long)num
  156. {
  157. return num;
  158. }
  159. - (void)testValueForKey
  160. {
  161. ASSERT(storedWebScriptObject);
  162. @try {
  163. [storedWebScriptObject valueForKey:@"ThisKeyDoesNotExist"];
  164. } @catch (NSException *e) {
  165. }
  166. pthread_t pthread;
  167. pthread_create(&pthread, 0, &runJavaScriptThread, 0);
  168. pthread_join(pthread, 0);
  169. }
  170. - (BOOL)testHasWebScriptKey:(NSString *)key
  171. {
  172. ASSERT(storedWebScriptObject);
  173. return [storedWebScriptObject hasWebScriptKey:key];
  174. }
  175. - (BOOL)testWrapperRoundTripping:(WebScriptObject *)webScriptObject
  176. {
  177. JSObjectRef jsObject = [webScriptObject JSObject];
  178. if (!jsObject)
  179. return false;
  180. if (!webScriptObject)
  181. return false;
  182. if ([[webScriptObject evaluateWebScript:@"({ })"] class] != [webScriptObject class])
  183. return false;
  184. [webScriptObject setValue:[NSNumber numberWithInt:666] forKey:@"key"];
  185. if (![[webScriptObject valueForKey:@"key"] isKindOfClass:[NSNumber class]] ||
  186. ![[webScriptObject valueForKey:@"key"] isEqualToNumber:[NSNumber numberWithInt:666]])
  187. return false;
  188. [webScriptObject removeWebScriptKey:@"key"];
  189. @try {
  190. if ([webScriptObject valueForKey:@"key"])
  191. return false;
  192. } @catch(NSException *exception) {
  193. // NSObject throws an exception if the key doesn't exist.
  194. }
  195. [webScriptObject setWebScriptValueAtIndex:0 value:webScriptObject];
  196. if ([webScriptObject webScriptValueAtIndex:0] != webScriptObject)
  197. return false;
  198. if ([[webScriptObject stringRepresentation] isEqualToString:@"[Object object]"])
  199. return false;
  200. if ([webScriptObject callWebScriptMethod:@"returnThis" withArguments:nil] != webScriptObject)
  201. return false;
  202. return true;
  203. }
  204. - (void)accessStoredWebScriptObject
  205. {
  206. #if !ASSERT_DISABLED
  207. BOOL isWindowObject = [storedWebScriptObject isKindOfClass:[DOMAbstractView class]];
  208. JSObjectRef jsObject = [storedWebScriptObject JSObject];
  209. ASSERT((jsObject && isWindowObject) || (!jsObject && !isWindowObject));
  210. #endif
  211. [storedWebScriptObject callWebScriptMethod:@"" withArguments:nil];
  212. [storedWebScriptObject evaluateWebScript:@""];
  213. [storedWebScriptObject setValue:[WebUndefined undefined] forKey:@"key"];
  214. [storedWebScriptObject valueForKey:@"key"];
  215. [storedWebScriptObject removeWebScriptKey:@"key"];
  216. [storedWebScriptObject stringRepresentation];
  217. [storedWebScriptObject webScriptValueAtIndex:0];
  218. [storedWebScriptObject setWebScriptValueAtIndex:0 value:[WebUndefined undefined]];
  219. [storedWebScriptObject setException:@"exception"];
  220. }
  221. - (void)storeWebScriptObject:(WebScriptObject *)webScriptObject
  222. {
  223. if (webScriptObject == storedWebScriptObject)
  224. return;
  225. [storedWebScriptObject release];
  226. storedWebScriptObject = [webScriptObject retain];
  227. }
  228. - (NSArray *)testArray
  229. {
  230. return [NSArray array];
  231. }
  232. - (void)dealloc
  233. {
  234. [storedWebScriptObject release];
  235. [super dealloc];
  236. }
  237. - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
  238. {
  239. // FIXME: Perhaps we should log that this has been called.
  240. return nil;
  241. }
  242. // MARK: -
  243. // MARK: Testing Objective-C DOM HTML Bindings
  244. - (void)setSelectElement:(WebScriptObject *)element selectedIndex:(int)index allowingMultiple:(BOOL)allowingMultiple
  245. {
  246. if (![element isKindOfClass:[DOMHTMLSelectElement class]])
  247. return;
  248. DOMHTMLSelectElement *select = (DOMHTMLSelectElement*)element;
  249. [select _activateItemAtIndex:index allowMultipleSelection:allowingMultiple];
  250. }
  251. @end