From e541d9b1088365778c8f7b1db6c2842fbac209c9 Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Tue, 22 Nov 2016 06:05:36 -0800 Subject: [PATCH] Define wrapper versions of all JSC methods Reviewed By: bnham Differential Revision: D4197369 fbshipit-source-id: 53869fe1b56010c8a5330025d06ef557369e4957 --- ReactCommon/cxxreact/JSCExecutor.h | 5 +- ReactCommon/cxxreact/JSCLegacyProfiler.cpp | 2 +- ReactCommon/cxxreact/JSCLegacyProfiler.h | 3 +- ReactCommon/cxxreact/JSCLegacyTracing.cpp | 5 +- ReactCommon/cxxreact/JSCLegacyTracing.h | 3 +- ReactCommon/cxxreact/JSCMemory.cpp | 2 +- ReactCommon/cxxreact/JSCMemory.h | 3 +- ReactCommon/cxxreact/JSCNativeModules.cpp | 2 + ReactCommon/cxxreact/JSCPerfStats.cpp | 2 - ReactCommon/cxxreact/JSCPerfStats.h | 2 +- ReactCommon/cxxreact/JSCSamplingProfiler.h | 2 +- ReactCommon/cxxreact/JSCTracing.cpp | 2 +- ReactCommon/cxxreact/JSCTracing.h | 3 +- ReactCommon/cxxreact/JSCWebWorker.cpp | 7 +- ReactCommon/cxxreact/JSCWebWorker.h | 3 +- ReactCommon/cxxreact/Platform.h | 2 +- ReactCommon/jschelpers/BUCK | 50 ++++---- ReactCommon/jschelpers/JSCHelpers.cpp | 2 +- ReactCommon/jschelpers/JSCHelpers.h | 7 +- ReactCommon/jschelpers/JSCWrapper.h | 13 ++ ReactCommon/jschelpers/JavaScriptCore.h | 132 +++++++++++++++++++++ ReactCommon/jschelpers/Value.cpp | 1 + ReactCommon/jschelpers/Value.h | 14 +-- 23 files changed, 199 insertions(+), 68 deletions(-) create mode 100644 ReactCommon/jschelpers/JavaScriptCore.h diff --git a/ReactCommon/cxxreact/JSCExecutor.h b/ReactCommon/cxxreact/JSCExecutor.h index fd1333f1db..622eb9f95a 100644 --- a/ReactCommon/cxxreact/JSCExecutor.h +++ b/ReactCommon/cxxreact/JSCExecutor.h @@ -6,11 +6,10 @@ #include #include -#include - #include #include +#include #include #include @@ -163,7 +162,7 @@ private: void terminateOwnedWebWorker(int worker); Object createMessageObject(const std::string& msgData); - template< JSValueRef (JSCExecutor::*method)(size_t, const JSValueRef[])> + template void installNativeHook(const char* name); JSValueRef getNativeModule(JSObjectRef object, JSStringRef propertyName); diff --git a/ReactCommon/cxxreact/JSCLegacyProfiler.cpp b/ReactCommon/cxxreact/JSCLegacyProfiler.cpp index 2c811127be..d3381b2d71 100644 --- a/ReactCommon/cxxreact/JSCLegacyProfiler.cpp +++ b/ReactCommon/cxxreact/JSCLegacyProfiler.cpp @@ -4,9 +4,9 @@ #include #include -#include #include #include +#include #include #include #include "JSCLegacyProfiler.h" diff --git a/ReactCommon/cxxreact/JSCLegacyProfiler.h b/ReactCommon/cxxreact/JSCLegacyProfiler.h index b5029e6ca5..4f0ec33bbf 100644 --- a/ReactCommon/cxxreact/JSCLegacyProfiler.h +++ b/ReactCommon/cxxreact/JSCLegacyProfiler.h @@ -4,7 +4,8 @@ #ifdef WITH_JSC_EXTRA_TRACING -#include +#include + namespace facebook { namespace react { diff --git a/ReactCommon/cxxreact/JSCLegacyTracing.cpp b/ReactCommon/cxxreact/JSCLegacyTracing.cpp index 7e684e5100..5c5a1e6d7b 100644 --- a/ReactCommon/cxxreact/JSCLegacyTracing.cpp +++ b/ReactCommon/cxxreact/JSCLegacyTracing.cpp @@ -4,11 +4,8 @@ #include "JSCLegacyTracing.h" -#include -#include - #include - +#include #include #include diff --git a/ReactCommon/cxxreact/JSCLegacyTracing.h b/ReactCommon/cxxreact/JSCLegacyTracing.h index 93908b03db..31863ddb92 100644 --- a/ReactCommon/cxxreact/JSCLegacyTracing.h +++ b/ReactCommon/cxxreact/JSCLegacyTracing.h @@ -4,7 +4,8 @@ #if defined(WITH_JSC_EXTRA_TRACING) -#include +#include + namespace facebook { namespace react { diff --git a/ReactCommon/cxxreact/JSCMemory.cpp b/ReactCommon/cxxreact/JSCMemory.cpp index b639eae63b..825547c83a 100644 --- a/ReactCommon/cxxreact/JSCMemory.cpp +++ b/ReactCommon/cxxreact/JSCMemory.cpp @@ -1,6 +1,6 @@ // Copyright 2004-present Facebook. All Rights Reserved. -#include +#include "JSCMemory.h" #ifdef WITH_FB_MEMORY_PROFILING diff --git a/ReactCommon/cxxreact/JSCMemory.h b/ReactCommon/cxxreact/JSCMemory.h index 6e05bba709..3d7a751914 100644 --- a/ReactCommon/cxxreact/JSCMemory.h +++ b/ReactCommon/cxxreact/JSCMemory.h @@ -2,7 +2,8 @@ #pragma once -#include +#include + namespace facebook { namespace react { diff --git a/ReactCommon/cxxreact/JSCNativeModules.cpp b/ReactCommon/cxxreact/JSCNativeModules.cpp index b1036ba376..d0937f4607 100644 --- a/ReactCommon/cxxreact/JSCNativeModules.cpp +++ b/ReactCommon/cxxreact/JSCNativeModules.cpp @@ -2,6 +2,8 @@ #include "JSCNativeModules.h" +#include + #include namespace facebook { diff --git a/ReactCommon/cxxreact/JSCPerfStats.cpp b/ReactCommon/cxxreact/JSCPerfStats.cpp index c02b504317..bf53100e7d 100644 --- a/ReactCommon/cxxreact/JSCPerfStats.cpp +++ b/ReactCommon/cxxreact/JSCPerfStats.cpp @@ -5,8 +5,6 @@ #ifdef JSC_HAS_PERF_STATS_API #include -#include - #include #include diff --git a/ReactCommon/cxxreact/JSCPerfStats.h b/ReactCommon/cxxreact/JSCPerfStats.h index 73ca29e8b0..9dab4b9c2a 100644 --- a/ReactCommon/cxxreact/JSCPerfStats.h +++ b/ReactCommon/cxxreact/JSCPerfStats.h @@ -2,7 +2,7 @@ #pragma once -#include +#include namespace facebook { namespace react { diff --git a/ReactCommon/cxxreact/JSCSamplingProfiler.h b/ReactCommon/cxxreact/JSCSamplingProfiler.h index ec8d4e8751..6331ec8b7f 100644 --- a/ReactCommon/cxxreact/JSCSamplingProfiler.h +++ b/ReactCommon/cxxreact/JSCSamplingProfiler.h @@ -4,7 +4,7 @@ #ifdef WITH_JSC_EXTRA_TRACING -#include +#include namespace facebook { namespace react { diff --git a/ReactCommon/cxxreact/JSCTracing.cpp b/ReactCommon/cxxreact/JSCTracing.cpp index 0ba67a7d5f..ea7323c48b 100644 --- a/ReactCommon/cxxreact/JSCTracing.cpp +++ b/ReactCommon/cxxreact/JSCTracing.cpp @@ -5,10 +5,10 @@ #include "JSCTracing.h" #include -#include #include #include #include +#include #include #include diff --git a/ReactCommon/cxxreact/JSCTracing.h b/ReactCommon/cxxreact/JSCTracing.h index b65f633c8f..21753e7fbe 100644 --- a/ReactCommon/cxxreact/JSCTracing.h +++ b/ReactCommon/cxxreact/JSCTracing.h @@ -5,8 +5,7 @@ #if defined(WITH_JSC_EXTRA_TRACING) || DEBUG #include - -#include +#include namespace facebook { namespace react { diff --git a/ReactCommon/cxxreact/JSCWebWorker.cpp b/ReactCommon/cxxreact/JSCWebWorker.cpp index e8bfa55cd7..cb976b6ab6 100644 --- a/ReactCommon/cxxreact/JSCWebWorker.cpp +++ b/ReactCommon/cxxreact/JSCWebWorker.cpp @@ -2,23 +2,20 @@ #include "JSCWebWorker.h" - #include #include #include #include +#include #include #include + #include "MessageQueueThread.h" #include "Platform.h" #include "JSCUtils.h" -#include - -#include - namespace facebook { namespace react { diff --git a/ReactCommon/cxxreact/JSCWebWorker.h b/ReactCommon/cxxreact/JSCWebWorker.h index cd5604ec89..d94a5820fc 100644 --- a/ReactCommon/cxxreact/JSCWebWorker.h +++ b/ReactCommon/cxxreact/JSCWebWorker.h @@ -7,8 +7,7 @@ #include #include -#include - +#include #include namespace facebook { diff --git a/ReactCommon/cxxreact/Platform.h b/ReactCommon/cxxreact/Platform.h index 81e4c82465..784f178d34 100644 --- a/ReactCommon/cxxreact/Platform.h +++ b/ReactCommon/cxxreact/Platform.h @@ -6,7 +6,7 @@ #include #include -#include +#include #include "Executor.h" #include "MessageQueueThread.h" diff --git a/ReactCommon/jschelpers/BUCK b/ReactCommon/jschelpers/BUCK index fb96390ce5..a51901fec8 100644 --- a/ReactCommon/jschelpers/BUCK +++ b/ReactCommon/jschelpers/BUCK @@ -1,8 +1,10 @@ EXPORTED_HEADERS = [ + 'JavaScriptCore.h', 'JSCHelpers.h', - 'Value.h', + 'JSCWrapper.h', 'noncopyable.h', 'Unicode.h', + 'Value.h', ] if THIS_IS_FBANDROID: @@ -30,26 +32,26 @@ if THIS_IS_FBANDROID: ) elif THIS_IS_FBOBJC: - ios_library( - name = 'jschelpers', - inherited_buck_flags = STATIC_LIBRARY_IOS_FLAGS, - compiler_flags = [ - '-Wall', - '-fexceptions', - '-fvisibility=hidden', - '-std=c++1y', - ], - exported_headers = EXPORTED_HEADERS, - headers = glob(['*.h'], excludes=EXPORTED_HEADERS), - header_namespace = 'jschelpers', - srcs = glob(['*.cpp']), - frameworks = [ - '$SDKROOT/System/Library/Frameworks/JavaScriptCore.framework', - ], - deps = [ - '//xplat/folly:molly', - ], - visibility = [ - 'PUBLIC', - ], - ) + ios_library( + name = 'jschelpers', + inherited_buck_flags = STATIC_LIBRARY_IOS_FLAGS, + compiler_flags = [ + '-Wall', + '-fexceptions', + '-fvisibility=hidden', + '-std=c++1y', + ], + exported_headers = EXPORTED_HEADERS, + headers = glob(['*.h'], excludes=EXPORTED_HEADERS), + header_namespace = 'jschelpers', + srcs = glob(['*.cpp']), + frameworks = [ + '$SDKROOT/System/Library/Frameworks/JavaScriptCore.framework', + ], + deps = [ + '//xplat/folly:molly', + ], + visibility = [ + 'PUBLIC', + ], + ) diff --git a/ReactCommon/jschelpers/JSCHelpers.cpp b/ReactCommon/jschelpers/JSCHelpers.cpp index 1b2d9f6c45..cd860ad10a 100644 --- a/ReactCommon/jschelpers/JSCHelpers.cpp +++ b/ReactCommon/jschelpers/JSCHelpers.cpp @@ -6,10 +6,10 @@ #include #endif -#include #include #include +#include "JavaScriptCore.h" #include "Value.h" namespace facebook { diff --git a/ReactCommon/jschelpers/JSCHelpers.h b/ReactCommon/jschelpers/JSCHelpers.h index dd11c76302..7e5c3dfe5c 100644 --- a/ReactCommon/jschelpers/JSCHelpers.h +++ b/ReactCommon/jschelpers/JSCHelpers.h @@ -2,11 +2,8 @@ #pragma once -#include "Value.h" - -#include -#include -#include +#include +#include #include #include diff --git a/ReactCommon/jschelpers/JSCWrapper.h b/ReactCommon/jschelpers/JSCWrapper.h index 485f89f0b1..9b50349996 100644 --- a/ReactCommon/jschelpers/JSCWrapper.h +++ b/ReactCommon/jschelpers/JSCWrapper.h @@ -128,4 +128,17 @@ const JSCWrapper *customJSCWrapper(); } } +#else + +namespace facebook { +namespace react { + +template +bool isCustomJSCPtr(T *x) { + // Always use system JSC pointers + return false; +} + +} } + #endif diff --git a/ReactCommon/jschelpers/JavaScriptCore.h b/ReactCommon/jschelpers/JavaScriptCore.h new file mode 100644 index 0000000000..aee5d47d36 --- /dev/null +++ b/ReactCommon/jschelpers/JavaScriptCore.h @@ -0,0 +1,132 @@ +/** + * Copyright (c) 2016-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. + */ + +#pragma once + +#include + +#if defined(__APPLE__) + +// Use for methods that are taking JSContextRef as a first param +#define __jsc_wrapper(method, ctx, ...) \ + (facebook::react::isCustomJSCPtr(ctx) ? \ + facebook::react::customJSCWrapper() : \ + facebook::react::systemJSCWrapper() \ + )->method(ctx, ## __VA_ARGS__) + +// Use for methods that don't take a JSContextRef as a first param. The wrapped version +// of this method will require context as an additional param, but it will be dropped +// before calling into the JSC method. +#define __jsc_drop_ctx_wrapper(method, ctx, ...) \ + (facebook::react::isCustomJSCPtr(ctx) ? \ + facebook::react::customJSCWrapper() : \ + facebook::react::systemJSCWrapper() \ + )->method(__VA_ARGS__) + +// Use for methods were access to a JSContextRef is impractical. The first bool param +// will be dropped before the JSC method is invoked. +#define __jsc_bool_wrapper(method, useCustomJSC, ...) \ + (useCustomJSC ? \ + facebook::react::customJSCWrapper() : \ + facebook::react::systemJSCWrapper() \ + )->method(__VA_ARGS__) + +// Used for wrapping properties +#define __jsc_prop_wrapper(prop, ctx) \ + (facebook::react::isCustomJSCPtr(ctx) ? \ + facebook::react::customJSCWrapper() : \ + facebook::react::systemJSCWrapper() \ + )->prop + +#else + +#define __jsc_wrapper(method, ctx, ...) method(ctx, ## __VA_ARGS__) +#define __jsc_drop_ctx_wrapper(method, ctx, ...) ((void)ctx, method(__VA_ARGS__)) +#define __jsc_bool_wrapper(method, useCustomJSC, ...) \ + ((void)useCustomJSC, method(__VA_ARGS__)) +#define __jsc_prop_wrapper(prop, ctx) prop + +#endif + +// JSGlobalContext +#define JSC_JSGlobalContextCreateInGroup(...) __jsc_bool_wrapper(JSGlobalContextCreateInGroup, __VA_ARGS__) +#define JSC_JSGlobalContextRelease(...) __jsc_wrapper(JSGlobalContextRelease, __VA_ARGS__) +#define JSC_JSGlobalContextSetName(...) __jsc_wrapper(JSGlobalContextSetName, __VA_ARGS__) + +// JSContext +#define JSC_JSContextGetGlobalContext(...) __jsc_wrapper(JSContextGetGlobalContext, __VA_ARGS__) +#define JSC_JSContextGetGlobalObject(...) __jsc_wrapper(JSContextGetGlobalObject, __VA_ARGS__) + +// JSEvaluate +#define JSC_JSEvaluateScript(...) __jsc_wrapper(JSEvaluateScript, __VA_ARGS__) +#define JSC_JSEvaluateBytecodeBundle(...) __jsc_wrapper(JSEvaluateBytecodeBundle, __VA_ARGS__) + +// JSString +#define JSC_JSStringCreateWithCFString(...) __jsc_drop_ctx_wrapper(JSStringCreateWithCFString, __VA_ARGS__) +#define JSC_JSStringCreateWithUTF8CString(...) __jsc_drop_ctx_wrapper(JSStringCreateWithUTF8CString, __VA_ARGS__) +#define JSC_JSStringCreateWithUTF8CStringExpectAscii(...) __jsc_drop_ctx_wrapper(JSStringCreateWithUTF8CStringExpectAscii, __VA_ARGS__) +#define JSC_JSStringCopyCFString(...) __jsc_drop_ctx_wrapper(JSStringCopyCFString, __VA_ARGS__) +#define JSC_JSStringGetCharactersPtr(...) __jsc_drop_ctx_wrapper(JSStringGetCharactersPtr, __VA_ARGS__) +#define JSC_JSStringGetLength(...) __jsc_drop_ctx_wrapper(JSStringGetLength, __VA_ARGS__) +#define JSC_JSStringGetMaximumUTF8CStringSize(...) __jsc_drop_ctx_wrapper(JSStringGetMaximumUTF8CStringSize, __VA_ARGS__) +#define JSC_JSStringIsEqualToUTF8CString(...) __jsc_drop_ctx_wrapper(JSStringIsEqualToUTF8CString, __VA_ARGS__) +#define JSC_JSStringRelease(...) __jsc_drop_ctx_wrapper(JSStringRelease, __VA_ARGS__) +#define JSC_JSStringRetain(...) __jsc_drop_ctx_wrapper(JSStringRetain, __VA_ARGS__) + +// JSValueRef +#define JSC_JSValueCreateJSONString(...) __jsc_wrapper(JSValueCreateJSONString, __VA_ARGS__) +#define JSC_JSValueGetType(...) __jsc_wrapper(JSValueGetType, __VA_ARGS__) +#define JSC_JSValueMakeFromJSONString(...) __jsc_wrapper(JSValueMakeFromJSONString, __VA_ARGS__) +#define JSC_JSValueMakeBoolean(...) __jsc_wrapper(JSValueMakeBoolean, __VA_ARGS__) +#define JSC_JSValueMakeNull(...) __jsc_wrapper(JSValueMakeNull, __VA_ARGS__) +#define JSC_JSValueMakeNumber(...) __jsc_wrapper(JSValueMakeNumber, __VA_ARGS__) +#define JSC_JSValueMakeString(...) __jsc_wrapper(JSValueMakeString, __VA_ARGS__) +#define JSC_JSValueMakeUndefined(...) __jsc_wrapper(JSValueMakeUndefined, __VA_ARGS__) +#define JSC_JSValueProtect(...) __jsc_wrapper(JSValueProtect, __VA_ARGS__) +#define JSC_JSValueToBoolean(...) __jsc_wrapper(JSValueToBoolean, __VA_ARGS__) +#define JSC_JSValueToNumber(...) __jsc_wrapper(JSValueToNumber, __VA_ARGS__) +#define JSC_JSValueToObject(...) __jsc_wrapper(JSValueToObject, __VA_ARGS__) +#define JSC_JSValueToStringCopy(...) __jsc_wrapper(JSValueToStringCopy, __VA_ARGS__) +#define JSC_JSValueUnprotect(...) __jsc_wrapper(JSValueUnprotect, __VA_ARGS__) + +// JSClass +#define JSC_JSClassCreate(...) __jsc_bool_wrapper(JSClassCreate, __VA_ARGS__) +#define JSC_JSClassRelease(...) __jsc_bool_wrapper(JSClassRelease, __VA_ARGS__) + +// JSObject +#define JSC_JSObjectCallAsConstructor(...) __jsc_wrapper(JSObjectCallAsConstructor, __VA_ARGS__) +#define JSC_JSObjectCallAsFunction(...) __jsc_wrapper(JSObjectCallAsFunction, __VA_ARGS__) +#define JSC_JSObjectGetPrivate(...) __jsc_bool_wrapper(JSObjectGetPrivate, __VA_ARGS__) +#define JSC_JSObjectGetProperty(...) __jsc_wrapper(JSObjectGetProperty, __VA_ARGS__) +#define JSC_JSObjectGetPropertyAtIndex(...) __jsc_wrapper(JSObjectGetPropertyAtIndex, __VA_ARGS__) +#define JSC_JSObjectIsConstructor(...) __jsc_wrapper(JSObjectIsConstructor, __VA_ARGS__) +#define JSC_JSObjectIsFunction(...) __jsc_wrapper(JSObjectIsFunction, __VA_ARGS__) +#define JSC_JSObjectMake(...) __jsc_wrapper(JSObjectMake, __VA_ARGS__) +#define JSC_JSObjectMakeArray(...) __jsc_wrapper(JSObjectMakeArray, __VA_ARGS__) +#define JSC_JSObjectMakeError(...) __jsc_wrapper(JSObjectMakeError, __VA_ARGS__) +#define JSC_JSObjectMakeFunctionWithCallback(...) __jsc_wrapper(JSObjectMakeFunctionWithCallback, __VA_ARGS__) +#define JSC_JSObjectSetPrivate(...) __jsc_bool_wrapper(JSObjectSetPrivate, __VA_ARGS__) +#define JSC_JSObjectSetProperty(...) __jsc_wrapper(JSObjectSetProperty, __VA_ARGS__) + +// JSPropertyNameArray +#define JSC_JSObjectCopyPropertyNames(...) __jsc_wrapper(JSObjectCopyPropertyNames, __VA_ARGS__) +#define JSC_JSPropertyNameArrayGetCount(...) __jsc_drop_ctx_wrapper(JSPropertyNameArrayGetCount, __VA_ARGS__) +#define JSC_JSPropertyNameArrayGetNameAtIndex(...) __jsc_drop_ctx_wrapper(JSPropertyNameArrayGetNameAtIndex, __VA_ARGS__) +#define JSC_JSPropertyNameArrayRelease(...) __jsc_drop_ctx_wrapper(JSPropertyNameArrayRelease, __VA_ARGS__) + +// Sampling profiler +#define JSC_JSSamplingProfilerEnabled(...) __jsc_drop_ctx_wrapper(JSSamplingProfilerEnabled, __VA_ARGS__) +#define JSC_JSPokeSamplingProfiler(...) __jsc_wrapper(JSPokeSamplingProfiler, __VA_ARGS__) +#define JSC_JSStartSamplingProfilingOnMainJSCThread(...) __jsc_wrapper(JSStartSamplingProfilingOnMainJSCThread, __VA_ARGS__) + +#define JSC_configureJSCForIOS(...) __jsc_bool_wrapper(configureJSCForIOS, __VA_ARGS__) + +// Objective-C API +#define JSC_JSContext(ctx) __jsc_prop_wrapper(JSContext, ctx) +#define JSC_JSValue(ctx) __jsc_prop_wrapper(JSValue, ctx) diff --git a/ReactCommon/jschelpers/Value.cpp b/ReactCommon/jschelpers/Value.cpp index 2bb0b61f2f..820881c77c 100644 --- a/ReactCommon/jschelpers/Value.cpp +++ b/ReactCommon/jschelpers/Value.cpp @@ -5,6 +5,7 @@ #include "Value.h" #include "JSCHelpers.h" +#include "JavaScriptCore.h" // See the comment under Value::fromDynamic() #if !defined(__APPLE__) && defined(WITH_FB_JSC_TUNING) diff --git a/ReactCommon/jschelpers/Value.h b/ReactCommon/jschelpers/Value.h index ff96cfd068..af035e78aa 100644 --- a/ReactCommon/jschelpers/Value.h +++ b/ReactCommon/jschelpers/Value.h @@ -7,19 +7,11 @@ #include #include -#include -#include -#include -#include - #include -#include "noncopyable.h" -#include "Unicode.h" - -#if WITH_FBJSCEXTENSIONS -#include -#endif +#include +#include +#include namespace facebook { namespace react {