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-03-28 20:21:36 +00:00
|
|
|
#include "js_collection.hpp"
|
|
|
|
#include "js_object.hpp"
|
|
|
|
#include "js_results.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 "object_accessor.hpp"
|
|
|
|
#include "parser.hpp"
|
|
|
|
#include "query_builder.hpp"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>;
|
2016-03-29 21:12:27 +00:00
|
|
|
namespace realm {
|
|
|
|
namespace js {
|
2016-03-28 20:21:36 +00:00
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
|
|
|
struct List {
|
|
|
|
using ContextType = typename T::Context;
|
|
|
|
using ObjectType = typename T::Object;
|
|
|
|
using ValueType = typename T::Value;
|
|
|
|
using ReturnType = typename T::Return;
|
2016-03-30 18:55:13 +00:00
|
|
|
|
|
|
|
static void Push(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
|
|
|
|
static void Pop(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
|
|
|
|
static void Unshift(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
|
|
|
|
static void Shift(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
|
|
|
|
static void Splice(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
|
|
|
|
static void StaticResults(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
|
|
|
|
static void Filtered(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
|
|
|
|
static void Sorted(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret);
|
2016-03-29 21:12:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2016-03-30 18:55:13 +00:00
|
|
|
void List<T>::Push(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
|
|
|
|
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
|
|
|
|
for (size_t i = 0; i < argumentCount; i++) {
|
|
|
|
list->add(ctx, arguments[i]);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
RJSSetReturnNumber(ctx, returnObject, list->size());
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-03-30 18:55:13 +00:00
|
|
|
void List<T>::Pop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
|
|
|
|
RJSValidateArgumentCount(argumentCount, 0);
|
|
|
|
|
|
|
|
size_t size = list->size();
|
|
|
|
if (size == 0) {
|
|
|
|
list->verify_in_transaction();
|
|
|
|
RJSSetReturnUndefined(ctx, returnObject);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
else {
|
|
|
|
size_t index = size - 1;
|
|
|
|
returnObject = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(index)));
|
|
|
|
list->remove(index);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-03-30 18:55:13 +00:00
|
|
|
void List<T>::Unshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
|
|
|
|
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
|
|
|
|
for (size_t i = 0; i < argumentCount; i++) {
|
|
|
|
list->insert(ctx, arguments[i], i);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
RJSSetReturnNumber(ctx, returnObject, list->size());
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-03-30 18:55:13 +00:00
|
|
|
void List<T>::Shift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
|
|
|
|
RJSValidateArgumentCount(argumentCount, 0);
|
|
|
|
if (list->size() == 0) {
|
|
|
|
list->verify_in_transaction();
|
|
|
|
RJSSetReturnUndefined(ctx, returnObject);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
else {
|
|
|
|
returnObject = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(0)));
|
|
|
|
list->remove(0);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-03-30 18:55:13 +00:00
|
|
|
void List<T>::Splice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
|
|
|
|
size_t size = list->size();
|
|
|
|
|
|
|
|
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
|
|
|
|
long index = std::min<long>(RJSValidatedValueToNumber(ctx, arguments[0]), size);
|
|
|
|
if (index < 0) {
|
|
|
|
index = std::max<long>(size + index, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
long remove;
|
|
|
|
if (argumentCount < 2) {
|
|
|
|
remove = size - index;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
remove = std::max<long>(RJSValidatedValueToNumber(ctx, arguments[1]), 0);
|
|
|
|
remove = std::min<long>(remove, size - index);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ReturnType> removedObjects(remove);
|
|
|
|
for (size_t i = 0; i < remove; i++) {
|
|
|
|
removedObjects[i] = RJSObjectCreate(ctx, Object(list->get_realm(), list->get_object_schema(), list->get(index)));
|
|
|
|
list->remove(index);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
for (size_t i = 2; i < argumentCount; i++) {
|
|
|
|
list->insert(ctx, arguments[i], index + i - 2);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
2016-03-30 18:55:13 +00:00
|
|
|
RJSSetReturnArray(ctx, remove, removedObjects.data(), returnObject);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-03-30 18:55:13 +00:00
|
|
|
void List<T>::StaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
|
|
|
|
RJSValidateArgumentCount(argumentCount, 0);
|
|
|
|
returnObject = RJSResultsCreate(ctx, list->get_realm(), list->get_object_schema(), std::move(list->get_query()), false);
|
2016-03-28 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 21:12:27 +00:00
|
|
|
template<typename T>
|
2016-03-30 18:55:13 +00:00
|
|
|
void List<T>::Filtered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
|
|
|
|
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
|
|
|
|
|
|
|
|
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
|
|
|
|
returnObject = RJSResultsCreateFiltered(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments);
|
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>
|
2016-03-30 18:55:13 +00:00
|
|
|
void List<T>::Sorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject) {
|
|
|
|
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
|
|
|
|
RJSValidateArgumentRange(argumentCount, 1, 2);
|
|
|
|
|
|
|
|
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
|
|
|
|
returnObject = RJSResultsCreateSorted(ctx, sharedRealm, list->get_object_schema(), std::move(list->get_query()), argumentCount, arguments);
|
2016-03-29 21:12:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|