barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 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 |
joepeck@webkit.org | 824b747 | 2014-03-11 02:39:40 +0000 | [diff] [blame] | 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #import "APICast.h" |
commit-queue@webkit.org | 3b16fcd | 2014-03-27 20:01:34 +0000 | [diff] [blame] | 29 | #import "JSCInlines.h" |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 30 | #import "JSContextInternal.h" |
commit-queue@webkit.org | 3b16fcd | 2014-03-27 20:01:34 +0000 | [diff] [blame] | 31 | #import "JSContextPrivate.h" |
joepeck@webkit.org | 9c8b10a | 2014-06-30 17:57:39 +0000 | [diff] [blame] | 32 | #import "JSContextRefInternal.h" |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 33 | #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.com | 4ca4347 | 2013-01-31 22:33:55 +0000 | [diff] [blame] | 39 | #import "StrongInlines.h" |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 40 | #import <wtf/HashSet.h> |
| 41 | |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 42 | #if JSC_OBJC_API_ENABLED |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 43 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 44 | @implementation JSContext { |
| 45 | JSVirtualMachine *m_virtualMachine; |
| 46 | JSGlobalContextRef m_context; |
| 47 | JSWrapperMap *m_wrapperMap; |
mhahnenberg@apple.com | 4ca4347 | 2013-01-31 22:33:55 +0000 | [diff] [blame] | 48 | JSC::Strong<JSC::JSObject> m_exception; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 49 | } |
| 50 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 51 | @synthesize exceptionHandler; |
| 52 | |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 53 | - (JSGlobalContextRef)JSGlobalContextRef |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 54 | { |
| 55 | return m_context; |
| 56 | } |
| 57 | |
mrowe@apple.com | 4a0e6c1 | 2013-12-11 22:13:23 +0000 | [diff] [blame] | 58 | - (instancetype)init |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 59 | { |
| 60 | return [self initWithVirtualMachine:[[[JSVirtualMachine alloc] init] autorelease]]; |
| 61 | } |
| 62 | |
mrowe@apple.com | 4a0e6c1 | 2013-12-11 22:13:23 +0000 | [diff] [blame] | 63 | - (instancetype)initWithVirtualMachine:(JSVirtualMachine *)virtualMachine |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 64 | { |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 65 | self = [super init]; |
| 66 | if (!self) |
| 67 | return nil; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 68 | |
| 69 | m_virtualMachine = [virtualMachine retain]; |
| 70 | m_context = JSGlobalContextCreateInGroup(getGroupFromVirtualMachine(virtualMachine), 0); |
| 71 | m_wrapperMap = [[JSWrapperMap alloc] initWithContext:self]; |
| 72 | |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 73 | self.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) { |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 74 | context.exception = exceptionValue; |
| 75 | }; |
| 76 | |
mhahnenberg@apple.com | db731ed | 2013-03-07 20:43:05 +0000 | [diff] [blame] | 77 | [m_virtualMachine addContext:self forGlobalContextRef:m_context]; |
| 78 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 79 | return self; |
| 80 | } |
| 81 | |
mhahnenberg@apple.com | 6791081 | 2013-03-22 03:12:49 +0000 | [diff] [blame] | 82 | - (void)dealloc |
| 83 | { |
ggaren@apple.com | 025423a | 2014-02-19 23:51:34 +0000 | [diff] [blame] | 84 | m_exception.clear(); |
mhahnenberg@apple.com | 6791081 | 2013-03-22 03:12:49 +0000 | [diff] [blame] | 85 | [m_wrapperMap release]; |
| 86 | JSGlobalContextRelease(m_context); |
| 87 | [m_virtualMachine release]; |
| 88 | [self.exceptionHandler release]; |
| 89 | [super dealloc]; |
| 90 | } |
| 91 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 92 | - (JSValue *)evaluateScript:(NSString *)script |
| 93 | { |
joepeck@webkit.org | 824b747 | 2014-03-11 02:39:40 +0000 | [diff] [blame] | 94 | return [self evaluateScript:script withSourceURL:nil]; |
| 95 | } |
| 96 | |
| 97 | - (JSValue *)evaluateScript:(NSString *)script withSourceURL:(NSURL *)sourceURL |
| 98 | { |
| 99 | JSValueRef exceptionValue = nullptr; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 100 | JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script); |
joepeck@webkit.org | 824b747 | 2014-03-11 02:39:40 +0000 | [diff] [blame] | 101 | 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.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 105 | JSStringRelease(scriptJS); |
| 106 | |
| 107 | if (exceptionValue) |
| 108 | return [self valueFromNotifyException:exceptionValue]; |
| 109 | |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 110 | return [JSValue valueWithJSValueRef:result inContext:self]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 111 | } |
| 112 | |
mhahnenberg@apple.com | 4ca4347 | 2013-01-31 22:33:55 +0000 | [diff] [blame] | 113 | - (void)setException:(JSValue *)value |
| 114 | { |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 115 | JSC::JSLockHolder locker(toJS(m_context)); |
mhahnenberg@apple.com | 4ca4347 | 2013-01-31 22:33:55 +0000 | [diff] [blame] | 116 | if (value) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 117 | m_exception.set(toJS(m_context)->vm(), toJS(JSValueToObject(m_context, valueInternalValue(value), 0))); |
mhahnenberg@apple.com | 4ca4347 | 2013-01-31 22:33:55 +0000 | [diff] [blame] | 118 | else |
| 119 | m_exception.clear(); |
| 120 | } |
| 121 | |
| 122 | - (JSValue *)exception |
| 123 | { |
| 124 | if (!m_exception) |
| 125 | return nil; |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 126 | return [JSValue valueWithJSValueRef:toRef(m_exception.get()) inContext:self]; |
mhahnenberg@apple.com | 4ca4347 | 2013-01-31 22:33:55 +0000 | [diff] [blame] | 127 | } |
| 128 | |
mhahnenberg@apple.com | 3eeaad5 | 2013-01-30 00:07:38 +0000 | [diff] [blame] | 129 | - (JSWrapperMap *)wrapperMap |
| 130 | { |
| 131 | return m_wrapperMap; |
| 132 | } |
| 133 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 134 | - (JSValue *)globalObject |
| 135 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 136 | return [JSValue valueWithJSValueRef:JSContextGetGlobalObject(m_context) inContext:self]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 137 | } |
| 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.com | a507c7c | 2013-10-10 22:03:25 +0000 | [diff] [blame] | 150 | if (!entry) |
| 151 | return nil; |
mhahnenberg@apple.com | 623a87b | 2013-10-10 20:50:18 +0000 | [diff] [blame] | 152 | return [JSValue valueWithJSValueRef:entry->thisValue inContext:[JSContext currentContext]]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
mhahnenberg@apple.com | 4f18e95 | 2014-02-07 01:56:45 +0000 | [diff] [blame] | 155 | + (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.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 164 | + (NSArray *)currentArguments |
| 165 | { |
| 166 | WTFThreadData& threadData = wtfThreadData(); |
| 167 | CallbackData *entry = (CallbackData *)threadData.m_apiData; |
| 168 | |
mhahnenberg@apple.com | a507c7c | 2013-10-10 22:03:25 +0000 | [diff] [blame] | 169 | if (!entry) |
| 170 | return nil; |
| 171 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 172 | if (!entry->currentArguments) { |
| 173 | JSContext *context = [JSContext currentContext]; |
| 174 | size_t count = entry->argumentCount; |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 175 | JSValue * argumentArray[count]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 176 | for (size_t i =0; i < count; ++i) |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 177 | argumentArray[i] = [JSValue valueWithJSValueRef:entry->arguments[i] inContext:context]; |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 178 | entry->currentArguments = [[NSArray alloc] initWithObjects:argumentArray count:count]; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | return entry->currentArguments; |
| 182 | } |
| 183 | |
barraclough@apple.com | e2c0742 | 2013-01-03 02:03:12 +0000 | [diff] [blame] | 184 | - (JSVirtualMachine *)virtualMachine |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 185 | { |
| 186 | return m_virtualMachine; |
| 187 | } |
| 188 | |
joepeck@webkit.org | d2ecf37 | 2013-12-04 18:20:37 +0000 | [diff] [blame] | 189 | - (NSString *)name |
| 190 | { |
| 191 | JSStringRef name = JSGlobalContextCopyName(m_context); |
| 192 | if (!name) |
| 193 | return nil; |
| 194 | |
mjs@apple.com | 27c4083 | 2014-08-30 22:43:33 +0000 | [diff] [blame] | 195 | return (NSString *)adoptCF(JSStringCopyCFString(kCFAllocatorDefault, name)).autorelease(); |
joepeck@webkit.org | d2ecf37 | 2013-12-04 18:20:37 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | - (void)setName:(NSString *)name |
| 199 | { |
ddkilzer@apple.com | ce497b1 | 2014-06-18 21:41:44 +0000 | [diff] [blame] | 200 | JSStringRef nameJS = name ? JSStringCreateWithCFString((CFStringRef)[[name copy] autorelease]) : nullptr; |
joepeck@webkit.org | d2ecf37 | 2013-12-04 18:20:37 +0000 | [diff] [blame] | 201 | JSGlobalContextSetName(m_context, nameJS); |
commit-queue@webkit.org | ace3fad | 2014-03-14 21:29:18 +0000 | [diff] [blame] | 202 | if (nameJS) |
| 203 | JSStringRelease(nameJS); |
joepeck@webkit.org | d2ecf37 | 2013-12-04 18:20:37 +0000 | [diff] [blame] | 204 | } |
| 205 | |
commit-queue@webkit.org | 3b16fcd | 2014-03-27 20:01:34 +0000 | [diff] [blame] | 206 | - (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.org | 2de1871 | 2014-04-04 00:50:49 +0000 | [diff] [blame] | 216 | - (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.org | 9c8b10a | 2014-06-30 17:57:39 +0000 | [diff] [blame] | 226 | - (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.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 236 | @end |
| 237 | |
| 238 | @implementation JSContext(SubscriptSupport) |
| 239 | |
| 240 | - (JSValue *)objectForKeyedSubscript:(id)key |
| 241 | { |
| 242 | return [self globalObject][key]; |
| 243 | } |
| 244 | |
barraclough@apple.com | a56aeb2 | 2013-01-02 23:34:48 +0000 | [diff] [blame] | 245 | - (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 246 | { |
| 247 | [self globalObject][key] = object; |
| 248 | } |
| 249 | |
| 250 | @end |
| 251 | |
mrowe@apple.com | 4a0e6c1 | 2013-12-11 22:13:23 +0000 | [diff] [blame] | 252 | @implementation JSContext (Internal) |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 253 | |
mrowe@apple.com | 4a0e6c1 | 2013-12-11 22:13:23 +0000 | [diff] [blame] | 254 | - (instancetype)initWithGlobalContextRef:(JSGlobalContextRef)context |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 255 | { |
| 256 | self = [super init]; |
| 257 | if (!self) |
| 258 | return nil; |
| 259 | |
| 260 | JSC::JSGlobalObject* globalObject = toJS(context)->lexicalGlobalObject(); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 261 | m_virtualMachine = [[JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&globalObject->vm())] retain]; |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 262 | 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.com | db731ed | 2013-03-07 20:43:05 +0000 | [diff] [blame] | 270 | [m_virtualMachine addContext:self forGlobalContextRef:m_context]; |
| 271 | |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 272 | return self; |
| 273 | } |
| 274 | |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 275 | - (void)notifyException:(JSValueRef)exceptionValue |
| 276 | { |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 277 | self.exceptionHandler(self, [JSValue valueWithJSValueRef:exceptionValue inContext:self]); |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 278 | } |
| 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.com | 4f18e95 | 2014-02-07 01:56:45 +0000 | [diff] [blame] | 292 | - (void)beginCallbackWithData:(CallbackData *)callbackData calleeValue:(JSValueRef)calleeValue thisValue:(JSValueRef)thisValue argumentCount:(size_t)argumentCount arguments:(const JSValueRef *)arguments |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 293 | { |
| 294 | WTFThreadData& threadData = wtfThreadData(); |
| 295 | [self retain]; |
| 296 | CallbackData *prevStack = (CallbackData *)threadData.m_apiData; |
mhahnenberg@apple.com | 4f18e95 | 2014-02-07 01:56:45 +0000 | [diff] [blame] | 297 | *callbackData = (CallbackData){ prevStack, self, [self.exception retain], calleeValue, thisValue, argumentCount, arguments, nil }; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 298 | 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.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 307 | [callbackData->currentArguments release]; |
| 308 | threadData.m_apiData = callbackData->next; |
| 309 | [self release]; |
| 310 | } |
| 311 | |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 312 | - (JSValue *)wrapperForObjCObject:(id)object |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 313 | { |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 314 | JSC::JSLockHolder locker(toJS(m_context)); |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 315 | return [m_wrapperMap jsWrapperForObject:object]; |
| 316 | } |
| 317 | |
| 318 | - (JSValue *)wrapperForJSObject:(JSValueRef)value |
| 319 | { |
mhahnenberg@apple.com | 085d24e | 2014-03-04 21:38:05 +0000 | [diff] [blame] | 320 | JSC::JSLockHolder locker(toJS(m_context)); |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 321 | return [m_wrapperMap objcWrapperForJSValueRef:value]; |
| 322 | } |
| 323 | |
ggaren@apple.com | 58bf4ea | 2013-04-30 21:55:43 +0000 | [diff] [blame] | 324 | + (JSContext *)contextWithJSGlobalContextRef:(JSGlobalContextRef)globalContext |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 325 | { |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 326 | JSVirtualMachine *virtualMachine = [JSVirtualMachine virtualMachineWithContextGroupRef:toRef(&toJS(globalContext)->vm())]; |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 327 | JSContext *context = [virtualMachine contextForGlobalContextRef:globalContext]; |
mhahnenberg@apple.com | db731ed | 2013-03-07 20:43:05 +0000 | [diff] [blame] | 328 | if (!context) |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 329 | context = [[[JSContext alloc] initWithGlobalContextRef:globalContext] autorelease]; |
mhahnenberg@apple.com | f0712f9 | 2013-02-21 20:00:33 +0000 | [diff] [blame] | 330 | return context; |
barraclough@apple.com | fc38188 | 2013-01-02 03:51:15 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | @end |
| 334 | |
| 335 | WeakContextRef::WeakContextRef(JSContext *context) |
| 336 | { |
| 337 | objc_initWeak(&m_weakContext, context); |
| 338 | } |
| 339 | |
| 340 | WeakContextRef::~WeakContextRef() |
| 341 | { |
| 342 | objc_destroyWeak(&m_weakContext); |
| 343 | } |
| 344 | |
| 345 | JSContext * WeakContextRef::get() |
| 346 | { |
| 347 | return objc_loadWeak(&m_weakContext); |
| 348 | } |
| 349 | |
| 350 | void WeakContextRef::set(JSContext *context) |
| 351 | { |
| 352 | objc_storeWeak(&m_weakContext, context); |
| 353 | } |
| 354 | |
| 355 | #endif |