2 * Copyright (C) 2013 Apple 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 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.
29 #import "JSCInlines.h"
30 #import "JSContextInternal.h"
31 #import "JSContextPrivate.h"
32 #import "JSContextRefInternal.h"
33 #import "JSGlobalObject.h"
34 #import "JSValueInternal.h"
35 #import "JSVirtualMachineInternal.h"
36 #import "JSWrapperMap.h"
37 #import "JavaScriptCore.h"
38 #import "ObjcRuntimeExtras.h"
39 #import "StrongInlines.h"
40 #import <wtf/HashSet.h>
42 #if JSC_OBJC_API_ENABLED
44 @implementation JSContext {
45 JSVirtualMachine *m_virtualMachine;
46 JSGlobalContextRef m_context;
47 JSWrapperMap *m_wrapperMap;
48 JSC::Strong<JSC::JSObject> m_exception;
51 @synthesize exceptionHandler;
53 - (JSGlobalContextRef)JSGlobalContextRef
60 return [self initWithVirtualMachine:[[[JSVirtualMachine alloc] init] autorelease]];
63 - (instancetype)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine
69 m_virtualMachine = [virtualMachine retain];
70 m_context = JSGlobalContextCreateInGroup(getGroupFromVirtualMachine(virtualMachine), 0);
71 m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self];
73 self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
74 context.exception = exceptionValue;
77 [m_virtualMachine addContext:self forGlobalContextRef:m_context];
85 [m_wrapperMap release];
86 JSGlobalContextRelease(m_context);
87 [m_virtualMachine release];
88 [self.exceptionHandler release];
92 - (JSValue *)evaluateScript:(NSString *)script
94 return [self evaluateScript:script withSourceURL:nil];
97 - (JSValue *)evaluateScript:(NSString *)script withSourceURL:(NSURL *)sourceURL
99 JSValueRef exceptionValue = nullptr;
100 JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
101 JSStringRef sourceURLJS = sourceURL ? JSStringCreateWithCFString((CFStringRef)[sourceURL absoluteString]) : nullptr;
102 JSValueRef result = JSEvaluateScript(m_context, scriptJS, nullptr, sourceURLJS, 0, &exceptionValue);
104 JSStringRelease(sourceURLJS);
105 JSStringRelease(scriptJS);
108 return [self valueFromNotifyException:exceptionValue];
110 return [JSValue valueWithJSValueRef:result inContext:self];
113 - (void)setException:(JSValue *)value
115 JSC::JSLockHolder locker(toJS(m_context));
117 m_exception.set(toJS(m_context)->vm(), toJS(JSValueToObject(m_context, valueInternalValue(value), 0)));
122 - (JSValue *)exception
126 return [JSValue valueWithJSValueRef:toRef(m_exception.get()) inContext:self];
129 - (JSWrapperMap *)wrapperMap
134 - (JSValue *)globalObject
136 return [JSValue valueWithJSValueRef:JSContextGetGlobalObject(m_context) inContext:self];
139 + (JSContext *)currentContext
141 WTFThreadData& threadData = wtfThreadData();
142 CallbackData *entry = (CallbackData *)threadData.m_apiData;
143 return entry ? entry->context : nil;
146 + (JSValue *)currentThis
148 WTFThreadData& threadData = wtfThreadData();
149 CallbackData *entry = (CallbackData *)threadData.m_apiData;
152 return [JSValue valueWithJSValueRef:entry->thisValue inContext:[JSContext currentContext]];
155 + (JSValue *)currentCallee
157 WTFThreadData& threadData = wtfThreadData();
158 CallbackData *entry = (CallbackData *)threadData.m_apiData;
161 return [JSValue valueWithJSValueRef:entry->calleeValue inContext:[JSContext currentContext]];
164 + (NSArray *)currentArguments
166 WTFThreadData& threadData = wtfThreadData();
167 CallbackData *entry = (CallbackData *)threadData.m_apiData;
172 if (!entry->currentArguments) {
173 JSContext *context = [JSContext currentContext];
174 size_t count = entry->argumentCount;
175 JSValue * argumentArray[count];
176 for (size_t i =0; i < count; ++i)
177 argumentArray[i] = [JSValue valueWithJSValueRef:entry->arguments[i] inContext:context];
178 entry->currentArguments = [[NSArray alloc] initWithObjects:argumentArray count:count];
181 return entry->currentArguments;
184 - (JSVirtualMachine *)virtualMachine
186 return m_virtualMachine;
191 JSStringRef name = JSGlobalContextCopyName(m_context);
195 return [(NSString *)JSStringCopyCFString(kCFAllocatorDefault, name) autorelease];
198 - (void)setName:(NSString *)name
200 JSStringRef nameJS = name ? JSStringCreateWithCFString((CFStringRef)[[name copy] autorelease]) : nullptr;
201 JSGlobalContextSetName(m_context, nameJS);
203 JSStringRelease(nameJS);
206 - (BOOL)_remoteInspectionEnabled
208 return JSGlobalContextGetRemoteInspectionEnabled(m_context);
211 - (void)_setRemoteInspectionEnabled:(BOOL)enabled
213 JSGlobalContextSetRemoteInspectionEnabled(m_context, enabled);
216 - (BOOL)_includesNativeCallStackWhenReportingExceptions
218 return JSGlobalContextGetIncludesNativeCallStackWhenReportingExceptions(m_context);
221 - (void)_setIncludesNativeCallStackWhenReportingExceptions:(BOOL)includeNativeCallStack
223 JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(m_context, includeNativeCallStack);
226 - (CFRunLoopRef)_debuggerRunLoop
228 return JSGlobalContextGetDebuggerRunLoop(m_context);
231 - (void)_setDebuggerRunLoop:(CFRunLoopRef)runLoop
233 JSGlobalContextSetDebuggerRunLoop(m_context, runLoop);
238 @implementation JSContext(SubscriptSupport)
240 - (JSValue *)objectForKeyedSubscript:(id)key
242 return [self globalObject][key];
245 - (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key
247 [self globalObject][key] = object;
252 @implementation JSContext (Internal)
254 - (instancetype)initWithGlobalContextRef:(JSGlobalContextRef)context
260 JSC::JSGlobalObject* globalObject = toJS(context)->lexicalGlobalObject();
261 m_virtualMachine = [[JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&globalObject->vm())] retain];
262 ASSERT(m_virtualMachine);
263 m_context = JSGlobalContextRetain(context);
264 m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self];
266 self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
267 context.exception = exceptionValue;
270 [m_virtualMachine addContext:self forGlobalContextRef:m_context];
275 - (void)notifyException:(JSValueRef)exceptionValue
277 self.exceptionHandler(self, [JSValue valueWithJSValueRef:exceptionValue inContext:self]);
280 - (JSValue *)valueFromNotifyException:(JSValueRef)exceptionValue
282 [self notifyException:exceptionValue];
283 return [JSValue valueWithUndefinedInContext:self];
286 - (BOOL)boolFromNotifyException:(JSValueRef)exceptionValue
288 [self notifyException:exceptionValue];
292 - (void)beginCallbackWithData:(CallbackData *)callbackData calleeValue:(JSValueRef)calleeValue thisValue:(JSValueRef)thisValue argumentCount:(size_t)argumentCount arguments:(const JSValueRef *)arguments
294 WTFThreadData& threadData = wtfThreadData();
296 CallbackData *prevStack = (CallbackData *)threadData.m_apiData;
297 *callbackData = (CallbackData){ prevStack, self, [self.exception retain], calleeValue, thisValue, argumentCount, arguments, nil };
298 threadData.m_apiData = callbackData;
299 self.exception = nil;
302 - (void)endCallbackWithData:(CallbackData *)callbackData
304 WTFThreadData& threadData = wtfThreadData();
305 self.exception = callbackData->preservedException;
306 [callbackData->preservedException release];
307 [callbackData->currentArguments release];
308 threadData.m_apiData = callbackData->next;
312 - (JSValue *)wrapperForObjCObject:(id)object
314 JSC::JSLockHolder locker(toJS(m_context));
315 return [m_wrapperMap jsWrapperForObject:object];
318 - (JSValue *)wrapperForJSObject:(JSValueRef)value
320 JSC::JSLockHolder locker(toJS(m_context));
321 return [m_wrapperMap objcWrapperForJSValueRef:value];
324 + (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)globalContext
326 JSVirtualMachine *virtualMachine = [JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&toJS(globalContext)->vm())];
327 JSContext *context = [virtualMachine contextForGlobalContextRef:globalContext];
329 context = [[[JSContext alloc] initWithGlobalContextRef:globalContext] autorelease];
335 WeakContextRef::WeakContextRef(JSContext *context)
337 objc_initWeak(&m_weakContext, context);
340 WeakContextRef::~WeakContextRef()
342 objc_destroyWeak(&m_weakContext);
345 JSContext * WeakContextRef::get()
347 return objc_loadWeak(&m_weakContext);
350 void WeakContextRef::set(JSContext *context)
352 objc_storeWeak(&m_weakContext, context);