From a4194586eab163f475c743b737f2f6009b11bd2f Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Mon, 12 Oct 2015 15:42:56 -0700 Subject: [PATCH] Native methods should convert to numbers/bools It will still throw if unable to make the conversion. Fixes #51 --- src/RJSRealm.mm | 2 +- src/RJSResults.mm | 2 +- src/RJSUtil.hpp | 15 --------------- 3 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/RJSRealm.mm b/src/RJSRealm.mm index 192b7a34..83fe4be1 100644 --- a/src/RJSRealm.mm +++ b/src/RJSRealm.mm @@ -261,7 +261,7 @@ JSValueRef RealmCreateObject(JSContextRef ctx, JSObjectRef function, JSObjectRef bool update = false; if (argumentCount == 3) { - update = RJSValidatedValueToBool(ctx, arguments[2]); + update = JSValueToBoolean(ctx, arguments[2]); } return RJSObjectCreate(ctx, Object::create(ctx, sharedRealm, *object_schema, object, update)); diff --git a/src/RJSResults.mm b/src/RJSResults.mm index 9443af85..7cdad982 100644 --- a/src/RJSResults.mm +++ b/src/RJSResults.mm @@ -75,7 +75,7 @@ JSValueRef SortByProperty(JSContextRef ctx, JSObjectRef function, JSObjectRef th bool ascending = true; if (argumentCount == 2) { - ascending = RJSValidatedValueToBool(ctx, arguments[1]); + ascending = JSValueToBoolean(ctx, arguments[1]); } SortOrder sort = {{prop->table_column}, {ascending}}; diff --git a/src/RJSUtil.hpp b/src/RJSUtil.hpp index f6369ac5..0a8412ed 100644 --- a/src/RJSUtil.hpp +++ b/src/RJSUtil.hpp @@ -108,9 +108,6 @@ static inline JSObjectRef RJSValidatedValueToObject(JSContextRef ctx, JSValueRef static inline double RJSValidatedValueToNumber(JSContextRef ctx, JSValueRef value) { JSValueRef exception = NULL; - if (!JSValueIsNumber(ctx, value)) { - throw std::runtime_error("Value is not a number"); - } double number = JSValueToNumber(ctx, value, &exception); if (exception) { throw RJSException(ctx, exception); @@ -118,18 +115,6 @@ static inline double RJSValidatedValueToNumber(JSContextRef ctx, JSValueRef valu return number; } -static inline bool RJSValidatedValueToBool(JSContextRef ctx, JSValueRef value) { - JSValueRef exception = NULL; - if (!JSValueIsBoolean(ctx, value)) { - throw std::runtime_error("Value is not a boolean"); - } - bool b = JSValueToNumber(ctx, value, &exception); - if (exception) { - throw RJSException(ctx, exception); - } - return b; -} - static inline JSValueRef RJSValidatedPropertyValue(JSContextRef ctx, JSObjectRef object, JSStringRef property) { JSValueRef exception = NULL; JSValueRef propertyValue = JSObjectGetProperty(ctx, object, property, &exception);