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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/jschelpers/JavaScriptCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
28 changes: 28 additions & 0 deletions ReactCommon/jschelpers/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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)
: m_context(context), m_value(value) {}

Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions ReactCommon/jschelpers/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <chrono>
#include <memory>
#include <sstream>
#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.
class Object : public noncopyable {
public:
using TimeType = std::chrono::time_point<std::chrono::system_clock>;

Object(JSContextRef context, JSObjectRef obj) :
m_context(context),
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>
ReturnType* getPrivate() const {
const bool isCustomJSC = isCustomJSCPtr(m_context);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/jschelpers/systemJSCWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const JSCWrapper* systemJSCWrapper() {
.JSObjectIsFunction = JSObjectIsFunction,
.JSObjectMake = JSObjectMake,
.JSObjectMakeArray = JSObjectMakeArray,
.JSObjectMakeDate = JSObjectMakeDate,
.JSObjectMakeError = JSObjectMakeError,
.JSObjectMakeFunctionWithCallback = JSObjectMakeFunctionWithCallback,
.JSObjectSetPrivate = JSObjectSetPrivate,
Expand Down

0 comments on commit 0ee502d

Please sign in to comment.