diff --git a/ReactCommon/jschelpers/JSCWrapper.h b/ReactCommon/jschelpers/JSCWrapper.h index 46ca0a67d..e2d69a922 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 1d6beb030..9344fd1d9 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 fcf7c5d19..f084335f3 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 13674817a..d78103a84 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 @@ private: // 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 @@ public: } } + 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 @@ public: 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 4b04a43cb..c501f1fa5 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,