More useful utilities in jschelpers
Reviewed By: mhorowitz Differential Revision: D5785204 fbshipit-source-id: 4c5240f24c6a69bb781896d3af32d4d16fc2387c
This commit is contained in:
parent
d6c519bc96
commit
0ee502d125
|
@ -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);
|
||||
|
|
|
@ -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__)
|
||||
|
|
|
@ -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) {}
|
||||
|
||||
|
@ -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);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
|
@ -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<std::chrono::system_clock>;
|
||||
|
||||
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<typename ReturnType>
|
||||
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);
|
||||
|
|
|
@ -94,6 +94,7 @@ const JSCWrapper* systemJSCWrapper() {
|
|||
.JSObjectIsFunction = JSObjectIsFunction,
|
||||
.JSObjectMake = JSObjectMake,
|
||||
.JSObjectMakeArray = JSObjectMakeArray,
|
||||
.JSObjectMakeDate = JSObjectMakeDate,
|
||||
.JSObjectMakeError = JSObjectMakeError,
|
||||
.JSObjectMakeFunctionWithCallback = JSObjectMakeFunctionWithCallback,
|
||||
.JSObjectSetPrivate = JSObjectSetPrivate,
|
||||
|
|
Loading…
Reference in New Issue