2016-03-30 21:11:57 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 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.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "js_list.hpp"
|
2016-04-18 08:14:48 +00:00
|
|
|
#include "js_realm_object.hpp"
|
2016-03-30 21:11:57 +00:00
|
|
|
#include "js_schema.hpp"
|
|
|
|
|
|
|
|
namespace realm {
|
|
|
|
namespace js {
|
|
|
|
|
|
|
|
template<typename T>
|
2016-04-14 18:06:17 +00:00
|
|
|
struct NativeAccessor {
|
2016-04-18 19:15:00 +00:00
|
|
|
using ContextType = typename T::Context;
|
|
|
|
using ObjectType = typename T::Object;
|
|
|
|
using ValueType = typename T::Value;
|
2016-04-19 01:30:55 +00:00
|
|
|
using Object = js::Object<T>;
|
|
|
|
using Value = js::Value<T>;
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
static bool dict_has_value_for_key(ContextType ctx, ValueType dict, const std::string &prop_name) {
|
|
|
|
ObjectType object = Value::validated_to_object(ctx, dict);
|
2016-03-30 21:11:57 +00:00
|
|
|
return Object::has_property(ctx, object, prop_name);
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType dict_value_for_key(ContextType ctx, ValueType dict, const std::string &prop_name) {
|
|
|
|
ObjectType object = Value::validated_to_object(ctx, dict);
|
2016-03-30 21:11:57 +00:00
|
|
|
return Object::get_property(ctx, object, prop_name);
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
static bool has_default_value_for_property(ContextType ctx, realm::Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name) {
|
2016-03-30 21:11:57 +00:00
|
|
|
auto defaults = get_delegate<T>(realm)->m_defaults[object_schema.name];
|
|
|
|
return defaults.count(prop_name) != 0;
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType default_value_for_property(ContextType ctx, realm::Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name) {
|
2016-03-30 21:11:57 +00:00
|
|
|
auto defaults = get_delegate<T>(realm)->m_defaults[object_schema.name];
|
|
|
|
return defaults.at(prop_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// These must be implemented for each JS engine.
|
2016-04-18 19:15:00 +00:00
|
|
|
static std::string to_binary(ContextType, ValueType &);
|
|
|
|
static ValueType from_binary(ContextType, BinaryData);
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
static bool to_bool(ContextType ctx, ValueType &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_boolean(ctx, value, "Property");
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType from_bool(ContextType ctx, bool boolean) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_boolean(ctx, boolean);
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static long long to_long(ContextType ctx, ValueType &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_number(ctx, value, "Property");
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType from_long(ContextType ctx, long long number) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_number(ctx, number);
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static float to_float(ContextType ctx, ValueType &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_number(ctx, value, "Property");
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType from_float(ContextType ctx, float number) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_number(ctx, number);
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static double to_double(ContextType ctx, ValueType &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_number(ctx, value, "Property");
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType from_double(ContextType ctx, double number) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_number(ctx, number);
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static std::string to_string(ContextType ctx, ValueType &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_string(ctx, value, "Property");
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType from_string(ContextType ctx, StringData string) {
|
2016-04-12 21:42:05 +00:00
|
|
|
return Value::from_string(ctx, string.data());
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
2016-05-05 20:09:07 +00:00
|
|
|
static Timestamp to_timestamp(ContextType ctx, ValueType &value) {
|
2016-04-18 19:15:00 +00:00
|
|
|
ObjectType date = Value::validated_to_date(ctx, value, "Property");
|
2016-05-05 20:09:07 +00:00
|
|
|
double milliseconds = Value::to_number(ctx, date);
|
2016-05-24 15:23:21 +00:00
|
|
|
int64_t seconds = milliseconds / 1000;
|
2016-05-24 15:25:44 +00:00
|
|
|
int32_t nanoseconds = ((int64_t)milliseconds % 1000) * 1000000;
|
2016-05-05 20:09:07 +00:00
|
|
|
return Timestamp(seconds, nanoseconds);
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
2016-05-05 20:09:07 +00:00
|
|
|
static ValueType from_timestamp(ContextType ctx, Timestamp ts) {
|
|
|
|
return Object::create_date(ctx, ts.get_seconds() * 1000 + ts.get_nanoseconds() / 1000000);
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
static bool is_null(ContextType ctx, ValueType &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::is_null(ctx, value) || Value::is_undefined(ctx, value);
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType null_value(ContextType ctx) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_null(ctx);
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
static size_t to_object_index(ContextType ctx, SharedRealm realm, ValueType &value, const std::string &type, bool try_update) {
|
|
|
|
ObjectType object = Value::validated_to_object(ctx, value);
|
2016-04-15 20:47:01 +00:00
|
|
|
if (Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
|
|
|
|
return get_internal<T, RealmObjectClass<T>>(object)->row().get_index();
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto object_schema = realm->config().schema->find(type);
|
|
|
|
if (Value::is_array(ctx, object)) {
|
|
|
|
object = Schema<T>::dict_for_property_array(ctx, *object_schema, object);
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
auto child = realm::Object::create<ValueType>(ctx, realm, *object_schema, static_cast<ValueType>(object), try_update);
|
2016-03-30 21:11:57 +00:00
|
|
|
return child.row().get_index();
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static size_t to_existing_object_index(ContextType ctx, ValueType &value) {
|
|
|
|
ObjectType object = Value::validated_to_object(ctx, value);
|
2016-04-15 20:47:01 +00:00
|
|
|
if (Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
|
|
|
|
return get_internal<T, RealmObjectClass<T>>(object)->row().get_index();
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
throw std::runtime_error("object is not a Realm Object");
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType from_object(ContextType ctx, realm::Object realm_object) {
|
2016-05-14 00:21:11 +00:00
|
|
|
return RealmObjectClass<T>::create_instance(ctx, realm_object);
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
static size_t list_size(ContextType ctx, ValueType &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Object::validated_get_length(ctx, Value::validated_to_object(ctx, value));
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType list_value_at_index(ContextType ctx, ValueType &value, size_t index) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Object::validated_get_object(ctx, Value::validated_to_object(ctx, value), (uint32_t)index);
|
|
|
|
}
|
2016-04-18 19:15:00 +00:00
|
|
|
static ValueType from_list(ContextType ctx, realm::List list) {
|
2016-05-14 00:12:06 +00:00
|
|
|
return ListClass<T>::create_instance(ctx, list);
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
static Mixed to_mixed(ContextType ctx, ValueType &val) {
|
2016-03-30 21:11:57 +00:00
|
|
|
throw std::runtime_error("'Any' type is unsupported");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // js
|
|
|
|
} // realm
|