diff --git a/src/ios/RealmJS.xcodeproj/project.pbxproj b/src/ios/RealmJS.xcodeproj/project.pbxproj index 7662ccf9..a3a86c77 100644 --- a/src/ios/RealmJS.xcodeproj/project.pbxproj +++ b/src/ios/RealmJS.xcodeproj/project.pbxproj @@ -51,7 +51,6 @@ F60102E01CBB96D900EC01BA /* realm_coordinator.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 02F59EDB1C88F2BA007F774C /* realm_coordinator.cpp */; }; F60102E11CBB96DD00EC01BA /* transact_log_handler.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 02F59EDD1C88F2BB007F774C /* transact_log_handler.cpp */; }; F60102E51CBBB19700EC01BA /* node_object_accessor.hpp in Headers */ = {isa = PBXBuildFile; fileRef = F60102E31CBBB19700EC01BA /* node_object_accessor.hpp */; }; - F60102E81CBBB36500EC01BA /* jsc_object_accessor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F60102E61CBBB36500EC01BA /* jsc_object_accessor.cpp */; }; F60102E91CBCAEC500EC01BA /* platform.mm in Sources */ = {isa = PBXBuildFile; fileRef = 029048381C042A8F00ABDED4 /* platform.mm */; }; F60102EA1CBCAFC300EC01BA /* node_dummy.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F6267BCA1CADC49200AC36B1 /* node_dummy.cpp */; }; F61378791C18EAC5008BFC51 /* js in Resources */ = {isa = PBXBuildFile; fileRef = F61378781C18EAAC008BFC51 /* js */; }; @@ -174,7 +173,6 @@ F60102CF1CBB814A00EC01BA /* node_init.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = node_init.hpp; sourceTree = ""; }; F60102D11CBB865A00EC01BA /* jsc_init.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsc_init.hpp; sourceTree = ""; }; F60102E31CBBB19700EC01BA /* node_object_accessor.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = node_object_accessor.hpp; sourceTree = ""; }; - F60102E61CBBB36500EC01BA /* jsc_object_accessor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jsc_object_accessor.cpp; sourceTree = ""; }; F60102E71CBBB36500EC01BA /* jsc_object_accessor.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = jsc_object_accessor.hpp; sourceTree = ""; }; F60102F71CBDA6D400EC01BA /* js_collection.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = js_collection.hpp; sourceTree = ""; }; F61378781C18EAAC008BFC51 /* js */ = {isa = PBXFileReference; lastKnownFileType = folder; path = js; sourceTree = ""; }; @@ -488,7 +486,6 @@ F620F0531CAF2EF70082977B /* jsc_class.hpp */, 025678951CAB392000FB8501 /* jsc_types.hpp */, F60102E71CBBB36500EC01BA /* jsc_object_accessor.hpp */, - F60102E61CBBB36500EC01BA /* jsc_object_accessor.cpp */, ); name = JSC; path = jsc; @@ -776,7 +773,6 @@ 02F59EC51C88F17D007F774C /* shared_realm.cpp in Sources */, F63FF2CB1C12469E00B3B8E0 /* js_schema.cpp in Sources */, 02F59ECB1C88F190007F774C /* query_builder.cpp in Sources */, - F60102E81CBBB36500EC01BA /* jsc_object_accessor.cpp in Sources */, 02F59EE21C88F2BB007F774C /* realm_coordinator.cpp in Sources */, 02F59EC41C88F17D007F774C /* schema.cpp in Sources */, F63FF2CD1C12469E00B3B8E0 /* rpc.cpp in Sources */, diff --git a/src/jsc/jsc_object_accessor.cpp b/src/jsc/jsc_object_accessor.cpp deleted file mode 100644 index 99c46832..00000000 --- a/src/jsc/jsc_object_accessor.cpp +++ /dev/null @@ -1,90 +0,0 @@ -//////////////////////////////////////////////////////////////////////////// -// -// 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. -// -//////////////////////////////////////////////////////////////////////////// - -#include "jsc_object_accessor.hpp" - -using namespace realm; -using namespace realm::jsc; - -using Accessor = js::NativeAccessor; - -template<> -std::string Accessor::to_binary(JSContextRef ctx, JSValueRef &value) { - static jsc::String s_array_buffer = "ArrayBuffer"; - static jsc::String s_buffer = "buffer"; - static jsc::String s_byte_length = "byteLength"; - static jsc::String s_byte_offset = "byteOffset"; - static jsc::String s_is_view = "isView"; - static jsc::String s_uint8_array = "Uint8Array"; - - JSObjectRef global_object = JSContextGetGlobalObject(ctx); - JSObjectRef array_buffer_constructor = jsc::Object::validated_get_constructor(ctx, global_object, s_array_buffer); - JSObjectRef uint8_array_constructor = jsc::Object::validated_get_constructor(ctx, global_object, s_uint8_array); - JSValueRef uint8_array_arguments[3]; - uint32_t uint8_array_argc = 0; - - // Value should either be an ArrayBuffer or ArrayBufferView (i.e. TypedArray or DataView). - if (JSValueIsInstanceOfConstructor(ctx, value, array_buffer_constructor, nullptr)) { - uint8_array_arguments[0] = value; - uint8_array_argc = 1; - } - else if (JSObjectRef object = JSValueToObject(ctx, value, nullptr)) { - // Check if value is an ArrayBufferView by calling ArrayBuffer.isView(val). - JSValueRef is_view = jsc::Object::call_method(ctx, array_buffer_constructor, s_is_view, 1, &object); - - if (jsc::Value::to_boolean(ctx, is_view)) { - uint8_array_arguments[0] = jsc::Object::validated_get_object(ctx, object, s_buffer); - uint8_array_arguments[1] = jsc::Object::get_property(ctx, object, s_byte_offset); - uint8_array_arguments[2] = jsc::Object::get_property(ctx, object, s_byte_length); - uint8_array_argc = 3; - } - } - - if (!uint8_array_argc) { - throw std::runtime_error("Can only convert ArrayBuffer and TypedArray objects to binary"); - } - - JSObjectRef uint8_array = jsc::Function::construct(ctx, uint8_array_constructor, uint8_array_argc, uint8_array_arguments); - uint32_t byte_count = jsc::Object::validated_get_length(ctx, uint8_array); - std::string bytes(byte_count, 0); - - for (uint32_t i = 0; i < byte_count; i++) { - JSValueRef byteValue = jsc::Object::get_property(ctx, uint8_array, i); - bytes[i] = jsc::Value::to_number(ctx, byteValue); - } - - return bytes; -} - -template<> -JSValueRef Accessor::from_binary(JSContextRef ctx, BinaryData data) { - static jsc::String s_buffer = "buffer"; - static jsc::String s_uint8_array = "Uint8Array"; - - size_t byte_count = data.size(); - JSValueRef byte_count_value = jsc::Value::from_number(ctx, byte_count); - JSObjectRef uint8_array_constructor = jsc::Object::validated_get_constructor(ctx, JSContextGetGlobalObject(ctx), s_uint8_array); - JSObjectRef uint8_array = jsc::Function::construct(ctx, uint8_array_constructor, 1, &byte_count_value); - - for (uint32_t i = 0; i < byte_count; i++) { - JSValueRef num = jsc::Value::from_number(ctx, data[i]); - jsc::Object::set_property(ctx, uint8_array, i, num); - } - - return jsc::Object::validated_get_object(ctx, uint8_array, s_buffer); -} diff --git a/src/jsc/jsc_object_accessor.hpp b/src/jsc/jsc_object_accessor.hpp index b93e6d9e..d210d90a 100644 --- a/src/jsc/jsc_object_accessor.hpp +++ b/src/jsc/jsc_object_accessor.hpp @@ -26,5 +26,74 @@ namespace realm { // Specialize a native accessor class for JSC. template<> class NativeAccessor : public js::NativeAccessor {}; + +namespace js { + +template<> +inline std::string NativeAccessor::to_binary(JSContextRef ctx, JSValueRef &value) { + static jsc::String s_array_buffer = "ArrayBuffer"; + static jsc::String s_buffer = "buffer"; + static jsc::String s_byte_length = "byteLength"; + static jsc::String s_byte_offset = "byteOffset"; + static jsc::String s_is_view = "isView"; + static jsc::String s_uint8_array = "Uint8Array"; + + JSObjectRef global_object = JSContextGetGlobalObject(ctx); + JSObjectRef array_buffer_constructor = jsc::Object::validated_get_constructor(ctx, global_object, s_array_buffer); + JSObjectRef uint8_array_constructor = jsc::Object::validated_get_constructor(ctx, global_object, s_uint8_array); + JSValueRef uint8_array_arguments[3]; + uint32_t uint8_array_argc = 0; + + // Value should either be an ArrayBuffer or ArrayBufferView (i.e. TypedArray or DataView). + if (JSValueIsInstanceOfConstructor(ctx, value, array_buffer_constructor, nullptr)) { + uint8_array_arguments[0] = value; + uint8_array_argc = 1; + } + else if (JSObjectRef object = JSValueToObject(ctx, value, nullptr)) { + // Check if value is an ArrayBufferView by calling ArrayBuffer.isView(val). + JSValueRef is_view = jsc::Object::call_method(ctx, array_buffer_constructor, s_is_view, 1, &object); + + if (jsc::Value::to_boolean(ctx, is_view)) { + uint8_array_arguments[0] = jsc::Object::validated_get_object(ctx, object, s_buffer); + uint8_array_arguments[1] = jsc::Object::get_property(ctx, object, s_byte_offset); + uint8_array_arguments[2] = jsc::Object::get_property(ctx, object, s_byte_length); + uint8_array_argc = 3; + } + } + + if (!uint8_array_argc) { + throw std::runtime_error("Can only convert ArrayBuffer and TypedArray objects to binary"); + } + + JSObjectRef uint8_array = jsc::Function::construct(ctx, uint8_array_constructor, uint8_array_argc, uint8_array_arguments); + uint32_t byte_count = jsc::Object::validated_get_length(ctx, uint8_array); + std::string bytes(byte_count, 0); + + for (uint32_t i = 0; i < byte_count; i++) { + JSValueRef byteValue = jsc::Object::get_property(ctx, uint8_array, i); + bytes[i] = jsc::Value::to_number(ctx, byteValue); + } + + return bytes; +} + +template<> +inline JSValueRef NativeAccessor::from_binary(JSContextRef ctx, BinaryData data) { + static jsc::String s_buffer = "buffer"; + static jsc::String s_uint8_array = "Uint8Array"; + + size_t byte_count = data.size(); + JSValueRef byte_count_value = jsc::Value::from_number(ctx, byte_count); + JSObjectRef uint8_array_constructor = jsc::Object::validated_get_constructor(ctx, JSContextGetGlobalObject(ctx), s_uint8_array); + JSObjectRef uint8_array = jsc::Function::construct(ctx, uint8_array_constructor, 1, &byte_count_value); + + for (uint32_t i = 0; i < byte_count; i++) { + JSValueRef num = jsc::Value::from_number(ctx, data[i]); + jsc::Object::set_property(ctx, uint8_array, i, num); + } + return jsc::Object::validated_get_object(ctx, uint8_array, s_buffer); +} + +} // js } // realm