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.
30 #import "JSContextInternal.h"
31 #import "JSGlobalObject.h"
32 #import "JSValueInternal.h"
33 #import "JSVirtualMachineInternal.h"
34 #import "JSWrapperMap.h"
35 #import "JavaScriptCore.h"
36 #import "ObjcRuntimeExtras.h"
37 #import "Operations.h"
38 #import "StrongInlines.h"
39 #import <wtf/HashSet.h>
41 #if JSC_OBJC_API_ENABLED
43 @implementation JSContext {
44 JSVirtualMachine *m_virtualMachine;
45 JSGlobalContextRef m_context;
46 JSWrapperMap *m_wrapperMap;
47 JSC::Strong<JSC::JSObject> m_exception;
50 @synthesize exceptionHandler;
52 - (JSGlobalContextRef)JSGlobalContextRef
59 return [self initWithVirtualMachine:[[[JSVirtualMachine alloc] init] autorelease]];
62 - (id)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine
68 m_virtualMachine = [virtualMachine retain];
69 m_context = JSGlobalContextCreateInGroup(getGroupFromVirtualMachine(virtualMachine), 0);
70 m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self];
72 self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
73 context.exception = exceptionValue;
76 [m_virtualMachine addContext:self forGlobalContextRef:m_context];
83 [m_wrapperMap release];
84 JSGlobalContextRelease(m_context);
85 [m_virtualMachine release];
86 [self.exceptionHandler release];
90 - (JSValue *)evaluateScript:(NSString *)script
92 JSValueRef exceptionValue = 0;
93 JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
94 JSValueRef result = JSEvaluateScript(m_context, scriptJS, 0, 0, 0, &exceptionValue);
95 JSStringRelease(scriptJS);
98 return [self valueFromNotifyException:exceptionValue];
100 return [JSValue valueWithJSValueRef:result inContext:self];
103 - (void)setException:(JSValue *)value
106 m_exception.set(toJS(m_context)->vm(), toJS(JSValueToObject(m_context, valueInternalValue(value), 0)));
111 - (JSValue *)exception
115 return [JSValue valueWithJSValueRef:toRef(m_exception.get()) inContext:self];
118 - (JSWrapperMap *)wrapperMap
123 - (JSValue *)globalObject
125 return [JSValue valueWithJSValueRef:JSContextGetGlobalObject(m_context) inContext:self];
128 + (JSContext *)currentContext
130 WTFThreadData& threadData = wtfThreadData();
131 CallbackData *entry = (CallbackData *)threadData.m_apiData;
132 return entry ? entry->context : nil;
135 + (JSValue *)currentThis
137 WTFThreadData& threadData = wtfThreadData();
138 CallbackData *entry = (CallbackData *)threadData.m_apiData;
140 if (!entry->currentThis)
141 entry->currentThis = [[JSValue alloc] initWithValue:entry->thisValue inContext:[JSContext currentContext]];
143 return entry->currentThis;
146 + (NSArray *)currentArguments
148 WTFThreadData& threadData = wtfThreadData();
149 CallbackData *entry = (CallbackData *)threadData.m_apiData;
151 if (!entry->currentArguments) {
152 JSContext *context = [JSContext currentContext];
153 size_t count = entry->argumentCount;
154 JSValue * argumentArray[count];
155 for (size_t i =0; i < count; ++i)
156 argumentArray[i] = [JSValue valueWithJSValueRef:entry->arguments[i] inContext:context];
157 entry->currentArguments = [[NSArray alloc] initWithObjects:argumentArray count:count];
160 return entry->currentArguments;
163 - (JSVirtualMachine *)virtualMachine
165 return m_virtualMachine;
170 @implementation JSContext(SubscriptSupport)
172 - (JSValue *)objectForKeyedSubscript:(id)key
174 return [self globalObject][key];
177 - (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key
179 [self globalObject][key] = object;
184 @implementation JSContext(Internal)
186 - (id)initWithGlobalContextRef:(JSGlobalContextRef)context
192 JSC::JSGlobalObject* globalObject = toJS(context)->lexicalGlobalObject();
193 m_virtualMachine = [[JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&globalObject->vm())] retain];
194 ASSERT(m_virtualMachine);
195 m_context = JSGlobalContextRetain(context);
196 m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self];
198 self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
199 context.exception = exceptionValue;
202 [m_virtualMachine addContext:self forGlobalContextRef:m_context];
207 - (void)notifyException:(JSValueRef)exceptionValue
209 self.exceptionHandler(self, [JSValue valueWithJSValueRef:exceptionValue inContext:self]);
212 - (JSValue *)valueFromNotifyException:(JSValueRef)exceptionValue
214 [self notifyException:exceptionValue];
215 return [JSValue valueWithUndefinedInContext:self];
218 - (BOOL)boolFromNotifyException:(JSValueRef)exceptionValue
220 [self notifyException:exceptionValue];
224 - (void)beginCallbackWithData:(CallbackData *)callbackData thisValue:(JSValueRef)thisValue argumentCount:(size_t)argumentCount arguments:(const JSValueRef *)arguments
226 WTFThreadData& threadData = wtfThreadData();
228 CallbackData *prevStack = (CallbackData *)threadData.m_apiData;
229 *callbackData = (CallbackData){ prevStack, self, [self.exception retain], thisValue, nil, argumentCount, arguments, nil };
230 threadData.m_apiData = callbackData;
231 self.exception = nil;
234 - (void)endCallbackWithData:(CallbackData *)callbackData
236 WTFThreadData& threadData = wtfThreadData();
237 self.exception = callbackData->preservedException;
238 [callbackData->preservedException release];
239 [callbackData->currentThis release];
240 [callbackData->currentArguments release];
241 threadData.m_apiData = callbackData->next;
245 - (JSValue *)wrapperForObjCObject:(id)object
247 // Lock access to m_wrapperMap
248 JSC::JSLockHolder lock(toJS(m_context));
249 return [m_wrapperMap jsWrapperForObject:object];
252 - (JSValue *)wrapperForJSObject:(JSValueRef)value
254 JSC::JSLockHolder lock(toJS(m_context));
255 return [m_wrapperMap objcWrapperForJSValueRef:value];
258 + (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)globalContext
260 JSVirtualMachine *virtualMachine = [JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&toJS(globalContext)->vm())];
261 JSContext *context = [virtualMachine contextForGlobalContextRef:globalContext];
263 context = [[[JSContext alloc] initWithGlobalContextRef:globalContext] autorelease];
269 WeakContextRef::WeakContextRef(JSContext *context)
271 objc_initWeak(&m_weakContext, context);
274 WeakContextRef::~WeakContextRef()
276 objc_destroyWeak(&m_weakContext);
279 JSContext * WeakContextRef::get()
281 return objc_loadWeak(&m_weakContext);
284 void WeakContextRef::set(JSContext *context)
286 objc_storeWeak(&m_weakContext, context);