Revert TValue to ValueType, etc.
This commit is contained in:
parent
49e2b54151
commit
1ca5a43982
|
@ -34,30 +34,30 @@ namespace js {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class List {
|
class List {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
using Object = Object<T>;
|
using Object = Object<T>;
|
||||||
using Value = Value<T>;
|
using Value = Value<T>;
|
||||||
using ReturnValue = ReturnValue<T>;
|
using ReturnValue = ReturnValue<T>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static TObject create_instance(TContext, realm::List &);
|
static ObjectType create_instance(ContextType, realm::List &);
|
||||||
|
|
||||||
// properties
|
// properties
|
||||||
static void get_length(TContext, TObject, ReturnValue &);
|
static void get_length(ContextType, ObjectType, ReturnValue &);
|
||||||
static void get_index(TContext, TObject, uint32_t, ReturnValue &);
|
static void get_index(ContextType, ObjectType, uint32_t, ReturnValue &);
|
||||||
static bool set_index(TContext, TObject, uint32_t, TValue);
|
static bool set_index(ContextType, ObjectType, uint32_t, ValueType);
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
static void push(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void push(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void pop(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void pop(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void unshift(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void unshift(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void shift(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void shift(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void splice(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void splice(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void snapshot(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void snapshot(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void filtered(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void filtered(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void sorted(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void sorted(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -85,18 +85,18 @@ struct ListClass : ClassDefinition<T, realm::List, CollectionClass<T>> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
typename T::Object List<T>::create_instance(TContext ctx, realm::List &list) {
|
typename T::Object List<T>::create_instance(ContextType ctx, realm::List &list) {
|
||||||
return create_object<T, ListClass<T>>(ctx, new realm::List(list));
|
return create_object<T, ListClass<T>>(ctx, new realm::List(list));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::get_length(TContext ctx, TObject object, ReturnValue &return_value) {
|
void List<T>::get_length(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
||||||
auto list = get_internal<T, ListClass<T>>(object);
|
auto list = get_internal<T, ListClass<T>>(object);
|
||||||
return_value.set((uint32_t)list->size());
|
return_value.set((uint32_t)list->size());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::get_index(TContext ctx, TObject object, uint32_t index, ReturnValue &return_value) {
|
void List<T>::get_index(ContextType ctx, ObjectType object, uint32_t index, ReturnValue &return_value) {
|
||||||
auto list = get_internal<T, ListClass<T>>(object);
|
auto list = get_internal<T, ListClass<T>>(object);
|
||||||
auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index));
|
auto realm_object = realm::Object(list->get_realm(), list->get_object_schema(), list->get(index));
|
||||||
|
|
||||||
|
@ -104,14 +104,14 @@ void List<T>::get_index(TContext ctx, TObject object, uint32_t index, ReturnValu
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool List<T>::set_index(TContext ctx, TObject object, uint32_t index, TValue value) {
|
bool List<T>::set_index(ContextType ctx, ObjectType object, uint32_t index, ValueType value) {
|
||||||
auto list = get_internal<T, ListClass<T>>(object);
|
auto list = get_internal<T, ListClass<T>>(object);
|
||||||
list->set(ctx, value, index);
|
list->set(ctx, value, index);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::push(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void List<T>::push(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count_at_least(argc, 1);
|
validate_argument_count_at_least(argc, 1);
|
||||||
|
|
||||||
auto list = get_internal<T, ListClass<T>>(this_object);
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
||||||
|
@ -123,7 +123,7 @@ void List<T>::push(TContext ctx, TObject this_object, size_t argc, const TValue
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::pop(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void List<T>::pop(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 0);
|
validate_argument_count(argc, 0);
|
||||||
|
|
||||||
auto list = get_internal<T, ListClass<T>>(this_object);
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
||||||
|
@ -142,7 +142,7 @@ void List<T>::pop(TContext ctx, TObject this_object, size_t argc, const TValue a
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::unshift(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void List<T>::unshift(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count_at_least(argc, 1);
|
validate_argument_count_at_least(argc, 1);
|
||||||
|
|
||||||
auto list = get_internal<T, ListClass<T>>(this_object);
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
||||||
|
@ -154,7 +154,7 @@ void List<T>::unshift(TContext ctx, TObject this_object, size_t argc, const TVal
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::shift(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void List<T>::shift(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 0);
|
validate_argument_count(argc, 0);
|
||||||
|
|
||||||
auto list = get_internal<T, ListClass<T>>(this_object);
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
||||||
|
@ -171,7 +171,7 @@ void List<T>::shift(TContext ctx, TObject this_object, size_t argc, const TValue
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::splice(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void List<T>::splice(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count_at_least(argc, 1);
|
validate_argument_count_at_least(argc, 1);
|
||||||
|
|
||||||
auto list = get_internal<T, ListClass<T>>(this_object);
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
||||||
|
@ -190,7 +190,7 @@ void List<T>::splice(TContext ctx, TObject this_object, size_t argc, const TValu
|
||||||
remove = std::min<long>(remove, size - index);
|
remove = std::min<long>(remove, size - index);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<TValue> removed_objects;
|
std::vector<ValueType> removed_objects;
|
||||||
removed_objects.reserve(remove);
|
removed_objects.reserve(remove);
|
||||||
|
|
||||||
for (size_t i = 0; i < remove; i++) {
|
for (size_t i = 0; i < remove; i++) {
|
||||||
|
@ -207,7 +207,7 @@ void List<T>::splice(TContext ctx, TObject this_object, size_t argc, const TValu
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::snapshot(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void List<T>::snapshot(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 0);
|
validate_argument_count(argc, 0);
|
||||||
|
|
||||||
auto list = get_internal<T, ListClass<T>>(this_object);
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
||||||
|
@ -215,7 +215,7 @@ void List<T>::snapshot(TContext ctx, TObject this_object, size_t argc, const TVa
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::filtered(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void List<T>::filtered(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count_at_least(argc, 1);
|
validate_argument_count_at_least(argc, 1);
|
||||||
|
|
||||||
auto list = get_internal<T, ListClass<T>>(this_object);
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
||||||
|
@ -223,7 +223,7 @@ void List<T>::filtered(TContext ctx, TObject this_object, size_t argc, const TVa
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void List<T>::sorted(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void List<T>::sorted(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 1, 2);
|
validate_argument_count(argc, 1, 2);
|
||||||
|
|
||||||
auto list = get_internal<T, ListClass<T>>(this_object);
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
||||||
|
|
|
@ -27,81 +27,81 @@ namespace js {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct NativeAccessor {
|
struct NativeAccessor {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
using Object = Object<T>;
|
using Object = Object<T>;
|
||||||
using Value = Value<T>;
|
using Value = Value<T>;
|
||||||
|
|
||||||
static bool dict_has_value_for_key(TContext ctx, TValue dict, const std::string &prop_name) {
|
static bool dict_has_value_for_key(ContextType ctx, ValueType dict, const std::string &prop_name) {
|
||||||
TObject object = Value::validated_to_object(ctx, dict);
|
ObjectType object = Value::validated_to_object(ctx, dict);
|
||||||
return Object::has_property(ctx, object, prop_name);
|
return Object::has_property(ctx, object, prop_name);
|
||||||
}
|
}
|
||||||
static TValue dict_value_for_key(TContext ctx, TValue dict, const std::string &prop_name) {
|
static ValueType dict_value_for_key(ContextType ctx, ValueType dict, const std::string &prop_name) {
|
||||||
TObject object = Value::validated_to_object(ctx, dict);
|
ObjectType object = Value::validated_to_object(ctx, dict);
|
||||||
return Object::get_property(ctx, object, prop_name);
|
return Object::get_property(ctx, object, prop_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool has_default_value_for_property(TContext ctx, realm::Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name) {
|
static bool has_default_value_for_property(ContextType ctx, realm::Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name) {
|
||||||
auto defaults = get_delegate<T>(realm)->m_defaults[object_schema.name];
|
auto defaults = get_delegate<T>(realm)->m_defaults[object_schema.name];
|
||||||
return defaults.count(prop_name) != 0;
|
return defaults.count(prop_name) != 0;
|
||||||
}
|
}
|
||||||
static TValue default_value_for_property(TContext ctx, realm::Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name) {
|
static ValueType default_value_for_property(ContextType ctx, realm::Realm *realm, const ObjectSchema &object_schema, const std::string &prop_name) {
|
||||||
auto defaults = get_delegate<T>(realm)->m_defaults[object_schema.name];
|
auto defaults = get_delegate<T>(realm)->m_defaults[object_schema.name];
|
||||||
return defaults.at(prop_name);
|
return defaults.at(prop_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// These must be implemented for each JS engine.
|
// These must be implemented for each JS engine.
|
||||||
static std::string to_binary(TContext, TValue &);
|
static std::string to_binary(ContextType, ValueType &);
|
||||||
static TValue from_binary(TContext, BinaryData);
|
static ValueType from_binary(ContextType, BinaryData);
|
||||||
|
|
||||||
static bool to_bool(TContext ctx, TValue &value) {
|
static bool to_bool(ContextType ctx, ValueType &value) {
|
||||||
return Value::validated_to_boolean(ctx, value, "Property");
|
return Value::validated_to_boolean(ctx, value, "Property");
|
||||||
}
|
}
|
||||||
static TValue from_bool(TContext ctx, bool boolean) {
|
static ValueType from_bool(ContextType ctx, bool boolean) {
|
||||||
return Value::from_boolean(ctx, boolean);
|
return Value::from_boolean(ctx, boolean);
|
||||||
}
|
}
|
||||||
static long long to_long(TContext ctx, TValue &value) {
|
static long long to_long(ContextType ctx, ValueType &value) {
|
||||||
return Value::validated_to_number(ctx, value, "Property");
|
return Value::validated_to_number(ctx, value, "Property");
|
||||||
}
|
}
|
||||||
static TValue from_long(TContext ctx, long long number) {
|
static ValueType from_long(ContextType ctx, long long number) {
|
||||||
return Value::from_number(ctx, number);
|
return Value::from_number(ctx, number);
|
||||||
}
|
}
|
||||||
static float to_float(TContext ctx, TValue &value) {
|
static float to_float(ContextType ctx, ValueType &value) {
|
||||||
return Value::validated_to_number(ctx, value, "Property");
|
return Value::validated_to_number(ctx, value, "Property");
|
||||||
}
|
}
|
||||||
static TValue from_float(TContext ctx, float number) {
|
static ValueType from_float(ContextType ctx, float number) {
|
||||||
return Value::from_number(ctx, number);
|
return Value::from_number(ctx, number);
|
||||||
}
|
}
|
||||||
static double to_double(TContext ctx, TValue &value) {
|
static double to_double(ContextType ctx, ValueType &value) {
|
||||||
return Value::validated_to_number(ctx, value, "Property");
|
return Value::validated_to_number(ctx, value, "Property");
|
||||||
}
|
}
|
||||||
static TValue from_double(TContext ctx, double number) {
|
static ValueType from_double(ContextType ctx, double number) {
|
||||||
return Value::from_number(ctx, number);
|
return Value::from_number(ctx, number);
|
||||||
}
|
}
|
||||||
static std::string to_string(TContext ctx, TValue &value) {
|
static std::string to_string(ContextType ctx, ValueType &value) {
|
||||||
return Value::validated_to_string(ctx, value, "Property");
|
return Value::validated_to_string(ctx, value, "Property");
|
||||||
}
|
}
|
||||||
static TValue from_string(TContext ctx, StringData string) {
|
static ValueType from_string(ContextType ctx, StringData string) {
|
||||||
return Value::from_string(ctx, string.data());
|
return Value::from_string(ctx, string.data());
|
||||||
}
|
}
|
||||||
static DateTime to_datetime(TContext ctx, TValue &value) {
|
static DateTime to_datetime(ContextType ctx, ValueType &value) {
|
||||||
TObject date = Value::validated_to_date(ctx, value, "Property");
|
ObjectType date = Value::validated_to_date(ctx, value, "Property");
|
||||||
return DateTime(Value::to_number(ctx, date));
|
return DateTime(Value::to_number(ctx, date));
|
||||||
}
|
}
|
||||||
static TValue from_datetime(TContext ctx, DateTime dt) {
|
static ValueType from_datetime(ContextType ctx, DateTime dt) {
|
||||||
return Object::create_date(ctx, dt.get_datetime());
|
return Object::create_date(ctx, dt.get_datetime());
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool is_null(TContext ctx, TValue &value) {
|
static bool is_null(ContextType ctx, ValueType &value) {
|
||||||
return Value::is_null(ctx, value) || Value::is_undefined(ctx, value);
|
return Value::is_null(ctx, value) || Value::is_undefined(ctx, value);
|
||||||
}
|
}
|
||||||
static TValue null_value(TContext ctx) {
|
static ValueType null_value(ContextType ctx) {
|
||||||
return Value::from_null(ctx);
|
return Value::from_null(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t to_object_index(TContext ctx, SharedRealm realm, TValue &value, const std::string &type, bool try_update) {
|
static size_t to_object_index(ContextType ctx, SharedRealm realm, ValueType &value, const std::string &type, bool try_update) {
|
||||||
TObject object = Value::validated_to_object(ctx, value);
|
ObjectType object = Value::validated_to_object(ctx, value);
|
||||||
if (Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
|
if (Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
|
||||||
return get_internal<T, RealmObjectClass<T>>(object)->row().get_index();
|
return get_internal<T, RealmObjectClass<T>>(object)->row().get_index();
|
||||||
}
|
}
|
||||||
|
@ -111,31 +111,31 @@ struct NativeAccessor {
|
||||||
object = Schema<T>::dict_for_property_array(ctx, *object_schema, object);
|
object = Schema<T>::dict_for_property_array(ctx, *object_schema, object);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto child = realm::Object::create<TValue>(ctx, realm, *object_schema, static_cast<TValue>(object), try_update);
|
auto child = realm::Object::create<ValueType>(ctx, realm, *object_schema, static_cast<ValueType>(object), try_update);
|
||||||
return child.row().get_index();
|
return child.row().get_index();
|
||||||
}
|
}
|
||||||
static size_t to_existing_object_index(TContext ctx, TValue &value) {
|
static size_t to_existing_object_index(ContextType ctx, ValueType &value) {
|
||||||
TObject object = Value::validated_to_object(ctx, value);
|
ObjectType object = Value::validated_to_object(ctx, value);
|
||||||
if (Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
|
if (Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
|
||||||
return get_internal<T, RealmObjectClass<T>>(object)->row().get_index();
|
return get_internal<T, RealmObjectClass<T>>(object)->row().get_index();
|
||||||
}
|
}
|
||||||
throw std::runtime_error("object is not a Realm Object");
|
throw std::runtime_error("object is not a Realm Object");
|
||||||
}
|
}
|
||||||
static TValue from_object(TContext ctx, realm::Object realm_object) {
|
static ValueType from_object(ContextType ctx, realm::Object realm_object) {
|
||||||
return RealmObject<T>::create_instance(ctx, realm_object);
|
return RealmObject<T>::create_instance(ctx, realm_object);
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t list_size(TContext ctx, TValue &value) {
|
static size_t list_size(ContextType ctx, ValueType &value) {
|
||||||
return Object::validated_get_length(ctx, Value::validated_to_object(ctx, value));
|
return Object::validated_get_length(ctx, Value::validated_to_object(ctx, value));
|
||||||
}
|
}
|
||||||
static TValue list_value_at_index(TContext ctx, TValue &value, size_t index) {
|
static ValueType list_value_at_index(ContextType ctx, ValueType &value, size_t index) {
|
||||||
return Object::validated_get_object(ctx, Value::validated_to_object(ctx, value), (uint32_t)index);
|
return Object::validated_get_object(ctx, Value::validated_to_object(ctx, value), (uint32_t)index);
|
||||||
}
|
}
|
||||||
static TValue from_list(TContext ctx, realm::List list) {
|
static ValueType from_list(ContextType ctx, realm::List list) {
|
||||||
return List<T>::create_instance(ctx, list);
|
return List<T>::create_instance(ctx, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Mixed to_mixed(TContext ctx, TValue &val) {
|
static Mixed to_mixed(ContextType ctx, ValueType &val) {
|
||||||
throw std::runtime_error("'Any' type is unsupported");
|
throw std::runtime_error("'Any' type is unsupported");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
140
src/js_realm.hpp
140
src/js_realm.hpp
|
@ -43,10 +43,10 @@ struct RealmClass;
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class RealmDelegate : public BindingContext {
|
class RealmDelegate : public BindingContext {
|
||||||
public:
|
public:
|
||||||
using TGlobalContext = typename T::GlobalContext;
|
using GlobalContextType = typename T::GlobalContext;
|
||||||
using TFunction = typename T::Function;
|
using FunctionType = typename T::Function;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
using Value = Value<T>;
|
using Value = Value<T>;
|
||||||
|
|
||||||
using ObjectDefaultsMap = typename Schema<T>::ObjectDefaultsMap;
|
using ObjectDefaultsMap = typename Schema<T>::ObjectDefaultsMap;
|
||||||
|
@ -60,13 +60,13 @@ class RealmDelegate : public BindingContext {
|
||||||
}
|
}
|
||||||
virtual void will_change(std::vector<ObserverState> const& observers, std::vector<void*> const& invalidated) {}
|
virtual void will_change(std::vector<ObserverState> const& observers, std::vector<void*> const& invalidated) {}
|
||||||
|
|
||||||
RealmDelegate(std::weak_ptr<Realm> realm, TGlobalContext ctx) : m_context(ctx), m_realm(realm) {}
|
RealmDelegate(std::weak_ptr<Realm> realm, GlobalContextType ctx) : m_context(ctx), m_realm(realm) {}
|
||||||
|
|
||||||
~RealmDelegate() {
|
~RealmDelegate() {
|
||||||
remove_all_notifications();
|
remove_all_notifications();
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_notification(TFunction notification) {
|
void add_notification(FunctionType notification) {
|
||||||
for (auto &handler : m_notifications) {
|
for (auto &handler : m_notifications) {
|
||||||
if (handler == notification) {
|
if (handler == notification) {
|
||||||
return;
|
return;
|
||||||
|
@ -74,7 +74,7 @@ class RealmDelegate : public BindingContext {
|
||||||
}
|
}
|
||||||
m_notifications.emplace_back(m_context, notification);
|
m_notifications.emplace_back(m_context, notification);
|
||||||
}
|
}
|
||||||
void remove_notification(TFunction notification) {
|
void remove_notification(FunctionType notification) {
|
||||||
for (auto iter = m_notifications.begin(); iter != m_notifications.end(); ++iter) {
|
for (auto iter = m_notifications.begin(); iter != m_notifications.end(); ++iter) {
|
||||||
if (*iter == notification) {
|
if (*iter == notification) {
|
||||||
m_notifications.erase(iter);
|
m_notifications.erase(iter);
|
||||||
|
@ -90,8 +90,8 @@ class RealmDelegate : public BindingContext {
|
||||||
ConstructorMap m_constructors;
|
ConstructorMap m_constructors;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Protected<TGlobalContext> m_context;
|
Protected<GlobalContextType> m_context;
|
||||||
std::list<Protected<TFunction>> m_notifications;
|
std::list<Protected<FunctionType>> m_notifications;
|
||||||
std::weak_ptr<Realm> m_realm;
|
std::weak_ptr<Realm> m_realm;
|
||||||
|
|
||||||
void notify(const char *notification_name) {
|
void notify(const char *notification_name) {
|
||||||
|
@ -100,8 +100,8 @@ class RealmDelegate : public BindingContext {
|
||||||
throw std::runtime_error("Realm no longer exists");
|
throw std::runtime_error("Realm no longer exists");
|
||||||
}
|
}
|
||||||
|
|
||||||
TObject realm_object = create_object<T, RealmClass<T>>(m_context, new SharedRealm(realm));
|
ObjectType realm_object = create_object<T, RealmClass<T>>(m_context, new SharedRealm(realm));
|
||||||
TValue arguments[2];
|
ValueType arguments[2];
|
||||||
arguments[0] = realm_object;
|
arguments[0] = realm_object;
|
||||||
arguments[1] = Value::from_string(m_context, notification_name);
|
arguments[1] = Value::from_string(m_context, notification_name);
|
||||||
|
|
||||||
|
@ -117,45 +117,45 @@ void delete_all_realms();
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Realm {
|
class Realm {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TFunction = typename T::Function;
|
using FunctionType = typename T::Function;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
using String = String<T>;
|
using String = String<T>;
|
||||||
using Object = Object<T>;
|
using Object = Object<T>;
|
||||||
using Value = Value<T>;
|
using Value = Value<T>;
|
||||||
using ReturnValue = ReturnValue<T>;
|
using ReturnValue = ReturnValue<T>;
|
||||||
using NativeAccessor = realm::NativeAccessor<TValue, TContext>;
|
using NativeAccessor = realm::NativeAccessor<ValueType, ContextType>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static TFunction create_constructor(TContext);
|
static FunctionType create_constructor(ContextType);
|
||||||
|
|
||||||
// methods
|
// methods
|
||||||
static void objects(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void objects(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void create(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void create(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void delete_one(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void delete_one(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void delete_all(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void delete_all(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void write(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void write(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void add_listener(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void add_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void remove_listener(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void remove_listener(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void remove_all_listeners(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void remove_all_listeners(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void close(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void close(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
|
|
||||||
// properties
|
// properties
|
||||||
static void get_path(TContext, TObject, ReturnValue &);
|
static void get_path(ContextType, ObjectType, ReturnValue &);
|
||||||
static void get_schema_version(TContext, TObject, ReturnValue &);
|
static void get_schema_version(ContextType, ObjectType, ReturnValue &);
|
||||||
|
|
||||||
// static methods
|
// static methods
|
||||||
static void constructor(TContext, TObject, size_t, const TValue[]);
|
static void constructor(ContextType, ObjectType, size_t, const ValueType[]);
|
||||||
static void schema_version(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void schema_version(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void clear_test_state(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void clear_test_state(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
|
|
||||||
// static properties
|
// static properties
|
||||||
static void get_default_path(TContext, TObject, ReturnValue &);
|
static void get_default_path(ContextType, ObjectType, ReturnValue &);
|
||||||
static void set_default_path(TContext, TObject, TValue value);
|
static void set_default_path(ContextType, ObjectType, ValueType value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::string validated_notification_name(TContext ctx, const TValue &value) {
|
static std::string validated_notification_name(ContextType ctx, const ValueType &value) {
|
||||||
std::string name = Value::validated_to_string(ctx, value, "notification name");
|
std::string name = Value::validated_to_string(ctx, value, "notification name");
|
||||||
if (name != "change") {
|
if (name != "change") {
|
||||||
throw std::runtime_error("Only the 'change' notification name is supported.");
|
throw std::runtime_error("Only the 'change' notification name is supported.");
|
||||||
|
@ -164,13 +164,13 @@ class Realm {
|
||||||
}
|
}
|
||||||
|
|
||||||
// converts constructor object or type name to type name
|
// converts constructor object or type name to type name
|
||||||
static std::string validated_object_type_for_value(SharedRealm &realm, TContext ctx, const TValue &value) {
|
static std::string validated_object_type_for_value(SharedRealm &realm, ContextType ctx, const ValueType &value) {
|
||||||
if (Value::is_constructor(ctx, value)) {
|
if (Value::is_constructor(ctx, value)) {
|
||||||
TFunction constructor = Value::to_constructor(ctx, value);
|
FunctionType constructor = Value::to_constructor(ctx, value);
|
||||||
|
|
||||||
auto delegate = get_delegate<T>(realm.get());
|
auto delegate = get_delegate<T>(realm.get());
|
||||||
for (auto &pair : delegate->m_constructors) {
|
for (auto &pair : delegate->m_constructors) {
|
||||||
if (TFunction(pair.second) == constructor) {
|
if (FunctionType(pair.second) == constructor) {
|
||||||
return pair.first;
|
return pair.first;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -223,11 +223,11 @@ struct RealmClass : ClassDefinition<T, SharedRealm> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline typename T::Function Realm<T>::create_constructor(TContext ctx) {
|
inline typename T::Function Realm<T>::create_constructor(ContextType ctx) {
|
||||||
TFunction realm_constructor = ObjectWrap<T, RealmClass<T>>::create_constructor(ctx);
|
FunctionType realm_constructor = ObjectWrap<T, RealmClass<T>>::create_constructor(ctx);
|
||||||
TFunction collection_constructor = ObjectWrap<T, CollectionClass<T>>::create_constructor(ctx);
|
FunctionType collection_constructor = ObjectWrap<T, CollectionClass<T>>::create_constructor(ctx);
|
||||||
TFunction list_constructor = ObjectWrap<T, ListClass<T>>::create_constructor(ctx);
|
FunctionType list_constructor = ObjectWrap<T, ListClass<T>>::create_constructor(ctx);
|
||||||
TFunction results_constructor = ObjectWrap<T, ResultsClass<T>>::create_constructor(ctx);
|
FunctionType results_constructor = ObjectWrap<T, ResultsClass<T>>::create_constructor(ctx);
|
||||||
|
|
||||||
PropertyAttributes attributes = PropertyAttributes(ReadOnly | DontEnum | DontDelete);
|
PropertyAttributes attributes = PropertyAttributes(ReadOnly | DontEnum | DontDelete);
|
||||||
Object::set_property(ctx, realm_constructor, "Collection", collection_constructor, attributes);
|
Object::set_property(ctx, realm_constructor, "Collection", collection_constructor, attributes);
|
||||||
|
@ -238,7 +238,7 @@ inline typename T::Function Realm<T>::create_constructor(TContext ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::constructor(TContext ctx, TObject this_object, size_t argc, const TValue arguments[]) {
|
void Realm<T>::constructor(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[]) {
|
||||||
static const String path_string = "path";
|
static const String path_string = "path";
|
||||||
static const String schema_string = "schema";
|
static const String schema_string = "schema";
|
||||||
static const String schema_version_string = "schemaVersion";
|
static const String schema_version_string = "schemaVersion";
|
||||||
|
@ -252,14 +252,14 @@ void Realm<T>::constructor(TContext ctx, TObject this_object, size_t argc, const
|
||||||
config.path = default_path();
|
config.path = default_path();
|
||||||
}
|
}
|
||||||
else if (argc == 1) {
|
else if (argc == 1) {
|
||||||
TValue value = arguments[0];
|
ValueType value = arguments[0];
|
||||||
if (Value::is_string(ctx, value)) {
|
if (Value::is_string(ctx, value)) {
|
||||||
config.path = Value::validated_to_string(ctx, value, "path");
|
config.path = Value::validated_to_string(ctx, value, "path");
|
||||||
}
|
}
|
||||||
else if (Value::is_object(ctx, value)) {
|
else if (Value::is_object(ctx, value)) {
|
||||||
TObject object = Value::validated_to_object(ctx, value);
|
ObjectType object = Value::validated_to_object(ctx, value);
|
||||||
|
|
||||||
TValue path_value = Object::get_property(ctx, object, path_string);
|
ValueType path_value = Object::get_property(ctx, object, path_string);
|
||||||
if (!Value::is_undefined(ctx, path_value)) {
|
if (!Value::is_undefined(ctx, path_value)) {
|
||||||
config.path = Value::validated_to_string(ctx, path_value, "path");
|
config.path = Value::validated_to_string(ctx, path_value, "path");
|
||||||
}
|
}
|
||||||
|
@ -267,13 +267,13 @@ void Realm<T>::constructor(TContext ctx, TObject this_object, size_t argc, const
|
||||||
config.path = js::default_path();
|
config.path = js::default_path();
|
||||||
}
|
}
|
||||||
|
|
||||||
TValue schema_value = Object::get_property(ctx, object, schema_string);
|
ValueType schema_value = Object::get_property(ctx, object, schema_string);
|
||||||
if (!Value::is_undefined(ctx, schema_value)) {
|
if (!Value::is_undefined(ctx, schema_value)) {
|
||||||
TObject schema_object = Value::validated_to_object(ctx, schema_value, "schema");
|
ObjectType schema_object = Value::validated_to_object(ctx, schema_value, "schema");
|
||||||
config.schema.reset(new realm::Schema(Schema<T>::parse_schema(ctx, schema_object, defaults, constructors)));
|
config.schema.reset(new realm::Schema(Schema<T>::parse_schema(ctx, schema_object, defaults, constructors)));
|
||||||
}
|
}
|
||||||
|
|
||||||
TValue version_value = Object::get_property(ctx, object, schema_version_string);
|
ValueType version_value = Object::get_property(ctx, object, schema_version_string);
|
||||||
if (!Value::is_undefined(ctx, version_value)) {
|
if (!Value::is_undefined(ctx, version_value)) {
|
||||||
config.schema_version = Value::validated_to_number(ctx, version_value, "schemaVersion");
|
config.schema_version = Value::validated_to_number(ctx, version_value, "schemaVersion");
|
||||||
}
|
}
|
||||||
|
@ -281,7 +281,7 @@ void Realm<T>::constructor(TContext ctx, TObject this_object, size_t argc, const
|
||||||
config.schema_version = 0;
|
config.schema_version = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
TValue encryption_key_value = Object::get_property(ctx, object, encryption_key_string);
|
ValueType encryption_key_value = Object::get_property(ctx, object, encryption_key_string);
|
||||||
if (!Value::is_undefined(ctx, encryption_key_value)) {
|
if (!Value::is_undefined(ctx, encryption_key_value)) {
|
||||||
std::string encryption_key = NativeAccessor::to_binary(ctx, encryption_key_value);
|
std::string encryption_key = NativeAccessor::to_binary(ctx, encryption_key_value);
|
||||||
config.encryption_key = std::vector<char>(encryption_key.begin(), encryption_key.end());
|
config.encryption_key = std::vector<char>(encryption_key.begin(), encryption_key.end());
|
||||||
|
@ -309,7 +309,7 @@ void Realm<T>::constructor(TContext ctx, TObject this_object, size_t argc, const
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::schema_version(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::schema_version(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 1, 2);
|
validate_argument_count(argc, 1, 2);
|
||||||
|
|
||||||
realm::Realm::Config config;
|
realm::Realm::Config config;
|
||||||
|
@ -330,35 +330,35 @@ void Realm<T>::schema_version(TContext ctx, TObject this_object, size_t argc, co
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::clear_test_state(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::clear_test_state(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 0);
|
validate_argument_count(argc, 0);
|
||||||
delete_all_realms();
|
delete_all_realms();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::get_default_path(TContext ctx, TObject object, ReturnValue &return_value) {
|
void Realm<T>::get_default_path(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
||||||
return_value.set(realm::js::default_path());
|
return_value.set(realm::js::default_path());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::set_default_path(TContext ctx, TObject object, TValue value) {
|
void Realm<T>::set_default_path(ContextType ctx, ObjectType object, ValueType value) {
|
||||||
js::set_default_path(Value::validated_to_string(ctx, value, "defaultPath"));
|
js::set_default_path(Value::validated_to_string(ctx, value, "defaultPath"));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::get_path(TContext ctx, TObject object, ReturnValue &return_value) {
|
void Realm<T>::get_path(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
||||||
std::string path = get_internal<T, RealmClass<T>>(object)->get()->config().path;
|
std::string path = get_internal<T, RealmClass<T>>(object)->get()->config().path;
|
||||||
return_value.set(path);
|
return_value.set(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::get_schema_version(TContext ctx, TObject object, ReturnValue &return_value) {
|
void Realm<T>::get_schema_version(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
||||||
double version = get_internal<T, RealmClass<T>>(object)->get()->config().schema_version;
|
double version = get_internal<T, RealmClass<T>>(object)->get()->config().schema_version;
|
||||||
return_value.set(version);
|
return_value.set(version);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::objects(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::objects(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 1);
|
validate_argument_count(argc, 1);
|
||||||
|
|
||||||
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
||||||
|
@ -368,7 +368,7 @@ void Realm<T>::objects(TContext ctx, TObject this_object, size_t argc, const TVa
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::create(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::create(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 2, 3);
|
validate_argument_count(argc, 2, 3);
|
||||||
|
|
||||||
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
||||||
|
@ -380,7 +380,7 @@ void Realm<T>::create(TContext ctx, TObject this_object, size_t argc, const TVal
|
||||||
throw std::runtime_error("Object type '" + className + "' not found in schema.");
|
throw std::runtime_error("Object type '" + className + "' not found in schema.");
|
||||||
}
|
}
|
||||||
|
|
||||||
TObject object = Value::validated_to_object(ctx, arguments[1], "properties");
|
ObjectType object = Value::validated_to_object(ctx, arguments[1], "properties");
|
||||||
if (Value::is_array(ctx, arguments[1])) {
|
if (Value::is_array(ctx, arguments[1])) {
|
||||||
object = Schema<T>::dict_for_property_array(ctx, *object_schema, object);
|
object = Schema<T>::dict_for_property_array(ctx, *object_schema, object);
|
||||||
}
|
}
|
||||||
|
@ -390,12 +390,12 @@ void Realm<T>::create(TContext ctx, TObject this_object, size_t argc, const TVal
|
||||||
update = Value::validated_to_boolean(ctx, arguments[2], "update");
|
update = Value::validated_to_boolean(ctx, arguments[2], "update");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto realm_object = realm::Object::create<TValue>(ctx, realm, *object_schema, object, update);
|
auto realm_object = realm::Object::create<ValueType>(ctx, realm, *object_schema, object, update);
|
||||||
return_value.set(RealmObject<T>::create_instance(ctx, realm_object));
|
return_value.set(RealmObject<T>::create_instance(ctx, realm_object));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::delete_one(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::delete_one(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 1);
|
validate_argument_count(argc, 1);
|
||||||
|
|
||||||
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
||||||
|
@ -403,7 +403,7 @@ void Realm<T>::delete_one(TContext ctx, TObject this_object, size_t argc, const
|
||||||
throw std::runtime_error("Can only delete objects within a transaction.");
|
throw std::runtime_error("Can only delete objects within a transaction.");
|
||||||
}
|
}
|
||||||
|
|
||||||
TObject arg = Value::validated_to_object(ctx, arguments[0]);
|
ObjectType arg = Value::validated_to_object(ctx, arguments[0]);
|
||||||
|
|
||||||
if (Object::template is_instance<RealmObjectClass<T>>(ctx, arg)) {
|
if (Object::template is_instance<RealmObjectClass<T>>(ctx, arg)) {
|
||||||
auto object = get_internal<T, RealmObjectClass<T>>(arg);
|
auto object = get_internal<T, RealmObjectClass<T>>(arg);
|
||||||
|
@ -413,7 +413,7 @@ void Realm<T>::delete_one(TContext ctx, TObject this_object, size_t argc, const
|
||||||
else if (Value::is_array(ctx, arg)) {
|
else if (Value::is_array(ctx, arg)) {
|
||||||
uint32_t length = Object::validated_get_length(ctx, arg);
|
uint32_t length = Object::validated_get_length(ctx, arg);
|
||||||
for (uint32_t i = length; i--;) {
|
for (uint32_t i = length; i--;) {
|
||||||
TObject object = Object::validated_get_object(ctx, arg, i);
|
ObjectType object = Object::validated_get_object(ctx, arg, i);
|
||||||
|
|
||||||
if (!Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
|
if (!Object::template is_instance<RealmObjectClass<T>>(ctx, object)) {
|
||||||
throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects.");
|
throw std::runtime_error("Argument to 'delete' must be a Realm object or a collection of Realm objects.");
|
||||||
|
@ -438,7 +438,7 @@ void Realm<T>::delete_one(TContext ctx, TObject this_object, size_t argc, const
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::delete_all(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::delete_all(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 0);
|
validate_argument_count(argc, 0);
|
||||||
|
|
||||||
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
||||||
|
@ -453,11 +453,11 @@ void Realm<T>::delete_all(TContext ctx, TObject this_object, size_t argc, const
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::write(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::write(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 1);
|
validate_argument_count(argc, 1);
|
||||||
|
|
||||||
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
||||||
TFunction callback = Value::validated_to_function(ctx, arguments[0]);
|
FunctionType callback = Value::validated_to_function(ctx, arguments[0]);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
realm->begin_transaction();
|
realm->begin_transaction();
|
||||||
|
@ -473,7 +473,7 @@ void Realm<T>::write(TContext ctx, TObject this_object, size_t argc, const TValu
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::add_listener(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::add_listener(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 2);
|
validate_argument_count(argc, 2);
|
||||||
|
|
||||||
__unused std::string name = validated_notification_name(ctx, arguments[0]);
|
__unused std::string name = validated_notification_name(ctx, arguments[0]);
|
||||||
|
@ -484,7 +484,7 @@ void Realm<T>::add_listener(TContext ctx, TObject this_object, size_t argc, cons
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::remove_listener(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::remove_listener(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 2);
|
validate_argument_count(argc, 2);
|
||||||
|
|
||||||
__unused std::string name = validated_notification_name(ctx, arguments[0]);
|
__unused std::string name = validated_notification_name(ctx, arguments[0]);
|
||||||
|
@ -495,7 +495,7 @@ void Realm<T>::remove_listener(TContext ctx, TObject this_object, size_t argc, c
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::remove_all_listeners(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::remove_all_listeners(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 0, 1);
|
validate_argument_count(argc, 0, 1);
|
||||||
if (argc) {
|
if (argc) {
|
||||||
validated_notification_name(ctx, arguments[0]);
|
validated_notification_name(ctx, arguments[0]);
|
||||||
|
@ -506,7 +506,7 @@ void Realm<T>::remove_all_listeners(TContext ctx, TObject this_object, size_t ar
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Realm<T>::close(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Realm<T>::close(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 0);
|
validate_argument_count(argc, 0);
|
||||||
|
|
||||||
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
SharedRealm realm = *get_internal<T, RealmClass<T>>(this_object);
|
||||||
|
|
|
@ -30,10 +30,10 @@ namespace js {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class RealmObject {
|
class RealmObject {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TFunction = typename T::Function;
|
using FunctionType = typename T::Function;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
using String = String<T>;
|
using String = String<T>;
|
||||||
using Value = Value<T>;
|
using Value = Value<T>;
|
||||||
using Object = Object<T>;
|
using Object = Object<T>;
|
||||||
|
@ -41,11 +41,11 @@ class RealmObject {
|
||||||
using ReturnValue = ReturnValue<T>;
|
using ReturnValue = ReturnValue<T>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static TObject create_instance(TContext, realm::Object &);
|
static ObjectType create_instance(ContextType, realm::Object &);
|
||||||
|
|
||||||
static void get_property(TContext, TObject, const String &, ReturnValue &);
|
static void get_property(ContextType, ObjectType, const String &, ReturnValue &);
|
||||||
static bool set_property(TContext, TObject, const String &, TValue);
|
static bool set_property(ContextType, ObjectType, const String &, ValueType);
|
||||||
static std::vector<String> get_property_names(TContext, TObject);
|
static std::vector<String> get_property_names(ContextType, ObjectType);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -62,7 +62,7 @@ struct RealmObjectClass : ClassDefinition<T, realm::Object> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
typename T::Object RealmObject<T>::create_instance(TContext ctx, realm::Object &realm_object) {
|
typename T::Object RealmObject<T>::create_instance(ContextType ctx, realm::Object &realm_object) {
|
||||||
static String prototype_string = "prototype";
|
static String prototype_string = "prototype";
|
||||||
|
|
||||||
auto delegate = get_delegate<T>(realm_object.realm().get());
|
auto delegate = get_delegate<T>(realm_object.realm().get());
|
||||||
|
@ -73,11 +73,11 @@ typename T::Object RealmObject<T>::create_instance(TContext ctx, realm::Object &
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
TFunction constructor = delegate->m_constructors.at(name);
|
FunctionType constructor = delegate->m_constructors.at(name);
|
||||||
TObject prototype = Object::validated_get_object(ctx, constructor, prototype_string);
|
ObjectType prototype = Object::validated_get_object(ctx, constructor, prototype_string);
|
||||||
Object::set_prototype(ctx, object, prototype);
|
Object::set_prototype(ctx, object, prototype);
|
||||||
|
|
||||||
TValue result = Function::call(ctx, constructor, object, 0, NULL);
|
ValueType result = Function::call(ctx, constructor, object, 0, NULL);
|
||||||
if (result != object && !Value::is_null(ctx, result) && !Value::is_undefined(ctx, result)) {
|
if (result != object && !Value::is_null(ctx, result) && !Value::is_undefined(ctx, result)) {
|
||||||
throw std::runtime_error("Realm object constructor must not return another value");
|
throw std::runtime_error("Realm object constructor must not return another value");
|
||||||
}
|
}
|
||||||
|
@ -86,10 +86,10 @@ typename T::Object RealmObject<T>::create_instance(TContext ctx, realm::Object &
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void RealmObject<T>::get_property(TContext ctx, TObject object, const String &property, ReturnValue &return_value) {
|
void RealmObject<T>::get_property(ContextType ctx, ObjectType object, const String &property, ReturnValue &return_value) {
|
||||||
try {
|
try {
|
||||||
auto realm_object = get_internal<T, RealmObjectClass<T>>(object);
|
auto realm_object = get_internal<T, RealmObjectClass<T>>(object);
|
||||||
auto result = realm_object->template get_property_value<TValue>(ctx, property);
|
auto result = realm_object->template get_property_value<ValueType>(ctx, property);
|
||||||
return_value.set(result);
|
return_value.set(result);
|
||||||
} catch (InvalidPropertyException &ex) {
|
} catch (InvalidPropertyException &ex) {
|
||||||
// getters for nonexistent properties in JS should always return undefined
|
// getters for nonexistent properties in JS should always return undefined
|
||||||
|
@ -97,14 +97,14 @@ void RealmObject<T>::get_property(TContext ctx, TObject object, const String &pr
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
bool RealmObject<T>::set_property(TContext ctx, TObject object, const String &property, TValue value) {
|
bool RealmObject<T>::set_property(ContextType ctx, ObjectType object, const String &property, ValueType value) {
|
||||||
auto realm_object = get_internal<T, RealmObjectClass<T>>(object);
|
auto realm_object = get_internal<T, RealmObjectClass<T>>(object);
|
||||||
realm_object->set_property_value(ctx, property, value, true);
|
realm_object->set_property_value(ctx, property, value, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::vector<String<T>> RealmObject<T>::get_property_names(TContext ctx, TObject object) {
|
std::vector<String<T>> RealmObject<T>::get_property_names(ContextType ctx, ObjectType object) {
|
||||||
auto realm_object = get_internal<T, RealmObjectClass<T>>(object);
|
auto realm_object = get_internal<T, RealmObjectClass<T>>(object);
|
||||||
auto &properties = realm_object->get_object_schema().properties;
|
auto &properties = realm_object->get_object_schema().properties;
|
||||||
|
|
||||||
|
|
|
@ -31,31 +31,31 @@ namespace js {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class Results {
|
class Results {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
using Object = Object<T>;
|
using Object = Object<T>;
|
||||||
using Value = Value<T>;
|
using Value = Value<T>;
|
||||||
using ReturnValue = ReturnValue<T>;
|
using ReturnValue = ReturnValue<T>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static TObject create_instance(TContext, const realm::Results &, bool live = true);
|
static ObjectType create_instance(ContextType, const realm::Results &, bool live = true);
|
||||||
static TObject create_instance(TContext, const realm::List &, bool live = true);
|
static ObjectType create_instance(ContextType, const realm::List &, bool live = true);
|
||||||
static TObject create_instance(TContext, SharedRealm, const std::string &type, bool live = true);
|
static ObjectType create_instance(ContextType, SharedRealm, const std::string &type, bool live = true);
|
||||||
static TObject create_instance(TContext, SharedRealm, const ObjectSchema &, Query, bool live = true);
|
static ObjectType create_instance(ContextType, SharedRealm, const ObjectSchema &, Query, bool live = true);
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
static TObject create_filtered(TContext, const U &, size_t, const TValue[]);
|
static ObjectType create_filtered(ContextType, const U &, size_t, const ValueType[]);
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
static TObject create_sorted(TContext, const U &, size_t, const TValue[]);
|
static ObjectType create_sorted(ContextType, const U &, size_t, const ValueType[]);
|
||||||
|
|
||||||
static void get_length(TContext, TObject, ReturnValue &);
|
static void get_length(ContextType, ObjectType, ReturnValue &);
|
||||||
static void get_index(TContext, TObject, uint32_t, ReturnValue &);
|
static void get_index(ContextType, ObjectType, uint32_t, ReturnValue &);
|
||||||
|
|
||||||
static void snapshot(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void snapshot(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void filtered(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void filtered(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
static void sorted(TContext, TObject, size_t, const TValue[], ReturnValue &);
|
static void sorted(ContextType, ObjectType, size_t, const ValueType[], ReturnValue &);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -78,7 +78,7 @@ struct ResultsClass : ClassDefinition<T, realm::Results, CollectionClass<T>> {
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
typename T::Object Results<T>::create_instance(TContext ctx, const realm::Results &results, bool live) {
|
typename T::Object Results<T>::create_instance(ContextType ctx, const realm::Results &results, bool live) {
|
||||||
auto new_results = new realm::Results(results);
|
auto new_results = new realm::Results(results);
|
||||||
new_results->set_live(live);
|
new_results->set_live(live);
|
||||||
|
|
||||||
|
@ -86,12 +86,12 @@ typename T::Object Results<T>::create_instance(TContext ctx, const realm::Result
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
typename T::Object Results<T>::create_instance(TContext ctx, const realm::List &list, bool live) {
|
typename T::Object Results<T>::create_instance(ContextType ctx, const realm::List &list, bool live) {
|
||||||
return create_instance(ctx, list.get_realm(), list.get_object_schema(), list.get_query(), live);
|
return create_instance(ctx, list.get_realm(), list.get_object_schema(), list.get_query(), live);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
typename T::Object Results<T>::create_instance(TContext ctx, SharedRealm realm, const std::string &type, bool live) {
|
typename T::Object Results<T>::create_instance(ContextType ctx, SharedRealm realm, const std::string &type, bool live) {
|
||||||
auto table = ObjectStore::table_for_object_type(realm->read_group(), type);
|
auto table = ObjectStore::table_for_object_type(realm->read_group(), type);
|
||||||
auto &schema = realm->config().schema;
|
auto &schema = realm->config().schema;
|
||||||
auto object_schema = schema->find(type);
|
auto object_schema = schema->find(type);
|
||||||
|
@ -107,7 +107,7 @@ typename T::Object Results<T>::create_instance(TContext ctx, SharedRealm realm,
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
typename T::Object Results<T>::create_instance(TContext ctx, SharedRealm realm, const ObjectSchema &object_schema, Query query, bool live) {
|
typename T::Object Results<T>::create_instance(ContextType ctx, SharedRealm realm, const ObjectSchema &object_schema, Query query, bool live) {
|
||||||
auto results = new realm::Results(realm, object_schema, std::move(query));
|
auto results = new realm::Results(realm, object_schema, std::move(query));
|
||||||
results->set_live(live);
|
results->set_live(live);
|
||||||
|
|
||||||
|
@ -116,13 +116,13 @@ typename T::Object Results<T>::create_instance(TContext ctx, SharedRealm realm,
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
template<typename U>
|
template<typename U>
|
||||||
typename T::Object Results<T>::create_filtered(TContext ctx, const U &collection, size_t argc, const TValue arguments[]) {
|
typename T::Object Results<T>::create_filtered(ContextType ctx, const U &collection, size_t argc, const ValueType arguments[]) {
|
||||||
auto query_string = Value::validated_to_string(ctx, arguments[0], "predicate");
|
auto query_string = Value::validated_to_string(ctx, arguments[0], "predicate");
|
||||||
auto query = collection.get_query();
|
auto query = collection.get_query();
|
||||||
auto const &realm = collection.get_realm();
|
auto const &realm = collection.get_realm();
|
||||||
auto const &object_schema = collection.get_object_schema();
|
auto const &object_schema = collection.get_object_schema();
|
||||||
|
|
||||||
std::vector<TValue> args;
|
std::vector<ValueType> args;
|
||||||
args.reserve(argc - 1);
|
args.reserve(argc - 1);
|
||||||
|
|
||||||
for (size_t i = 1; i < argc; i++) {
|
for (size_t i = 1; i < argc; i++) {
|
||||||
|
@ -130,7 +130,7 @@ typename T::Object Results<T>::create_filtered(TContext ctx, const U &collection
|
||||||
}
|
}
|
||||||
|
|
||||||
parser::Predicate predicate = parser::parse(query_string);
|
parser::Predicate predicate = parser::parse(query_string);
|
||||||
query_builder::ArgumentConverter<TValue, TContext> converter(ctx, args);
|
query_builder::ArgumentConverter<ValueType, ContextType> converter(ctx, args);
|
||||||
query_builder::apply_predicate(query, predicate, converter, *realm->config().schema, object_schema.name);
|
query_builder::apply_predicate(query, predicate, converter, *realm->config().schema, object_schema.name);
|
||||||
|
|
||||||
return create_instance(ctx, realm, object_schema, std::move(query));
|
return create_instance(ctx, realm, object_schema, std::move(query));
|
||||||
|
@ -138,7 +138,7 @@ typename T::Object Results<T>::create_filtered(TContext ctx, const U &collection
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
template<typename U>
|
template<typename U>
|
||||||
typename T::Object Results<T>::create_sorted(TContext ctx, const U &collection, size_t argc, const TValue arguments[]) {
|
typename T::Object Results<T>::create_sorted(ContextType ctx, const U &collection, size_t argc, const ValueType arguments[]) {
|
||||||
auto const &realm = collection.get_realm();
|
auto const &realm = collection.get_realm();
|
||||||
auto const &object_schema = collection.get_object_schema();
|
auto const &object_schema = collection.get_object_schema();
|
||||||
std::vector<std::string> prop_names;
|
std::vector<std::string> prop_names;
|
||||||
|
@ -148,7 +148,7 @@ typename T::Object Results<T>::create_sorted(TContext ctx, const U &collection,
|
||||||
if (Value::is_array(ctx, arguments[0])) {
|
if (Value::is_array(ctx, arguments[0])) {
|
||||||
validate_argument_count(argc, 1, "Second argument is not allowed if passed an array of sort descriptors");
|
validate_argument_count(argc, 1, "Second argument is not allowed if passed an array of sort descriptors");
|
||||||
|
|
||||||
TObject js_prop_names = Value::validated_to_object(ctx, arguments[0]);
|
ObjectType js_prop_names = Value::validated_to_object(ctx, arguments[0]);
|
||||||
prop_count = Object::validated_get_length(ctx, js_prop_names);
|
prop_count = Object::validated_get_length(ctx, js_prop_names);
|
||||||
if (!prop_count) {
|
if (!prop_count) {
|
||||||
throw std::invalid_argument("Sort descriptor array must not be empty");
|
throw std::invalid_argument("Sort descriptor array must not be empty");
|
||||||
|
@ -158,10 +158,10 @@ typename T::Object Results<T>::create_sorted(TContext ctx, const U &collection,
|
||||||
ascending.resize(prop_count);
|
ascending.resize(prop_count);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < prop_count; i++) {
|
for (unsigned int i = 0; i < prop_count; i++) {
|
||||||
TValue value = Object::validated_get_property(ctx, js_prop_names, i);
|
ValueType value = Object::validated_get_property(ctx, js_prop_names, i);
|
||||||
|
|
||||||
if (Value::is_array(ctx, value)) {
|
if (Value::is_array(ctx, value)) {
|
||||||
TObject array = Value::to_array(ctx, value);
|
ObjectType array = Value::to_array(ctx, value);
|
||||||
prop_names[i] = Object::validated_get_string(ctx, array, 0);
|
prop_names[i] = Object::validated_get_string(ctx, array, 0);
|
||||||
ascending[i] = !Object::validated_get_boolean(ctx, array, 1);
|
ascending[i] = !Object::validated_get_boolean(ctx, array, 1);
|
||||||
}
|
}
|
||||||
|
@ -195,13 +195,13 @@ typename T::Object Results<T>::create_sorted(TContext ctx, const U &collection,
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Results<T>::get_length(TContext ctx, TObject object, ReturnValue &return_value) {
|
void Results<T>::get_length(ContextType ctx, ObjectType object, ReturnValue &return_value) {
|
||||||
auto results = get_internal<T, ResultsClass<T>>(object);
|
auto results = get_internal<T, ResultsClass<T>>(object);
|
||||||
return_value.set((uint32_t)results->size());
|
return_value.set((uint32_t)results->size());
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Results<T>::get_index(TContext ctx, TObject object, uint32_t index, ReturnValue &return_value) {
|
void Results<T>::get_index(ContextType ctx, ObjectType object, uint32_t index, ReturnValue &return_value) {
|
||||||
auto results = get_internal<T, ResultsClass<T>>(object);
|
auto results = get_internal<T, ResultsClass<T>>(object);
|
||||||
auto row = results->get(index);
|
auto row = results->get(index);
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ void Results<T>::get_index(TContext ctx, TObject object, uint32_t index, ReturnV
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Results<T>::snapshot(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Results<T>::snapshot(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 0);
|
validate_argument_count(argc, 0);
|
||||||
|
|
||||||
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
||||||
|
@ -224,7 +224,7 @@ void Results<T>::snapshot(TContext ctx, TObject this_object, size_t argc, const
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Results<T>::filtered(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Results<T>::filtered(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count_at_least(argc, 1);
|
validate_argument_count_at_least(argc, 1);
|
||||||
|
|
||||||
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
||||||
|
@ -232,7 +232,7 @@ void Results<T>::filtered(TContext ctx, TObject this_object, size_t argc, const
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void Results<T>::sorted(TContext ctx, TObject this_object, size_t argc, const TValue arguments[], ReturnValue &return_value) {
|
void Results<T>::sorted(ContextType ctx, ObjectType this_object, size_t argc, const ValueType arguments[], ReturnValue &return_value) {
|
||||||
validate_argument_count(argc, 1, 2);
|
validate_argument_count(argc, 1, 2);
|
||||||
|
|
||||||
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
auto results = get_internal<T, ResultsClass<T>>(this_object);
|
||||||
|
|
|
@ -28,36 +28,36 @@ namespace js {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Schema {
|
struct Schema {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TFunction = typename T::Function;
|
using FunctionType = typename T::Function;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
using String = String<T>;
|
using String = String<T>;
|
||||||
using Object = Object<T>;
|
using Object = Object<T>;
|
||||||
using Value = Value<T>;
|
using Value = Value<T>;
|
||||||
|
|
||||||
using ObjectDefaults = std::map<std::string, Protected<TValue>>;
|
using ObjectDefaults = std::map<std::string, Protected<ValueType>>;
|
||||||
using ObjectDefaultsMap = std::map<std::string, ObjectDefaults>;
|
using ObjectDefaultsMap = std::map<std::string, ObjectDefaults>;
|
||||||
using ConstructorMap = std::map<std::string, Protected<TFunction>>;
|
using ConstructorMap = std::map<std::string, Protected<FunctionType>>;
|
||||||
|
|
||||||
static TObject dict_for_property_array(TContext, const ObjectSchema &, TObject);
|
static ObjectType dict_for_property_array(ContextType, const ObjectSchema &, ObjectType);
|
||||||
static Property parse_property(TContext, TValue, std::string, ObjectDefaults &);
|
static Property parse_property(ContextType, ValueType, std::string, ObjectDefaults &);
|
||||||
static ObjectSchema parse_object_schema(TContext, TObject, ObjectDefaultsMap &, ConstructorMap &);
|
static ObjectSchema parse_object_schema(ContextType, ObjectType, ObjectDefaultsMap &, ConstructorMap &);
|
||||||
static realm::Schema parse_schema(TContext, TObject, ObjectDefaultsMap &, ConstructorMap &);
|
static realm::Schema parse_schema(ContextType, ObjectType, ObjectDefaultsMap &, ConstructorMap &);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
typename T::Object Schema<T>::dict_for_property_array(TContext ctx, const ObjectSchema &object_schema, TObject array) {
|
typename T::Object Schema<T>::dict_for_property_array(ContextType ctx, const ObjectSchema &object_schema, ObjectType array) {
|
||||||
size_t count = object_schema.properties.size();
|
size_t count = object_schema.properties.size();
|
||||||
|
|
||||||
if (count != Object::validated_get_length(ctx, array)) {
|
if (count != Object::validated_get_length(ctx, array)) {
|
||||||
throw std::runtime_error("Array must contain values for all object properties");
|
throw std::runtime_error("Array must contain values for all object properties");
|
||||||
}
|
}
|
||||||
|
|
||||||
TObject dict = Object::create_empty(ctx);
|
ObjectType dict = Object::create_empty(ctx);
|
||||||
|
|
||||||
for (uint32_t i = 0; i < count; i++) {
|
for (uint32_t i = 0; i < count; i++) {
|
||||||
TValue value = Object::get_property(ctx, array, i);
|
ValueType value = Object::get_property(ctx, array, i);
|
||||||
Object::set_property(ctx, dict, object_schema.properties[i].name, value);
|
Object::set_property(ctx, dict, object_schema.properties[i].name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ typename T::Object Schema<T>::dict_for_property_array(TContext ctx, const Object
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Property Schema<T>::parse_property(TContext ctx, TValue attributes, std::string property_name, ObjectDefaults &object_defaults) {
|
Property Schema<T>::parse_property(ContextType ctx, ValueType attributes, std::string property_name, ObjectDefaults &object_defaults) {
|
||||||
static const String default_string = "default";
|
static const String default_string = "default";
|
||||||
static const String indexed_string = "indexed";
|
static const String indexed_string = "indexed";
|
||||||
static const String type_string = "type";
|
static const String type_string = "type";
|
||||||
|
@ -75,14 +75,14 @@ Property Schema<T>::parse_property(TContext ctx, TValue attributes, std::string
|
||||||
Property prop;
|
Property prop;
|
||||||
prop.name = property_name;
|
prop.name = property_name;
|
||||||
|
|
||||||
TObject property_object = {};
|
ObjectType property_object = {};
|
||||||
std::string type;
|
std::string type;
|
||||||
|
|
||||||
if (Value::is_object(ctx, attributes)) {
|
if (Value::is_object(ctx, attributes)) {
|
||||||
property_object = Value::validated_to_object(ctx, attributes);
|
property_object = Value::validated_to_object(ctx, attributes);
|
||||||
type = Object::validated_get_string(ctx, property_object, type_string);
|
type = Object::validated_get_string(ctx, property_object, type_string);
|
||||||
|
|
||||||
TValue optional_value = Object::get_property(ctx, property_object, optional_string);
|
ValueType optional_value = Object::get_property(ctx, property_object, optional_string);
|
||||||
if (!Value::is_undefined(ctx, optional_value)) {
|
if (!Value::is_undefined(ctx, optional_value)) {
|
||||||
prop.is_nullable = Value::validated_to_boolean(ctx, optional_value, "optional");
|
prop.is_nullable = Value::validated_to_boolean(ctx, optional_value, "optional");
|
||||||
}
|
}
|
||||||
|
@ -136,12 +136,12 @@ Property Schema<T>::parse_property(TContext ctx, TValue attributes, std::string
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Value::is_valid(property_object)) {
|
if (Value::is_valid(property_object)) {
|
||||||
TValue default_value = Object::get_property(ctx, property_object, default_string);
|
ValueType default_value = Object::get_property(ctx, property_object, default_string);
|
||||||
if (!Value::is_undefined(ctx, default_value)) {
|
if (!Value::is_undefined(ctx, default_value)) {
|
||||||
object_defaults.emplace(prop.name, Protected<TValue>(ctx, default_value));
|
object_defaults.emplace(prop.name, Protected<ValueType>(ctx, default_value));
|
||||||
}
|
}
|
||||||
|
|
||||||
TValue indexed_value = Object::get_property(ctx, property_object, indexed_string);
|
ValueType indexed_value = Object::get_property(ctx, property_object, indexed_string);
|
||||||
if (!Value::is_undefined(ctx, indexed_value)) {
|
if (!Value::is_undefined(ctx, indexed_value)) {
|
||||||
prop.is_indexed = Value::validated_to_boolean(ctx, indexed_value);
|
prop.is_indexed = Value::validated_to_boolean(ctx, indexed_value);
|
||||||
}
|
}
|
||||||
|
@ -151,13 +151,13 @@ Property Schema<T>::parse_property(TContext ctx, TValue attributes, std::string
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
ObjectSchema Schema<T>::parse_object_schema(TContext ctx, TObject object_schema_object, ObjectDefaultsMap &defaults, ConstructorMap &constructors) {
|
ObjectSchema Schema<T>::parse_object_schema(ContextType ctx, ObjectType object_schema_object, ObjectDefaultsMap &defaults, ConstructorMap &constructors) {
|
||||||
static const String name_string = "name";
|
static const String name_string = "name";
|
||||||
static const String primary_string = "primaryKey";
|
static const String primary_string = "primaryKey";
|
||||||
static const String properties_string = "properties";
|
static const String properties_string = "properties";
|
||||||
static const String schema_string = "schema";
|
static const String schema_string = "schema";
|
||||||
|
|
||||||
TFunction object_constructor = {};
|
FunctionType object_constructor = {};
|
||||||
if (Value::is_constructor(ctx, object_schema_object)) {
|
if (Value::is_constructor(ctx, object_schema_object)) {
|
||||||
object_constructor = Value::to_constructor(ctx, object_schema_object);
|
object_constructor = Value::to_constructor(ctx, object_schema_object);
|
||||||
object_schema_object = Object::validated_get_object(ctx, object_constructor, schema_string, "Realm object constructor must have a 'schema' property.");
|
object_schema_object = Object::validated_get_object(ctx, object_constructor, schema_string, "Realm object constructor must have a 'schema' property.");
|
||||||
|
@ -167,11 +167,11 @@ ObjectSchema Schema<T>::parse_object_schema(TContext ctx, TObject object_schema_
|
||||||
ObjectSchema object_schema;
|
ObjectSchema object_schema;
|
||||||
object_schema.name = Object::validated_get_string(ctx, object_schema_object, name_string);
|
object_schema.name = Object::validated_get_string(ctx, object_schema_object, name_string);
|
||||||
|
|
||||||
TObject properties_object = Object::validated_get_object(ctx, object_schema_object, properties_string, "ObjectSchema must have a 'properties' object.");
|
ObjectType properties_object = Object::validated_get_object(ctx, object_schema_object, properties_string, "ObjectSchema must have a 'properties' object.");
|
||||||
if (Value::is_array(ctx, properties_object)) {
|
if (Value::is_array(ctx, properties_object)) {
|
||||||
uint32_t length = Object::validated_get_length(ctx, properties_object);
|
uint32_t length = Object::validated_get_length(ctx, properties_object);
|
||||||
for (uint32_t i = 0; i < length; i++) {
|
for (uint32_t i = 0; i < length; i++) {
|
||||||
TObject property_object = Object::validated_get_object(ctx, properties_object, i);
|
ObjectType property_object = Object::validated_get_object(ctx, properties_object, i);
|
||||||
std::string property_name = Object::validated_get_string(ctx, property_object, name_string);
|
std::string property_name = Object::validated_get_string(ctx, property_object, name_string);
|
||||||
object_schema.properties.emplace_back(parse_property(ctx, property_object, property_name, object_defaults));
|
object_schema.properties.emplace_back(parse_property(ctx, property_object, property_name, object_defaults));
|
||||||
}
|
}
|
||||||
|
@ -179,12 +179,12 @@ ObjectSchema Schema<T>::parse_object_schema(TContext ctx, TObject object_schema_
|
||||||
else {
|
else {
|
||||||
auto property_names = Object::get_property_names(ctx, properties_object);
|
auto property_names = Object::get_property_names(ctx, properties_object);
|
||||||
for (auto &property_name : property_names) {
|
for (auto &property_name : property_names) {
|
||||||
TValue property_value = Object::get_property(ctx, properties_object, property_name);
|
ValueType property_value = Object::get_property(ctx, properties_object, property_name);
|
||||||
object_schema.properties.emplace_back(parse_property(ctx, property_value, property_name, object_defaults));
|
object_schema.properties.emplace_back(parse_property(ctx, property_value, property_name, object_defaults));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TValue primary_value = Object::get_property(ctx, object_schema_object, primary_string);
|
ValueType primary_value = Object::get_property(ctx, object_schema_object, primary_string);
|
||||||
if (!Value::is_undefined(ctx, primary_value)) {
|
if (!Value::is_undefined(ctx, primary_value)) {
|
||||||
object_schema.primary_key = Value::validated_to_string(ctx, primary_value);
|
object_schema.primary_key = Value::validated_to_string(ctx, primary_value);
|
||||||
Property *property = object_schema.primary_key_property();
|
Property *property = object_schema.primary_key_property();
|
||||||
|
@ -196,7 +196,7 @@ ObjectSchema Schema<T>::parse_object_schema(TContext ctx, TObject object_schema_
|
||||||
|
|
||||||
// Store prototype so that objects of this type will have their prototype set to this prototype object.
|
// Store prototype so that objects of this type will have their prototype set to this prototype object.
|
||||||
if (Value::is_valid(object_constructor)) {
|
if (Value::is_valid(object_constructor)) {
|
||||||
constructors.emplace(object_schema.name, Protected<TFunction>(ctx, object_constructor));
|
constructors.emplace(object_schema.name, Protected<FunctionType>(ctx, object_constructor));
|
||||||
}
|
}
|
||||||
|
|
||||||
defaults.emplace(object_schema.name, std::move(object_defaults));
|
defaults.emplace(object_schema.name, std::move(object_defaults));
|
||||||
|
@ -205,12 +205,12 @@ ObjectSchema Schema<T>::parse_object_schema(TContext ctx, TObject object_schema_
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
realm::Schema Schema<T>::parse_schema(TContext ctx, TObject schema_object, ObjectDefaultsMap &defaults, ConstructorMap &constructors) {
|
realm::Schema Schema<T>::parse_schema(ContextType ctx, ObjectType schema_object, ObjectDefaultsMap &defaults, ConstructorMap &constructors) {
|
||||||
std::vector<ObjectSchema> schema;
|
std::vector<ObjectSchema> schema;
|
||||||
uint32_t length = Object::validated_get_length(ctx, schema_object);
|
uint32_t length = Object::validated_get_length(ctx, schema_object);
|
||||||
|
|
||||||
for (uint32_t i = 0; i < length; i++) {
|
for (uint32_t i = 0; i < length; i++) {
|
||||||
TObject object_schema_object = Object::validated_get_object(ctx, schema_object, i);
|
ObjectType object_schema_object = Object::validated_get_object(ctx, schema_object, i);
|
||||||
ObjectSchema object_schema = parse_object_schema(ctx, object_schema_object, defaults, constructors);
|
ObjectSchema object_schema = parse_object_schema(ctx, object_schema_object, defaults, constructors);
|
||||||
schema.emplace_back(std::move(object_schema));
|
schema.emplace_back(std::move(object_schema));
|
||||||
}
|
}
|
||||||
|
|
194
src/js_types.hpp
194
src/js_types.hpp
|
@ -58,50 +58,50 @@ struct String {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Context {
|
struct Context {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using GlobalTContext = typename T::GlobalContext;
|
using GlobalContextType = typename T::GlobalContext;
|
||||||
|
|
||||||
static GlobalTContext get_global_context(TContext);
|
static GlobalContextType get_global_context(ContextType);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Value {
|
struct Value {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TFunction = typename T::Function;
|
using FunctionType = typename T::Function;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
|
|
||||||
static bool is_array(TContext, const TValue &);
|
static bool is_array(ContextType, const ValueType &);
|
||||||
static bool is_array_buffer(TContext, const TValue &);
|
static bool is_array_buffer(ContextType, const ValueType &);
|
||||||
static bool is_array_buffer_view(TContext, const TValue &);
|
static bool is_array_buffer_view(ContextType, const ValueType &);
|
||||||
static bool is_boolean(TContext, const TValue &);
|
static bool is_boolean(ContextType, const ValueType &);
|
||||||
static bool is_constructor(TContext, const TValue &);
|
static bool is_constructor(ContextType, const ValueType &);
|
||||||
static bool is_date(TContext, const TValue &);
|
static bool is_date(ContextType, const ValueType &);
|
||||||
static bool is_function(TContext, const TValue &);
|
static bool is_function(ContextType, const ValueType &);
|
||||||
static bool is_null(TContext, const TValue &);
|
static bool is_null(ContextType, const ValueType &);
|
||||||
static bool is_number(TContext, const TValue &);
|
static bool is_number(ContextType, const ValueType &);
|
||||||
static bool is_object(TContext, const TValue &);
|
static bool is_object(ContextType, const ValueType &);
|
||||||
static bool is_string(TContext, const TValue &);
|
static bool is_string(ContextType, const ValueType &);
|
||||||
static bool is_undefined(TContext, const TValue &);
|
static bool is_undefined(ContextType, const ValueType &);
|
||||||
static bool is_valid(const TValue &);
|
static bool is_valid(const ValueType &);
|
||||||
|
|
||||||
static TValue from_boolean(TContext, bool);
|
static ValueType from_boolean(ContextType, bool);
|
||||||
static TValue from_null(TContext);
|
static ValueType from_null(ContextType);
|
||||||
static TValue from_number(TContext, double);
|
static ValueType from_number(ContextType, double);
|
||||||
static TValue from_string(TContext, const String<T> &);
|
static ValueType from_string(ContextType, const String<T> &);
|
||||||
static TValue from_undefined(TContext);
|
static ValueType from_undefined(ContextType);
|
||||||
|
|
||||||
static TObject to_array(TContext, const TValue &);
|
static ObjectType to_array(ContextType, const ValueType &);
|
||||||
static bool to_boolean(TContext, const TValue &);
|
static bool to_boolean(ContextType, const ValueType &);
|
||||||
static TFunction to_constructor(TContext, const TValue &);
|
static FunctionType to_constructor(ContextType, const ValueType &);
|
||||||
static TObject to_date(TContext, const TValue &);
|
static ObjectType to_date(ContextType, const ValueType &);
|
||||||
static TFunction to_function(TContext, const TValue &);
|
static FunctionType to_function(ContextType, const ValueType &);
|
||||||
static double to_number(TContext, const TValue &);
|
static double to_number(ContextType, const ValueType &);
|
||||||
static TObject to_object(TContext, const TValue &);
|
static ObjectType to_object(ContextType, const ValueType &);
|
||||||
static String<T> to_string(TContext, const TValue &);
|
static String<T> to_string(ContextType, const ValueType &);
|
||||||
|
|
||||||
#define VALIDATED(return_t, type) \
|
#define VALIDATED(return_t, type) \
|
||||||
static return_t validated_to_##type(TContext ctx, const TValue &value, const char *name = nullptr) { \
|
static return_t validated_to_##type(ContextType ctx, const ValueType &value, const char *name = nullptr) { \
|
||||||
if (!is_##type(ctx, value)) { \
|
if (!is_##type(ctx, value)) { \
|
||||||
std::string prefix = name ? std::string("'") + name + "'" : "JS value"; \
|
std::string prefix = name ? std::string("'") + name + "'" : "JS value"; \
|
||||||
throw std::invalid_argument(prefix + " must be: " #type); \
|
throw std::invalid_argument(prefix + " must be: " #type); \
|
||||||
|
@ -109,13 +109,13 @@ struct Value {
|
||||||
return to_##type(ctx, value); \
|
return to_##type(ctx, value); \
|
||||||
}
|
}
|
||||||
|
|
||||||
VALIDATED(TObject, array)
|
VALIDATED(ObjectType, array)
|
||||||
VALIDATED(bool, boolean)
|
VALIDATED(bool, boolean)
|
||||||
VALIDATED(TFunction, constructor)
|
VALIDATED(FunctionType, constructor)
|
||||||
VALIDATED(TObject, date)
|
VALIDATED(ObjectType, date)
|
||||||
VALIDATED(TFunction, function)
|
VALIDATED(FunctionType, function)
|
||||||
VALIDATED(double, number)
|
VALIDATED(double, number)
|
||||||
VALIDATED(TObject, object)
|
VALIDATED(ObjectType, object)
|
||||||
VALIDATED(String<T>, string)
|
VALIDATED(String<T>, string)
|
||||||
|
|
||||||
#undef VALIDATED
|
#undef VALIDATED
|
||||||
|
@ -123,56 +123,56 @@ struct Value {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Function {
|
struct Function {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TFunction = typename T::Function;
|
using FunctionType = typename T::Function;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
|
|
||||||
static TValue call(TContext, const TFunction &, const TObject &, size_t, const TValue[]);
|
static ValueType call(ContextType, const FunctionType &, const ObjectType &, size_t, const ValueType[]);
|
||||||
static TValue call(TContext ctx, const TFunction &function, const TObject &this_object, const std::vector<TValue> &arguments) {
|
static ValueType call(ContextType ctx, const FunctionType &function, const ObjectType &this_object, const std::vector<ValueType> &arguments) {
|
||||||
return call(ctx, function, this_object, arguments.size(), arguments.data());
|
return call(ctx, function, this_object, arguments.size(), arguments.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
static TObject construct(TContext, const TFunction &, size_t, const TValue[]);
|
static ObjectType construct(ContextType, const FunctionType &, size_t, const ValueType[]);
|
||||||
static TValue construct(TContext ctx, const TFunction &function, const std::vector<TValue> &arguments) {
|
static ValueType construct(ContextType ctx, const FunctionType &function, const std::vector<ValueType> &arguments) {
|
||||||
return construct(ctx, function, arguments.size(), arguments.data());
|
return construct(ctx, function, arguments.size(), arguments.data());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Object {
|
struct Object {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TFunction = typename T::Function;
|
using FunctionType = typename T::Function;
|
||||||
using TObject = typename T::Object;
|
using ObjectType = typename T::Object;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static TValue get_prototype(TContext, const TObject &);
|
static ValueType get_prototype(ContextType, const ObjectType &);
|
||||||
static void set_prototype(TContext, const TObject &, const TValue &);
|
static void set_prototype(ContextType, const ObjectType &, const ValueType &);
|
||||||
|
|
||||||
static bool has_property(TContext, const TObject &, const String<T> &);
|
static bool has_property(ContextType, const ObjectType &, const String<T> &);
|
||||||
static bool has_property(TContext, const TObject &, uint32_t);
|
static bool has_property(ContextType, const ObjectType &, uint32_t);
|
||||||
static TValue get_property(TContext, const TObject &, const String<T> &);
|
static ValueType get_property(ContextType, const ObjectType &, const String<T> &);
|
||||||
static TValue get_property(TContext, const TObject &, uint32_t);
|
static ValueType get_property(ContextType, const ObjectType &, uint32_t);
|
||||||
static void set_property(TContext, const TObject &, const String<T> &, const TValue &, PropertyAttributes attributes = None);
|
static void set_property(ContextType, const ObjectType &, const String<T> &, const ValueType &, PropertyAttributes attributes = None);
|
||||||
static void set_property(TContext, const TObject &, uint32_t, const TValue &);
|
static void set_property(ContextType, const ObjectType &, uint32_t, const ValueType &);
|
||||||
static std::vector<String<T>> get_property_names(TContext, const TObject &);
|
static std::vector<String<T>> get_property_names(ContextType, const ObjectType &);
|
||||||
|
|
||||||
template<typename P>
|
template<typename P>
|
||||||
static TValue validated_get_property(TContext ctx, const TObject &object, const P &property, const char *message = nullptr) {
|
static ValueType validated_get_property(ContextType ctx, const ObjectType &object, const P &property, const char *message = nullptr) {
|
||||||
if (!has_property(ctx, object, property)) {
|
if (!has_property(ctx, object, property)) {
|
||||||
throw std::out_of_range(message ?: "Object missing expected property: " + util::to_string(property));
|
throw std::out_of_range(message ?: "Object missing expected property: " + util::to_string(property));
|
||||||
}
|
}
|
||||||
return get_property(ctx, object, property);
|
return get_property(ctx, object, property);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t validated_get_length(TContext ctx, const TObject &object) {
|
static uint32_t validated_get_length(ContextType ctx, const ObjectType &object) {
|
||||||
static const String<T> length_string = "length";
|
static const String<T> length_string = "length";
|
||||||
return Value<T>::validated_to_number(ctx, get_property(ctx, object, length_string));
|
return Value<T>::validated_to_number(ctx, get_property(ctx, object, length_string));
|
||||||
}
|
}
|
||||||
|
|
||||||
#define VALIDATED(return_t, type) \
|
#define VALIDATED(return_t, type) \
|
||||||
static return_t validated_get_##type(TContext ctx, const TObject &object, const String<T> &key, const char *message = nullptr) { \
|
static return_t validated_get_##type(ContextType ctx, const ObjectType &object, const String<T> &key, const char *message = nullptr) { \
|
||||||
try { \
|
try { \
|
||||||
return Value<T>::validated_to_##type(ctx, get_property(ctx, object, key), std::string(key).c_str()); \
|
return Value<T>::validated_to_##type(ctx, get_property(ctx, object, key), std::string(key).c_str()); \
|
||||||
} \
|
} \
|
||||||
|
@ -180,7 +180,7 @@ struct Object {
|
||||||
throw message ? std::invalid_argument(message) : e; \
|
throw message ? std::invalid_argument(message) : e; \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
static return_t validated_get_##type(TContext ctx, const TObject &object, uint32_t index, const char *message = nullptr) { \
|
static return_t validated_get_##type(ContextType ctx, const ObjectType &object, uint32_t index, const char *message = nullptr) { \
|
||||||
try { \
|
try { \
|
||||||
return Value<T>::validated_to_##type(ctx, get_property(ctx, object, index)); \
|
return Value<T>::validated_to_##type(ctx, get_property(ctx, object, index)); \
|
||||||
} \
|
} \
|
||||||
|
@ -189,78 +189,78 @@ struct Object {
|
||||||
} \
|
} \
|
||||||
}
|
}
|
||||||
|
|
||||||
VALIDATED(TObject, array)
|
VALIDATED(ObjectType, array)
|
||||||
VALIDATED(bool, boolean)
|
VALIDATED(bool, boolean)
|
||||||
VALIDATED(TFunction, constructor)
|
VALIDATED(FunctionType, constructor)
|
||||||
VALIDATED(TObject, date)
|
VALIDATED(ObjectType, date)
|
||||||
VALIDATED(TFunction, function)
|
VALIDATED(FunctionType, function)
|
||||||
VALIDATED(double, number)
|
VALIDATED(double, number)
|
||||||
VALIDATED(TObject, object)
|
VALIDATED(ObjectType, object)
|
||||||
VALIDATED(String<T>, string)
|
VALIDATED(String<T>, string)
|
||||||
|
|
||||||
#undef VALIDATED
|
#undef VALIDATED
|
||||||
|
|
||||||
static TValue call_method(TContext ctx, const TObject &object, const String<T> &name, uint32_t argc, const TValue arguments[]) {
|
static ValueType call_method(ContextType ctx, const ObjectType &object, const String<T> &name, uint32_t argc, const ValueType arguments[]) {
|
||||||
TFunction method = validated_get_function(ctx, object, name);
|
FunctionType method = validated_get_function(ctx, object, name);
|
||||||
return Function<T>::call(ctx, method, object, argc, arguments);
|
return Function<T>::call(ctx, method, object, argc, arguments);
|
||||||
}
|
}
|
||||||
static TValue call_method(TContext ctx, const TObject &object, const String<T> &name, const std::vector<TValue> &arguments) {
|
static ValueType call_method(ContextType ctx, const ObjectType &object, const String<T> &name, const std::vector<ValueType> &arguments) {
|
||||||
return call_method(ctx, object, name, (uint32_t)arguments.size(), arguments.data());
|
return call_method(ctx, object, name, (uint32_t)arguments.size(), arguments.data());
|
||||||
}
|
}
|
||||||
|
|
||||||
static TObject create_empty(TContext);
|
static ObjectType create_empty(ContextType);
|
||||||
static TObject create_array(TContext, uint32_t, const TValue[]);
|
static ObjectType create_array(ContextType, uint32_t, const ValueType[]);
|
||||||
|
|
||||||
static TObject create_array(TContext ctx, const std::vector<TValue> &values) {
|
static ObjectType create_array(ContextType ctx, const std::vector<ValueType> &values) {
|
||||||
return create_array(ctx, (uint32_t)values.size(), values.data());
|
return create_array(ctx, (uint32_t)values.size(), values.data());
|
||||||
}
|
}
|
||||||
static TObject create_array(TContext ctx) {
|
static ObjectType create_array(ContextType ctx) {
|
||||||
return create_array(ctx, 0, nullptr);
|
return create_array(ctx, 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static TObject create_date(TContext, double);
|
static ObjectType create_date(ContextType, double);
|
||||||
|
|
||||||
template<typename ClassType>
|
template<typename ClassType>
|
||||||
static TObject create_instance(TContext, typename ClassType::Internal*);
|
static ObjectType create_instance(ContextType, typename ClassType::Internal*);
|
||||||
|
|
||||||
template<typename ClassType>
|
template<typename ClassType>
|
||||||
static bool is_instance(TContext, const TObject &);
|
static bool is_instance(ContextType, const ObjectType &);
|
||||||
|
|
||||||
template<typename ClassType>
|
template<typename ClassType>
|
||||||
static typename ClassType::Internal* get_internal(const TObject &);
|
static typename ClassType::Internal* get_internal(const ObjectType &);
|
||||||
|
|
||||||
template<typename ClassType>
|
template<typename ClassType>
|
||||||
static void set_internal(const TObject &, typename ClassType::Internal*);
|
static void set_internal(const ObjectType &, typename ClassType::Internal*);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename TValue>
|
template<typename ValueType>
|
||||||
class Protected {
|
class Protected {
|
||||||
operator TValue() const;
|
operator ValueType() const;
|
||||||
bool operator==(const TValue &) const;
|
bool operator==(const ValueType &) const;
|
||||||
bool operator!=(const TValue &) const;
|
bool operator!=(const ValueType &) const;
|
||||||
bool operator==(const Protected<TValue> &) const;
|
bool operator==(const Protected<ValueType> &) const;
|
||||||
bool operator!=(const Protected<TValue> &) const;
|
bool operator!=(const Protected<ValueType> &) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct Exception : public std::runtime_error {
|
struct Exception : public std::runtime_error {
|
||||||
using TContext = typename T::Context;
|
using ContextType = typename T::Context;
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
|
|
||||||
const Protected<TValue> m_value;
|
const Protected<ValueType> m_value;
|
||||||
|
|
||||||
Exception(TContext ctx, const std::string &message)
|
Exception(ContextType ctx, const std::string &message)
|
||||||
: std::runtime_error(message), m_value(value(ctx, message)) {}
|
: std::runtime_error(message), m_value(value(ctx, message)) {}
|
||||||
Exception(TContext ctx, const TValue &val)
|
Exception(ContextType ctx, const ValueType &val)
|
||||||
: std::runtime_error(std::string(Value<T>::to_string(ctx, val))), m_value(ctx, val) {}
|
: std::runtime_error(std::string(Value<T>::to_string(ctx, val))), m_value(ctx, val) {}
|
||||||
|
|
||||||
operator TValue() const {
|
operator ValueType() const {
|
||||||
return m_value;
|
return m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
static TValue value(TContext ctx, const std::string &message);
|
static ValueType value(ContextType ctx, const std::string &message);
|
||||||
|
|
||||||
static TValue value(TContext ctx, const std::exception &exp) {
|
static ValueType value(ContextType ctx, const std::exception &exp) {
|
||||||
if (const Exception<T> *js_exp = dynamic_cast<const Exception<T> *>(&exp)) {
|
if (const Exception<T> *js_exp = dynamic_cast<const Exception<T> *>(&exp)) {
|
||||||
return *js_exp;
|
return *js_exp;
|
||||||
}
|
}
|
||||||
|
@ -270,9 +270,9 @@ struct Exception : public std::runtime_error {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct ReturnValue {
|
struct ReturnValue {
|
||||||
using TValue = typename T::Value;
|
using ValueType = typename T::Value;
|
||||||
|
|
||||||
void set(const TValue &);
|
void set(const ValueType &);
|
||||||
void set(const std::string &);
|
void set(const std::string &);
|
||||||
void set(bool);
|
void set(bool);
|
||||||
void set(double);
|
void set(double);
|
||||||
|
|
Loading…
Reference in New Issue