2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTContextExecutor.h"
|
|
|
|
|
|
|
|
#import <pthread.h>
|
|
|
|
|
|
|
|
#import <JavaScriptCore/JavaScriptCore.h>
|
2015-07-21 02:30:59 +00:00
|
|
|
#import <UIKit/UIDevice.h>
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
2015-04-21 12:26:51 +00:00
|
|
|
#import "RCTDefines.h"
|
2015-08-28 17:11:02 +00:00
|
|
|
#import "RCTDevMenu.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTLog.h"
|
2015-04-20 11:55:05 +00:00
|
|
|
#import "RCTProfile.h"
|
2015-06-19 21:59:42 +00:00
|
|
|
#import "RCTPerformanceLogger.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2015-07-20 16:14:53 +00:00
|
|
|
#ifndef RCT_JSC_PROFILER
|
2015-09-04 17:44:18 +00:00
|
|
|
#if RCT_DEV
|
2015-07-20 16:14:53 +00:00
|
|
|
#define RCT_JSC_PROFILER 1
|
|
|
|
#else
|
|
|
|
#define RCT_JSC_PROFILER 0
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if RCT_JSC_PROFILER
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2015-09-15 09:49:04 +00:00
|
|
|
static NSString * const RCTJSCProfilerEnabledDefaultsKey = @"RCTJSCProfilerEnabled";
|
|
|
|
|
2015-07-20 16:14:53 +00:00
|
|
|
#ifndef RCT_JSC_PROFILER_DYLIB
|
2015-09-04 17:44:18 +00:00
|
|
|
#define RCT_JSC_PROFILER_DYLIB [[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"RCTJSCProfiler.ios%zd", [[[UIDevice currentDevice] systemVersion] integerValue]] ofType:@"dylib" inDirectory:@"RCTJSCProfiler"] UTF8String]
|
2015-07-20 16:14:53 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-04-10 14:28:10 +00:00
|
|
|
@interface RCTJavaScriptContext : NSObject <RCTInvalidating>
|
|
|
|
|
2015-10-19 10:49:43 +00:00
|
|
|
@property (nonatomic, strong, readonly) JSContext *context;
|
2015-04-10 14:28:10 +00:00
|
|
|
@property (nonatomic, assign, readonly) JSGlobalContextRef ctx;
|
|
|
|
|
2015-10-19 10:49:43 +00:00
|
|
|
- (instancetype)initWithJSContext:(JSContext *)context NS_DESIGNATED_INITIALIZER;
|
2015-04-10 14:28:10 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTJavaScriptContext
|
|
|
|
{
|
|
|
|
RCTJavaScriptContext *_self;
|
|
|
|
}
|
|
|
|
|
2015-10-19 10:49:43 +00:00
|
|
|
- (instancetype)initWithJSContext:(JSContext *)context
|
2015-04-10 14:28:10 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-10-19 10:49:43 +00:00
|
|
|
_context = context;
|
2015-04-10 14:28:10 +00:00
|
|
|
_self = self;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(-(instancetype)init)
|
|
|
|
|
2015-10-19 10:49:43 +00:00
|
|
|
- (JSGlobalContextRef)ctx
|
|
|
|
{
|
|
|
|
return _context.JSGlobalContextRef;
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:28:10 +00:00
|
|
|
- (BOOL)isValid
|
|
|
|
{
|
2015-10-19 10:49:43 +00:00
|
|
|
return _context != nil;
|
2015-04-10 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
2015-04-20 12:40:42 +00:00
|
|
|
if (self.isValid) {
|
2015-10-19 10:49:43 +00:00
|
|
|
_context = nil;
|
2015-04-20 12:40:42 +00:00
|
|
|
_self = nil;
|
|
|
|
}
|
2015-04-10 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 10:47:48 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]);
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:28:10 +00:00
|
|
|
@end
|
|
|
|
|
2015-10-19 15:02:50 +00:00
|
|
|
// Private bridge interface to allow middle-batch calls
|
|
|
|
@interface RCTBridge (RCTContextExecutor)
|
|
|
|
|
|
|
|
- (void)handleBuffer:(NSArray *)buffer batchEnded:(BOOL)hasEnded;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTContextExecutor
|
|
|
|
{
|
2015-04-10 14:28:10 +00:00
|
|
|
RCTJavaScriptContext *_context;
|
2015-02-20 04:10:52 +00:00
|
|
|
NSThread *_javaScriptThread;
|
|
|
|
}
|
|
|
|
|
2015-06-12 17:43:22 +00:00
|
|
|
@synthesize valid = _valid;
|
2015-08-28 17:11:02 +00:00
|
|
|
@synthesize bridge = _bridge;
|
2015-06-12 17:43:22 +00:00
|
|
|
|
2015-06-09 22:42:10 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* The one tiny pure native hook that we implement is a native logging hook.
|
|
|
|
* You could even argue that this is not necessary - we could plumb logging
|
|
|
|
* calls through a batched bridge, but having the pure native hook allows
|
|
|
|
* logging to successfully come through even in the event that a batched bridge
|
|
|
|
* crashes.
|
|
|
|
*/
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
static JSValueRef RCTNativeLoggingHook(JSContextRef context, __unused JSObjectRef object, __unused JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
if (argumentCount > 0) {
|
2015-04-25 01:00:20 +00:00
|
|
|
JSStringRef messageRef = JSValueToStringCopy(context, arguments[0], exception);
|
|
|
|
if (!messageRef) {
|
2015-02-20 04:10:52 +00:00
|
|
|
return JSValueMakeUndefined(context);
|
|
|
|
}
|
2015-04-25 01:00:20 +00:00
|
|
|
NSString *message = (__bridge_transfer NSString *)JSStringCopyCFString(kCFAllocatorDefault, messageRef);
|
|
|
|
JSStringRelease(messageRef);
|
2015-02-20 04:10:52 +00:00
|
|
|
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:
|
2015-08-29 13:37:43 +00:00
|
|
|
@"( stack: )?([_a-z0-9]*)@?(http://|file:///)[a-z.0-9:/_-]+/([a-z0-9_]+).bundle(:[0-9]+:[0-9]+)"
|
2015-02-20 04:10:52 +00:00
|
|
|
options:NSRegularExpressionCaseInsensitive
|
2015-04-07 14:36:26 +00:00
|
|
|
error:NULL];
|
|
|
|
message = [regex stringByReplacingMatchesInString:message
|
|
|
|
options:0
|
|
|
|
range:(NSRange){0, message.length}
|
|
|
|
withTemplate:@"[$4$5] \t$2"];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-19 19:55:46 +00:00
|
|
|
RCTLogLevel level = RCTLogLevelInfo;
|
2015-04-25 01:00:20 +00:00
|
|
|
if (argumentCount > 1) {
|
|
|
|
level = MAX(level, JSValueToNumber(context, arguments[1], exception) - 1);
|
2015-04-19 19:55:46 +00:00
|
|
|
}
|
2015-04-25 01:00:20 +00:00
|
|
|
RCTGetLogFunction()(level, nil, nil, message);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return JSValueMakeUndefined(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do-very-little native hook for testing.
|
2015-06-15 14:53:45 +00:00
|
|
|
static JSValueRef RCTNoop(JSContextRef context, __unused JSObjectRef object, __unused JSObjectRef thisObject, __unused size_t argumentCount, __unused const JSValueRef arguments[], __unused JSValueRef *exception)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
static int counter = 0;
|
|
|
|
counter++;
|
|
|
|
return JSValueMakeUndefined(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString *RCTJSValueToNSString(JSContextRef context, JSValueRef value)
|
|
|
|
{
|
|
|
|
JSStringRef JSString = JSValueToStringCopy(context, value, NULL);
|
|
|
|
CFStringRef string = JSStringCopyCFString(kCFAllocatorDefault, JSString);
|
|
|
|
JSStringRelease(JSString);
|
|
|
|
|
|
|
|
return (__bridge_transfer NSString *)string;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSString *RCTJSValueToJSONString(JSContextRef context, JSValueRef value, unsigned indent)
|
|
|
|
{
|
|
|
|
JSStringRef JSString = JSValueCreateJSONString(context, value, indent, NULL);
|
|
|
|
CFStringRef string = JSStringCopyCFString(kCFAllocatorDefault, JSString);
|
|
|
|
JSStringRelease(JSString);
|
|
|
|
|
|
|
|
return (__bridge_transfer NSString *)string;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSError *RCTNSErrorFromJSError(JSContextRef context, JSValueRef jsError)
|
|
|
|
{
|
|
|
|
NSString *errorMessage = jsError ? RCTJSValueToNSString(context, jsError) : @"unknown JS error";
|
|
|
|
NSString *details = jsError ? RCTJSValueToJSONString(context, jsError, 2) : @"no details";
|
|
|
|
return [NSError errorWithDomain:@"JS" code:1 userInfo:@{NSLocalizedDescriptionKey: errorMessage, NSLocalizedFailureReasonErrorKey: details}];
|
|
|
|
}
|
|
|
|
|
2015-08-25 08:31:22 +00:00
|
|
|
#if RCT_DEV
|
|
|
|
|
|
|
|
static JSValueRef RCTNativeTraceBeginSection(JSContextRef context, __unused JSObjectRef object, __unused JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], __unused JSValueRef *exception)
|
|
|
|
{
|
|
|
|
static int profileCounter = 1;
|
|
|
|
NSString *profileName;
|
|
|
|
double tag = 0;
|
|
|
|
|
|
|
|
if (argumentCount > 0) {
|
|
|
|
if (JSValueIsNumber(context, arguments[0])) {
|
|
|
|
tag = JSValueToNumber(context, arguments[0], NULL);
|
|
|
|
} else {
|
|
|
|
profileName = RCTJSValueToNSString(context, arguments[0]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
profileName = [NSString stringWithFormat:@"Profile %d", profileCounter++];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argumentCount > 1 && JSValueIsString(context, arguments[1])) {
|
|
|
|
profileName = RCTJSValueToNSString(context, arguments[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (profileName) {
|
|
|
|
RCTProfileBeginEvent(tag, profileName, nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSValueMakeUndefined(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef RCTNativeTraceEndSection(JSContextRef context, __unused JSObjectRef object, __unused JSObjectRef thisObject, __unused size_t argumentCount, __unused const JSValueRef arguments[], __unused JSValueRef *exception)
|
|
|
|
{
|
|
|
|
if (argumentCount > 0) {
|
|
|
|
JSValueRef *error = NULL;
|
|
|
|
double tag = JSValueToNumber(context, arguments[0], error);
|
|
|
|
|
|
|
|
if (error == NULL) {
|
|
|
|
RCTProfileEndEvent((uint64_t)tag, @"console", nil);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSValueMakeUndefined(context);
|
|
|
|
}
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context)
|
|
|
|
{
|
|
|
|
#if RCT_JSC_PROFILER
|
|
|
|
void *JSCProfiler = dlopen(RCT_JSC_PROFILER_DYLIB, RTLD_NOW);
|
|
|
|
if (JSCProfiler != NULL) {
|
|
|
|
void (*nativeProfilerStart)(JSContextRef, const char *) =
|
|
|
|
(__typeof__(nativeProfilerStart))dlsym(JSCProfiler, "nativeProfilerStart");
|
|
|
|
void (*nativeProfilerEnd)(JSContextRef, const char *, const char *) =
|
|
|
|
(__typeof__(nativeProfilerEnd))dlsym(JSCProfiler, "nativeProfilerEnd");
|
|
|
|
|
|
|
|
if (nativeProfilerStart != NULL && nativeProfilerEnd != NULL) {
|
|
|
|
void (*nativeProfilerEnableByteCode)(void) =
|
|
|
|
(__typeof__(nativeProfilerEnableByteCode))dlsym(JSCProfiler, "nativeProfilerEnableByteCode");
|
|
|
|
|
|
|
|
if (nativeProfilerEnableByteCode != NULL) {
|
|
|
|
nativeProfilerEnableByteCode();
|
|
|
|
}
|
|
|
|
|
2015-10-02 11:50:47 +00:00
|
|
|
static BOOL isProfiling = NO;
|
2015-09-15 09:49:04 +00:00
|
|
|
[bridge.devMenu addItem:[RCTDevMenuItem toggleItemWithKey:RCTJSCProfilerEnabledDefaultsKey title:@"Start Profiling" selectedTitle:@"Stop Profiling" handler:^(BOOL shouldStart) {
|
2015-10-02 11:50:47 +00:00
|
|
|
|
|
|
|
if (shouldStart == isProfiling) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isProfiling = shouldStart;
|
|
|
|
|
2015-09-15 09:49:04 +00:00
|
|
|
if (shouldStart) {
|
|
|
|
nativeProfilerStart(context, "profile");
|
|
|
|
} else {
|
2015-09-10 16:03:03 +00:00
|
|
|
NSString *outputFile = [NSTemporaryDirectory() stringByAppendingPathComponent:@"cpu_profile.json"];
|
|
|
|
nativeProfilerEnd(context, "profile", outputFile.UTF8String);
|
2015-09-11 13:35:25 +00:00
|
|
|
NSData *profileData = [NSData dataWithContentsOfFile:outputFile
|
|
|
|
options:NSDataReadingMappedIfSafe
|
|
|
|
error:NULL];
|
|
|
|
|
|
|
|
RCTProfileSendResult(bridge, @"cpu-profile", profileData);
|
2015-09-10 16:03:03 +00:00
|
|
|
}
|
2015-09-14 16:34:33 +00:00
|
|
|
}]];
|
2015-09-10 16:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-25 08:31:22 +00:00
|
|
|
#endif
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
+ (void)runRunLoopThread
|
|
|
|
{
|
|
|
|
@autoreleasepool {
|
|
|
|
// copy thread name to pthread name
|
2015-08-24 10:14:33 +00:00
|
|
|
pthread_setname_np([NSThread currentThread].name.UTF8String);
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
// Set up a dummy runloop source to avoid spinning
|
|
|
|
CFRunLoopSourceContext noSpinCtx = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
|
|
|
|
CFRunLoopSourceRef noSpinSource = CFRunLoopSourceCreate(NULL, 0, &noSpinCtx);
|
|
|
|
CFRunLoopAddSource(CFRunLoopGetCurrent(), noSpinSource, kCFRunLoopDefaultMode);
|
|
|
|
CFRelease(noSpinSource);
|
|
|
|
|
|
|
|
// run the run loop
|
2015-08-24 10:14:33 +00:00
|
|
|
while (kCFRunLoopRunStopped != CFRunLoopRunInMode(kCFRunLoopDefaultMode, ((NSDate *)[NSDate distantFuture]).timeIntervalSinceReferenceDate, NO)) {
|
2015-03-26 16:44:58 +00:00
|
|
|
RCTAssert(NO, @"not reached assertion"); // runloop spun. that's bad.
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
{
|
2015-04-27 10:47:48 +00:00
|
|
|
NSThread *javaScriptThread = [[NSThread alloc] initWithTarget:[self class]
|
|
|
|
selector:@selector(runRunLoopThread)
|
|
|
|
object:nil];
|
2015-08-24 10:14:33 +00:00
|
|
|
javaScriptThread.name = @"com.facebook.React.JavaScript";
|
|
|
|
javaScriptThread.threadPriority = [NSThread mainThread].threadPriority;
|
2015-04-27 10:47:48 +00:00
|
|
|
[javaScriptThread start];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-10-19 10:49:43 +00:00
|
|
|
return [self initWithJavaScriptThread:javaScriptThread context:nil];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:37:03 +00:00
|
|
|
- (instancetype)initWithJavaScriptThread:(NSThread *)javaScriptThread
|
2015-10-19 10:49:43 +00:00
|
|
|
context:(JSContext *)context
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-27 10:47:48 +00:00
|
|
|
RCTAssert(javaScriptThread != nil,
|
|
|
|
@"Can't initialize RCTContextExecutor without a javaScriptThread");
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
if ((self = [super init])) {
|
2015-06-12 17:43:22 +00:00
|
|
|
_valid = YES;
|
2015-02-20 04:10:52 +00:00
|
|
|
_javaScriptThread = javaScriptThread;
|
2015-04-10 14:28:10 +00:00
|
|
|
__weak RCTContextExecutor *weakSelf = self;
|
2015-02-20 04:10:52 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue: ^{
|
2015-04-10 14:28:10 +00:00
|
|
|
RCTContextExecutor *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
// Assumes that no other JS tasks are scheduled before.
|
|
|
|
if (context) {
|
2015-10-19 10:49:43 +00:00
|
|
|
strongSelf->_context = [[RCTJavaScriptContext alloc] initWithJSContext:context];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-10-19 10:49:43 +00:00
|
|
|
- (instancetype)initWithJavaScriptThread:(NSThread *)javaScriptThread
|
|
|
|
globalContextRef:(JSGlobalContextRef)contextRef
|
|
|
|
{
|
|
|
|
JSContext *context = contextRef ? [JSContext contextWithJSGlobalContextRef:contextRef] : nil;
|
|
|
|
return [self initWithJavaScriptThread:javaScriptThread context:context];
|
|
|
|
}
|
|
|
|
|
2015-06-09 22:42:10 +00:00
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
__weak RCTContextExecutor *weakSelf = self;
|
|
|
|
[self executeBlockOnJavaScriptQueue:^{
|
|
|
|
RCTContextExecutor *strongSelf = weakSelf;
|
2015-06-12 17:43:22 +00:00
|
|
|
if (!strongSelf.isValid) {
|
2015-06-09 22:42:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strongSelf->_context) {
|
2015-10-19 10:49:43 +00:00
|
|
|
JSContext *context = [[JSContext alloc] init];
|
|
|
|
strongSelf->_context = [[RCTJavaScriptContext alloc] initWithJSContext:context];
|
2015-06-09 22:42:10 +00:00
|
|
|
}
|
|
|
|
[strongSelf _addNativeHook:RCTNativeLoggingHook withName:"nativeLoggingHook"];
|
|
|
|
[strongSelf _addNativeHook:RCTNoop withName:"noop"];
|
2015-10-19 15:02:50 +00:00
|
|
|
|
|
|
|
__weak RCTBridge *bridge = strongSelf->_bridge;
|
|
|
|
strongSelf->_context.context[@"nativeFlushQueueImmediate"] = ^(NSArray *calls){
|
|
|
|
if (!weakSelf.valid || !calls) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[bridge handleBuffer:calls batchEnded:NO];
|
|
|
|
};
|
|
|
|
|
2015-06-09 22:42:10 +00:00
|
|
|
#if RCT_DEV
|
2015-08-25 08:31:22 +00:00
|
|
|
[strongSelf _addNativeHook:RCTNativeTraceBeginSection withName:"nativeTraceBeginSection"];
|
|
|
|
[strongSelf _addNativeHook:RCTNativeTraceEndSection withName:"nativeTraceEndSection"];
|
2015-06-09 22:42:10 +00:00
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
RCTInstallJSCProfiler(_bridge, strongSelf->_context.ctx);
|
2015-07-20 16:14:53 +00:00
|
|
|
|
2015-06-09 22:42:10 +00:00
|
|
|
for (NSString *event in @[RCTProfileDidStartProfiling, RCTProfileDidEndProfiling]) {
|
2015-06-12 17:43:22 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:strongSelf
|
2015-06-09 22:42:10 +00:00
|
|
|
selector:@selector(toggleProfilingFlag:)
|
|
|
|
name:event
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-06-02 13:15:53 +00:00
|
|
|
- (void)toggleProfilingFlag:(NSNotification *)notification
|
|
|
|
{
|
2015-10-01 21:08:13 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:^{
|
|
|
|
BOOL enabled = [notification.name isEqualToString:RCTProfileDidStartProfiling];
|
|
|
|
NSString *script = [NSString stringWithFormat:@"var p = require('BridgeProfiling') || {}; p.setEnabled && p.setEnabled(%@)", enabled ? @"true" : @"false"];
|
|
|
|
JSStringRef scriptJSRef = JSStringCreateWithUTF8CString(script.UTF8String);
|
|
|
|
JSEvaluateScript(_context.ctx, scriptJSRef, NULL, NULL, 0, NULL);
|
|
|
|
JSStringRelease(scriptJSRef);
|
|
|
|
}];
|
2015-06-02 13:15:53 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (void)_addNativeHook:(JSObjectCallAsFunctionCallback)hook withName:(const char *)name
|
|
|
|
{
|
2015-04-10 14:28:10 +00:00
|
|
|
JSObjectRef globalObject = JSContextGetGlobalObject(_context.ctx);
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
JSStringRef JSName = JSStringCreateWithUTF8CString(name);
|
2015-04-10 14:28:10 +00:00
|
|
|
JSObjectSetProperty(_context.ctx, globalObject, JSName, JSObjectMakeFunctionWithCallback(_context.ctx, JSName, hook), kJSPropertyAttributeNone, NULL);
|
2015-02-20 04:10:52 +00:00
|
|
|
JSStringRelease(JSName);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
2015-06-12 17:43:22 +00:00
|
|
|
if (!self.isValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_valid = NO;
|
|
|
|
|
2015-06-02 13:15:53 +00:00
|
|
|
#if RCT_DEV
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
#endif
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
[_context performSelector:@selector(invalidate)
|
|
|
|
onThread:_javaScriptThread
|
|
|
|
withObject:nil
|
|
|
|
waitUntilDone:NO];
|
2015-07-20 09:34:11 +00:00
|
|
|
_context = nil;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2015-04-16 15:42:15 +00:00
|
|
|
[self invalidate];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)executeJSCall:(NSString *)name
|
|
|
|
method:(NSString *)method
|
|
|
|
arguments:(NSArray *)arguments
|
|
|
|
callback:(RCTJavaScriptCallback)onComplete
|
|
|
|
{
|
|
|
|
RCTAssert(onComplete != nil, @"onComplete block should not be nil");
|
2015-04-10 14:28:10 +00:00
|
|
|
__weak RCTContextExecutor *weakSelf = self;
|
2015-04-20 11:55:05 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:RCTProfileBlock((^{
|
2015-04-10 14:28:10 +00:00
|
|
|
RCTContextExecutor *strongSelf = weakSelf;
|
2015-07-14 23:16:21 +00:00
|
|
|
if (!strongSelf || !strongSelf.isValid) {
|
2015-04-10 14:28:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
NSError *error;
|
2015-05-23 02:33:21 +00:00
|
|
|
NSString *argsString = (arguments.count == 1) ? RCTJSONStringify(arguments[0], &error) : RCTJSONStringify(arguments, &error);
|
2015-02-20 04:10:52 +00:00
|
|
|
if (!argsString) {
|
|
|
|
RCTLogError(@"Cannot convert argument to string: %@", error);
|
|
|
|
onComplete(nil, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-23 02:33:21 +00:00
|
|
|
JSValueRef errorJSRef = NULL;
|
|
|
|
JSValueRef resultJSRef = NULL;
|
|
|
|
JSGlobalContextRef contextJSRef = JSContextGetGlobalContext(strongSelf->_context.ctx);
|
|
|
|
JSObjectRef globalObjectJSRef = JSContextGetGlobalObject(strongSelf->_context.ctx);
|
|
|
|
|
|
|
|
// get require
|
|
|
|
JSStringRef requireNameJSStringRef = JSStringCreateWithUTF8CString("require");
|
|
|
|
JSValueRef requireJSRef = JSObjectGetProperty(contextJSRef, globalObjectJSRef, requireNameJSStringRef, &errorJSRef);
|
|
|
|
JSStringRelease(requireNameJSStringRef);
|
|
|
|
|
2015-05-30 20:21:42 +00:00
|
|
|
if (requireJSRef != NULL && !JSValueIsUndefined(contextJSRef, requireJSRef) && errorJSRef == NULL) {
|
2015-05-23 02:33:21 +00:00
|
|
|
|
|
|
|
// get module
|
|
|
|
JSStringRef moduleNameJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)name);
|
|
|
|
JSValueRef moduleNameJSRef = JSValueMakeString(contextJSRef, moduleNameJSStringRef);
|
|
|
|
JSValueRef moduleJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)requireJSRef, NULL, 1, (const JSValueRef *)&moduleNameJSRef, &errorJSRef);
|
|
|
|
JSStringRelease(moduleNameJSStringRef);
|
|
|
|
|
2015-06-15 20:01:39 +00:00
|
|
|
if (moduleJSRef != NULL && errorJSRef == NULL && !JSValueIsUndefined(contextJSRef, moduleJSRef)) {
|
2015-05-23 02:33:21 +00:00
|
|
|
|
|
|
|
// get method
|
|
|
|
JSStringRef methodNameJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)method);
|
|
|
|
JSValueRef methodJSRef = JSObjectGetProperty(contextJSRef, (JSObjectRef)moduleJSRef, methodNameJSStringRef, &errorJSRef);
|
|
|
|
JSStringRelease(methodNameJSStringRef);
|
|
|
|
|
|
|
|
if (methodJSRef != NULL && errorJSRef == NULL) {
|
|
|
|
|
|
|
|
// direct method invoke with no arguments
|
|
|
|
if (arguments.count == 0) {
|
|
|
|
resultJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)methodJSRef, (JSObjectRef)moduleJSRef, 0, NULL, &errorJSRef);
|
|
|
|
}
|
2015-05-27 15:21:14 +00:00
|
|
|
|
2015-05-23 02:33:21 +00:00
|
|
|
// direct method invoke with 1 argument
|
|
|
|
else if(arguments.count == 1) {
|
|
|
|
JSStringRef argsJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)argsString);
|
|
|
|
JSValueRef argsJSRef = JSValueMakeFromJSONString(contextJSRef, argsJSStringRef);
|
|
|
|
resultJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)methodJSRef, (JSObjectRef)moduleJSRef, 1, &argsJSRef, &errorJSRef);
|
|
|
|
JSStringRelease(argsJSStringRef);
|
2015-05-27 15:21:14 +00:00
|
|
|
|
2015-05-23 02:33:21 +00:00
|
|
|
} else {
|
|
|
|
// apply invoke with array of arguments
|
|
|
|
JSStringRef applyNameJSStringRef = JSStringCreateWithUTF8CString("apply");
|
|
|
|
JSValueRef applyJSRef = JSObjectGetProperty(contextJSRef, (JSObjectRef)methodJSRef, applyNameJSStringRef, &errorJSRef);
|
|
|
|
JSStringRelease(applyNameJSStringRef);
|
|
|
|
|
|
|
|
if (applyJSRef != NULL && errorJSRef == NULL) {
|
|
|
|
// invoke apply
|
|
|
|
JSStringRef argsJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)argsString);
|
|
|
|
JSValueRef argsJSRef = JSValueMakeFromJSONString(contextJSRef, argsJSStringRef);
|
|
|
|
|
|
|
|
JSValueRef args[2];
|
|
|
|
args[0] = JSValueMakeNull(contextJSRef);
|
|
|
|
args[1] = argsJSRef;
|
|
|
|
|
|
|
|
resultJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)applyJSRef, (JSObjectRef)methodJSRef, 2, args, &errorJSRef);
|
|
|
|
JSStringRelease(argsJSStringRef);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-10-13 10:22:40 +00:00
|
|
|
if (errorJSRef) {
|
2015-05-23 02:33:21 +00:00
|
|
|
onComplete(nil, RCTNSErrorFromJSError(contextJSRef, errorJSRef));
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looks like making lots of JSC API calls is slower than communicating by using a JSON
|
|
|
|
// string. Also it ensures that data stuctures don't have cycles and non-serializable fields.
|
|
|
|
// see [RCTContextExecutorTests testDeserializationPerf]
|
|
|
|
id objcValue;
|
|
|
|
// We often return `null` from JS when there is nothing for native side. JSONKit takes an extra hundred microseconds
|
|
|
|
// to handle this simple case, so we are adding a shortcut to make executeJSCall method even faster
|
2015-05-23 02:33:21 +00:00
|
|
|
if (!JSValueIsNull(contextJSRef, resultJSRef)) {
|
|
|
|
JSStringRef jsJSONString = JSValueCreateJSONString(contextJSRef, resultJSRef, 0, nil);
|
2015-02-20 04:10:52 +00:00
|
|
|
if (jsJSONString) {
|
|
|
|
NSString *objcJSONString = (__bridge_transfer NSString *)JSStringCopyCFString(kCFAllocatorDefault, jsJSONString);
|
|
|
|
JSStringRelease(jsJSONString);
|
|
|
|
|
|
|
|
objcValue = RCTJSONParse(objcJSONString, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onComplete(objcValue, nil);
|
2015-08-20 06:36:11 +00:00
|
|
|
}), 0, @"js_call", (@{@"module":name, @"method": method, @"args": arguments}))];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 15:10:25 +00:00
|
|
|
- (void)executeApplicationScript:(NSData *)script
|
2015-04-19 19:55:46 +00:00
|
|
|
sourceURL:(NSURL *)sourceURL
|
2015-02-20 04:10:52 +00:00
|
|
|
onComplete:(RCTJavaScriptCompleteBlock)onComplete
|
|
|
|
{
|
2015-08-07 13:42:34 +00:00
|
|
|
RCTAssertParam(script);
|
|
|
|
RCTAssertParam(sourceURL);
|
2015-04-19 19:55:46 +00:00
|
|
|
|
2015-04-10 14:28:10 +00:00
|
|
|
__weak RCTContextExecutor *weakSelf = self;
|
2015-04-20 11:55:05 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:RCTProfileBlock((^{
|
2015-04-10 14:28:10 +00:00
|
|
|
RCTContextExecutor *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf || !strongSelf.isValid) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-21 18:33:04 +00:00
|
|
|
|
2015-10-22 13:39:55 +00:00
|
|
|
RCTPerformanceLoggerStart(RCTPLScriptExecution);
|
|
|
|
|
2015-10-16 15:10:25 +00:00
|
|
|
// JSStringCreateWithUTF8CString expects a null terminated C string
|
|
|
|
NSMutableData *nullTerminatedScript = [NSMutableData dataWithCapacity:script.length + 1];
|
|
|
|
|
|
|
|
[nullTerminatedScript appendData:script];
|
|
|
|
[nullTerminatedScript appendBytes:"" length:1];
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
JSValueRef jsError = NULL;
|
2015-10-16 15:10:25 +00:00
|
|
|
JSStringRef execJSString = JSStringCreateWithUTF8CString(nullTerminatedScript.bytes);
|
2015-04-19 19:55:46 +00:00
|
|
|
JSStringRef jsURL = JSStringCreateWithCFString((__bridge CFStringRef)sourceURL.absoluteString);
|
|
|
|
JSValueRef result = JSEvaluateScript(strongSelf->_context.ctx, execJSString, NULL, jsURL, 0, &jsError);
|
|
|
|
JSStringRelease(jsURL);
|
2015-02-20 04:10:52 +00:00
|
|
|
JSStringRelease(execJSString);
|
2015-08-21 18:33:04 +00:00
|
|
|
RCTPerformanceLoggerEnd(RCTPLScriptExecution);
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-19 19:55:46 +00:00
|
|
|
if (onComplete) {
|
|
|
|
NSError *error;
|
|
|
|
if (!result) {
|
|
|
|
error = RCTNSErrorFromJSError(strongSelf->_context.ctx, jsError);
|
|
|
|
}
|
|
|
|
onComplete(error);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-08-20 06:36:11 +00:00
|
|
|
}), 0, @"js_call", (@{ @"url": sourceURL.absoluteString }))];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)executeBlockOnJavaScriptQueue:(dispatch_block_t)block
|
|
|
|
{
|
2015-05-28 20:16:06 +00:00
|
|
|
if ([NSThread currentThread] != _javaScriptThread) {
|
|
|
|
[self performSelector:@selector(executeBlockOnJavaScriptQueue:)
|
|
|
|
onThread:_javaScriptThread withObject:block waitUntilDone:NO];
|
|
|
|
} else {
|
|
|
|
block();
|
|
|
|
}
|
2015-04-28 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)executeAsyncBlockOnJavaScriptQueue:(dispatch_block_t)block
|
|
|
|
{
|
|
|
|
[self performSelector:@selector(executeBlockOnJavaScriptQueue:)
|
|
|
|
onThread:_javaScriptThread
|
|
|
|
withObject:block
|
|
|
|
waitUntilDone:NO];
|
2015-04-26 02:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_runBlock:(dispatch_block_t)block
|
|
|
|
{
|
|
|
|
block();
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)injectJSONText:(NSString *)script
|
|
|
|
asGlobalObjectNamed:(NSString *)objectName
|
|
|
|
callback:(RCTJavaScriptCompleteBlock)onComplete
|
|
|
|
{
|
2015-04-21 12:26:51 +00:00
|
|
|
if (RCT_DEBUG) {
|
|
|
|
RCTAssert(RCTJSONParse(script, NULL) != nil, @"%@ wasn't valid JSON!", script);
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-10 14:28:10 +00:00
|
|
|
__weak RCTContextExecutor *weakSelf = self;
|
2015-04-20 11:55:05 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:RCTProfileBlock((^{
|
2015-04-10 14:28:10 +00:00
|
|
|
RCTContextExecutor *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf || !strongSelf.isValid) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
JSStringRef execJSString = JSStringCreateWithCFString((__bridge CFStringRef)script);
|
2015-04-10 14:28:10 +00:00
|
|
|
JSValueRef valueToInject = JSValueMakeFromJSONString(strongSelf->_context.ctx, execJSString);
|
2015-02-20 04:10:52 +00:00
|
|
|
JSStringRelease(execJSString);
|
|
|
|
|
|
|
|
if (!valueToInject) {
|
|
|
|
NSString *errorDesc = [NSString stringWithFormat:@"Can't make JSON value from script '%@'", script];
|
|
|
|
RCTLogError(@"%@", errorDesc);
|
|
|
|
|
2015-04-19 19:55:46 +00:00
|
|
|
if (onComplete) {
|
|
|
|
NSError *error = [NSError errorWithDomain:@"JS" code:2 userInfo:@{NSLocalizedDescriptionKey: errorDesc}];
|
|
|
|
onComplete(error);
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-10 14:28:10 +00:00
|
|
|
JSObjectRef globalObject = JSContextGetGlobalObject(strongSelf->_context.ctx);
|
2015-02-20 04:10:52 +00:00
|
|
|
JSStringRef JSName = JSStringCreateWithCFString((__bridge CFStringRef)objectName);
|
2015-04-10 14:28:10 +00:00
|
|
|
JSObjectSetProperty(strongSelf->_context.ctx, globalObject, JSName, valueToInject, kJSPropertyAttributeNone, NULL);
|
2015-02-20 04:10:52 +00:00
|
|
|
JSStringRelease(JSName);
|
2015-04-19 19:55:46 +00:00
|
|
|
if (onComplete) {
|
|
|
|
onComplete(nil);
|
|
|
|
}
|
2015-08-20 06:36:11 +00:00
|
|
|
}), 0, @"js_call,json_call", (@{@"objectName": objectName}))];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 20:18:08 +00:00
|
|
|
RCT_EXPORT_METHOD(setContextName:(nonnull NSString *)name)
|
2015-08-04 13:05:31 +00:00
|
|
|
{
|
2015-09-24 09:47:43 +00:00
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wtautological-pointer-compare"
|
2015-08-04 13:05:31 +00:00
|
|
|
if (JSGlobalContextSetName != NULL) {
|
2015-09-24 09:47:43 +00:00
|
|
|
#pragma clang diagnostic pop
|
2015-08-04 13:05:31 +00:00
|
|
|
JSStringRef JSName = JSStringCreateWithCFString((__bridge CFStringRef)name);
|
|
|
|
JSGlobalContextSetName(_context.ctx, JSName);
|
|
|
|
JSStringRelease(JSName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|