blob: b3a4b7ad3e1b58d9da9191857f3d92df3edfb8c0 [file] [log] [blame]
barraclough@apple.comfc381882013-01-02 03:51:15 +00001/*
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
joepeck@webkit.org824b7472014-03-11 02:39:40 +000023 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
barraclough@apple.comfc381882013-01-02 03:51:15 +000024 */
25
26#include "config.h"
27
28#import "APICast.h"
commit-queue@webkit.org3b16fcd2014-03-27 20:01:34 +000029#import "JSCInlines.h"
barraclough@apple.comfc381882013-01-02 03:51:15 +000030#import "JSContextInternal.h"
commit-queue@webkit.org3b16fcd2014-03-27 20:01:34 +000031#import "JSContextPrivate.h"
joepeck@webkit.org9c8b10a2014-06-30 17:57:39 +000032#import "JSContextRefInternal.h"
barraclough@apple.comfc381882013-01-02 03:51:15 +000033#import "JSGlobalObject.h"
34#import "JSValueInternal.h"
35#import "JSVirtualMachineInternal.h"
36#import "JSWrapperMap.h"
37#import "JavaScriptCore.h"
38#import "ObjcRuntimeExtras.h"
mhahnenberg@apple.com4ca43472013-01-31 22:33:55 +000039#import "StrongInlines.h"
barraclough@apple.comfc381882013-01-02 03:51:15 +000040#import <wtf/HashSet.h>
41
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +000042#if JSC_OBJC_API_ENABLED
barraclough@apple.comfc381882013-01-02 03:51:15 +000043
barraclough@apple.comfc381882013-01-02 03:51:15 +000044@implementation JSContext {
45 JSVirtualMachine *m_virtualMachine;
46 JSGlobalContextRef m_context;
47 JSWrapperMap *m_wrapperMap;
mhahnenberg@apple.com4ca43472013-01-31 22:33:55 +000048 JSC::Strong<JSC::JSObject> m_exception;
barraclough@apple.comfc381882013-01-02 03:51:15 +000049}
50
barraclough@apple.comfc381882013-01-02 03:51:15 +000051@synthesize exceptionHandler;
52
ggaren@apple.com58bf4ea2013-04-30 21:55:43 +000053- (JSGlobalContextRef)JSGlobalContextRef
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +000054{
55 return m_context;
56}
57
mrowe@apple.com4a0e6c12013-12-11 22:13:23 +000058- (instancetype)init
barraclough@apple.comfc381882013-01-02 03:51:15 +000059{
60 return [self initWithVirtualMachine:[[[JSVirtualMachine alloc] init] autorelease]];
61}
62
mrowe@apple.com4a0e6c12013-12-11 22:13:23 +000063- (instancetype)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine
barraclough@apple.comfc381882013-01-02 03:51:15 +000064{
barraclough@apple.coma56aeb22013-01-02 23:34:48 +000065 self = [super init];
66 if (!self)
67 return nil;
barraclough@apple.comfc381882013-01-02 03:51:15 +000068
69 m_virtualMachine = [virtualMachine retain];
70 m_context = JSGlobalContextCreateInGroup(getGroupFromVirtualMachine(virtualMachine), 0);
71 m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self];
72
barraclough@apple.come2c07422013-01-03 02:03:12 +000073 self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
barraclough@apple.comfc381882013-01-02 03:51:15 +000074 context.exception = exceptionValue;
75 };
76
mhahnenberg@apple.comdb731ed2013-03-07 20:43:05 +000077 [m_virtualMachine addContext:self forGlobalContextRef:m_context];
78
barraclough@apple.comfc381882013-01-02 03:51:15 +000079 return self;
80}
81
mhahnenberg@apple.com67910812013-03-22 03:12:49 +000082- (void)dealloc
83{
ggaren@apple.com025423a2014-02-19 23:51:34 +000084 m_exception.clear();
mhahnenberg@apple.com67910812013-03-22 03:12:49 +000085 [m_wrapperMap release];
86 JSGlobalContextRelease(m_context);
87 [m_virtualMachine release];
88 [self.exceptionHandler release];
89 [super dealloc];
90}
91
barraclough@apple.comfc381882013-01-02 03:51:15 +000092- (JSValue *)evaluateScript:(NSString *)script
93{
joepeck@webkit.org824b7472014-03-11 02:39:40 +000094 return [self evaluateScript:script withSourceURL:nil];
95}
96
97- (JSValue *)evaluateScript:(NSString *)script withSourceURL:(NSURL *)sourceURL
98{
99 JSValueRef exceptionValue = nullptr;
barraclough@apple.comfc381882013-01-02 03:51:15 +0000100 JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
joepeck@webkit.org824b7472014-03-11 02:39:40 +0000101 JSStringRef sourceURLJS = sourceURL ? JSStringCreateWithCFString((CFStringRef)[sourceURL absoluteString]) : nullptr;
102 JSValueRef result = JSEvaluateScript(m_context, scriptJS, nullptr, sourceURLJS, 0, &exceptionValue);
103 if (sourceURLJS)
104 JSStringRelease(sourceURLJS);
barraclough@apple.comfc381882013-01-02 03:51:15 +0000105 JSStringRelease(scriptJS);
106
107 if (exceptionValue)
108 return [self valueFromNotifyException:exceptionValue];
109
ggaren@apple.com58bf4ea2013-04-30 21:55:43 +0000110 return [JSValue valueWithJSValueRef:result inContext:self];
barraclough@apple.comfc381882013-01-02 03:51:15 +0000111}
112
mhahnenberg@apple.com4ca43472013-01-31 22:33:55 +0000113- (void)setException:(JSValue *)value
114{
mhahnenberg@apple.com085d24e2014-03-04 21:38:05 +0000115 JSC::JSLockHolder locker(toJS(m_context));
mhahnenberg@apple.com4ca43472013-01-31 22:33:55 +0000116 if (value)
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000117 m_exception.set(toJS(m_context)->vm(), toJS(JSValueToObject(m_context, valueInternalValue(value), 0)));
mhahnenberg@apple.com4ca43472013-01-31 22:33:55 +0000118 else
119 m_exception.clear();
120}
121
122- (JSValue *)exception
123{
124 if (!m_exception)
125 return nil;
ggaren@apple.com58bf4ea2013-04-30 21:55:43 +0000126 return [JSValue valueWithJSValueRef:toRef(m_exception.get()) inContext:self];
mhahnenberg@apple.com4ca43472013-01-31 22:33:55 +0000127}
128
mhahnenberg@apple.com3eeaad52013-01-30 00:07:38 +0000129- (JSWrapperMap *)wrapperMap
130{
131 return m_wrapperMap;
132}
133
barraclough@apple.comfc381882013-01-02 03:51:15 +0000134- (JSValue *)globalObject
135{
ggaren@apple.com58bf4ea2013-04-30 21:55:43 +0000136 return [JSValue valueWithJSValueRef:JSContextGetGlobalObject(m_context) inContext:self];
barraclough@apple.comfc381882013-01-02 03:51:15 +0000137}
138
139+ (JSContext *)currentContext
140{
141 WTFThreadData& threadData = wtfThreadData();
142 CallbackData *entry = (CallbackData *)threadData.m_apiData;
143 return entry ? entry->context : nil;
144}
145
146+ (JSValue *)currentThis
147{
148 WTFThreadData& threadData = wtfThreadData();
149 CallbackData *entry = (CallbackData *)threadData.m_apiData;
mhahnenberg@apple.coma507c7c2013-10-10 22:03:25 +0000150 if (!entry)
151 return nil;
mhahnenberg@apple.com623a87b2013-10-10 20:50:18 +0000152 return [JSValue valueWithJSValueRef:entry->thisValue inContext:[JSContext currentContext]];
barraclough@apple.comfc381882013-01-02 03:51:15 +0000153}
154
mhahnenberg@apple.com4f18e952014-02-07 01:56:45 +0000155+ (JSValue *)currentCallee
156{
157 WTFThreadData& threadData = wtfThreadData();
158 CallbackData *entry = (CallbackData *)threadData.m_apiData;
159 if (!entry)
160 return nil;
161 return [JSValue valueWithJSValueRef:entry->calleeValue inContext:[JSContext currentContext]];
162}
163
barraclough@apple.comfc381882013-01-02 03:51:15 +0000164+ (NSArray *)currentArguments
165{
166 WTFThreadData& threadData = wtfThreadData();
167 CallbackData *entry = (CallbackData *)threadData.m_apiData;
168
mhahnenberg@apple.coma507c7c2013-10-10 22:03:25 +0000169 if (!entry)
170 return nil;
171
barraclough@apple.comfc381882013-01-02 03:51:15 +0000172 if (!entry->currentArguments) {
173 JSContext *context = [JSContext currentContext];
174 size_t count = entry->argumentCount;
barraclough@apple.coma56aeb22013-01-02 23:34:48 +0000175 JSValue * argumentArray[count];
barraclough@apple.comfc381882013-01-02 03:51:15 +0000176 for (size_t i =0; i < count; ++i)
ggaren@apple.com58bf4ea2013-04-30 21:55:43 +0000177 argumentArray[i] = [JSValue valueWithJSValueRef:entry->arguments[i] inContext:context];
barraclough@apple.coma56aeb22013-01-02 23:34:48 +0000178 entry->currentArguments = [[NSArray alloc] initWithObjects:argumentArray count:count];
barraclough@apple.comfc381882013-01-02 03:51:15 +0000179 }
180
181 return entry->currentArguments;
182}
183
barraclough@apple.come2c07422013-01-03 02:03:12 +0000184- (JSVirtualMachine *)virtualMachine
barraclough@apple.comfc381882013-01-02 03:51:15 +0000185{
186 return m_virtualMachine;
187}
188
joepeck@webkit.orgd2ecf372013-12-04 18:20:37 +0000189- (NSString *)name
190{
191 JSStringRef name = JSGlobalContextCopyName(m_context);
192 if (!name)
193 return nil;
194
mjs@apple.com27c40832014-08-30 22:43:33 +0000195 return (NSString *)adoptCF(JSStringCopyCFString(kCFAllocatorDefault, name)).autorelease();
joepeck@webkit.orgd2ecf372013-12-04 18:20:37 +0000196}
197
198- (void)setName:(NSString *)name
199{
ddkilzer@apple.comce497b12014-06-18 21:41:44 +0000200 JSStringRef nameJS = name ? JSStringCreateWithCFString((CFStringRef)[[name copy] autorelease]) : nullptr;
joepeck@webkit.orgd2ecf372013-12-04 18:20:37 +0000201 JSGlobalContextSetName(m_context, nameJS);
commit-queue@webkit.orgace3fad2014-03-14 21:29:18 +0000202 if (nameJS)
203 JSStringRelease(nameJS);
joepeck@webkit.orgd2ecf372013-12-04 18:20:37 +0000204}
205
commit-queue@webkit.org3b16fcd2014-03-27 20:01:34 +0000206- (BOOL)_remoteInspectionEnabled
207{
208 return JSGlobalContextGetRemoteInspectionEnabled(m_context);
209}
210
211- (void)_setRemoteInspectionEnabled:(BOOL)enabled
212{
213 JSGlobalContextSetRemoteInspectionEnabled(m_context, enabled);
214}
215
commit-queue@webkit.org2de18712014-04-04 00:50:49 +0000216- (BOOL)_includesNativeCallStackWhenReportingExceptions
217{
218 return JSGlobalContextGetIncludesNativeCallStackWhenReportingExceptions(m_context);
219}
220
221- (void)_setIncludesNativeCallStackWhenReportingExceptions:(BOOL)includeNativeCallStack
222{
223 JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions(m_context, includeNativeCallStack);
224}
225
joepeck@webkit.org9c8b10a2014-06-30 17:57:39 +0000226- (CFRunLoopRef)_debuggerRunLoop
227{
228 return JSGlobalContextGetDebuggerRunLoop(m_context);
229}
230
231- (void)_setDebuggerRunLoop:(CFRunLoopRef)runLoop
232{
233 JSGlobalContextSetDebuggerRunLoop(m_context, runLoop);
234}
235
barraclough@apple.comfc381882013-01-02 03:51:15 +0000236@end
237
238@implementation JSContext(SubscriptSupport)
239
240- (JSValue *)objectForKeyedSubscript:(id)key
241{
242 return [self globalObject][key];
243}
244
barraclough@apple.coma56aeb22013-01-02 23:34:48 +0000245- (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key
barraclough@apple.comfc381882013-01-02 03:51:15 +0000246{
247 [self globalObject][key] = object;
248}
249
250@end
251
mrowe@apple.com4a0e6c12013-12-11 22:13:23 +0000252@implementation JSContext (Internal)
barraclough@apple.comfc381882013-01-02 03:51:15 +0000253
mrowe@apple.com4a0e6c12013-12-11 22:13:23 +0000254- (instancetype)initWithGlobalContextRef:(JSGlobalContextRef)context
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000255{
256 self = [super init];
257 if (!self)
258 return nil;
259
260 JSC::JSGlobalObject* globalObject = toJS(context)->lexicalGlobalObject();
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000261 m_virtualMachine = [[JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&globalObject->vm())] retain];
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000262 ASSERT(m_virtualMachine);
263 m_context = JSGlobalContextRetain(context);
264 m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self];
265
266 self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
267 context.exception = exceptionValue;
268 };
269
mhahnenberg@apple.comdb731ed2013-03-07 20:43:05 +0000270 [m_virtualMachine addContext:self forGlobalContextRef:m_context];
271
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000272 return self;
273}
274
barraclough@apple.comfc381882013-01-02 03:51:15 +0000275- (void)notifyException:(JSValueRef)exceptionValue
276{
ggaren@apple.com58bf4ea2013-04-30 21:55:43 +0000277 self.exceptionHandler(self, [JSValue valueWithJSValueRef:exceptionValue inContext:self]);
barraclough@apple.comfc381882013-01-02 03:51:15 +0000278}
279
280- (JSValue *)valueFromNotifyException:(JSValueRef)exceptionValue
281{
282 [self notifyException:exceptionValue];
283 return [JSValue valueWithUndefinedInContext:self];
284}
285
286- (BOOL)boolFromNotifyException:(JSValueRef)exceptionValue
287{
288 [self notifyException:exceptionValue];
289 return NO;
290}
291
mhahnenberg@apple.com4f18e952014-02-07 01:56:45 +0000292- (void)beginCallbackWithData:(CallbackData *)callbackData calleeValue:(JSValueRef)calleeValue thisValue:(JSValueRef)thisValue argumentCount:(size_t)argumentCount arguments:(const JSValueRef *)arguments
barraclough@apple.comfc381882013-01-02 03:51:15 +0000293{
294 WTFThreadData& threadData = wtfThreadData();
295 [self retain];
296 CallbackData *prevStack = (CallbackData *)threadData.m_apiData;
mhahnenberg@apple.com4f18e952014-02-07 01:56:45 +0000297 *callbackData = (CallbackData){ prevStack, self, [self.exception retain], calleeValue, thisValue, argumentCount, arguments, nil };
barraclough@apple.comfc381882013-01-02 03:51:15 +0000298 threadData.m_apiData = callbackData;
299 self.exception = nil;
300}
301
302- (void)endCallbackWithData:(CallbackData *)callbackData
303{
304 WTFThreadData& threadData = wtfThreadData();
305 self.exception = callbackData->preservedException;
306 [callbackData->preservedException release];
barraclough@apple.comfc381882013-01-02 03:51:15 +0000307 [callbackData->currentArguments release];
308 threadData.m_apiData = callbackData->next;
309 [self release];
310}
311
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000312- (JSValue *)wrapperForObjCObject:(id)object
barraclough@apple.comfc381882013-01-02 03:51:15 +0000313{
mhahnenberg@apple.com085d24e2014-03-04 21:38:05 +0000314 JSC::JSLockHolder locker(toJS(m_context));
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000315 return [m_wrapperMap jsWrapperForObject:object];
316}
317
318- (JSValue *)wrapperForJSObject:(JSValueRef)value
319{
mhahnenberg@apple.com085d24e2014-03-04 21:38:05 +0000320 JSC::JSLockHolder locker(toJS(m_context));
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000321 return [m_wrapperMap objcWrapperForJSValueRef:value];
322}
323
ggaren@apple.com58bf4ea2013-04-30 21:55:43 +0000324+ (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)globalContext
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000325{
ggaren@apple.com9a9a4b52013-04-18 19:32:17 +0000326 JSVirtualMachine *virtualMachine = [JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&toJS(globalContext)->vm())];
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000327 JSContext *context = [virtualMachine contextForGlobalContextRef:globalContext];
mhahnenberg@apple.comdb731ed2013-03-07 20:43:05 +0000328 if (!context)
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000329 context = [[[JSContext alloc] initWithGlobalContextRef:globalContext] autorelease];
mhahnenberg@apple.comf0712f92013-02-21 20:00:33 +0000330 return context;
barraclough@apple.comfc381882013-01-02 03:51:15 +0000331}
332
333@end
334
335WeakContextRef::WeakContextRef(JSContext *context)
336{
337 objc_initWeak(&m_weakContext, context);
338}
339
340WeakContextRef::~WeakContextRef()
341{
342 objc_destroyWeak(&m_weakContext);
343}
344
345JSContext * WeakContextRef::get()
346{
347 return objc_loadWeak(&m_weakContext);
348}
349
350void WeakContextRef::set(JSContext *context)
351{
352 objc_storeWeak(&m_weakContext, context);
353}
354
355#endif