Skip to content

Commit

Permalink
More useful utilities in jschelpers
Browse files Browse the repository at this point in the history
Reviewed By: mhorowitz

Differential Revision: D5785204

fbshipit-source-id: 4c5240f24c6a69bb781896d3af32d4d16fc2387c
  • Loading branch information
Michał Gregorczyk authored and facebook-github-bot committed Sep 19, 2017
1 parent d6c519b commit 0ee502d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions ReactCommon/jschelpers/JSCWrapper.h
Expand Up @@ -103,6 +103,7 @@ struct JSCWrapper {
JSC_WRAPPER_METHOD(JSObjectIsFunction); JSC_WRAPPER_METHOD(JSObjectIsFunction);
JSC_WRAPPER_METHOD(JSObjectMake); JSC_WRAPPER_METHOD(JSObjectMake);
JSC_WRAPPER_METHOD(JSObjectMakeArray); JSC_WRAPPER_METHOD(JSObjectMakeArray);
JSC_WRAPPER_METHOD(JSObjectMakeDate);
JSC_WRAPPER_METHOD(JSObjectMakeError); JSC_WRAPPER_METHOD(JSObjectMakeError);
JSC_WRAPPER_METHOD(JSObjectMakeFunctionWithCallback); JSC_WRAPPER_METHOD(JSObjectMakeFunctionWithCallback);
JSC_WRAPPER_METHOD(JSObjectSetPrivate); JSC_WRAPPER_METHOD(JSObjectSetPrivate);
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/jschelpers/JavaScriptCore.h
Expand Up @@ -145,6 +145,7 @@ jsc_poison(JSClassCreate JSClassRelease JSClassRetain)
#define JSC_JSObjectIsFunction(...) __jsc_wrapper(JSObjectIsFunction, __VA_ARGS__) #define JSC_JSObjectIsFunction(...) __jsc_wrapper(JSObjectIsFunction, __VA_ARGS__)
#define JSC_JSObjectMake(...) __jsc_wrapper(JSObjectMake, __VA_ARGS__) #define JSC_JSObjectMake(...) __jsc_wrapper(JSObjectMake, __VA_ARGS__)
#define JSC_JSObjectMakeArray(...) __jsc_wrapper(JSObjectMakeArray, __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_JSObjectMakeError(...) __jsc_wrapper(JSObjectMakeError, __VA_ARGS__)
#define JSC_JSObjectMakeFunctionWithCallback(...) __jsc_wrapper(JSObjectMakeFunctionWithCallback, __VA_ARGS__) #define JSC_JSObjectMakeFunctionWithCallback(...) __jsc_wrapper(JSObjectMakeFunctionWithCallback, __VA_ARGS__)
#define JSC_JSObjectSetPrivate(...) __jsc_bool_wrapper(JSObjectSetPrivate, __VA_ARGS__) #define JSC_JSObjectSetPrivate(...) __jsc_bool_wrapper(JSObjectSetPrivate, __VA_ARGS__)
Expand Down
28 changes: 28 additions & 0 deletions ReactCommon/jschelpers/Value.cpp
Expand Up @@ -18,6 +18,33 @@
namespace facebook { namespace facebook {
namespace react { 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<milliseconds>(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) Value::Value(JSContextRef context, JSValueRef value)
: m_context(context), m_value(value) {} : m_context(context), m_value(value) {}


Expand All @@ -28,6 +55,7 @@ JSContextRef Value::context() const {
return m_context; return m_context;
} }


/* static */
std::string Value::toJSONString(unsigned indent) const { std::string Value::toJSONString(unsigned indent) const {
JSValueRef exn; JSValueRef exn;
auto stringToAdopt = JSC_JSValueCreateJSONString(m_context, m_value, indent, &exn); auto stringToAdopt = JSC_JSValueCreateJSONString(m_context, m_value, indent, &exn);
Expand Down
14 changes: 14 additions & 0 deletions ReactCommon/jschelpers/Value.h
Expand Up @@ -2,6 +2,7 @@


#pragma once #pragma once


#include <chrono>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <unordered_map> #include <unordered_map>
Expand Down Expand Up @@ -150,6 +151,8 @@ class String : public noncopyable {
// heap-allocated, since otherwise you may end up with an invalid reference. // heap-allocated, since otherwise you may end up with an invalid reference.
class Object : public noncopyable { class Object : public noncopyable {
public: public:
using TimeType = std::chrono::time_point<std::chrono::system_clock>;

Object(JSContextRef context, JSObjectRef obj) : Object(JSContextRef context, JSObjectRef obj) :
m_context(context), m_context(context),
m_obj(obj) m_obj(obj)
Expand Down Expand Up @@ -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<typename ReturnType> template<typename ReturnType>
ReturnType* getPrivate() const { ReturnType* getPrivate() const {
const bool isCustomJSC = isCustomJSCPtr(m_context); const bool isCustomJSC = isCustomJSCPtr(m_context);
Expand Down Expand Up @@ -332,6 +338,14 @@ class Value : public noncopyable {
return Value(ctx, JSC_JSValueMakeNull(ctx)); 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 std::string toJSONString(unsigned indent = 0) const;
RN_EXPORT static Value fromJSON(const String& json); RN_EXPORT static Value fromJSON(const String& json);
RN_EXPORT static Value fromDynamic(JSContextRef ctx, const folly::dynamic& value); RN_EXPORT static Value fromDynamic(JSContextRef ctx, const folly::dynamic& value);
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/jschelpers/systemJSCWrapper.cpp
Expand Up @@ -94,6 +94,7 @@ const JSCWrapper* systemJSCWrapper() {
.JSObjectIsFunction = JSObjectIsFunction, .JSObjectIsFunction = JSObjectIsFunction,
.JSObjectMake = JSObjectMake, .JSObjectMake = JSObjectMake,
.JSObjectMakeArray = JSObjectMakeArray, .JSObjectMakeArray = JSObjectMakeArray,
.JSObjectMakeDate = JSObjectMakeDate,
.JSObjectMakeError = JSObjectMakeError, .JSObjectMakeError = JSObjectMakeError,
.JSObjectMakeFunctionWithCallback = JSObjectMakeFunctionWithCallback, .JSObjectMakeFunctionWithCallback = JSObjectMakeFunctionWithCallback,
.JSObjectSetPrivate = JSObjectSetPrivate, .JSObjectSetPrivate = JSObjectSetPrivate,
Expand Down

0 comments on commit 0ee502d

Please sign in to comment.