wrap all types in a single type and use static class methods

This commit is contained in:
Ari Lazier 2016-03-29 14:12:27 -07:00
parent b4990fbbff
commit e7d954a727
5 changed files with 83 additions and 55 deletions

View File

@ -18,7 +18,6 @@
#pragma once
#include "js_list.hpp"
#include "js_collection.hpp"
#include "js_object.hpp"
#include "js_results.hpp"
@ -33,12 +32,31 @@
#include <assert.h>
using RJSAccessor = realm::NativeAccessor<JSValueRef, JSContextRef>;
using namespace realm;
namespace realm {
namespace js {
template<typename ContextType, typename ObjectType, typename ValueType, typename ReturnType, typename ExceptionType>
void ListPush(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
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;
using ExceptionType = typename T::Exception;
static void Push(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception);
static void Pop(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception);
static void Unshift(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception);
static void Shift(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception);
static void Splice(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception);
static void StaticResults(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception);
static void Filtered(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception);
static void Sorted(ContextType ctx, ObjectType thisObject, size_t argCount, const ValueType args[], ReturnType &ret, ExceptionType &exception);
};
template<typename T>
void List<T>::Push(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
try {
List *list = RJSGetInternal<List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
for (size_t i = 0; i < argumentCount; i++) {
list->add(ctx, arguments[i]);
@ -50,10 +68,10 @@ void ListPush(ContextType ctx, ObjectType thisObject, size_t argumentCount, cons
}
}
template<typename ContextType, typename ObjectType, typename ValueType, typename ReturnType, typename ExceptionType>
void ListPop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
template<typename T>
void List<T>::Pop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
try {
List *list = RJSGetInternal<List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
RJSValidateArgumentCount(argumentCount, 0);
size_t size = list->size();
@ -73,10 +91,10 @@ void ListPop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const
}
template<typename ContextType, typename ObjectType, typename ValueType, typename ReturnType, typename ExceptionType>
void ListUnshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
template<typename T>
void List<T>::Unshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
try {
List *list = RJSGetInternal<List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
for (size_t i = 0; i < argumentCount; i++) {
list->insert(ctx, arguments[i], i);
@ -88,10 +106,10 @@ void ListUnshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, c
}
}
template<typename ContextType, typename ObjectType, typename ValueType, typename ReturnType, typename ExceptionType>
void ListShift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
template<typename T>
void List<T>::Shift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
try {
List *list = RJSGetInternal<List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
RJSValidateArgumentCount(argumentCount, 0);
if (list->size() == 0) {
list->verify_in_transaction();
@ -107,10 +125,10 @@ void ListShift(ContextType ctx, ObjectType thisObject, size_t argumentCount, con
}
}
template<typename ContextType, typename ObjectType, typename ValueType, typename ReturnType, typename ExceptionType>
void ListSplice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
template<typename T>
void List<T>::Splice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
try {
List *list = RJSGetInternal<List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
size_t size = list->size();
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
@ -144,10 +162,10 @@ void ListSplice(ContextType ctx, ObjectType thisObject, size_t argumentCount, co
}
template<typename ContextType, typename ObjectType, typename ValueType, typename ReturnType, typename ExceptionType>
void ListStaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
template<typename T>
void List<T>::StaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
try {
List *list = RJSGetInternal<List *>(thisObject);
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);
}
@ -156,10 +174,10 @@ void ListStaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCo
}
}
template<typename ContextType, typename ObjectType, typename ValueType, typename ReturnType, typename ExceptionType>
void ListFiltered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
template<typename T>
void List<T>::Filtered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
try {
List *list = RJSGetInternal<List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
RJSValidateArgumentCountIsAtLeast(argumentCount, 1);
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
@ -170,10 +188,10 @@ void ListFiltered(ContextType ctx, ObjectType thisObject, size_t argumentCount,
}
}
template<typename ContextType, typename ObjectType, typename ValueType, typename ReturnType, typename ExceptionType>
void ListSorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
template<typename T>
void List<T>::Sorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) {
try {
List *list = RJSGetInternal<List *>(thisObject);
realm::List *list = RJSGetInternal<realm::List *>(thisObject);
RJSValidateArgumentRange(argumentCount, 1, 2);
SharedRealm sharedRealm = *RJSGetInternal<SharedRealm *>(thisObject);
@ -183,3 +201,6 @@ void ListSorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, co
RJSSetException(ctx, exceptionObject, exp);
}
}
}
}

View File

@ -33,10 +33,17 @@
#include "js_compat.hpp"
#include "schema.hpp"
#define WRAP_CLASS_METHOD(CLASS_NAME, METHOD_NAME) \
JSValueRef CLASS_NAME ## METHOD_NAME(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { \
JSValueRef returnObject = NULL; \
CLASS_NAME::METHOD_NAME(ctx, thisObject, argumentCount, arguments, returnObject, *jsException); \
return returnObject; \
}
#define WRAP_METHOD(METHOD_NAME) \
JSValueRef METHOD_NAME(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* jsException) { \
JSValueRef returnObject = NULL; \
METHOD_NAME(ctx, thisObject, argumentCount, arguments, returnObject, jsException); \
METHOD_NAME(ctx, thisObject, argumentCount, arguments, returnObject, *jsException); \
return returnObject; \
}
@ -274,10 +281,8 @@ static inline void RJSSetReturnArray(JSContextRef ctx, size_t count, const JSVal
returnObject = JSObjectMakeArray(ctx, count, objects, NULL);
}
static inline void RJSSetException(JSContextRef ctx, JSValueRef * &exceptionObject, std::exception &exception) {
if (exceptionObject) {
*exceptionObject = RJSMakeError(ctx, exception);
}
static inline void RJSSetException(JSContextRef ctx, JSValueRef &exceptionObject, std::exception &exception) {
exceptionObject = RJSMakeError(ctx, exception);
}
static JSObjectRef RJSDictForPropertyArray(JSContextRef ctx, const realm::ObjectSchema &object_schema, JSObjectRef array) {

View File

@ -24,11 +24,11 @@
namespace realm {
namespace js {
static bool ValueIsUndefined(Types::Context ctx, Types::Value value) { return JSValueIsUndefined(ctx, value); }
static bool ValueIsNull(Types::Context ctx, Types::Value value) { return JSValueIsNull(ctx, value); }
static bool ValueIsBoolean(Types::Context ctx, Types::Value value) { return JSValueIsBoolean(ctx, value); }
static bool ValueIsNumber(Types::Context ctx, Types::Value value) { return JSValueIsNumber(ctx, value); }
static bool ValueIsString(Types::Context ctx, Types::Value value) { return JSValueIsString(ctx, value); }
static bool ValueIsObject(Types::Context ctx, Types::Value value) { return JSValueIsObject(ctx, value); }
static bool ValueIsUndefined(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsUndefined(ctx, value); }
static bool ValueIsNull(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsNull(ctx, value); }
static bool ValueIsBoolean(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsBoolean(ctx, value); }
static bool ValueIsNumber(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsNumber(ctx, value); }
static bool ValueIsString(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsString(ctx, value); }
static bool ValueIsObject(jsc::Types::Context ctx, jsc::Types::Value value) { return JSValueIsObject(ctx, value); }
}}

View File

@ -86,28 +86,29 @@ void ListPropertyNames(JSContextRef ctx, JSObjectRef object, JSPropertyNameAccum
}
}
WRAP_METHOD(ListPush)
WRAP_METHOD(ListPop)
WRAP_METHOD(ListUnshift)
WRAP_METHOD(ListShift)
WRAP_METHOD(ListSplice)
WRAP_METHOD(ListStaticResults)
WRAP_METHOD(ListFiltered)
WRAP_METHOD(ListSorted)
using RJSList = realm::js::List<realm::jsc::Types>;
WRAP_CLASS_METHOD(RJSList, Push)
WRAP_CLASS_METHOD(RJSList, Pop)
WRAP_CLASS_METHOD(RJSList, Unshift)
WRAP_CLASS_METHOD(RJSList, Shift)
WRAP_CLASS_METHOD(RJSList, Splice)
WRAP_CLASS_METHOD(RJSList, StaticResults)
WRAP_CLASS_METHOD(RJSList, Filtered)
WRAP_CLASS_METHOD(RJSList, Sorted)
JSObjectRef RJSListCreate(JSContextRef ctx, List &list) {
return RJSWrapObject<List *>(ctx, RJSListClass(), new List(list));
}
static const JSStaticFunction RJSListFuncs[] = {
{"push", ListPush, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"pop", ListPop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"shift", ListShift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"unshift", ListUnshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"splice", ListSplice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"filtered", ListFiltered, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"sorted", ListSorted, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"snapshot", ListStaticResults, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"push", RJSListPush, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"pop", RJSListPop, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"shift", RJSListShift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"unshift", RJSListUnshift, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"splice", RJSListSplice, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"filtered", RJSListFiltered, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"sorted", RJSListSorted, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"snapshot", RJSListStaticResults, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL},
};

View File

@ -23,7 +23,7 @@
#include <JavaScriptCore/JSStringRef.h>
namespace realm {
namespace js {
namespace jsc {
struct Types {
using Context = JSContextRef;
@ -32,6 +32,7 @@ struct Types {
using String = JSStringRef;
using Function = JSObjectRef;
using Return = JSValueRef;
using Exception = JSValueRef;
};
}}