JSContext.mm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (C) 2013 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. * 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 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 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. #import "APICast.h"
  27. #import "APIShims.h"
  28. #import "JSContextInternal.h"
  29. #import "JSGlobalObject.h"
  30. #import "JSValueInternal.h"
  31. #import "JSVirtualMachineInternal.h"
  32. #import "JSWrapperMap.h"
  33. #import "JavaScriptCore.h"
  34. #import "ObjcRuntimeExtras.h"
  35. #import "Operations.h"
  36. #import "StrongInlines.h"
  37. #import <wtf/HashSet.h>
  38. #if JSC_OBJC_API_ENABLED
  39. @implementation JSContext {
  40. JSVirtualMachine *m_virtualMachine;
  41. JSGlobalContextRef m_context;
  42. JSWrapperMap *m_wrapperMap;
  43. JSC::Strong<JSC::JSObject> m_exception;
  44. }
  45. @synthesize exceptionHandler;
  46. - (JSGlobalContextRef)JSGlobalContextRef
  47. {
  48. return m_context;
  49. }
  50. - (id)init
  51. {
  52. return [self initWithVirtualMachine:[[[JSVirtualMachine alloc] init] autorelease]];
  53. }
  54. - (id)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine
  55. {
  56. self = [super init];
  57. if (!self)
  58. return nil;
  59. m_virtualMachine = [virtualMachine retain];
  60. m_context = JSGlobalContextCreateInGroup(getGroupFromVirtualMachine(virtualMachine), 0);
  61. m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self];
  62. self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
  63. context.exception = exceptionValue;
  64. };
  65. [m_virtualMachine addContext:self forGlobalContextRef:m_context];
  66. return self;
  67. }
  68. - (void)dealloc
  69. {
  70. [m_wrapperMap release];
  71. JSGlobalContextRelease(m_context);
  72. [m_virtualMachine release];
  73. [self.exceptionHandler release];
  74. [super dealloc];
  75. }
  76. - (JSValue *)evaluateScript:(NSString *)script
  77. {
  78. JSValueRef exceptionValue = 0;
  79. JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
  80. JSValueRef result = JSEvaluateScript(m_context, scriptJS, 0, 0, 0, &exceptionValue);
  81. JSStringRelease(scriptJS);
  82. if (exceptionValue)
  83. return [self valueFromNotifyException:exceptionValue];
  84. return [JSValue valueWithJSValueRef:result inContext:self];
  85. }
  86. - (void)setException:(JSValue *)value
  87. {
  88. if (value)
  89. m_exception.set(toJS(m_context)->vm(), toJS(JSValueToObject(m_context, valueInternalValue(value), 0)));
  90. else
  91. m_exception.clear();
  92. }
  93. - (JSValue *)exception
  94. {
  95. if (!m_exception)
  96. return nil;
  97. return [JSValue valueWithJSValueRef:toRef(m_exception.get()) inContext:self];
  98. }
  99. - (JSWrapperMap *)wrapperMap
  100. {
  101. return m_wrapperMap;
  102. }
  103. - (JSValue *)globalObject
  104. {
  105. return [JSValue valueWithJSValueRef:JSContextGetGlobalObject(m_context) inContext:self];
  106. }
  107. + (JSContext *)currentContext
  108. {
  109. WTFThreadData& threadData = wtfThreadData();
  110. CallbackData *entry = (CallbackData *)threadData.m_apiData;
  111. return entry ? entry->context : nil;
  112. }
  113. + (JSValue *)currentThis
  114. {
  115. WTFThreadData& threadData = wtfThreadData();
  116. CallbackData *entry = (CallbackData *)threadData.m_apiData;
  117. if (!entry->currentThis)
  118. entry->currentThis = [[JSValue alloc] initWithValue:entry->thisValue inContext:[JSContext currentContext]];
  119. return entry->currentThis;
  120. }
  121. + (NSArray *)currentArguments
  122. {
  123. WTFThreadData& threadData = wtfThreadData();
  124. CallbackData *entry = (CallbackData *)threadData.m_apiData;
  125. if (!entry->currentArguments) {
  126. JSContext *context = [JSContext currentContext];
  127. size_t count = entry->argumentCount;
  128. JSValue * argumentArray[count];
  129. for (size_t i =0; i < count; ++i)
  130. argumentArray[i] = [JSValue valueWithJSValueRef:entry->arguments[i] inContext:context];
  131. entry->currentArguments = [[NSArray alloc] initWithObjects:argumentArray count:count];
  132. }
  133. return entry->currentArguments;
  134. }
  135. - (JSVirtualMachine *)virtualMachine
  136. {
  137. return m_virtualMachine;
  138. }
  139. @end
  140. @implementation JSContext(SubscriptSupport)
  141. - (JSValue *)objectForKeyedSubscript:(id)key
  142. {
  143. return [self globalObject][key];
  144. }
  145. - (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key
  146. {
  147. [self globalObject][key] = object;
  148. }
  149. @end
  150. @implementation JSContext(Internal)
  151. - (id)initWithGlobalContextRef:(JSGlobalContextRef)context
  152. {
  153. self = [super init];
  154. if (!self)
  155. return nil;
  156. JSC::JSGlobalObject* globalObject = toJS(context)->lexicalGlobalObject();
  157. m_virtualMachine = [[JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&globalObject->vm())] retain];
  158. ASSERT(m_virtualMachine);
  159. m_context = JSGlobalContextRetain(context);
  160. m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self];
  161. self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
  162. context.exception = exceptionValue;
  163. };
  164. [m_virtualMachine addContext:self forGlobalContextRef:m_context];
  165. return self;
  166. }
  167. - (void)notifyException:(JSValueRef)exceptionValue
  168. {
  169. self.exceptionHandler(self, [JSValue valueWithJSValueRef:exceptionValue inContext:self]);
  170. }
  171. - (JSValue *)valueFromNotifyException:(JSValueRef)exceptionValue
  172. {
  173. [self notifyException:exceptionValue];
  174. return [JSValue valueWithUndefinedInContext:self];
  175. }
  176. - (BOOL)boolFromNotifyException:(JSValueRef)exceptionValue
  177. {
  178. [self notifyException:exceptionValue];
  179. return NO;
  180. }
  181. - (void)beginCallbackWithData:(CallbackData *)callbackData thisValue:(JSValueRef)thisValue argumentCount:(size_t)argumentCount arguments:(const JSValueRef *)arguments
  182. {
  183. WTFThreadData& threadData = wtfThreadData();
  184. [self retain];
  185. CallbackData *prevStack = (CallbackData *)threadData.m_apiData;
  186. *callbackData = (CallbackData){ prevStack, self, [self.exception retain], thisValue, nil, argumentCount, arguments, nil };
  187. threadData.m_apiData = callbackData;
  188. self.exception = nil;
  189. }
  190. - (void)endCallbackWithData:(CallbackData *)callbackData
  191. {
  192. WTFThreadData& threadData = wtfThreadData();
  193. self.exception = callbackData->preservedException;
  194. [callbackData->preservedException release];
  195. [callbackData->currentThis release];
  196. [callbackData->currentArguments release];
  197. threadData.m_apiData = callbackData->next;
  198. [self release];
  199. }
  200. - (JSValue *)wrapperForObjCObject:(id)object
  201. {
  202. // Lock access to m_wrapperMap
  203. JSC::JSLockHolder lock(toJS(m_context));
  204. return [m_wrapperMap jsWrapperForObject:object];
  205. }
  206. - (JSValue *)wrapperForJSObject:(JSValueRef)value
  207. {
  208. JSC::JSLockHolder lock(toJS(m_context));
  209. return [m_wrapperMap objcWrapperForJSValueRef:value];
  210. }
  211. + (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)globalContext
  212. {
  213. JSVirtualMachine *virtualMachine = [JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&toJS(globalContext)->vm())];
  214. JSContext *context = [virtualMachine contextForGlobalContextRef:globalContext];
  215. if (!context)
  216. context = [[[JSContext alloc] initWithGlobalContextRef:globalContext] autorelease];
  217. return context;
  218. }
  219. @end
  220. WeakContextRef::WeakContextRef(JSContext *context)
  221. {
  222. objc_initWeak(&m_weakContext, context);
  223. }
  224. WeakContextRef::~WeakContextRef()
  225. {
  226. objc_destroyWeak(&m_weakContext);
  227. }
  228. JSContext * WeakContextRef::get()
  229. {
  230. return objc_loadWeak(&m_weakContext);
  231. }
  232. void WeakContextRef::set(JSContext *context)
  233. {
  234. objc_storeWeak(&m_weakContext, context);
  235. }
  236. #endif