2016-02-18 19:59:34 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Copyright 2016 Realm Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2015-12-01 22:52:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
2016-04-12 21:42:05 +00:00
|
|
|
#include "js_collection.hpp"
|
2017-04-26 19:33:15 +00:00
|
|
|
#include "js_object_accessor.hpp"
|
2016-04-18 08:14:48 +00:00
|
|
|
#include "js_realm_object.hpp"
|
2016-04-12 21:42:05 +00:00
|
|
|
#include "js_results.hpp"
|
2016-03-30 21:11:57 +00:00
|
|
|
#include "js_types.hpp"
|
2015-12-01 22:52:38 +00:00
|
|
|
#include "js_util.hpp"
|
2016-03-28 20:21:36 +00:00
|
|
|
|
2015-12-01 22:52:38 +00:00
|
|
|
#include "shared_realm.hpp"
|
|
|
|
#include "list.hpp"
|
2016-03-28 20:21:36 +00:00
|
|
|
#include "parser.hpp"
|
|
|
|
#include "query_builder.hpp"
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
namespace realm {
|
|
|
|
namespace js {
|
2016-03-28 20:21:36 +00:00
|
|
|
|
2017-04-26 19:33:15 +00:00
|
|
|
template<typename JSEngine>
|
|
|
|
class NativeAccessor;
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-10-04 22:02:51 +00:00
|
|
|
class List : public realm::List {
|
|
|
|
public:
|
2017-03-07 22:24:30 +00:00
|
|
|
List(std::shared_ptr<realm::Realm> r, const ObjectSchema& s, LinkViewRef l) noexcept : realm::List(r, l) {}
|
2016-10-04 22:02:51 +00:00
|
|
|
List(const realm::List &l) : realm::List(l) {}
|
2017-04-26 19:33:15 +00:00
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
std::vector<std::pair<Protected<typename T::Function>, NotificationToken>> m_notification_tokens;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct ListClass : ClassDefinition<T, realm::js::List<T>, CollectionClass<T>> {
|
2016-04-18 19:15:00 +00:00
|
|
|
using ContextType = typename T::Context;
|
|
|
|
using ObjectType = typename T::Object;
|
|
|
|
using ValueType = typename T::Value;
|
2016-10-04 22:02:51 +00:00
|
|
|
using FunctionType = typename T::Function;
|
2016-04-19 01:30:55 +00:00
|
|
|
using Object = js::Object<T>;
|
|
|
|
using Value = js::Value<T>;
|
|
|
|
using ReturnValue = js::ReturnValue<T>;
|
2017-09-11 21:57:31 +00:00
|
|
|
using Arguments = js::Arguments<T>;
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-05-31 22:27:48 +00:00
|
|
|
static ObjectType create_instance(ContextType, realm::List);
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-04-18 05:49:07 +00:00
|
|
|
// properties
|
2016-04-18 19:15:00 +00:00
|
|
|
static void get_length(ContextType, ObjectType, ReturnValue &);
|
2017-09-11 21:57:31 +00:00
|
|
|
static void get_type(ContextType, ObjectType, ReturnValue &);
|
|
|
|
static void get_optional(ContextType, ObjectType, ReturnValue &);
|
2016-04-18 19:15:00 +00:00
|
|
|
static void get_index(ContextType, ObjectType, uint32_t, ReturnValue &);
|
|
|
|
static bool set_index(ContextType, ObjectType, uint32_t, ValueType);
|
2016-04-18 05:49:07 +00:00
|
|
|
|
|
|
|
// methods
|
2017-09-11 21:57:31 +00:00
|
|
|
static void push(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void pop(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void unshift(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void shift(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void splice(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void snapshot(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void filtered(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void sorted(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void is_valid(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void index_of(ContextType, ObjectType, Arguments, ReturnValue &);
|
2017-06-26 11:43:49 +00:00
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
// observable
|
2017-09-11 21:57:31 +00:00
|
|
|
static void add_listener(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void remove_listener(ContextType, ObjectType, Arguments, ReturnValue &);
|
|
|
|
static void remove_all_listeners(ContextType, ObjectType, Arguments, ReturnValue &);
|
2016-10-04 22:02:51 +00:00
|
|
|
|
2016-03-30 21:11:57 +00:00
|
|
|
std::string const name = "List";
|
|
|
|
|
|
|
|
MethodMap<T> const methods = {
|
2016-05-14 00:12:06 +00:00
|
|
|
{"push", wrap<push>},
|
|
|
|
{"pop", wrap<pop>},
|
|
|
|
{"unshift", wrap<unshift>},
|
|
|
|
{"shift", wrap<shift>},
|
|
|
|
{"splice", wrap<splice>},
|
|
|
|
{"snapshot", wrap<snapshot>},
|
|
|
|
{"filtered", wrap<filtered>},
|
|
|
|
{"sorted", wrap<sorted>},
|
2016-05-16 23:01:14 +00:00
|
|
|
{"isValid", wrap<is_valid>},
|
2017-06-26 11:43:49 +00:00
|
|
|
{"indexOf", wrap<index_of>},
|
2016-10-04 22:02:51 +00:00
|
|
|
{"addListener", wrap<add_listener>},
|
|
|
|
{"removeListener", wrap<remove_listener>},
|
|
|
|
{"removeAllListeners", wrap<remove_all_listeners>},
|
2016-03-30 21:11:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PropertyMap<T> const properties = {
|
2016-05-14 00:12:06 +00:00
|
|
|
{"length", {wrap<get_length>, nullptr}},
|
2017-09-11 21:57:31 +00:00
|
|
|
{"type", {wrap<get_type>, nullptr}},
|
|
|
|
{"optional", {wrap<get_optional>, nullptr}},
|
2016-03-30 21:11:57 +00:00
|
|
|
};
|
|
|
|
|
2016-05-14 00:12:06 +00:00
|
|
|
IndexPropertyType<T> const index_accessor = {wrap<get_index>, wrap<set_index>};
|
2017-09-11 21:57:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void validate_value(ContextType, realm::List&, ValueType);
|
2016-03-30 21:11:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-05-31 22:27:48 +00:00
|
|
|
typename T::Object ListClass<T>::create_instance(ContextType ctx, realm::List list) {
|
2016-10-04 22:02:51 +00:00
|
|
|
return create_object<T, ListClass<T>>(ctx, new realm::js::List<T>(std::move(list)));
|
2016-03-30 21:18:44 +00:00
|
|
|
}
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-03-30 21:18:44 +00:00
|
|
|
template<typename T>
|
2017-04-26 19:33:15 +00:00
|
|
|
void ListClass<T>::get_length(ContextType, ObjectType object, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(object);
|
2016-03-30 21:11:57 +00:00
|
|
|
return_value.set((uint32_t)list->size());
|
2016-03-30 21:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::get_type(ContextType, ObjectType object, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(object);
|
2017-09-11 21:57:31 +00:00
|
|
|
return_value.set(string_for_property_type(list->get_type() & ~realm::PropertyType::Flags));
|
|
|
|
}
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
template<typename T>
|
|
|
|
void ListClass<T>::get_optional(ContextType, ObjectType object, ReturnValue &return_value) {
|
|
|
|
auto list = get_internal<T, ListClass<T>>(object);
|
|
|
|
return_value.set(is_nullable(list->get_type()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void ListClass<T>::get_index(ContextType ctx, ObjectType object, uint32_t index, ReturnValue &return_value) {
|
|
|
|
auto list = get_internal<T, ListClass<T>>(object);
|
|
|
|
NativeAccessor<T> accessor(ctx, *list);
|
|
|
|
return_value.set(list->get(accessor, index));
|
2016-03-30 21:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-05-14 00:12:06 +00:00
|
|
|
bool ListClass<T>::set_index(ContextType ctx, ObjectType object, uint32_t index, ValueType value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(object);
|
2017-09-11 21:57:31 +00:00
|
|
|
validate_value(ctx, *list, value);
|
|
|
|
NativeAccessor<T> accessor(ctx, *list);
|
2017-04-26 19:33:15 +00:00
|
|
|
list->set(accessor, index, value);
|
2016-03-30 21:11:57 +00:00
|
|
|
return true;
|
2016-03-30 21:18:44 +00:00
|
|
|
}
|
2016-03-29 21:12:27 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::push(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2017-09-11 21:57:31 +00:00
|
|
|
for (size_t i = 0; i < args.count; i++) {
|
|
|
|
validate_value(ctx, *list, args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
NativeAccessor<T> accessor(ctx, *list);
|
|
|
|
for (size_t i = 0; i < args.count; i++) {
|
|
|
|
list->add(accessor, args[i]);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 21:11:57 +00:00
|
|
|
|
|
|
|
return_value.set((uint32_t)list->size());
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::pop(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
|
|
|
args.validate_maximum(0);
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2017-09-11 21:57:31 +00:00
|
|
|
auto size = static_cast<unsigned int>(list->size());
|
2016-03-30 18:55:13 +00:00
|
|
|
if (size == 0) {
|
|
|
|
list->verify_in_transaction();
|
2016-03-30 21:11:57 +00:00
|
|
|
return_value.set_undefined();
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
else {
|
2017-09-11 21:57:31 +00:00
|
|
|
get_index(ctx, this_object, size - 1, return_value);
|
|
|
|
list->remove(size - 1);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::unshift(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2017-09-11 21:57:31 +00:00
|
|
|
for (size_t i = 0; i < args.count; i++) {
|
|
|
|
validate_value(ctx, *list, args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
NativeAccessor<T> accessor(ctx, *list);
|
|
|
|
for (size_t i = 0; i < args.count; i++) {
|
|
|
|
list->insert(accessor, i, args[i]);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 21:11:57 +00:00
|
|
|
|
|
|
|
return_value.set((uint32_t)list->size());
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::shift(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
|
|
|
args.validate_maximum(0);
|
2016-03-30 21:11:57 +00:00
|
|
|
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-03-30 18:55:13 +00:00
|
|
|
if (list->size() == 0) {
|
|
|
|
list->verify_in_transaction();
|
2016-03-30 21:11:57 +00:00
|
|
|
return_value.set_undefined();
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
else {
|
2017-09-11 21:57:31 +00:00
|
|
|
get_index(ctx, this_object, 0, return_value);
|
2016-03-30 18:55:13 +00:00
|
|
|
list->remove(0);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::splice(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-03-30 18:55:13 +00:00
|
|
|
size_t size = list->size();
|
2017-09-11 21:57:31 +00:00
|
|
|
long index = std::min<long>(Value::to_number(ctx, args[0]), size);
|
2016-03-30 18:55:13 +00:00
|
|
|
if (index < 0) {
|
|
|
|
index = std::max<long>(size + index, 0);
|
|
|
|
}
|
2016-04-18 05:49:07 +00:00
|
|
|
|
|
|
|
size_t remove;
|
2017-09-11 21:57:31 +00:00
|
|
|
if (args.count < 2) {
|
2016-03-30 18:55:13 +00:00
|
|
|
remove = size - index;
|
|
|
|
}
|
|
|
|
else {
|
2017-09-11 21:57:31 +00:00
|
|
|
remove = std::max<long>(Value::to_number(ctx, args[1]), 0);
|
2016-03-30 18:55:13 +00:00
|
|
|
remove = std::min<long>(remove, size - index);
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:15:00 +00:00
|
|
|
std::vector<ValueType> removed_objects;
|
2016-03-30 21:11:57 +00:00
|
|
|
removed_objects.reserve(remove);
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
NativeAccessor<T> accessor(ctx, *list);
|
2016-03-30 18:55:13 +00:00
|
|
|
for (size_t i = 0; i < remove; i++) {
|
2017-09-11 21:57:31 +00:00
|
|
|
removed_objects.push_back(list->get(accessor, index));
|
2016-03-30 18:55:13 +00:00
|
|
|
list->remove(index);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2017-09-11 21:57:31 +00:00
|
|
|
for (size_t i = 2; i < args.count; i++) {
|
|
|
|
list->insert(accessor, index + i - 2, args[i]);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 21:11:57 +00:00
|
|
|
return_value.set(Object::create_array(ctx, removed_objects));
|
|
|
|
}
|
2016-03-28 20:21:36 +00:00
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::snapshot(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
|
|
|
args.validate_maximum(0);
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2016-07-18 21:41:21 +00:00
|
|
|
return_value.set(ResultsClass<T>::create_instance(ctx, list->snapshot()));
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::filtered(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2017-09-11 21:57:31 +00:00
|
|
|
return_value.set(ResultsClass<T>::create_filtered(ctx, *list, args));
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2015-08-13 16:12:48 +00:00
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::sorted(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
2016-04-15 20:47:01 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2017-09-11 21:57:31 +00:00
|
|
|
return_value.set(ResultsClass<T>::create_instance(ctx, list->sort(ResultsClass<T>::get_keypaths(ctx, args))));
|
2016-03-29 21:12:27 +00:00
|
|
|
}
|
|
|
|
|
2016-05-16 23:01:14 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::is_valid(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
2016-05-16 23:01:14 +00:00
|
|
|
return_value.set(get_internal<T, ListClass<T>>(this_object)->is_valid());
|
|
|
|
}
|
2017-06-26 11:43:49 +00:00
|
|
|
|
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::index_of(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
|
|
|
auto fn = [&](auto&& row) {
|
2017-06-26 11:43:49 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2017-09-11 21:57:31 +00:00
|
|
|
NativeAccessor<T> accessor(ctx, *list);
|
|
|
|
return list->find(accessor, row);
|
|
|
|
};
|
|
|
|
ResultsClass<T>::index_of(ctx, fn, args, return_value);
|
2017-06-26 11:43:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 22:02:51 +00:00
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::add_listener(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
2016-10-04 22:02:51 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2017-09-11 21:57:31 +00:00
|
|
|
ResultsClass<T>::add_listener(ctx, *list, this_object, args);
|
2016-10-04 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-09-11 21:57:31 +00:00
|
|
|
void ListClass<T>::remove_listener(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
2016-10-04 22:02:51 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
2017-09-11 21:57:31 +00:00
|
|
|
ResultsClass<T>::remove_listener(ctx, *list, this_object, args);
|
2016-10-04 22:02:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
template<typename T>
|
|
|
|
void ListClass<T>::remove_all_listeners(ContextType ctx, ObjectType this_object, Arguments args, ReturnValue &return_value) {
|
|
|
|
args.validate_maximum(0);
|
2016-10-04 22:02:51 +00:00
|
|
|
auto list = get_internal<T, ListClass<T>>(this_object);
|
|
|
|
list->m_notification_tokens.clear();
|
|
|
|
}
|
2016-05-16 23:01:14 +00:00
|
|
|
|
2017-09-11 21:57:31 +00:00
|
|
|
template<typename T>
|
|
|
|
void ListClass<T>::validate_value(ContextType ctx, realm::List& list, ValueType value) {
|
|
|
|
auto type = list.get_type();
|
|
|
|
StringData object_type;
|
|
|
|
if (type == realm::PropertyType::Object) {
|
|
|
|
object_type = list.get_object_schema().name;
|
|
|
|
}
|
|
|
|
if (!Value::is_valid_for_property_type(ctx, value, type, object_type)) {
|
|
|
|
throw TypeErrorException("Property", object_type ? object_type : string_for_property_type(type), Value::to_string(ctx, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-30 21:11:57 +00:00
|
|
|
} // js
|
|
|
|
} // realm
|