2015-10-08 17:57:07 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2015 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
#import "RealmRPC.hpp"
|
2015-10-08 17:57:07 +00:00
|
|
|
|
2015-10-19 22:26:42 +00:00
|
|
|
#include <dlfcn.h>
|
2015-10-08 17:57:07 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include "RealmJS.h"
|
|
|
|
#include "RJSObject.hpp"
|
|
|
|
#include "RJSResults.hpp"
|
2015-10-13 22:27:24 +00:00
|
|
|
#include "RJSList.hpp"
|
2015-10-08 17:57:07 +00:00
|
|
|
#include "RJSRealm.hpp"
|
|
|
|
#include "RJSUtil.hpp"
|
|
|
|
|
|
|
|
#include "object_accessor.hpp"
|
|
|
|
#include "shared_realm.hpp"
|
|
|
|
#include "results.hpp"
|
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
using namespace realm_js;
|
2015-10-08 17:57:07 +00:00
|
|
|
|
2015-10-19 19:06:47 +00:00
|
|
|
static const char * const RealmObjectTypesFunction = "ObjectTypesFUNCTION";
|
|
|
|
static const char * const RealmObjectTypesNotification = "ObjectTypesNOTIFICATION";
|
|
|
|
static const char * const RealmObjectTypesResults = "ObjectTypesRESULTS";
|
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
RPCServer::RPCServer() {
|
2015-10-23 00:59:05 +00:00
|
|
|
m_context = JSGlobalContextCreate(NULL);
|
2015-10-08 17:57:07 +00:00
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
// JavaScriptCore crashes when trying to walk up the native stack to print the stacktrace.
|
|
|
|
// FIXME: Avoid having to do this!
|
|
|
|
static void (*setIncludesNativeCallStack)(JSGlobalContextRef, bool) = (void (*)(JSGlobalContextRef, bool))dlsym(RTLD_DEFAULT, "JSGlobalContextSetIncludesNativeCallStackWhenReportingExceptions");
|
|
|
|
if (setIncludesNativeCallStack) {
|
2015-10-23 00:59:05 +00:00
|
|
|
setIncludesNativeCallStack(m_context, false);
|
2015-10-16 01:50:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/create_session"] = [this](const json dict) {
|
|
|
|
[RealmJS initializeContext:m_context];
|
2015-10-16 01:50:20 +00:00
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
JSStringRef realm_string = RJSStringForString("Realm");
|
2015-10-23 00:59:05 +00:00
|
|
|
JSObjectRef realm_constructor = RJSValidatedObjectProperty(m_context, JSContextGetGlobalObject(m_context), realm_string);
|
2015-10-22 17:44:10 +00:00
|
|
|
JSStringRelease(realm_string);
|
2015-10-15 10:00:13 +00:00
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
m_session_id = store_object(realm_constructor);
|
|
|
|
return (json){{"result", m_session_id}};
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/create_realm"] = [this](const json dict) {
|
|
|
|
JSObjectRef realm_constructor = m_session_id ? m_objects[m_session_id] : NULL;
|
2015-10-22 17:44:10 +00:00
|
|
|
if (!realm_constructor) {
|
|
|
|
throw std::runtime_error("Realm constructor not found!");
|
2015-10-19 22:26:42 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
json::array_t args = dict["arguments"];
|
|
|
|
size_t arg_count = args.size();
|
|
|
|
JSValueRef arg_values[arg_count];
|
2015-10-16 01:50:20 +00:00
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
for (size_t i = 0; i < arg_count; i++) {
|
|
|
|
arg_values[i] = deserialize_json_value(args[i]);
|
|
|
|
}
|
2015-10-20 22:10:22 +00:00
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
JSValueRef exception = NULL;
|
2015-10-23 00:59:05 +00:00
|
|
|
JSObjectRef realm_object = JSObjectCallAsConstructor(m_context, realm_constructor, arg_count, arg_values, &exception);
|
2015-10-22 17:44:10 +00:00
|
|
|
if (exception) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return (json){{"error", RJSStringForValue(m_context, exception)}};
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
2015-10-20 22:10:22 +00:00
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
RPCObjectID realm_id = store_object(realm_object);
|
|
|
|
return (json){{"result", realm_id}};
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/begin_transaction"] = [this](const json dict) {
|
|
|
|
RPCObjectID realm_id = dict["realmId"].get<RPCObjectID>();
|
|
|
|
RJSGetInternal<realm::SharedRealm *>(m_objects[realm_id])->get()->begin_transaction();
|
2015-10-22 23:49:32 +00:00
|
|
|
return json::object();
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/cancel_transaction"] = [this](const json dict) {
|
|
|
|
RPCObjectID realm_id = dict["realmId"].get<RPCObjectID>();
|
|
|
|
RJSGetInternal<realm::SharedRealm *>(m_objects[realm_id])->get()->cancel_transaction();
|
2015-10-22 23:49:32 +00:00
|
|
|
return json::object();
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/commit_transaction"] = [this](const json dict) {
|
|
|
|
RPCObjectID realm_id = dict["realmId"].get<RPCObjectID>();
|
|
|
|
RJSGetInternal<realm::SharedRealm *>(m_objects[realm_id])->get()->commit_transaction();
|
2015-10-22 23:49:32 +00:00
|
|
|
return json::object();
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/call_method"] = [this](const json dict) {
|
|
|
|
JSObjectRef object = m_objects[dict["id"].get<RPCObjectID>()];
|
|
|
|
JSStringRef method_string = RJSStringForString(dict["name"].get<std::string>());
|
|
|
|
JSObjectRef function = RJSValidatedObjectProperty(m_context, object, method_string);
|
|
|
|
JSStringRelease(method_string);
|
2015-10-22 17:44:10 +00:00
|
|
|
|
|
|
|
json args = dict["arguments"];
|
2015-10-23 00:59:05 +00:00
|
|
|
size_t count = args.size();
|
|
|
|
JSValueRef arg_values[count];
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
arg_values[i] = deserialize_json_value(args[i]);
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
2015-10-15 10:00:13 +00:00
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
JSValueRef exception = NULL;
|
2015-10-23 00:59:05 +00:00
|
|
|
JSValueRef result = JSObjectCallAsFunction(m_context, function, object, count, arg_values, &exception);
|
2015-10-22 17:44:10 +00:00
|
|
|
if (exception) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return (json){{"error", RJSStringForValue(m_context, exception)}};
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
2015-10-22 23:49:32 +00:00
|
|
|
return (json){{"result", serialize_json_value(result)}};
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/get_property"] = [this](const json dict) {
|
2015-10-22 17:44:10 +00:00
|
|
|
RPCObjectID oid = dict["id"].get<RPCObjectID>();
|
|
|
|
json name = dict["name"];
|
|
|
|
JSValueRef value = NULL;
|
|
|
|
JSValueRef exception = NULL;
|
|
|
|
|
|
|
|
if (name.is_number()) {
|
2015-10-23 00:59:05 +00:00
|
|
|
value = JSObjectGetPropertyAtIndex(m_context, m_objects[oid], name.get<unsigned int>(), &exception);
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-10-23 00:59:05 +00:00
|
|
|
JSStringRef prop_string = RJSStringForString(name.get<std::string>());
|
|
|
|
value = JSObjectGetProperty(m_context, m_objects[oid], prop_string, &exception);
|
|
|
|
JSStringRelease(prop_string);
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
2015-10-15 10:00:13 +00:00
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
if (exception) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return (json){{"error", RJSStringForValue(m_context, exception)}};
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
2015-10-22 23:49:32 +00:00
|
|
|
return (json){{"result", serialize_json_value(value)}};
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/set_property"] = [this](const json dict) {
|
2015-10-22 17:44:10 +00:00
|
|
|
RPCObjectID oid = dict["id"].get<RPCObjectID>();
|
|
|
|
json name = dict["name"];
|
|
|
|
JSValueRef value = deserialize_json_value(dict["value"]);
|
|
|
|
JSValueRef exception = NULL;
|
|
|
|
|
|
|
|
if (name.is_number()) {
|
2015-10-23 00:59:05 +00:00
|
|
|
JSObjectSetPropertyAtIndex(m_context, m_objects[oid], name.get<unsigned int>(), value, &exception);
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-10-23 01:06:11 +00:00
|
|
|
JSStringRef prop_string = RJSStringForString(name.get<std::string>());
|
2015-10-23 00:59:05 +00:00
|
|
|
JSObjectSetProperty(m_context, m_objects[oid], prop_string, value, 0, &exception);
|
2015-10-23 01:06:11 +00:00
|
|
|
JSStringRelease(prop_string);
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
2015-10-15 10:00:13 +00:00
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
if (exception) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return (json){{"error", RJSStringForValue(m_context, exception)}};
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
2015-10-22 23:49:32 +00:00
|
|
|
return json::object();
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/dispose_object"] = [this](const json dict) {
|
2015-10-22 17:44:10 +00:00
|
|
|
RPCObjectID oid = dict["id"].get<RPCObjectID>();
|
2015-10-23 00:59:05 +00:00
|
|
|
JSValueUnprotect(m_context, m_objects[oid]);
|
|
|
|
m_objects.erase(oid);
|
2015-10-22 23:49:32 +00:00
|
|
|
return json::object();
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
2015-10-23 00:59:05 +00:00
|
|
|
m_requests["/clear_test_state"] = [this](const json dict) {
|
|
|
|
for (auto object : m_objects) {
|
2015-10-22 17:44:10 +00:00
|
|
|
// The session ID points to the Realm constructor object, which should remain.
|
2015-10-23 00:59:05 +00:00
|
|
|
if (object.first == m_session_id) {
|
2015-10-22 17:44:10 +00:00
|
|
|
continue;
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-08 22:30:29 +00:00
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
JSValueUnprotect(m_context, object.second);
|
|
|
|
m_objects.erase(object.first);
|
2015-10-22 17:44:10 +00:00
|
|
|
}
|
2015-10-23 00:59:05 +00:00
|
|
|
JSGarbageCollect(m_context);
|
2015-10-22 17:44:10 +00:00
|
|
|
[RealmJS clearTestState];
|
2015-10-22 23:49:32 +00:00
|
|
|
return json::object();
|
2015-10-22 17:44:10 +00:00
|
|
|
};
|
|
|
|
}
|
2015-10-16 00:07:10 +00:00
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
RPCServer::~RPCServer() {
|
2015-10-23 00:59:05 +00:00
|
|
|
for (auto item : m_objects) {
|
|
|
|
JSValueUnprotect(m_context, item.second);
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-22 17:44:10 +00:00
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
JSGlobalContextRelease(m_context);
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 23:49:32 +00:00
|
|
|
json RPCServer::perform_request(std::string name, json &args) {
|
2015-10-23 01:06:11 +00:00
|
|
|
try {
|
|
|
|
RPCRequest action = m_requests[name];
|
|
|
|
assert(action);
|
|
|
|
|
|
|
|
if (name == "/create_session" || m_session_id == args["sessionId"].get<RPCObjectID>()) {
|
|
|
|
return action(args);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {{"error", "Invalid session ID"}};
|
2015-10-08 18:23:42 +00:00
|
|
|
}
|
2015-10-23 01:06:11 +00:00
|
|
|
} catch (std::exception &exception) {
|
|
|
|
return {{"error", (std::string)"exception thrown: " + exception.what()}};
|
|
|
|
}
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
RPCObjectID RPCServer::store_object(JSObjectRef object) {
|
2015-10-08 17:57:07 +00:00
|
|
|
static RPCObjectID s_next_id = 1;
|
|
|
|
RPCObjectID next_id = s_next_id++;
|
2015-10-23 00:59:05 +00:00
|
|
|
JSValueProtect(m_context, object);
|
|
|
|
m_objects[next_id] = object;
|
2015-10-08 17:57:07 +00:00
|
|
|
return next_id;
|
|
|
|
}
|
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
json RPCServer::serialize_json_value(JSValueRef value) {
|
2015-10-23 00:59:05 +00:00
|
|
|
switch (JSValueGetType(m_context, value)) {
|
2015-10-08 17:57:07 +00:00
|
|
|
case kJSTypeUndefined:
|
2015-10-22 23:49:32 +00:00
|
|
|
return json::object();
|
2015-10-08 17:57:07 +00:00
|
|
|
case kJSTypeNull:
|
2015-10-22 23:49:32 +00:00
|
|
|
return {{"value", json(nullptr)}};
|
2015-10-08 17:57:07 +00:00
|
|
|
case kJSTypeBoolean:
|
2015-10-23 00:59:05 +00:00
|
|
|
return {{"value", JSValueToBoolean(m_context, value)}};
|
2015-10-08 17:57:07 +00:00
|
|
|
case kJSTypeNumber:
|
2015-10-23 00:59:05 +00:00
|
|
|
return {{"value", JSValueToNumber(m_context, value, NULL)}};
|
2015-10-08 17:57:07 +00:00
|
|
|
case kJSTypeString:
|
2015-10-23 00:59:05 +00:00
|
|
|
return {{"value", RJSStringForValue(m_context, value)}};
|
2015-10-08 17:57:07 +00:00
|
|
|
case kJSTypeObject:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
JSObjectRef js_object = JSValueToObject(m_context, value, NULL);
|
2015-10-08 17:57:07 +00:00
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
if (JSValueIsObjectOfClass(m_context, value, RJSObjectClass())) {
|
|
|
|
realm::Object *object = RJSGetInternal<realm::Object *>(js_object);
|
2015-10-22 17:44:10 +00:00
|
|
|
return {
|
2015-10-23 00:59:05 +00:00
|
|
|
{"type", RJSTypeGet(realm::PropertyTypeObject)},
|
|
|
|
{"id", store_object(js_object)},
|
2015-10-22 23:49:32 +00:00
|
|
|
{"schema", serialize_object_schema(object->object_schema)}
|
2015-10-08 17:57:07 +00:00
|
|
|
};
|
|
|
|
}
|
2015-10-23 00:59:05 +00:00
|
|
|
else if (JSValueIsObjectOfClass(m_context, value, RJSListClass())) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(js_object);
|
2015-10-22 17:44:10 +00:00
|
|
|
return {
|
2015-10-22 23:49:32 +00:00
|
|
|
{"type", RJSTypeGet(realm::PropertyTypeArray)},
|
2015-10-23 00:59:05 +00:00
|
|
|
{"id", store_object(js_object)},
|
2015-10-22 23:49:32 +00:00
|
|
|
{"size", list->link_view->size()},
|
|
|
|
{"schema", serialize_object_schema(list->object_schema)}
|
2015-10-08 17:57:07 +00:00
|
|
|
};
|
|
|
|
}
|
2015-10-23 00:59:05 +00:00
|
|
|
else if (JSValueIsObjectOfClass(m_context, value, RJSResultsClass())) {
|
|
|
|
realm::Results *results = RJSGetInternal<realm::Results *>(js_object);
|
2015-10-22 17:44:10 +00:00
|
|
|
return {
|
2015-10-22 23:49:32 +00:00
|
|
|
{"type", RealmObjectTypesResults},
|
2015-10-23 00:59:05 +00:00
|
|
|
{"id", store_object(js_object)},
|
2015-10-22 23:49:32 +00:00
|
|
|
{"size", results->size()},
|
|
|
|
{"schema", serialize_object_schema(results->object_schema)}
|
2015-10-08 17:57:07 +00:00
|
|
|
};
|
|
|
|
}
|
2015-10-23 00:59:05 +00:00
|
|
|
else if (JSValueIsObjectOfClass(m_context, value, RJSNotificationClass())) {
|
2015-10-22 17:44:10 +00:00
|
|
|
return {
|
2015-10-22 23:49:32 +00:00
|
|
|
{"type", RealmObjectTypesNotification},
|
2015-10-23 00:59:05 +00:00
|
|
|
{"id", store_object(js_object)},
|
2015-10-15 01:00:21 +00:00
|
|
|
};
|
|
|
|
}
|
2015-10-23 00:59:05 +00:00
|
|
|
else if (RJSIsValueArray(m_context, value)) {
|
|
|
|
size_t length = RJSValidatedListLength(m_context, js_object);
|
2015-10-22 17:44:10 +00:00
|
|
|
std::vector<json> array;
|
2015-10-08 17:57:07 +00:00
|
|
|
for (unsigned int i = 0; i < length; i++) {
|
2015-10-23 00:59:05 +00:00
|
|
|
array.push_back(serialize_json_value(JSObjectGetPropertyAtIndex(m_context, js_object, i, NULL)));
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-22 23:49:32 +00:00
|
|
|
return {{"value", array}};
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-23 00:59:05 +00:00
|
|
|
else if (RJSIsValueDate(m_context, value)) {
|
2015-10-22 17:44:10 +00:00
|
|
|
return {
|
2015-10-22 23:49:32 +00:00
|
|
|
{"type", RJSTypeGet(realm::PropertyTypeDate)},
|
2015-10-23 00:59:05 +00:00
|
|
|
{"value", RJSValidatedValueToNumber(m_context, value)},
|
2015-10-19 23:46:56 +00:00
|
|
|
};
|
|
|
|
}
|
2015-10-22 17:44:10 +00:00
|
|
|
assert(0);
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
json RPCServer::serialize_object_schema(realm::ObjectSchema &object_schema) {
|
2015-10-22 23:49:32 +00:00
|
|
|
json properties = json::array();
|
2015-10-23 00:59:05 +00:00
|
|
|
for (realm::Property prop : object_schema.properties) {
|
2015-10-22 17:44:10 +00:00
|
|
|
properties.push_back({
|
2015-10-22 23:49:32 +00:00
|
|
|
{"name", prop.name},
|
|
|
|
{"type", RJSTypeGet(prop.type)},
|
2015-10-22 17:44:10 +00:00
|
|
|
});
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
return {
|
2015-10-23 00:59:05 +00:00
|
|
|
{"name", object_schema.name},
|
2015-10-22 23:49:32 +00:00
|
|
|
{"properties", properties},
|
2015-10-08 17:57:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-10-22 17:44:10 +00:00
|
|
|
JSValueRef RPCServer::deserialize_json_value(const json dict)
|
|
|
|
{
|
2015-10-22 23:49:32 +00:00
|
|
|
json oid = dict["id"];
|
|
|
|
if (oid.is_number()) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return m_objects[oid.get<RPCObjectID>()];
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-22 22:31:26 +00:00
|
|
|
json value = dict["value"];
|
2015-10-22 23:49:32 +00:00
|
|
|
json type = dict["type"];
|
|
|
|
if (type.is_string()) {
|
|
|
|
std::string type_string = type.get<std::string>();
|
|
|
|
if (type_string == RealmObjectTypesFunction) {
|
|
|
|
// FIXME: Make this actually call the function by its id once we need it to.
|
2015-10-23 00:59:05 +00:00
|
|
|
JSStringRef js_body = JSStringCreateWithUTF8CString("");
|
|
|
|
JSObjectRef js_function = JSObjectMakeFunction(m_context, NULL, 0, NULL, js_body, NULL, 1, NULL);
|
|
|
|
JSStringRelease(js_body);
|
2015-10-22 23:49:32 +00:00
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
return js_function;
|
2015-10-22 23:49:32 +00:00
|
|
|
}
|
|
|
|
else if (type_string == RJSTypeGet(realm::PropertyTypeDate)) {
|
|
|
|
JSValueRef exception = NULL;
|
2015-10-23 00:59:05 +00:00
|
|
|
JSValueRef time = JSValueMakeNumber(m_context, value.get<double>());
|
|
|
|
JSObjectRef date = JSObjectMakeDate(m_context, 1, &time, &exception);
|
2015-10-19 23:46:56 +00:00
|
|
|
|
2015-10-22 23:49:32 +00:00
|
|
|
if (exception) {
|
2015-10-23 00:59:05 +00:00
|
|
|
throw RJSException(m_context, exception);
|
2015-10-22 23:49:32 +00:00
|
|
|
}
|
|
|
|
return date;
|
2015-10-19 23:46:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-15 01:00:21 +00:00
|
|
|
|
2015-10-22 23:49:32 +00:00
|
|
|
if (value.is_null()) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return JSValueMakeNull(m_context);
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-22 22:31:26 +00:00
|
|
|
else if (value.is_boolean()) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return JSValueMakeBoolean(m_context, value.get<bool>());
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-22 22:31:26 +00:00
|
|
|
else if (value.is_number()) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return JSValueMakeNumber(m_context, value.get<double>());
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-22 22:31:26 +00:00
|
|
|
else if (value.is_string()) {
|
2015-10-23 00:59:05 +00:00
|
|
|
return RJSValueForString(m_context, value.get<std::string>());
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-22 22:31:26 +00:00
|
|
|
else if (value.is_array()) {
|
|
|
|
size_t count = value.size();
|
2015-10-23 00:59:05 +00:00
|
|
|
JSValueRef js_values[count];
|
2015-10-08 17:57:07 +00:00
|
|
|
|
2015-10-22 22:31:26 +00:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2015-10-23 00:59:05 +00:00
|
|
|
js_values[i] = deserialize_json_value(value.at(i));
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
return JSObjectMakeArray(m_context, count, js_values, NULL);
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
2015-10-22 22:31:26 +00:00
|
|
|
else if (value.is_object()) {
|
2015-10-23 00:59:05 +00:00
|
|
|
JSObjectRef js_object = JSObjectMake(m_context, NULL, NULL);
|
2015-10-08 17:57:07 +00:00
|
|
|
|
2015-10-22 22:31:26 +00:00
|
|
|
for (json::iterator it = value.begin(); it != value.end(); ++it) {
|
2015-10-23 00:59:05 +00:00
|
|
|
JSValueRef js_value = deserialize_json_value(it.value());
|
|
|
|
JSStringRef js_key = JSStringCreateWithUTF8CString(it.key().c_str());
|
2015-10-08 17:57:07 +00:00
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
JSObjectSetProperty(m_context, js_object, js_key, js_value, 0, NULL);
|
|
|
|
JSStringRelease(js_key);
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
return js_object;
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 00:59:05 +00:00
|
|
|
return JSValueMakeUndefined(m_context);
|
2015-10-08 17:57:07 +00:00
|
|
|
}
|