From 0ee502d1254b828efb119cc06bcd213c3d843269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gregorczyk?= Date: Mon, 18 Sep 2017 18:35:48 -0700 Subject: [PATCH] More useful utilities in jschelpers Reviewed By: mhorowitz Differential Revision: D5785204 fbshipit-source-id: 4c5240f24c6a69bb781896d3af32d4d16fc2387c --- ReactCommon/jschelpers/JSCWrapper.h | 1 + ReactCommon/jschelpers/JavaScriptCore.h | 1 + ReactCommon/jschelpers/Value.cpp | 28 +++++++++++++++++++++ ReactCommon/jschelpers/Value.h | 14 +++++++++++ ReactCommon/jschelpers/systemJSCWrapper.cpp | 1 + 5 files changed, 45 insertions(+) diff --git a/ReactCommon/jschelpers/JSCWrapper.h b/ReactCommon/jschelpers/JSCWrapper.h index 46ca0a67d9c257..e2d69a922d9030 100644 --- a/ReactCommon/jschelpers/JSCWrapper.h +++ b/ReactCommon/jschelpers/JSCWrapper.h @@ -103,6 +103,7 @@ struct JSCWrapper { JSC_WRAPPER_METHOD(JSObjectIsFunction); JSC_WRAPPER_METHOD(JSObjectMake); JSC_WRAPPER_METHOD(JSObjectMakeArray); + JSC_WRAPPER_METHOD(JSObjectMakeDate); JSC_WRAPPER_METHOD(JSObjectMakeError); JSC_WRAPPER_METHOD(JSObjectMakeFunctionWithCallback); JSC_WRAPPER_METHOD(JSObjectSetPrivate); diff --git a/ReactCommon/jschelpers/JavaScriptCore.h b/ReactCommon/jschelpers/JavaScriptCore.h index 1d6beb0306d154..9344fd1d995c1d 100644 --- a/ReactCommon/jschelpers/JavaScriptCore.h +++ b/ReactCommon/jschelpers/JavaScriptCore.h @@ -145,6 +145,7 @@ jsc_poison(JSClassCreate JSClassRelease JSClassRetain) #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_JSObjectMakeDate(...) __jsc_wrapper(JSObjectMakeDate, __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__) diff --git a/ReactCommon/jschelpers/Value.cpp b/ReactCommon/jschelpers/Value.cpp index fcf7c5d19b6dea..f084335f3032c1 100644 --- a/ReactCommon/jschelpers/Value.cpp +++ b/ReactCommon/jschelpers/Value.cpp @@ -18,6 +18,33 @@ namespace facebook { namespace react { +/* static */ +Object Object::makeDate(JSContextRef ctx, Object::TimeType time) { + using std::chrono::duration_cast; + using std::chrono::milliseconds; + + JSValueRef arguments[1]; + arguments[0] = JSC_JSValueMakeNumber( + ctx, + duration_cast(time.time_since_epoch()).count()); + + JSValueRef exn; + auto result = JSC_JSObjectMakeDate(ctx, 1, arguments, &exn); + if (!result) { + throw JSException(ctx, exn, "Failed to create Date"); + } + return Object(ctx, result); +} + +Object Object::makeArray(JSContextRef ctx, JSValueRef* elements, unsigned length) { + JSValueRef exn; + auto arr = JSC_JSObjectMakeArray(ctx, length, elements, &exn); + if (!arr) { + throw JSException(ctx, exn, "Failed to create an Array"); + } + return Object(ctx, arr); +} + Value::Value(JSContextRef context, JSValueRef value) : m_context(context), m_value(value) {} @@ -28,6 +55,7 @@ JSContextRef Value::context() const { return m_context; } +/* static */ std::string Value::toJSONString(unsigned indent) const { JSValueRef exn; auto stringToAdopt = JSC_JSValueCreateJSONString(m_context, m_value, indent, &exn); diff --git a/ReactCommon/jschelpers/Value.h b/ReactCommon/jschelpers/Value.h index 13674817a0ece6..d78103a8418482 100644 --- a/ReactCommon/jschelpers/Value.h +++ b/ReactCommon/jschelpers/Value.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include #include @@ -150,6 +151,8 @@ class String : public noncopyable { // heap-allocated, since otherwise you may end up with an invalid reference. class Object : public noncopyable { public: + using TimeType = std::chrono::time_point; + Object(JSContextRef context, JSObjectRef obj) : m_context(context), m_obj(obj) @@ -209,6 +212,9 @@ class Object : public noncopyable { } } + RN_EXPORT static Object makeArray(JSContextRef ctx, JSValueRef* elements, unsigned length); + RN_EXPORT static Object makeDate(JSContextRef ctx, TimeType time); + template ReturnType* getPrivate() const { const bool isCustomJSC = isCustomJSCPtr(m_context); @@ -332,6 +338,14 @@ class Value : public noncopyable { return Value(ctx, JSC_JSValueMakeNull(ctx)); } + static Value makeBoolean(JSContextRef ctx, bool value) { + return Value(ctx, JSC_JSValueMakeBoolean(ctx, value)); + } + + static Value makeString(JSContextRef ctx, const char* utf8) { + return Value(ctx, String(ctx, utf8)); + } + RN_EXPORT std::string toJSONString(unsigned indent = 0) const; RN_EXPORT static Value fromJSON(const String& json); RN_EXPORT static Value fromDynamic(JSContextRef ctx, const folly::dynamic& value); diff --git a/ReactCommon/jschelpers/systemJSCWrapper.cpp b/ReactCommon/jschelpers/systemJSCWrapper.cpp index 4b04a43cbb6994..c501f1fa56f4f9 100644 --- a/ReactCommon/jschelpers/systemJSCWrapper.cpp +++ b/ReactCommon/jschelpers/systemJSCWrapper.cpp @@ -94,6 +94,7 @@ const JSCWrapper* systemJSCWrapper() { .JSObjectIsFunction = JSObjectIsFunction, .JSObjectMake = JSObjectMake, .JSObjectMakeArray = JSObjectMakeArray, + .JSObjectMakeDate = JSObjectMakeDate, .JSObjectMakeError = JSObjectMakeError, .JSObjectMakeFunctionWithCallback = JSObjectMakeFunctionWithCallback, .JSObjectSetPrivate = JSObjectSetPrivate,