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
|
|
|
|
2015-12-16 10:49:27 +00:00
|
|
|
#import "RCTJSCExecutor.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#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-12-15 13:39:30 +00:00
|
|
|
#import "RCTBridge+Private.h"
|
2015-04-21 12:26:51 +00:00
|
|
|
#import "RCTDefines.h"
|
2015-08-28 17:11:02 +00:00
|
|
|
#import "RCTDevMenu.h"
|
2016-03-17 17:34:46 +00:00
|
|
|
#import "RCTJavaScriptLoader.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-12-15 11:11:30 +00:00
|
|
|
#import "RCTJSCProfiler.h"
|
2016-01-27 22:55:02 +00:00
|
|
|
#import "RCTRedBox.h"
|
|
|
|
#import "RCTSourceCode.h"
|
2015-07-20 16:14:53 +00:00
|
|
|
|
2016-02-12 11:49:51 +00:00
|
|
|
NSString *const RCTJSCThreadName = @"com.facebook.React.JavaScript";
|
|
|
|
|
2016-02-15 20:57:21 +00:00
|
|
|
NSString *const RCTJavaScriptContextCreatedNotification = @"RCTJavaScriptContextCreatedNotification";
|
|
|
|
|
2015-11-05 20:20:15 +00:00
|
|
|
static NSString *const RCTJSCProfilerEnabledDefaultsKey = @"RCTJSCProfilerEnabled";
|
2015-09-15 09:49:04 +00:00
|
|
|
|
2016-03-21 10:20:49 +00:00
|
|
|
typedef struct ModuleData {
|
2016-03-13 18:13:39 +00:00
|
|
|
uint32_t offset;
|
2016-03-17 17:34:46 +00:00
|
|
|
uint32_t length;
|
2016-03-13 18:13:40 +00:00
|
|
|
uint32_t lineNo;
|
2016-03-13 18:13:39 +00:00
|
|
|
} ModuleData;
|
|
|
|
|
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
|
|
|
|
{
|
2015-12-09 12:37:40 +00:00
|
|
|
RCTJavaScriptContext *_selfReference;
|
2015-04-10 14:28:10 +00:00
|
|
|
}
|
|
|
|
|
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-12-09 12:37:40 +00:00
|
|
|
|
|
|
|
/**
|
2015-12-16 10:49:27 +00:00
|
|
|
* Explicitly introduce a retain cycle here - The RCTJSCExecutor might
|
2015-12-09 12:37:40 +00:00
|
|
|
* be deallocated while there's still work enqueued in the JS thread, so
|
|
|
|
* we wouldn't be able kill the JSContext. Instead we create this retain
|
|
|
|
* cycle, and enqueue the -invalidate message in this object, it then
|
|
|
|
* releases the JSContext, breaks the cycle and stops the runloop.
|
|
|
|
*/
|
|
|
|
_selfReference = self;
|
2015-04-10 14:28:10 +00:00
|
|
|
}
|
|
|
|
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-12-09 12:37:40 +00:00
|
|
|
_selfReference = nil;
|
2015-04-20 12:40:42 +00:00
|
|
|
}
|
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-12-16 10:49:27 +00:00
|
|
|
@implementation RCTJSCExecutor
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-10 14:28:10 +00:00
|
|
|
RCTJavaScriptContext *_context;
|
2015-02-20 04:10:52 +00:00
|
|
|
NSThread *_javaScriptThread;
|
2016-03-13 18:13:39 +00:00
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
FILE *_bundle;
|
2016-03-13 18:13:39 +00:00
|
|
|
JSStringRef _bundleURL;
|
|
|
|
CFMutableDictionaryRef _jsModules;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
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-11-05 20:20:15 +00:00
|
|
|
static NSString *RCTJSValueToNSString(JSContextRef context, JSValueRef value, JSValueRef *exception)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-11-05 20:20:15 +00:00
|
|
|
JSStringRef JSString = JSValueToStringCopy(context, value, exception);
|
|
|
|
if (!JSString) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
CFStringRef string = JSStringCopyCFString(kCFAllocatorDefault, JSString);
|
|
|
|
JSStringRelease(JSString);
|
|
|
|
|
|
|
|
return (__bridge_transfer NSString *)string;
|
|
|
|
}
|
|
|
|
|
2015-11-05 20:20:15 +00:00
|
|
|
static NSString *RCTJSValueToJSONString(JSContextRef context, JSValueRef value, JSValueRef *exception, unsigned indent)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-11-05 20:20:15 +00:00
|
|
|
JSStringRef JSString = JSValueCreateJSONString(context, value, indent, exception);
|
2015-02-20 04:10:52 +00:00
|
|
|
CFStringRef string = JSStringCopyCFString(kCFAllocatorDefault, JSString);
|
|
|
|
JSStringRelease(JSString);
|
|
|
|
|
|
|
|
return (__bridge_transfer NSString *)string;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NSError *RCTNSErrorFromJSError(JSContextRef context, JSValueRef jsError)
|
|
|
|
{
|
2015-12-18 01:26:12 +00:00
|
|
|
NSString *errorMessage = jsError ? RCTJSValueToNSString(context, jsError, NULL) : @"Unknown JS error";
|
|
|
|
NSString *details = jsError ? RCTJSValueToJSONString(context, jsError, NULL, 2) : @"No details";
|
2015-02-20 04:10:52 +00:00
|
|
|
return [NSError errorWithDomain:@"JS" code:1 userInfo:@{NSLocalizedDescriptionKey: errorMessage, NSLocalizedFailureReasonErrorKey: details}];
|
|
|
|
}
|
|
|
|
|
2015-08-25 08:31:22 +00:00
|
|
|
#if RCT_DEV
|
|
|
|
|
2015-09-10 16:03:03 +00:00
|
|
|
static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context)
|
|
|
|
{
|
2015-12-15 11:11:30 +00:00
|
|
|
if (RCTJSCProfilerIsSupported()) {
|
|
|
|
[bridge.devMenu addItem:[RCTDevMenuItem toggleItemWithKey:RCTJSCProfilerEnabledDefaultsKey title:@"Start Profiling" selectedTitle:@"Stop Profiling" handler:^(BOOL shouldStart) {
|
|
|
|
if (shouldStart != RCTJSCProfilerIsProfiling(context)) {
|
2015-09-15 09:49:04 +00:00
|
|
|
if (shouldStart) {
|
2015-12-15 11:11:30 +00:00
|
|
|
RCTJSCProfilerStart(context);
|
2015-09-15 09:49:04 +00:00
|
|
|
} else {
|
2015-12-15 11:11:30 +00:00
|
|
|
NSString *outputFile = RCTJSCProfilerStop(context);
|
|
|
|
NSData *profileData = [NSData dataWithContentsOfFile:outputFile options:NSDataReadingMappedIfSafe error:NULL];
|
2015-09-11 13:35:25 +00:00
|
|
|
RCTProfileSendResult(bridge, @"cpu-profile", profileData);
|
2015-09-10 16:03:03 +00:00
|
|
|
}
|
2015-12-15 11:11:30 +00:00
|
|
|
}
|
|
|
|
}]];
|
2015-09-10 16:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2016-03-16 17:22:38 +00:00
|
|
|
if (self = [super init]) {
|
|
|
|
_valid = YES;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-03-16 17:22:38 +00:00
|
|
|
_javaScriptThread = [[NSThread alloc] initWithTarget:[self class]
|
|
|
|
selector:@selector(runRunLoopThread)
|
|
|
|
object:nil];
|
|
|
|
_javaScriptThread.name = @"com.facebook.React.JavaScript";
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-03-16 17:22:38 +00:00
|
|
|
if ([_javaScriptThread respondsToSelector:@selector(setQualityOfService:)]) {
|
|
|
|
[_javaScriptThread setQualityOfService:NSOperationQualityOfServiceUserInteractive];
|
|
|
|
} else {
|
|
|
|
_javaScriptThread.threadPriority = [NSThread mainThread].threadPriority;
|
|
|
|
}
|
2015-04-27 10:47:48 +00:00
|
|
|
|
2016-03-16 17:22:38 +00:00
|
|
|
[_javaScriptThread start];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
- (RCTJavaScriptContext *)context
|
|
|
|
{
|
|
|
|
RCTAssertThread(_javaScriptThread, @"Must be called on JS thread.");
|
|
|
|
|
|
|
|
if (!self.isValid) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_context) {
|
|
|
|
JSContext *context = [JSContext new];
|
|
|
|
_context = [[RCTJavaScriptContext alloc] initWithJSContext:context];
|
|
|
|
}
|
|
|
|
|
|
|
|
return _context;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addSynchronousHookWithName:(NSString *)name usingBlock:(id)block
|
2015-06-09 22:42:10 +00:00
|
|
|
{
|
2015-12-16 10:49:27 +00:00
|
|
|
__weak RCTJSCExecutor *weakSelf = self;
|
2015-06-09 22:42:10 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:^{
|
2016-01-05 15:59:54 +00:00
|
|
|
weakSelf.context.context[name] = block;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUp
|
|
|
|
{
|
|
|
|
__weak RCTJSCExecutor *weakSelf = self;
|
|
|
|
[self addSynchronousHookWithName:@"noop" usingBlock:^{}];
|
2015-12-15 13:32:24 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativeLoggingHook" usingBlock:^(NSString *message, NSNumber *logLevel) {
|
|
|
|
RCTLogLevel level = RCTLogLevelInfo;
|
|
|
|
if (logLevel) {
|
|
|
|
level = MAX(level, logLevel.integerValue);
|
2015-06-09 22:42:10 +00:00
|
|
|
}
|
2015-10-19 15:02:50 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
_RCTLogJavaScriptInternal(level, message);
|
|
|
|
}];
|
2015-12-15 13:32:24 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativeRequireModuleConfig" usingBlock:^NSString *(NSString *moduleName) {
|
|
|
|
RCTJSCExecutor *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf.valid) {
|
|
|
|
return nil;
|
|
|
|
}
|
2015-12-15 13:32:24 +00:00
|
|
|
|
2016-02-03 15:10:52 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(0, @"nativeRequireModuleConfig", nil);
|
2016-01-05 15:59:54 +00:00
|
|
|
NSArray *config = [strongSelf->_bridge configForModuleName:moduleName];
|
2016-02-03 15:10:52 +00:00
|
|
|
NSString *result = config ? RCTJSONStringify(config, NULL) : nil;
|
|
|
|
RCT_PROFILE_END_EVENT(0, @"js_call,config", @{ @"moduleName": moduleName });
|
|
|
|
return result;
|
2016-01-05 15:59:54 +00:00
|
|
|
}];
|
2015-12-10 12:27:45 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativeFlushQueueImmediate" usingBlock:^(NSArray<NSArray *> *calls){
|
|
|
|
RCTJSCExecutor *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf.valid || !calls) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-10 12:27:45 +00:00
|
|
|
|
2016-02-03 15:10:52 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(0, @"nativeFlushQueueImmediate", nil);
|
2016-01-05 15:59:54 +00:00
|
|
|
[strongSelf->_bridge handleBuffer:calls batchEnded:NO];
|
2016-02-03 15:10:52 +00:00
|
|
|
RCT_PROFILE_END_EVENT(0, @"js_call", nil);
|
2016-01-05 15:59:54 +00:00
|
|
|
}];
|
2015-10-19 15:02:50 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativePerformanceNow" usingBlock:^{
|
|
|
|
return @(CACurrentMediaTime() * 1000);
|
|
|
|
}];
|
2015-11-02 16:13:42 +00:00
|
|
|
|
2015-06-09 22:42:10 +00:00
|
|
|
#if RCT_DEV
|
2016-01-05 15:59:54 +00:00
|
|
|
if (RCTProfileIsProfiling()) {
|
|
|
|
// Cheating, since it's not a "hook", but meh
|
|
|
|
[self addSynchronousHookWithName:@"__RCTProfileIsProfiling" usingBlock:@YES];
|
|
|
|
}
|
2015-12-10 12:27:45 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
CFMutableDictionaryRef cookieMap = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
|
|
|
|
[self addSynchronousHookWithName:@"nativeTraceBeginAsyncSection" usingBlock:^(uint64_t tag, NSString *name, NSUInteger cookie) {
|
|
|
|
NSUInteger newCookie = RCTProfileBeginAsyncEvent(tag, name, nil);
|
|
|
|
CFDictionarySetValue(cookieMap, (const void *)cookie, (const void *)newCookie);
|
|
|
|
}];
|
2015-11-02 13:30:16 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativeTraceEndAsyncSection" usingBlock:^(uint64_t tag, NSString *name, NSUInteger cookie) {
|
|
|
|
NSUInteger newCookie = (NSUInteger)CFDictionaryGetValue(cookieMap, (const void *)cookie);
|
2016-02-03 15:10:52 +00:00
|
|
|
RCTProfileEndAsyncEvent(tag, @"js,async", newCookie, name, @"JS async", nil);
|
2016-01-05 15:59:54 +00:00
|
|
|
CFDictionaryRemoveValue(cookieMap, (const void *)cookie);
|
|
|
|
}];
|
2015-12-15 13:32:24 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativeTraceBeginSection" usingBlock:^(NSNumber *tag, NSString *profileName){
|
|
|
|
static int profileCounter = 1;
|
|
|
|
if (!profileName) {
|
|
|
|
profileName = [NSString stringWithFormat:@"Profile %d", profileCounter++];
|
|
|
|
}
|
2015-12-15 13:32:24 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
RCT_PROFILE_BEGIN_EVENT(tag.longLongValue, profileName, nil);
|
|
|
|
}];
|
2015-06-09 22:42:10 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativeTraceEndSection" usingBlock:^(NSNumber *tag) {
|
|
|
|
RCT_PROFILE_END_EVENT(tag.longLongValue, @"console", nil);
|
|
|
|
}];
|
2015-12-29 00:43:22 +00:00
|
|
|
|
2016-02-08 15:06:52 +00:00
|
|
|
__weak RCTBridge *weakBridge = _bridge;
|
|
|
|
#ifndef __clang_analyzer__
|
2016-03-30 15:07:23 +00:00
|
|
|
_bridge.flowIDMap = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
|
2016-02-08 15:06:52 +00:00
|
|
|
#endif
|
2016-03-30 15:07:23 +00:00
|
|
|
_bridge.flowIDMapLock = [NSLock new];
|
2016-02-08 15:06:52 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativeTraceBeginAsyncFlow" usingBlock:^(__unused uint64_t tag, __unused NSString *name, int64_t cookie) {
|
2016-03-30 15:07:23 +00:00
|
|
|
if (RCTProfileIsProfiling()) {
|
|
|
|
[weakBridge.flowIDMapLock lock];
|
|
|
|
int64_t newCookie = [_RCTProfileBeginFlowEvent() longLongValue];
|
|
|
|
CFDictionarySetValue(weakBridge.flowIDMap, (const void *)cookie, (const void *)newCookie);
|
|
|
|
[weakBridge.flowIDMapLock unlock];
|
|
|
|
}
|
2016-02-08 15:06:52 +00:00
|
|
|
}];
|
|
|
|
|
|
|
|
[self addSynchronousHookWithName:@"nativeTraceEndAsyncFlow" usingBlock:^(__unused uint64_t tag, __unused NSString *name, int64_t cookie) {
|
2016-03-30 15:07:23 +00:00
|
|
|
if (RCTProfileIsProfiling()) {
|
|
|
|
[weakBridge.flowIDMapLock lock];
|
|
|
|
int64_t newCookie = (int64_t)CFDictionaryGetValue(weakBridge.flowIDMap, (const void *)cookie);
|
|
|
|
_RCTProfileEndFlowEvent(@(newCookie));
|
|
|
|
CFDictionaryRemoveValue(weakBridge.flowIDMap, (const void *)cookie);
|
|
|
|
[weakBridge.flowIDMapLock unlock];
|
|
|
|
}
|
2016-02-08 15:06:52 +00:00
|
|
|
}];
|
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:^{
|
2016-02-15 20:57:21 +00:00
|
|
|
RCTJSCExecutor *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf.valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext *context = strongSelf.context.context;
|
|
|
|
RCTInstallJSCProfiler(_bridge, context.JSGlobalContextRef);
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptContextCreatedNotification
|
|
|
|
object:context];
|
2016-01-05 15:59:54 +00:00
|
|
|
}];
|
2015-12-10 12:27:45 +00:00
|
|
|
|
2016-01-05 15:59:54 +00:00
|
|
|
for (NSString *event in @[RCTProfileDidStartProfiling, RCTProfileDidEndProfiling]) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(toggleProfilingFlag:)
|
|
|
|
name:event
|
|
|
|
object:nil];
|
|
|
|
}
|
2016-02-12 16:09:43 +00:00
|
|
|
|
|
|
|
// Inject handler used by HMR
|
|
|
|
[self addSynchronousHookWithName:@"nativeInjectHMRUpdate" usingBlock:^(NSString *sourceCode, NSString *sourceCodeURL) {
|
|
|
|
RCTJSCExecutor *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf.valid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSStringRef execJSString = JSStringCreateWithUTF8CString(sourceCode.UTF8String);
|
|
|
|
JSStringRef jsURL = JSStringCreateWithUTF8CString(sourceCodeURL.UTF8String);
|
|
|
|
JSEvaluateScript(strongSelf->_context.ctx, execJSString, NULL, jsURL, 0, NULL);
|
|
|
|
JSStringRelease(jsURL);
|
|
|
|
JSStringRelease(execJSString);
|
|
|
|
}];
|
2015-06-09 22:42:10 +00:00
|
|
|
#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];
|
2015-12-12 00:35:36 +00:00
|
|
|
[_bridge enqueueJSCall:@"Systrace.setEnabled" args:@[enabled ? @YES : @NO]];
|
2015-10-01 21:08:13 +00:00
|
|
|
}];
|
2015-06-02 13:15:53 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
2015-06-12 17:43:22 +00:00
|
|
|
if (!self.isValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_valid = NO;
|
|
|
|
|
2016-03-13 18:13:39 +00:00
|
|
|
if (_jsModules) {
|
|
|
|
CFRelease(_jsModules);
|
2016-03-17 17:34:46 +00:00
|
|
|
fclose(_bundle);
|
2016-03-13 18:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 13:15:53 +00:00
|
|
|
#if RCT_DEV
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
#endif
|
2016-03-07 17:45:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self invalidate];
|
2015-06-02 13:15:53 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
- (void)flushedQueue:(RCTJavaScriptCallback)onComplete
|
|
|
|
{
|
|
|
|
// TODO: Make this function handle first class instead of dynamically dispatching it. #9317773
|
|
|
|
[self _executeJSCall:@"flushedQueue" arguments:@[] callback:onComplete];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)callFunctionOnModule:(NSString *)module
|
|
|
|
method:(NSString *)method
|
|
|
|
arguments:(NSArray *)args
|
|
|
|
callback:(RCTJavaScriptCallback)onComplete
|
|
|
|
{
|
|
|
|
// TODO: Make this function handle first class instead of dynamically dispatching it. #9317773
|
|
|
|
[self _executeJSCall:@"callFunctionReturnFlushedQueue" arguments:@[module, method, args] callback:onComplete];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invokeCallbackID:(NSNumber *)cbID
|
|
|
|
arguments:(NSArray *)args
|
|
|
|
callback:(RCTJavaScriptCallback)onComplete
|
|
|
|
{
|
|
|
|
// TODO: Make this function handle first class instead of dynamically dispatching it. #9317773
|
|
|
|
[self _executeJSCall:@"invokeCallbackAndReturnFlushedQueue" arguments:@[cbID, args] callback:onComplete];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)_executeJSCall:(NSString *)method
|
|
|
|
arguments:(NSArray *)arguments
|
|
|
|
callback:(RCTJavaScriptCallback)onComplete
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
RCTAssert(onComplete != nil, @"onComplete block should not be nil");
|
2015-12-16 10:49:27 +00:00
|
|
|
__weak RCTJSCExecutor *weakSelf = self;
|
2015-04-20 11:55:05 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:RCTProfileBlock((^{
|
2015-12-16 10:49:27 +00:00
|
|
|
RCTJSCExecutor *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);
|
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
// get the BatchedBridge object
|
|
|
|
JSStringRef moduleNameJSStringRef = JSStringCreateWithUTF8CString("__fbBatchedBridge");
|
|
|
|
JSValueRef moduleJSRef = JSObjectGetProperty(contextJSRef, globalObjectJSRef, moduleNameJSStringRef, &errorJSRef);
|
|
|
|
JSStringRelease(moduleNameJSStringRef);
|
2015-05-23 02:33:21 +00:00
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
if (moduleJSRef != NULL && errorJSRef == NULL && !JSValueIsUndefined(contextJSRef, moduleJSRef)) {
|
|
|
|
// get method
|
|
|
|
JSStringRef methodNameJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)method);
|
|
|
|
JSValueRef methodJSRef = JSObjectGetProperty(contextJSRef, (JSObjectRef)moduleJSRef, methodNameJSStringRef, &errorJSRef);
|
|
|
|
JSStringRelease(methodNameJSStringRef);
|
2015-05-23 02:33:21 +00:00
|
|
|
|
2015-12-18 01:26:12 +00:00
|
|
|
if (methodJSRef != NULL && errorJSRef == NULL && !JSValueIsUndefined(contextJSRef, methodJSRef)) {
|
2015-12-08 23:57:34 +00:00
|
|
|
// direct method invoke with no arguments
|
|
|
|
if (arguments.count == 0) {
|
|
|
|
resultJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)methodJSRef, (JSObjectRef)moduleJSRef, 0, NULL, &errorJSRef);
|
|
|
|
}
|
2015-05-23 02:33:21 +00:00
|
|
|
|
2015-12-08 23:57:34 +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-23 02:33:21 +00:00
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
} else {
|
|
|
|
// apply invoke with array of arguments
|
|
|
|
JSStringRef applyNameJSStringRef = JSStringCreateWithUTF8CString("apply");
|
|
|
|
JSValueRef applyJSRef = JSObjectGetProperty(contextJSRef, (JSObjectRef)methodJSRef, applyNameJSStringRef, &errorJSRef);
|
|
|
|
JSStringRelease(applyNameJSStringRef);
|
2015-05-27 15:21:14 +00:00
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
if (applyJSRef != NULL && errorJSRef == NULL) {
|
|
|
|
// invoke apply
|
2015-05-23 02:33:21 +00:00
|
|
|
JSStringRef argsJSStringRef = JSStringCreateWithCFString((__bridge CFStringRef)argsString);
|
|
|
|
JSValueRef argsJSRef = JSValueMakeFromJSONString(contextJSRef, argsJSStringRef);
|
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
JSValueRef args[2];
|
|
|
|
args[0] = JSValueMakeNull(contextJSRef);
|
|
|
|
args[1] = argsJSRef;
|
2015-05-23 02:33:21 +00:00
|
|
|
|
2015-12-08 23:57:34 +00:00
|
|
|
resultJSRef = JSObjectCallAsFunction(contextJSRef, (JSObjectRef)applyJSRef, (JSObjectRef)methodJSRef, 2, args, &errorJSRef);
|
|
|
|
JSStringRelease(argsJSStringRef);
|
2015-05-23 02:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-18 01:26:12 +00:00
|
|
|
} else {
|
|
|
|
if (!errorJSRef && JSValueIsUndefined(contextJSRef, methodJSRef)) {
|
|
|
|
error = RCTErrorWithMessage([NSString stringWithFormat:@"Unable to execute JS call: method %@ is undefined", method]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!errorJSRef && JSValueIsUndefined(contextJSRef, moduleJSRef)) {
|
|
|
|
error = RCTErrorWithMessage(@"Unable to execute JS call: __fbBatchedBridge is undefined");
|
2015-05-23 02:33:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-12-18 01:26:12 +00:00
|
|
|
if (errorJSRef || error) {
|
|
|
|
if (!error) {
|
|
|
|
error = RCTNSErrorFromJSError(contextJSRef, errorJSRef);
|
|
|
|
}
|
|
|
|
onComplete(nil, error);
|
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.
|
2015-12-16 10:49:27 +00:00
|
|
|
// see [RCTJSCExecutorTests testDeserializationPerf]
|
2015-02-20 04:10:52 +00:00
|
|
|
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-12-08 23:57:34 +00:00
|
|
|
}), 0, @"js_call", (@{@"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
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
// The RAM bundle has a magic number in the 4 first bytes `(0xFB0BD1E5)`.
|
|
|
|
uint32_t magicNumber = NSSwapLittleIntToHost(*((uint32_t *)script.bytes));
|
|
|
|
BOOL isRAMBundle = magicNumber == RCTRAMBundleMagicNumber;
|
|
|
|
if (isRAMBundle) {
|
|
|
|
NSError *error;
|
|
|
|
script = [self loadRAMBundle:sourceURL error:&error];
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
if (onComplete) {
|
|
|
|
onComplete(error);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// JSStringCreateWithUTF8CString expects a null terminated C string.
|
|
|
|
// RAM Bundling already provides a null terminated one.
|
|
|
|
NSMutableData *nullTerminatedScript = [NSMutableData dataWithCapacity:script.length + 1];
|
|
|
|
|
|
|
|
[nullTerminatedScript appendData:script];
|
|
|
|
[nullTerminatedScript appendBytes:"" length:1];
|
|
|
|
|
|
|
|
script = nullTerminatedScript;
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:13:39 +00:00
|
|
|
_bundleURL = JSStringCreateWithUTF8CString(sourceURL.absoluteString.UTF8String);
|
|
|
|
|
2015-12-16 10:49:27 +00:00
|
|
|
__weak RCTJSCExecutor *weakSelf = self;
|
2016-01-27 22:55:02 +00:00
|
|
|
|
2015-04-20 11:55:05 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:RCTProfileBlock((^{
|
2015-12-16 10:49:27 +00:00
|
|
|
RCTJSCExecutor *strongSelf = weakSelf;
|
2015-04-10 14:28:10 +00:00
|
|
|
if (!strongSelf || !strongSelf.isValid) {
|
|
|
|
return;
|
|
|
|
}
|
2015-08-21 18:33:04 +00:00
|
|
|
|
2015-10-22 13:39:55 +00:00
|
|
|
RCTPerformanceLoggerStart(RCTPLScriptExecution);
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
JSValueRef jsError = NULL;
|
2016-03-17 17:34:46 +00:00
|
|
|
JSStringRef execJSString = JSStringCreateWithUTF8CString(script.bytes);
|
2016-03-23 16:27:19 +00:00
|
|
|
JSValueRef result = JSEvaluateScript(strongSelf->_context.ctx, execJSString, NULL, _bundleURL, 0, &jsError);
|
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
|
|
|
}
|
|
|
|
|
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-12-16 10:49:27 +00:00
|
|
|
__weak RCTJSCExecutor *weakSelf = self;
|
2015-04-20 11:55:05 +00:00
|
|
|
[self executeBlockOnJavaScriptQueue:RCTProfileBlock((^{
|
2015-12-16 10:49:27 +00:00
|
|
|
RCTJSCExecutor *strongSelf = weakSelf;
|
2015-04-10 14:28:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-13 18:13:39 +00:00
|
|
|
static int streq(const char *a, const char *b)
|
|
|
|
{
|
|
|
|
return strcmp(a, b) == 0;
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
static void freeModule(__unused CFAllocatorRef allocator, void *ptr)
|
2016-03-13 18:13:39 +00:00
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:13:40 +00:00
|
|
|
static uint32_t readUint32(const void **ptr) {
|
|
|
|
uint32_t data;
|
|
|
|
memcpy(&data, *ptr, sizeof(uint32_t));
|
|
|
|
data = NSSwapLittleIntToHost(data);
|
|
|
|
*ptr += sizeof(uint32_t);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
static int readBundle(FILE *fd, size_t offset, size_t length, void *ptr) {
|
|
|
|
if (fseek(fd, offset, SEEK_SET) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread(ptr, sizeof(uint8_t), length, fd) != length) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)registerNativeRequire
|
2016-03-13 18:13:39 +00:00
|
|
|
{
|
|
|
|
__weak RCTJSCExecutor *weakSelf = self;
|
2016-03-23 16:27:20 +00:00
|
|
|
[self addSynchronousHookWithName:@"nativeRequire" usingBlock:^(NSString *moduleName) {
|
2016-03-13 18:13:39 +00:00
|
|
|
RCTJSCExecutor *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf || !moduleName) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
ModuleData *data = (ModuleData *)CFDictionaryGetValue(strongSelf->_jsModules, moduleName.UTF8String);
|
|
|
|
char bytes[data->length];
|
|
|
|
if (readBundle(strongSelf->_bundle, data->offset, data->length, bytes) != 0) {
|
|
|
|
RCTFatal(RCTErrorWithMessage(@"Error loading RAM module"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSStringRef code = JSStringCreateWithUTF8CString(bytes);
|
2016-03-13 18:13:39 +00:00
|
|
|
JSValueRef jsError = NULL;
|
2016-03-17 17:34:46 +00:00
|
|
|
JSValueRef result = JSEvaluateScript(strongSelf->_context.ctx, code, NULL, strongSelf->_bundleURL, data->lineNo, NULL);
|
2016-03-13 18:13:39 +00:00
|
|
|
|
|
|
|
CFDictionaryRemoveValue(strongSelf->_jsModules, moduleName.UTF8String);
|
2016-03-17 17:34:46 +00:00
|
|
|
JSStringRelease(code);
|
2016-03-13 18:13:39 +00:00
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
RCTFatal(RCTNSErrorFromJSError(strongSelf->_context.ctx, jsError));
|
|
|
|
[strongSelf invalidate];
|
|
|
|
});
|
|
|
|
}
|
2016-03-23 16:27:20 +00:00
|
|
|
}];
|
2016-03-17 17:34:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSData *)loadRAMBundle:(NSURL *)sourceURL error:(NSError **)error
|
|
|
|
{
|
|
|
|
_bundle = fopen(sourceURL.path.UTF8String, "r");
|
|
|
|
if (!_bundle) {
|
2016-03-21 10:20:49 +00:00
|
|
|
if (error) {
|
|
|
|
*error = RCTErrorWithMessage([NSString stringWithFormat:@"Bundle %@ cannot be opened: %d", sourceURL.path, errno]);
|
|
|
|
}
|
2016-03-17 17:34:46 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self registerNativeRequire];
|
2016-03-13 18:13:39 +00:00
|
|
|
|
|
|
|
// once a module has been loaded free its space from the heap, remove it from the index and release the module name
|
2016-03-17 17:34:46 +00:00
|
|
|
CFDictionaryKeyCallBacks keyCallbacks = { 0, NULL, (CFDictionaryReleaseCallBack)freeModule, NULL, (CFDictionaryEqualCallBack)streq, (CFDictionaryHashCallBack)strlen };
|
|
|
|
CFDictionaryValueCallBacks valueCallbacks = { 0, NULL, (CFDictionaryReleaseCallBack)freeModule, NULL, NULL };
|
2016-03-13 18:13:39 +00:00
|
|
|
_jsModules = CFDictionaryCreateMutable(NULL, 0, &keyCallbacks, &valueCallbacks);
|
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
uint32_t currentOffset = sizeof(uint32_t); // skip magic number
|
2016-03-13 18:13:39 +00:00
|
|
|
|
|
|
|
uint32_t tableLength;
|
2016-03-17 17:34:46 +00:00
|
|
|
if (readBundle(_bundle, currentOffset, sizeof(tableLength), &tableLength) != 0) {
|
2016-03-21 10:20:49 +00:00
|
|
|
if (error) {
|
|
|
|
*error = RCTErrorWithMessage(@"Error loading RAM Bundle");
|
|
|
|
}
|
2016-03-17 17:34:46 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2016-03-13 18:13:39 +00:00
|
|
|
tableLength = NSSwapLittleIntToHost(tableLength);
|
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
currentOffset += sizeof(uint32_t); // skip table length
|
2016-03-13 18:13:40 +00:00
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
// base offset to add to every module's offset to skip the header of the RAM bundle
|
|
|
|
uint32_t baseOffset = 4 + tableLength;
|
2016-03-13 18:13:39 +00:00
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
char tableStart[tableLength];
|
|
|
|
if (readBundle(_bundle, currentOffset, tableLength, tableStart) != 0) {
|
2016-03-21 10:20:49 +00:00
|
|
|
if (error) {
|
|
|
|
*error = RCTErrorWithMessage(@"Error loading RAM Bundle");
|
|
|
|
}
|
2016-03-17 17:34:46 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *tableCursor = tableStart;
|
|
|
|
void *endOfTable = tableCursor + tableLength;
|
|
|
|
|
|
|
|
while (tableCursor < endOfTable) {
|
|
|
|
uint32_t nameLength = strlen((const char *)tableCursor);
|
|
|
|
char *name = malloc(nameLength + 1);
|
|
|
|
|
|
|
|
if (!name) {
|
2016-03-21 10:20:49 +00:00
|
|
|
if (error) {
|
|
|
|
*error = RCTErrorWithMessage(@"Error loading RAM Bundle");
|
|
|
|
}
|
2016-03-17 17:34:46 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2016-03-13 18:13:40 +00:00
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
strcpy(name, tableCursor);
|
2016-03-13 18:13:39 +00:00
|
|
|
|
2016-03-13 18:13:40 +00:00
|
|
|
// the space allocated for each module's metada gets freed when the module is injected into JSC on `nativeRequire`
|
|
|
|
ModuleData *moduleData = malloc(sizeof(ModuleData));
|
2016-03-13 18:13:39 +00:00
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
tableCursor += nameLength + 1; // null byte terminator
|
2016-03-13 18:13:39 +00:00
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
moduleData->offset = baseOffset + readUint32((const void **)&tableCursor);
|
|
|
|
moduleData->length = readUint32((const void **)&tableCursor);
|
|
|
|
moduleData->lineNo = readUint32((const void **)&tableCursor);
|
2016-03-13 18:13:39 +00:00
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
CFDictionarySetValue(_jsModules, name, moduleData);
|
2016-03-13 18:13:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 17:34:46 +00:00
|
|
|
ModuleData *startupData = ((ModuleData *)CFDictionaryGetValue(_jsModules, ""));
|
|
|
|
|
|
|
|
void *startupCode;
|
|
|
|
if (!(startupCode = malloc(startupData->length))) {
|
2016-03-21 10:20:49 +00:00
|
|
|
if (error) {
|
|
|
|
*error = RCTErrorWithMessage(@"Error loading RAM Bundle");
|
|
|
|
}
|
2016-03-17 17:34:46 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readBundle(_bundle, startupData->offset, startupData->length, startupCode) != 0) {
|
2016-03-21 10:20:49 +00:00
|
|
|
if (error) {
|
|
|
|
*error = RCTErrorWithMessage(@"Error loading RAM Bundle");
|
|
|
|
}
|
|
|
|
free(startupCode);
|
2016-03-17 17:34:46 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
return [NSData dataWithBytesNoCopy:startupCode length:startupData->length freeWhenDone:YES];
|
2016-03-13 18:13:39 +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
|