From f5d92130895825a7dd74da4e36e45862869bed97 Mon Sep 17 00:00:00 2001 From: Ryan Tremblay Date: Fri, 17 Jul 2020 14:53:49 -0700 Subject: [PATCH 1/2] Put custom JSCRuntime in its own namespace Use deprectated but functional functions for doing character encoding conversions --- .../@babylonjs/react-native/JSCRuntime.cpp | 28 +++++-------------- .../@babylonjs/react-native/JSCRuntime.h | 2 +- .../src/main/cpp/BabylonNativeInterop.cpp | 2 +- .../react-native/ios/BabylonNative.cpp | 8 ++++-- 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/Apps/Playground/node_modules/@babylonjs/react-native/JSCRuntime.cpp b/Apps/Playground/node_modules/@babylonjs/react-native/JSCRuntime.cpp index 75da25cc0..754262c83 100644 --- a/Apps/Playground/node_modules/@babylonjs/react-native/JSCRuntime.cpp +++ b/Apps/Playground/node_modules/@babylonjs/react-native/JSCRuntime.cpp @@ -22,9 +22,10 @@ #include #include #include +#include namespace facebook { -namespace jsc { +namespace jsc2 { namespace detail { class ArgsConverter; @@ -33,8 +34,8 @@ class ArgsConverter; class JSCRuntime; struct Lock { - void lock(const jsc::JSCRuntime &) const {} - void unlock(const jsc::JSCRuntime &) const {} + void lock(const jsc2::JSCRuntime &) const {} + void unlock(const jsc2::JSCRuntime &) const {} }; class JSCRuntime : public jsi::Runtime { @@ -674,25 +675,10 @@ jsi::String JSCRuntime::createStringFromUtf8( const uint8_t *str, size_t length) { - std::vector chars(length); + auto u8 = reinterpret_cast(str); + std::u16string chars = std::wstring_convert, char16_t>{}.from_bytes(u8, u8 + length); - { - const char *from = reinterpret_cast(str); - char16_t *to = reinterpret_cast(chars.data()); - - std::locale::global(std::locale("en_US.UTF-8")); - auto& facet = std::use_facet>(std::locale()); - std::mbstate_t state{}; - const char *from_next; - char16_t *to_next; - auto result = facet.in(state, from, from + length, from_next, to, to + length, to_next); - if (result != std::codecvt_base::ok) { - throw std::invalid_argument{"Invalid unicode string"}; - } - chars.resize(to_next - to); - } - - JSStringRef stringRef = JSStringCreateWithCharacters(chars.data(), chars.size()); + JSStringRef stringRef = JSStringCreateWithCharacters(reinterpret_cast(chars.data()), chars.size()); auto result = createString(stringRef); JSStringRelease(stringRef); return result; diff --git a/Apps/Playground/node_modules/@babylonjs/react-native/JSCRuntime.h b/Apps/Playground/node_modules/@babylonjs/react-native/JSCRuntime.h index b72d21320..78588d6eb 100644 --- a/Apps/Playground/node_modules/@babylonjs/react-native/JSCRuntime.h +++ b/Apps/Playground/node_modules/@babylonjs/react-native/JSCRuntime.h @@ -16,7 +16,7 @@ #include namespace facebook { -namespace jsc { +namespace jsc2 { std::unique_ptr makeJSCRuntime(JSGlobalContextRef ctx); diff --git a/Apps/Playground/node_modules/@babylonjs/react-native/android/src/main/cpp/BabylonNativeInterop.cpp b/Apps/Playground/node_modules/@babylonjs/react-native/android/src/main/cpp/BabylonNativeInterop.cpp index 37495bf31..740aa88bd 100644 --- a/Apps/Playground/node_modules/@babylonjs/react-native/android/src/main/cpp/BabylonNativeInterop.cpp +++ b/Apps/Playground/node_modules/@babylonjs/react-native/android/src/main/cpp/BabylonNativeInterop.cpp @@ -39,7 +39,7 @@ namespace Babylon public: // This class must be constructed from the JavaScript thread Native(facebook::jsi::Runtime* jsiRuntime, ANativeWindow* windowPtr) - : m_jsiRuntime{ facebook::jsc::makeJSCRuntime(facebook::jsc::getJSGlobalContextRefFromJSCRuntime(*jsiRuntime)) } + : m_jsiRuntime{ facebook::jsc2::makeJSCRuntime(facebook::jsc2::getJSGlobalContextRefFromJSCRuntime(*jsiRuntime)) } , m_env{ Napi::Attach(*m_jsiRuntime) } { auto looper_scheduler = std::make_shared(looper_scheduler_t::get_for_current_thread()); diff --git a/Apps/Playground/node_modules/@babylonjs/react-native/ios/BabylonNative.cpp b/Apps/Playground/node_modules/@babylonjs/react-native/ios/BabylonNative.cpp index c34905bb8..1e4363f5c 100644 --- a/Apps/Playground/node_modules/@babylonjs/react-native/ios/BabylonNative.cpp +++ b/Apps/Playground/node_modules/@babylonjs/react-native/ios/BabylonNative.cpp @@ -27,14 +27,16 @@ namespace Babylon { public: Impl(facebook::jsi::Runtime* jsiRuntime) - : jsiRuntime{ facebook::jsc::makeJSCRuntime(facebook::jsc::getJSGlobalContextRefFromJSCRuntime(*jsiRuntime)) } - , env{ Napi::Attach(*jsiRuntime) } + : m_jsiRuntime{ facebook::jsc2::makeJSCRuntime(facebook::jsc2::getJSGlobalContextRefFromJSCRuntime(*jsiRuntime)) } + , env{ Napi::Attach(*m_jsiRuntime) } { } + private: // custom jsi runtime based on custom jsc runtime - std::unique_ptr jsiRuntime; + std::unique_ptr m_jsiRuntime; + public: Napi::Env env; JsRuntime* runtime{}; Plugins::NativeInput* nativeInput{}; From c2e962b7b298a63c280830e5d791a10921f40da8 Mon Sep 17 00:00:00 2001 From: Gary Hsu Date: Fri, 17 Jul 2020 15:27:42 -0700 Subject: [PATCH 2/2] Update BabylonNative submodule --- .../@babylonjs/react-native/submodules/BabylonNative | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Apps/Playground/node_modules/@babylonjs/react-native/submodules/BabylonNative b/Apps/Playground/node_modules/@babylonjs/react-native/submodules/BabylonNative index 7c1c51587..5a359712c 160000 --- a/Apps/Playground/node_modules/@babylonjs/react-native/submodules/BabylonNative +++ b/Apps/Playground/node_modules/@babylonjs/react-native/submodules/BabylonNative @@ -1 +1 @@ -Subproject commit 7c1c51587eac90ed5538626bde2dd2760bf39625 +Subproject commit 5a359712c06c4fbb4554dd45d4c7be19544b270f