Skip to content

Commit

Permalink
Base class for all object private data passed to JSC
Browse files Browse the repository at this point in the history
Reviewed By: amnn

Differential Revision: D5761937

fbshipit-source-id: de88cf8b959fc855c3c0786f817bb6032491c819
  • Loading branch information
Michał Gregorczyk authored and facebook-github-bot committed Sep 19, 2017
1 parent 8bf8b21 commit d6c519b
Show file tree
Hide file tree
Showing 12 changed files with 386 additions and 12 deletions.
253 changes: 253 additions & 0 deletions React/React.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion ReactAndroid/src/main/jni/react/jni/Android.mk
Expand Up @@ -35,12 +35,13 @@ LOCAL_CFLAGS += $(CXX11_FLAGS)
LOCAL_EXPORT_CPPFLAGS := $(CXX11_FLAGS)

LOCAL_LDLIBS += -landroid
LOCAL_SHARED_LIBRARIES := libfolly_json libfbjni libjsc libglog_init libyoga
LOCAL_SHARED_LIBRARIES := libfolly_json libfbjni libjsc libglog_init libyoga libprivatedata
LOCAL_STATIC_LIBRARIES := libreactnative

include $(BUILD_SHARED_LIBRARY)

$(call import-module,cxxreact)
$(call import-module,privatedata)
$(call import-module,fb)
$(call import-module,fbgloginit)
$(call import-module,folly)
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/cxxreact/Android.mk
Expand Up @@ -44,3 +44,4 @@ $(call import-module,folly)
$(call import-module,jsc)
$(call import-module,glog)
$(call import-module,jschelpers)
$(call import-module,privatedata)
3 changes: 2 additions & 1 deletion ReactCommon/cxxreact/JSCExecutor.h
Expand Up @@ -14,6 +14,7 @@
#include <jschelpers/JSCHelpers.h>
#include <jschelpers/JavaScriptCore.h>
#include <jschelpers/Value.h>
#include <privatedata/PrivateDataBase.h>

#ifndef RN_EXPORT
#define RN_EXPORT __attribute__((visibility("default")))
Expand Down Expand Up @@ -50,7 +51,7 @@ struct JSCValueEncoder<folly::dynamic> {
}
};

class RN_EXPORT JSCExecutor : public JSExecutor {
class RN_EXPORT JSCExecutor : public JSExecutor, public PrivateDataBase {
public:
/**
* Must be invoked from thread this Executor will run on.
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/jschelpers/Android.mk
Expand Up @@ -27,3 +27,4 @@ include $(BUILD_STATIC_LIBRARY)
$(call import-module,folly)
$(call import-module,jsc)
$(call import-module,glog)
$(call import-module,privatedata)
4 changes: 4 additions & 0 deletions ReactCommon/jschelpers/BUCK
Expand Up @@ -31,9 +31,13 @@ rn_xplat_cxx_library(
compiler_flags = [
"-Wall",
"-fexceptions",
"-frtti",
"-fvisibility=hidden",
"-std=c++1y",
],
exported_deps = [
react_native_xplat_target("privatedata:privatedata"),
],
fbobjc_inherited_buck_flags = STATIC_LIBRARY_IOS_FLAGS,
force_static = True,
visibility = [
Expand Down
32 changes: 24 additions & 8 deletions ReactCommon/jschelpers/JSCHelpers.cpp
Expand Up @@ -14,6 +14,7 @@

#include "JavaScriptCore.h"
#include "Value.h"
#include <privatedata/PrivateDataBase.h>

#if WITH_FBJSCEXTENSIONS
#undef ASSERT
Expand All @@ -31,6 +32,18 @@ namespace react {

namespace {

class JSFunctionPrivateData : public PrivateDataBase {
public:
explicit JSFunctionPrivateData(JSFunction&& function) : jsFunction_{std::move(function)} {}

JSFunction& getJSFunction() {
return jsFunction_;
}

private:
JSFunction jsFunction_;
};

JSValueRef functionCaller(
JSContextRef ctx,
JSObjectRef function,
Expand All @@ -39,8 +52,9 @@ JSValueRef functionCaller(
const JSValueRef arguments[],
JSValueRef* exception) {
const bool isCustomJSC = isCustomJSCPtr(ctx);
auto* f = static_cast<JSFunction*>(JSC_JSObjectGetPrivate(isCustomJSC, function));
return (*f)(ctx, thisObject, argumentCount, arguments);
auto* privateData = PrivateDataBase::cast<JSFunctionPrivateData>(
JSC_JSObjectGetPrivate(isCustomJSC, function));
return (privateData->getJSFunction())(ctx, thisObject, argumentCount, arguments);
}

JSClassRef createFuncClass(JSContextRef ctx) {
Expand All @@ -52,13 +66,15 @@ JSClassRef createFuncClass(JSContextRef ctx) {
const bool isCustomJSC = isCustomJSCPtr(ctx);
if (isCustomJSC) {
definition.finalize = [](JSObjectRef object) {
auto* function = static_cast<JSFunction*>(JSC_JSObjectGetPrivate(true, object));
delete function;
auto* privateData = PrivateDataBase::cast<JSFunctionPrivateData>(
JSC_JSObjectGetPrivate(true, object));
delete privateData;
};
} else {
definition.finalize = [](JSObjectRef object) {
auto* function = static_cast<JSFunction*>(JSC_JSObjectGetPrivate(false, object));
delete function;
auto* privateData = PrivateDataBase::cast<JSFunctionPrivateData>(
JSC_JSObjectGetPrivate(false, object));
delete privateData;
};
}
definition.callAsFunction = exceptionWrapMethod<&functionCaller>();
Expand All @@ -77,8 +93,8 @@ JSObjectRef makeFunction(
}

// dealloc in kClassDef.finalize
JSFunction *functionPtr = new JSFunction(std::move(function));
auto functionObject = Object(ctx, JSC_JSObjectMake(ctx, *classRef, functionPtr));
JSFunctionPrivateData *functionDataPtr = new JSFunctionPrivateData(std::move(function));
auto functionObject = Object(ctx, JSC_JSObjectMake(ctx, *classRef, functionDataPtr));
functionObject.setProperty("name", Value(ctx, name));
return functionObject;
}
Expand Down
5 changes: 3 additions & 2 deletions ReactCommon/jschelpers/Value.h
Expand Up @@ -11,6 +11,7 @@
#include <jschelpers/JavaScriptCore.h>
#include <jschelpers/Unicode.h>
#include <jschelpers/noncopyable.h>
#include <privatedata/PrivateDataBase.h>

#ifndef RN_EXPORT
#define RN_EXPORT __attribute__((visibility("default")))
Expand Down Expand Up @@ -211,10 +212,10 @@ class Object : public noncopyable {
template<typename ReturnType>
ReturnType* getPrivate() const {
const bool isCustomJSC = isCustomJSCPtr(m_context);
return static_cast<ReturnType*>(JSC_JSObjectGetPrivate(isCustomJSC, m_obj));
return PrivateDataBase::cast<ReturnType>(JSC_JSObjectGetPrivate(isCustomJSC, m_obj));
}

void setPrivate(void* data) const {
void setPrivate(PrivateDataBase* data) const {
const bool isCustomJSC = isCustomJSCPtr(m_context);
JSC_JSObjectSetPrivate(isCustomJSC, m_obj, data);
}
Expand Down
21 changes: 21 additions & 0 deletions ReactCommon/privatedata/Android.mk
@@ -0,0 +1,21 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := privatedata

LOCAL_SRC_FILES := \
PrivateDataBase.cpp \

LOCAL_C_INCLUDES := $(LOCAL_PATH)/..
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)

LOCAL_CFLAGS := \
-DLOG_TAG=\"ReactNative\"

LOCAL_CFLAGS += -Wall -Werror -fexceptions -frtti
CXX11_FLAGS := -std=c++11
LOCAL_CFLAGS += $(CXX11_FLAGS)
LOCAL_EXPORT_CPPFLAGS := $(CXX11_FLAGS)

include $(BUILD_SHARED_LIBRARY)
23 changes: 23 additions & 0 deletions ReactCommon/privatedata/BUCK
@@ -0,0 +1,23 @@
include_defs("//ReactCommon/DEFS")

rn_xplat_cxx_library(
name = "privatedata",
srcs = glob(["**/*.cpp"]),
header_namespace = "",
exported_headers = subdir_glob(
[
("", "**/*.h"),
],
prefix = "privatedata",
),
compiler_flags = [
"-Wall",
"-fexceptions",
"-frtti",
"-fvisibility=hidden",
"-std=c++1y",
],
visibility = [
"PUBLIC",
],
)
10 changes: 10 additions & 0 deletions ReactCommon/privatedata/PrivateDataBase.cpp
@@ -0,0 +1,10 @@
// Copyright 2004-present Facebook. All Rights Reserved.

#include "PrivateDataBase.h"

namespace facebook {
namespace react {

PrivateDataBase::~PrivateDataBase() {}

} }
42 changes: 42 additions & 0 deletions ReactCommon/privatedata/PrivateDataBase.h
@@ -0,0 +1,42 @@
// Copyright 2004-present Facebook. All Rights Reserved.

#pragma once

#include <cassert>
#include <cstdlib>
#include <type_traits>

#ifndef RN_EXPORT
#define RN_EXPORT __attribute__((visibility("default")))
#endif

namespace facebook {
namespace react {

// Base class for private data used to implement hybrid JS-native objects. A common root class,
// rtti and dynamic_cast allow us to do some runtime type checking that makes it possible
// for multiple hybrid object implementations to co-exist.
class RN_EXPORT PrivateDataBase {
public:
virtual ~PrivateDataBase();

// Casts given void* to PrivateDataBase and performs dynamic_cast to desired type. Returns null on
// failure.
template <typename T>
static typename std::enable_if<std::is_base_of<PrivateDataBase, T>::value, T>::type* tryCast(void* ptr) {
return dynamic_cast<T*>(reinterpret_cast<PrivateDataBase*>(ptr));
}

// Like tryCast, but aborts on failure.
template <typename T>
static typename std::enable_if<std::is_base_of<PrivateDataBase, T>::value, T>::type* cast(void* ptr) {
auto result = tryCast<T>(ptr);
if (!result) {
assert(false && "could not cast to desired type");
abort();
}
return result;
}
};

} }

0 comments on commit d6c519b

Please sign in to comment.