Permalink
Browse files

Base class for all object private data passed to JSC

Reviewed By: amnn

Differential Revision: D5761937

fbshipit-source-id: de88cf8b959fc855c3c0786f817bb6032491c819
  • Loading branch information...
michalgr authored and facebook-github-bot committed Sep 19, 2017
1 parent 8bf8b21 commit d6c519bc9669221025cfedeeca08de1c795d6aa5

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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)
@@ -44,3 +44,4 @@ $(call import-module,folly)
$(call import-module,jsc)
$(call import-module,glog)
$(call import-module,jschelpers)
$(call import-module,privatedata)
@@ -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")))
@@ -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.
@@ -27,3 +27,4 @@ include $(BUILD_STATIC_LIBRARY)
$(call import-module,folly)
$(call import-module,jsc)
$(call import-module,glog)
$(call import-module,privatedata)
@@ -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 = [
@@ -14,6 +14,7 @@
#include "JavaScriptCore.h"
#include "Value.h"
#include <privatedata/PrivateDataBase.h>
#if WITH_FBJSCEXTENSIONS
#undef ASSERT
@@ -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,
@@ -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) {
@@ -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>();
@@ -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;
}
@@ -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")))
@@ -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);
}
@@ -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)
@@ -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",
],
)
@@ -0,0 +1,10 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#include "PrivateDataBase.h"
namespace facebook {
namespace react {
PrivateDataBase::~PrivateDataBase() {}
} }
@@ -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.