Add a default context name to RCTJSCExecutor

Reviewed By: mmmulani

Differential Revision: D4015748

fbshipit-source-id: 077287b8438aa13f2a1479d68eacb0662bf295aa
This commit is contained in:
Pieter De Baets 2016-10-14 11:29:47 -07:00 committed by Facebook Github Bot
parent 82d6ac1a51
commit 8355ef91ad
4 changed files with 24 additions and 11 deletions

View File

@ -72,6 +72,12 @@ RCT_EXTERN NSString *const RCTFBJSValueClassKey;
*/ */
@property (nonatomic, readonly, assign) BOOL useCustomJSCLibrary; @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 * Inits a new executor instance with given flag that's used
* to initialize RCTJSCWrapper. * to initialize RCTJSCWrapper.

View File

@ -108,6 +108,7 @@ struct RCTJSContextData {
{ {
if ((self = [super init])) { if ((self = [super init])) {
_context = context; _context = context;
_context.name = @"RCTJSContext";
_javaScriptThread = javaScriptThread; _javaScriptThread = javaScriptThread;
/** /**
@ -436,7 +437,6 @@ static NSThread *newJavaScriptThread(void)
/** Installs synchronous hooks that don't require a weak reference back to the RCTJSCExecutor. */ /** Installs synchronous hooks that don't require a weak reference back to the RCTJSCExecutor. */
static void installBasicSynchronousHooksOnContext(JSContext *context) static void installBasicSynchronousHooksOnContext(JSContext *context)
{ {
context[@"noop"] = ^{};
context[@"nativeLoggingHook"] = ^(NSString *message, NSNumber *logLevel) { context[@"nativeLoggingHook"] = ^(NSString *message, NSNumber *logLevel) {
RCTLogLevel level = RCTLogLevelInfo; RCTLogLevel level = RCTLogLevelInfo;
if (logLevel) { if (logLevel) {
@ -500,11 +500,21 @@ static void installBasicSynchronousHooksOnContext(JSContext *context)
_valid = NO; _valid = NO;
#if RCT_DEV #if RCT_PROFILE
[[NSNotificationCenter defaultCenter] removeObserver:self]; [[NSNotificationCenter defaultCenter] removeObserver:self];
#endif #endif
} }
- (NSString *)contextName
{
return [_context.context name];
}
RCT_EXPORT_METHOD(setContextName:(nonnull NSString *)contextName)
{
[_context.context setName:contextName];
}
- (void)dealloc - (void)dealloc
{ {
[self invalidate]; [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]; 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 @end
@implementation RCTJSContextProvider @implementation RCTJSContextProvider

View File

@ -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<JSStringRef>(jsName));
}
void JSCExecutor::initOnJSVMThread() throw(JSException) { void JSCExecutor::initOnJSVMThread() throw(JSException) {
SystraceSection s("JSCExecutor.initOnJSVMThread"); SystraceSection s("JSCExecutor.initOnJSVMThread");

View File

@ -92,6 +92,7 @@ public:
virtual void handleMemoryPressureModerate() override; virtual void handleMemoryPressureModerate() override;
virtual void handleMemoryPressureCritical() override; virtual void handleMemoryPressureCritical() override;
virtual void destroy() override; virtual void destroy() override;
void setContextName(const std::string& name);
private: private:
JSGlobalContextRef m_context; JSGlobalContextRef m_context;