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-14 17:54:43 +00:00
|
|
|
using TContext = typename T::Context;
|
|
|
|
using TObject = typename T::Object;
|
|
|
|
using TValue = typename T::Value;
|
2016-03-30 21:11:57 +00:00
|
|
|
using Object = Object<T>;
|
|
|
|
using Value = Value<T>;
|
|
|
|
|
2016-04-14 17:54:43 +00:00
|
|
|
static bool dict_has_value_for_key(TContext ctx, TValue dict, const std::string &prop_name) {
|
|
|
|
TObject 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-14 17:54:43 +00:00
|
|
|
static TValue dict_value_for_key(TContext ctx, TValue dict, const std::string &prop_name) {
|
|
|
|
TObject 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-14 17:54:43 +00:00
|
|
|
static bool has_default_value_for_property(TContext 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-14 17:54:43 +00:00
|
|
|
static TValue default_value_for_property(TContext 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-14 17:54:43 +00:00
|
|
|
static std::string to_binary(TContext, TValue &);
|
|
|
|
static TValue from_binary(TContext, BinaryData);
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-04-14 17:54:43 +00:00
|
|
|
static bool to_bool(TContext ctx, TValue &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_boolean(ctx, value, "Property");
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static TValue from_bool(TContext ctx, bool boolean) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_boolean(ctx, boolean);
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static long long to_long(TContext ctx, TValue &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_number(ctx, value, "Property");
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static TValue from_long(TContext ctx, long long number) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_number(ctx, number);
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static float to_float(TContext ctx, TValue &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_number(ctx, value, "Property");
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static TValue from_float(TContext ctx, float number) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_number(ctx, number);
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static double to_double(TContext ctx, TValue &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_number(ctx, value, "Property");
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static TValue from_double(TContext ctx, double number) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_number(ctx, number);
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static std::string to_string(TContext ctx, TValue &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::validated_to_string(ctx, value, "Property");
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static TValue from_string(TContext 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-04-14 17:54:43 +00:00
|
|
|
static DateTime to_datetime(TContext ctx, TValue &value) {
|
|
|
|
TObject date = Value::validated_to_date(ctx, value, "Property");
|
2016-03-30 21:11:57 +00:00
|
|
|
return DateTime(Value::to_number(ctx, date));
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static TValue from_datetime(TContext ctx, DateTime dt) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Object::create_date(ctx, dt.get_datetime());
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:54:43 +00:00
|
|
|
static bool is_null(TContext ctx, TValue &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::is_null(ctx, value) || Value::is_undefined(ctx, value);
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static TValue null_value(TContext ctx) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Value::from_null(ctx);
|
|
|
|
}
|
|
|
|
|
2016-04-14 17:54:43 +00:00
|
|
|
static size_t to_object_index(TContext ctx, SharedRealm realm, TValue &value, const std::string &type, bool try_update) {
|
|
|
|
TObject 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-14 17:54:43 +00:00
|
|
|
auto child = realm::Object::create<TValue>(ctx, realm, *object_schema, static_cast<TValue>(object), try_update);
|
2016-03-30 21:11:57 +00:00
|
|
|
return child.row().get_index();
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static size_t to_existing_object_index(TContext ctx, TValue &value) {
|
|
|
|
TObject 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-14 17:54:43 +00:00
|
|
|
static TValue from_object(TContext ctx, realm::Object realm_object) {
|
2016-04-14 18:19:01 +00:00
|
|
|
return RealmObject<T>::create_instance(ctx, realm_object);
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 17:54:43 +00:00
|
|
|
static size_t list_size(TContext ctx, TValue &value) {
|
2016-03-30 21:11:57 +00:00
|
|
|
return Object::validated_get_length(ctx, Value::validated_to_object(ctx, value));
|
|
|
|
}
|
2016-04-14 17:54:43 +00:00
|
|
|
static TValue list_value_at_index(TContext ctx, TValue &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-14 17:54:43 +00:00
|
|
|
static TValue from_list(TContext ctx, realm::List list) {
|
2016-04-14 18:19:01 +00:00
|
|
|
return List<T>::create_instance(ctx, list);
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 17:54:43 +00:00
|
|
|
static Mixed to_mixed(TContext ctx, TValue &val) {
|
2016-03-30 21:11:57 +00:00
|
|
|
throw std::runtime_error("'Any' type is unsupported");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // js
|
|
|
|
} // realm
|