From 8355ef91adde2a0279613193d4d8ff45d6f6db30 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Fri, 14 Oct 2016 11:29:47 -0700 Subject: [PATCH] Add a default context name to RCTJSCExecutor Reviewed By: mmmulani Differential Revision: D4015748 fbshipit-source-id: 077287b8438aa13f2a1479d68eacb0662bf295aa --- React/Executors/RCTJSCExecutor.h | 6 ++++++ React/Executors/RCTJSCExecutor.mm | 23 ++++++++++++----------- ReactCommon/cxxreact/JSCExecutor.cpp | 5 +++++ ReactCommon/cxxreact/JSCExecutor.h | 1 + 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/React/Executors/RCTJSCExecutor.h b/React/Executors/RCTJSCExecutor.h index 8a5567a27..3a7ae96f3 100644 --- a/React/Executors/RCTJSCExecutor.h +++ b/React/Executors/RCTJSCExecutor.h @@ -72,6 +72,12 @@ RCT_EXTERN NSString *const RCTFBJSValueClassKey; */ @property (nonatomic, readonly, assign) BOOL useCustomJSCLibrary; +/** + * Specify a name for the JSContext used, which will be visible in debugging tools + * @default is "RCTJSContext" + */ +@property (nonatomic, copy) NSString *contextName; + /** * Inits a new executor instance with given flag that's used * to initialize RCTJSCWrapper. diff --git a/React/Executors/RCTJSCExecutor.mm b/React/Executors/RCTJSCExecutor.mm index db5cea01d..296474b4b 100644 --- a/React/Executors/RCTJSCExecutor.mm +++ b/React/Executors/RCTJSCExecutor.mm @@ -108,6 +108,7 @@ struct RCTJSContextData { { if ((self = [super init])) { _context = context; + _context.name = @"RCTJSContext"; _javaScriptThread = javaScriptThread; /** @@ -436,7 +437,6 @@ static NSThread *newJavaScriptThread(void) /** Installs synchronous hooks that don't require a weak reference back to the RCTJSCExecutor. */ static void installBasicSynchronousHooksOnContext(JSContext *context) { - context[@"noop"] = ^{}; context[@"nativeLoggingHook"] = ^(NSString *message, NSNumber *logLevel) { RCTLogLevel level = RCTLogLevelInfo; if (logLevel) { @@ -500,11 +500,21 @@ static void installBasicSynchronousHooksOnContext(JSContext *context) _valid = NO; -#if RCT_DEV +#if RCT_PROFILE [[NSNotificationCenter defaultCenter] removeObserver:self]; #endif } +- (NSString *)contextName +{ + return [_context.context name]; +} + +RCT_EXPORT_METHOD(setContextName:(nonnull NSString *)contextName) +{ + [_context.context setName:contextName]; +} + - (void)dealloc { [self invalidate]; @@ -917,15 +927,6 @@ static NSData *loadRAMBundle(NSURL *sourceURL, NSError **error, RandomAccessBund return [NSData dataWithBytesNoCopy:startupCode.code.release() length:startupCode.size freeWhenDone:YES]; } -RCT_EXPORT_METHOD(setContextName:(nonnull NSString *)name) -{ - if (_jscWrapper->JSGlobalContextSetName != NULL) { - JSStringRef JSName = _jscWrapper->JSStringCreateWithCFString((__bridge CFStringRef)name); - _jscWrapper->JSGlobalContextSetName(_context.context.JSGlobalContextRef, JSName); - _jscWrapper->JSStringRelease(JSName); - } -} - @end @implementation RCTJSContextProvider diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index 210ee64e8..15e073b7d 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -200,6 +200,11 @@ void JSCExecutor::destroy() { }); } +void JSCExecutor::setContextName(const std::string& name) { + String jsName = String(name.c_str()); + JSGlobalContextSetName(m_context, static_cast(jsName)); +} + void JSCExecutor::initOnJSVMThread() throw(JSException) { SystraceSection s("JSCExecutor.initOnJSVMThread"); diff --git a/ReactCommon/cxxreact/JSCExecutor.h b/ReactCommon/cxxreact/JSCExecutor.h index ce7d4992f..0462abc71 100644 --- a/ReactCommon/cxxreact/JSCExecutor.h +++ b/ReactCommon/cxxreact/JSCExecutor.h @@ -92,6 +92,7 @@ public: virtual void handleMemoryPressureModerate() override; virtual void handleMemoryPressureCritical() override; virtual void destroy() override; + void setContextName(const std::string& name); private: JSGlobalContextRef m_context;