2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-06-21 17:08:31 +00:00
|
|
|
#include <folly/dynamic.h>
|
2016-11-22 14:05:36 +00:00
|
|
|
#include <jschelpers/JavaScriptCore.h>
|
|
|
|
#include <jschelpers/Unicode.h>
|
2017-02-15 14:12:46 +00:00
|
|
|
#include <jschelpers/noncopyable.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
class Value;
|
|
|
|
class Context;
|
|
|
|
|
2017-02-15 14:12:46 +00:00
|
|
|
class JSException : public std::exception {
|
2016-06-16 23:00:22 +00:00
|
|
|
public:
|
|
|
|
explicit JSException(const char* msg)
|
2017-02-15 14:12:46 +00:00
|
|
|
: msg_(msg), stack_("") {}
|
2016-06-16 23:00:22 +00:00
|
|
|
|
|
|
|
JSException(const char* msg, const char* stack)
|
2017-02-15 14:12:46 +00:00
|
|
|
: msg_(msg), stack_(stack) {}
|
2016-06-16 23:00:22 +00:00
|
|
|
|
|
|
|
const std::string& getStack() const {
|
|
|
|
return stack_;
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:12:46 +00:00
|
|
|
virtual const char* what() const noexcept override {
|
|
|
|
return msg_.c_str();
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
private:
|
2017-02-15 14:12:46 +00:00
|
|
|
std::string msg_;
|
2016-06-16 23:00:22 +00:00
|
|
|
std::string stack_;
|
|
|
|
};
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
class String : public noncopyable {
|
|
|
|
public:
|
2016-11-18 14:25:29 +00:00
|
|
|
explicit String(JSContextRef context, const char* utf8) :
|
2016-11-22 14:05:38 +00:00
|
|
|
m_context(context), m_string(JSC_JSStringCreateWithUTF8CString(context, utf8))
|
2016-05-04 02:29:58 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
String(String&& other) :
|
2016-11-18 14:25:29 +00:00
|
|
|
m_context(other.m_context), m_string(other.m_string)
|
2016-07-06 13:30:25 +00:00
|
|
|
{
|
|
|
|
other.m_string = nullptr;
|
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
String(const String& other) :
|
2016-11-18 14:25:29 +00:00
|
|
|
m_context(other.m_context), m_string(other.m_string)
|
2016-05-04 02:29:58 +00:00
|
|
|
{
|
|
|
|
if (m_string) {
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSStringRetain(m_context, m_string);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~String() {
|
|
|
|
if (m_string) {
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSStringRelease(m_context, m_string);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
operator JSStringRef() const {
|
|
|
|
return m_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Length in characters
|
|
|
|
size_t length() const {
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSStringGetLength(m_context, m_string);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Length in bytes of a null-terminated utf8 encoded value
|
|
|
|
size_t utf8Size() const {
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSStringGetMaximumUTF8CStringSize(m_context, m_string);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 00:06:18 +00:00
|
|
|
/*
|
|
|
|
* JavaScriptCore is built with strict utf16 -> utf8 conversion.
|
|
|
|
* This means if JSC's built-in conversion function encounters a JavaScript
|
|
|
|
* string which contains half of a 32-bit UTF-16 symbol, it produces an error
|
|
|
|
* rather than returning a string.
|
|
|
|
*
|
|
|
|
* Instead of relying on this, we use our own utf16 -> utf8 conversion function
|
|
|
|
* which is more lenient and always returns a string. When an invalid UTF-16
|
|
|
|
* string is provided, it'll likely manifest as a rendering glitch in the app for
|
|
|
|
* the invalid symbol.
|
|
|
|
*
|
|
|
|
* For details on JavaScript's unicode support see:
|
|
|
|
* https://mathiasbynens.be/notes/javascript-unicode
|
|
|
|
*/
|
2016-05-04 02:29:58 +00:00
|
|
|
std::string str() const {
|
2016-11-22 14:05:38 +00:00
|
|
|
const JSChar* utf16 = JSC_JSStringGetCharactersPtr(m_context, m_string);
|
|
|
|
int stringLength = JSC_JSStringGetLength(m_context, m_string);
|
2016-08-24 00:06:18 +00:00
|
|
|
return unicode::utf16toUTF8(utf16, stringLength);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assumes that utf8 is null terminated
|
|
|
|
bool equals(const char* utf8) {
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSStringIsEqualToUTF8CString(m_context, m_string, utf8);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-30 23:41:27 +00:00
|
|
|
// This assumes ascii is nul-terminated.
|
2016-11-18 14:25:29 +00:00
|
|
|
static String createExpectingAscii(JSContextRef context, const char* ascii, size_t len) {
|
2016-06-22 05:06:09 +00:00
|
|
|
#if WITH_FBJSCEXTENSIONS
|
2016-11-22 14:05:38 +00:00
|
|
|
return String(context, JSC_JSStringCreateWithUTF8CStringExpectAscii(context, ascii, len), true);
|
2016-06-22 05:06:09 +00:00
|
|
|
#else
|
2016-11-22 14:05:38 +00:00
|
|
|
return String(context, JSC_JSStringCreateWithUTF8CString(context, ascii), true);
|
2016-06-22 05:06:09 +00:00
|
|
|
#endif
|
2016-05-14 00:15:06 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:25:29 +00:00
|
|
|
static String createExpectingAscii(JSContextRef context, std::string const &ascii) {
|
|
|
|
return createExpectingAscii(context, ascii.c_str(), ascii.size());
|
2016-06-30 23:41:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:25:29 +00:00
|
|
|
static String ref(JSContextRef context, JSStringRef string) {
|
|
|
|
return String(context, string, false);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:25:29 +00:00
|
|
|
static String adopt(JSContextRef context, JSStringRef string) {
|
|
|
|
return String(context, string, true);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-18 14:25:29 +00:00
|
|
|
explicit String(JSContextRef context, JSStringRef string, bool adopt) :
|
|
|
|
m_context(context), m_string(string)
|
2016-05-04 02:29:58 +00:00
|
|
|
{
|
|
|
|
if (!adopt && string) {
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSStringRetain(context, string);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 14:25:29 +00:00
|
|
|
JSContextRef m_context;
|
2016-05-04 02:29:58 +00:00
|
|
|
JSStringRef m_string;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Object : public noncopyable {
|
|
|
|
public:
|
|
|
|
Object(JSContextRef context, JSObjectRef obj) :
|
|
|
|
m_context(context),
|
|
|
|
m_obj(obj)
|
|
|
|
{}
|
|
|
|
|
|
|
|
Object(Object&& other) :
|
|
|
|
m_context(other.m_context),
|
|
|
|
m_obj(other.m_obj),
|
|
|
|
m_isProtected(other.m_isProtected) {
|
|
|
|
other.m_obj = nullptr;
|
|
|
|
other.m_isProtected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Object() {
|
|
|
|
if (m_isProtected && m_obj) {
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSValueUnprotect(m_context, m_obj);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 17:08:31 +00:00
|
|
|
Object& operator=(Object&& other) {
|
|
|
|
std::swap(m_context, other.m_context);
|
|
|
|
std::swap(m_obj, other.m_obj);
|
|
|
|
std::swap(m_isProtected, other.m_isProtected);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
operator JSObjectRef() const {
|
|
|
|
return m_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator Value() const;
|
|
|
|
|
|
|
|
bool isFunction() const {
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSObjectIsFunction(m_context, m_obj);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 17:08:31 +00:00
|
|
|
Value callAsFunction(std::initializer_list<JSValueRef> args) const;
|
2016-07-06 13:30:25 +00:00
|
|
|
Value callAsFunction(const Object& thisObj, std::initializer_list<JSValueRef> args) const;
|
2016-06-21 17:08:31 +00:00
|
|
|
Value callAsFunction(int nArgs, const JSValueRef args[]) const;
|
2016-07-06 13:30:25 +00:00
|
|
|
Value callAsFunction(const Object& thisObj, int nArgs, const JSValueRef args[]) const;
|
|
|
|
|
|
|
|
Object callAsConstructor(std::initializer_list<JSValueRef> args) const;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
Value getProperty(const String& propName) const;
|
|
|
|
Value getProperty(const char *propName) const;
|
|
|
|
Value getPropertyAtIndex(unsigned index) const;
|
|
|
|
void setProperty(const String& propName, const Value& value) const;
|
|
|
|
void setProperty(const char *propName, const Value& value) const;
|
2016-07-06 13:30:25 +00:00
|
|
|
std::vector<String> getPropertyNames() const;
|
2016-05-04 02:29:58 +00:00
|
|
|
std::unordered_map<std::string, std::string> toJSONMap() const;
|
|
|
|
|
|
|
|
void makeProtected() {
|
|
|
|
if (!m_isProtected && m_obj) {
|
2016-11-22 14:05:38 +00:00
|
|
|
JSC_JSValueProtect(m_context, m_obj);
|
2016-05-04 02:29:58 +00:00
|
|
|
m_isProtected = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:36:00 +00:00
|
|
|
template<typename ReturnType>
|
|
|
|
ReturnType* getPrivate() const {
|
2016-11-22 14:05:38 +00:00
|
|
|
const bool isCustomJSC = isCustomJSCPtr(m_context);
|
|
|
|
return static_cast<ReturnType*>(JSC_JSObjectGetPrivate(isCustomJSC, m_obj));
|
2016-07-07 11:36:00 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:25:24 +00:00
|
|
|
void setPrivate(void* data) const {
|
2016-11-22 14:05:38 +00:00
|
|
|
const bool isCustomJSC = isCustomJSCPtr(m_context);
|
|
|
|
JSC_JSObjectSetPrivate(isCustomJSC, m_obj, data);
|
2016-11-18 14:25:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-06 13:30:25 +00:00
|
|
|
JSContextRef context() const {
|
|
|
|
return m_context;
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
static Object getGlobalObject(JSContextRef ctx) {
|
2016-11-22 14:05:38 +00:00
|
|
|
auto globalObj = JSC_JSContextGetGlobalObject(ctx);
|
2016-05-04 02:29:58 +00:00
|
|
|
return Object(ctx, globalObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an instance of the default object class.
|
|
|
|
*/
|
|
|
|
static Object create(JSContextRef ctx);
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSContextRef m_context;
|
|
|
|
JSObjectRef m_obj;
|
|
|
|
bool m_isProtected = false;
|
2016-07-06 13:30:25 +00:00
|
|
|
|
|
|
|
Value callAsFunction(JSObjectRef thisObj, int nArgs, const JSValueRef args[]) const;
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Value : public noncopyable {
|
|
|
|
public:
|
|
|
|
Value(JSContextRef context, JSValueRef value);
|
2016-05-10 12:02:56 +00:00
|
|
|
Value(JSContextRef context, JSStringRef value);
|
2016-05-04 02:29:58 +00:00
|
|
|
Value(Value&&);
|
|
|
|
|
|
|
|
operator JSValueRef() const {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2016-07-07 11:36:00 +00:00
|
|
|
JSType getType() const {
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueGetType(m_context, m_value);
|
2016-07-07 11:36:00 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
bool isBoolean() const {
|
2016-11-18 14:25:28 +00:00
|
|
|
return getType() == kJSTypeBoolean;
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool asBoolean() const {
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueToBoolean(context(), m_value);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isNumber() const {
|
2016-11-18 14:25:28 +00:00
|
|
|
return getType() == kJSTypeNumber;
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isNull() const {
|
2016-11-18 14:25:28 +00:00
|
|
|
return getType() == kJSTypeNull;
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isUndefined() const {
|
2016-11-18 14:25:28 +00:00
|
|
|
return getType() == kJSTypeUndefined;
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double asNumber() const {
|
|
|
|
if (isNumber()) {
|
2016-11-22 14:05:38 +00:00
|
|
|
return JSC_JSValueToNumber(context(), m_value, nullptr);
|
2016-05-04 02:29:58 +00:00
|
|
|
} else {
|
|
|
|
return 0.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t asInteger() const {
|
|
|
|
return static_cast<int32_t>(asNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t asUnsignedInteger() const {
|
|
|
|
return static_cast<uint32_t>(asNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isObject() const {
|
2016-11-18 14:25:28 +00:00
|
|
|
return getType() == kJSTypeObject;
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object asObject();
|
|
|
|
|
|
|
|
bool isString() const {
|
2016-11-18 14:25:28 +00:00
|
|
|
return getType() == kJSTypeString;
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
String toString() noexcept {
|
2016-11-22 14:05:38 +00:00
|
|
|
return String::adopt(context(), JSC_JSValueToStringCopy(context(), m_value, nullptr));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 14:25:24 +00:00
|
|
|
static Value makeError(JSContextRef ctx, const char *error);
|
|
|
|
|
|
|
|
static Value makeNumber(JSContextRef ctx, double value) {
|
2016-11-22 14:05:38 +00:00
|
|
|
return Value(ctx, JSC_JSValueMakeNumber(ctx, value));
|
2016-11-18 14:25:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Value makeUndefined(JSContextRef ctx) {
|
2016-11-22 14:05:38 +00:00
|
|
|
return Value(ctx, JSC_JSValueMakeUndefined(ctx));
|
2016-11-18 14:25:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 00:32:42 +00:00
|
|
|
std::string toJSONString(unsigned indent = 0) const;
|
|
|
|
static Value fromJSON(JSContextRef ctx, const String& json);
|
2016-07-20 11:25:53 +00:00
|
|
|
static JSValueRef fromDynamic(JSContextRef ctx, const folly::dynamic& value);
|
2016-05-04 02:29:58 +00:00
|
|
|
JSContextRef context() const;
|
2016-07-06 13:30:25 +00:00
|
|
|
protected:
|
2016-05-04 02:29:58 +00:00
|
|
|
JSContextRef m_context;
|
|
|
|
JSValueRef m_value;
|
2016-07-20 11:25:53 +00:00
|
|
|
static JSValueRef fromDynamicInner(JSContextRef ctx, const folly::dynamic& obj);
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} }
|