From e7d954a72767c1b6e9fa13c5b328a8f83256b4b9 Mon Sep 17 00:00:00 2001 From: Ari Lazier Date: Tue, 29 Mar 2016 14:12:27 -0700 Subject: [PATCH] wrap all types in a single type and use static class methods --- src/js_list.hpp | 75 +++++++++++++++++++++++++++---------------- src/js_util.hpp | 15 ++++++--- src/jsc/js_compat.hpp | 12 +++---- src/jsc/jsc_list.cpp | 33 ++++++++++--------- src/jsc/types.hpp | 3 +- 5 files changed, 83 insertions(+), 55 deletions(-) diff --git a/src/js_list.hpp b/src/js_list.hpp index 660bc981..76a53dd7 100644 --- a/src/js_list.hpp +++ b/src/js_list.hpp @@ -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 using RJSAccessor = realm::NativeAccessor; -using namespace realm; +namespace realm { +namespace js { -template -void ListPush(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { +template +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 +void List::Push(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { try { - List *list = RJSGetInternal(thisObject); + realm::List *list = RJSGetInternal(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 -void ListPop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { +template +void List::Pop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { try { - List *list = RJSGetInternal(thisObject); + realm::List *list = RJSGetInternal(thisObject); RJSValidateArgumentCount(argumentCount, 0); size_t size = list->size(); @@ -73,10 +91,10 @@ void ListPop(ContextType ctx, ObjectType thisObject, size_t argumentCount, const } -template -void ListUnshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { +template +void List::Unshift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { try { - List *list = RJSGetInternal(thisObject); + realm::List *list = RJSGetInternal(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 -void ListShift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { +template +void List::Shift(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { try { - List *list = RJSGetInternal(thisObject); + realm::List *list = RJSGetInternal(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 -void ListSplice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { +template +void List::Splice(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { try { - List *list = RJSGetInternal(thisObject); + realm::List *list = RJSGetInternal(thisObject); size_t size = list->size(); RJSValidateArgumentCountIsAtLeast(argumentCount, 1); @@ -144,10 +162,10 @@ void ListSplice(ContextType ctx, ObjectType thisObject, size_t argumentCount, co } -template -void ListStaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { +template +void List::StaticResults(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { try { - List *list = RJSGetInternal(thisObject); + realm::List *list = RJSGetInternal(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 -void ListFiltered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { +template +void List::Filtered(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { try { - List *list = RJSGetInternal(thisObject); + realm::List *list = RJSGetInternal(thisObject); RJSValidateArgumentCountIsAtLeast(argumentCount, 1); SharedRealm sharedRealm = *RJSGetInternal(thisObject); @@ -170,10 +188,10 @@ void ListFiltered(ContextType ctx, ObjectType thisObject, size_t argumentCount, } } -template -void ListSorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { +template +void List::Sorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, const ValueType arguments[], ReturnType &returnObject, ExceptionType &exceptionObject) { try { - List *list = RJSGetInternal(thisObject); + realm::List *list = RJSGetInternal(thisObject); RJSValidateArgumentRange(argumentCount, 1, 2); SharedRealm sharedRealm = *RJSGetInternal(thisObject); @@ -182,4 +200,7 @@ void ListSorted(ContextType ctx, ObjectType thisObject, size_t argumentCount, co catch (std::exception &exp) { RJSSetException(ctx, exceptionObject, exp); } -} \ No newline at end of file +} + +} +} diff --git a/src/js_util.hpp b/src/js_util.hpp index c56e3fc3..72c891c6 100644 --- a/src/js_util.hpp +++ b/src/js_util.hpp @@ -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) { diff --git a/src/jsc/js_compat.hpp b/src/jsc/js_compat.hpp index 9218484a..8e36cd85 100644 --- a/src/jsc/js_compat.hpp +++ b/src/jsc/js_compat.hpp @@ -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); } }} \ No newline at end of file diff --git a/src/jsc/jsc_list.cpp b/src/jsc/jsc_list.cpp index 653249c5..dc059cd6 100644 --- a/src/jsc/jsc_list.cpp +++ b/src/jsc/jsc_list.cpp @@ -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; +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(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}, }; diff --git a/src/jsc/types.hpp b/src/jsc/types.hpp index 1d2c5406..b5f8db3d 100644 --- a/src/jsc/types.hpp +++ b/src/jsc/types.hpp @@ -23,7 +23,7 @@ #include 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; }; }} \ No newline at end of file