From dfcfb90baa1940fb7ceb0c729d181374ed752561 Mon Sep 17 00:00:00 2001 From: Dan Caspi Date: Tue, 27 Sep 2016 09:33:10 -0700 Subject: [PATCH] Fix [JSValue ...] & [JSContext ...] access with custom JSC Reviewed By: bnham Differential Revision: D3859956 fbshipit-source-id: 966aba9a267371eb553b8be574fa247b21930d1c --- React/Executors/RCTJSCExecutor.h | 17 +++++++++++++++++ React/Executors/RCTJSCExecutor.mm | 8 ++++++++ 2 files changed, 25 insertions(+) diff --git a/React/Executors/RCTJSCExecutor.h b/React/Executors/RCTJSCExecutor.h index 47a259ed3..ef87c8b12 100644 --- a/React/Executors/RCTJSCExecutor.h +++ b/React/Executors/RCTJSCExecutor.h @@ -27,6 +27,23 @@ RCT_EXTERN NSString *const RCTJSCThreadName; */ RCT_EXTERN NSString *const RCTJavaScriptContextCreatedNotification; +/** + * A key to a reference to a JSContext class, held in the the current thread's + * dictionary. The reference would point to the JSContext class in the JS VM + * used in React (or ComponenetScript). It is recommended not to access it + * through the thread's dictionary, but rather to use the `FBJSCurrentContext()` + * accessor, which will return the current JSContext in the currently used VM. + */ +RCT_EXTERN NSString *const RCTFBJSContextClassKey; + +/** + * A key to a reference to a JSValue class, held in the the current thread's + * dictionary. The reference would point to the JSValue class in the JS VM + * used in React (or ComponenetScript). It is recommended not to access it + * through the thread's dictionary, but rather to use the `FBJSValue()` accessor. + */ +RCT_EXTERN NSString *const RCTFBJSValueClassKey; + /** * @experimental * May be used to pre-create the JSContext to make RCTJSCExecutor creation less costly. diff --git a/React/Executors/RCTJSCExecutor.mm b/React/Executors/RCTJSCExecutor.mm index 876527931..61fdfb495 100644 --- a/React/Executors/RCTJSCExecutor.mm +++ b/React/Executors/RCTJSCExecutor.mm @@ -34,6 +34,8 @@ NSString *const RCTJSCThreadName = @"com.facebook.react.JavaScript"; NSString *const RCTJavaScriptContextCreatedNotification = @"RCTJavaScriptContextCreatedNotification"; +RCT_EXTERN NSString *const RCTFBJSContextClassKey = @"_RCTFBJSContextClassKey"; +RCT_EXTERN NSString *const RCTFBJSValueClassKey = @"_RCTFBJSValueClassKey"; static NSString *const RCTJSCProfilerEnabledDefaultsKey = @"RCTJSCProfilerEnabled"; @@ -346,6 +348,12 @@ static NSThread *newJavaScriptThread(void) installBasicSynchronousHooksOnContext(context); } + NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary]; + if (!threadDictionary[RCTFBJSContextClassKey] || !threadDictionary[RCTFBJSValueClassKey]) { + threadDictionary[RCTFBJSContextClassKey] = self->_jscWrapper->JSContext; + threadDictionary[RCTFBJSValueClassKey] = self->_jscWrapper->JSValue; + } + __weak RCTJSCExecutor *weakSelf = self; context[@"nativeRequireModuleConfig"] = ^NSArray *(NSString *moduleName) {