Skip to content

Commit

Permalink
Switching UWP react native instance to use JSI (#2485)
Browse files Browse the repository at this point in the history
* Switching UWP SampleApplication to use JSI

* Update ChakraJsiRuntime.h

* Integration ChakraJsiRuntime with desktop integration tests and fixing the logging
  • Loading branch information
mganandraj authored May 17, 2019
1 parent a366955 commit 761e2e7
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 22 deletions.
5 changes: 2 additions & 3 deletions vnext/Chakra/ChakraJsiRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,9 +975,8 @@ std::shared_ptr<jsi::HostObject> ChakraJsiRuntime::getHostObject(const jsi::Obje
}

jsi::Object ChakraJsiRuntime::createProxy(jsi::Object&& target, jsi::Object&& handler) noexcept {
// Note: We are lazy initializing and cachine the constructor.
static jsi::Function proxyConstructor = createProxyConstructor();

// TODO :: Avoid creating the constuctor on each call.
jsi::Function proxyConstructor = createProxyConstructor();
jsi::Value hostObjectProxy = proxyConstructor.call(*this, target, handler);

if (!hostObjectProxy.isObject())
Expand Down
2 changes: 1 addition & 1 deletion vnext/Chakra/ChakraJsiRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace facebook {
namespace jsi {
namespace chakraruntime {

class ByteArrayBuffer : public jsi::Buffer {
class ByteArrayBuffer final : public jsi::Buffer {
public:
size_t size() const override {
return size_;
Expand Down
62 changes: 62 additions & 0 deletions vnext/Desktop.IntegrationTests/ChakraJSIRuntimeHolder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "pch.h"

#include "ChakraJSIRuntimeHolder.h"

#include <Chakra/ChakraJsiRuntimeFactory.h>

using namespace facebook;
using namespace facebook::react;
using namespace facebook::jsi::chakraruntime;

namespace facebook {
namespace react {
namespace test {

std::shared_ptr<facebook::jsi::Runtime> ChakraJSIRuntimeHolder::getRuntime() noexcept {
std::call_once(once_flag_, [this]() { initRuntime(); });

if (!runtime_) std::terminate();

// ChakraJsiRuntime is not thread safe as of now.
if (own_thread_id_ != std::this_thread::get_id()) std::terminate();

return runtime_;
}

void ChakraJSIRuntimeHolder::initRuntime() noexcept {
runtime_ = facebook::jsi::chakraruntime::makeChakraJsiRuntime(std::move(args_));
own_thread_id_ = std::this_thread::get_id();
}

Logger ChakraJSIRuntimeHolder::ChakraRuntimeLoggerFromReactLogger(facebook::react::NativeLoggingHook loggingCallback) noexcept
{
return[loggingCallback = std::move(loggingCallback)](const char* message, LogLevel logLevel) -> void
{
loggingCallback(static_cast<facebook::react::RCTLogLevel>(logLevel), message);
};
}

ChakraJsiRuntimeArgs ChakraJSIRuntimeHolder::RuntimeArgsFromDevSettings(std::shared_ptr<facebook::react::DevSettings> devSettings) noexcept
{
ChakraJsiRuntimeArgs runtimeArgs;

runtimeArgs.debuggerBreakOnNextLine = devSettings->debuggerBreakOnNextLine;
runtimeArgs.debuggerPort = devSettings->debuggerPort;
runtimeArgs.debuggerRuntimeName = devSettings->debuggerRuntimeName;

runtimeArgs.enableDebugging = devSettings->useDirectDebugger;

if (devSettings->loggingCallback) {
runtimeArgs.loggingCallback = ChakraRuntimeLoggerFromReactLogger(devSettings->loggingCallback);
}

runtimeArgs.enableNativePerformanceNow = devSettings->enableNativePerformanceNow;

runtimeArgs.enableJITCompilation = devSettings->useJITCompilation;

runtimeArgs.memoryTracker = devSettings->memoryTracker;

return runtimeArgs;
}

}}}
44 changes: 44 additions & 0 deletions vnext/Desktop.IntegrationTests/ChakraJSIRuntimeHolder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <DevSettings.h>

#include <jsi/ScriptStore.h>
#include <jsi/RuntimeHolder.h>

#include <Chakra/ChakraJsiRuntimeArgs.h>

#include <Logging.h>

namespace facebook {
namespace react {
namespace test {

class ChakraJSIRuntimeHolder : public facebook::jsi::RuntimeHolderLazyInit {
public:
std::shared_ptr<facebook::jsi::Runtime> getRuntime() noexcept override;

ChakraJSIRuntimeHolder(std::shared_ptr<facebook::react::DevSettings> devSettings,
std::shared_ptr<facebook::react::MessageQueueThread> jsQueue,
std::unique_ptr<facebook::jsi::ScriptStore>&& scriptStore,
std::unique_ptr<facebook::jsi::PreparedScriptStore>&& preparedScriptStore) noexcept
: args_(RuntimeArgsFromDevSettings(devSettings)) {
args_.jsQueue = std::move(jsQueue);
args_.scriptStore = std::move(scriptStore);
args_.preparedScriptStore = std::move(preparedScriptStore);
}

private:
facebook::jsi::chakraruntime::ChakraJsiRuntimeArgs RuntimeArgsFromDevSettings(std::shared_ptr<facebook::react::DevSettings> devSettings) noexcept;
facebook::jsi::chakraruntime::Logger ChakraRuntimeLoggerFromReactLogger(facebook::react::NativeLoggingHook loggingCallback) noexcept;

void initRuntime() noexcept;

facebook::jsi::chakraruntime::ChakraJsiRuntimeArgs args_;
std::shared_ptr<facebook::jsi::Runtime> runtime_;

std::once_flag once_flag_;
std::thread::id own_thread_id_;

};

}}}
3 changes: 3 additions & 0 deletions vnext/Desktop.IntegrationTests/DesktopTestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "TestMessageQueueThread.h"
#include "TestModule.h"
#include "TestRootView.h"
#include "ChakraJSIRuntimeHolder.h"

using namespace facebook::react;
using namespace facebook::xplat::module;
Expand Down Expand Up @@ -46,6 +47,8 @@ shared_ptr<ITestInstance> TestRunner::GetInstance(
auto nativeQueue = make_shared<TestMessageQueueThread>();
auto jsQueue = make_shared<TestMessageQueueThread>();

devSettings->jsiRuntimeHolder = std::make_shared<ChakraJSIRuntimeHolder>(devSettings, jsQueue, nullptr, nullptr);

vector<tuple<string, CxxModule::Provider, shared_ptr<MessageQueueThread>>> extraModules
{
make_tuple(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup>
<IncludePath>$(ReactNativeWindowsDir)stubs;$(FollyDir);$(ReactNativeDir)\ReactCommon;$(ReactNativeWindowsDir)ReactWindowsCore;$(ReactNativeWindowsDir)include\ReactWindowsCore;$(ReactNativeWindowsDir)Desktop;$(ReactNativeWindowsDir)IntegrationTests;$(MSBuildProjectDirectory);$(IncludePath)</IncludePath>
<IncludePath>$(ReactNativeWindowsDir);$(ReactNativeWindowsDir)stubs;$(FollyDir);$(ReactNativeDir)\ReactCommon;$(ReactNativeWindowsDir)ReactWindowsCore;$(ReactNativeWindowsDir)include\ReactWindowsCore;$(ReactNativeWindowsDir)Desktop;$(ReactNativeWindowsDir)IntegrationTests;$(MSBuildProjectDirectory);$(IncludePath)</IncludePath>
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup>
Expand Down Expand Up @@ -92,6 +92,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="ChakraJSIRuntimeHolder.cpp" />
<ClCompile Include="HttpResourceIntegrationTests.cpp" />
<ClCompile Include="RNTesterIntegrationTests.cpp" />
<ClCompile Include="DesktopTestInstance.cpp" />
Expand Down Expand Up @@ -126,6 +127,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChakraJSIRuntimeHolder.h" />
<ClInclude Include="DesktopTestInstance.h" />
<ClInclude Include="TestMessageQueueThread.h" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
<ClCompile Include="WebSocketResourcePerformanceTests.cpp">
<Filter>Integration Tests</Filter>
</ClCompile>
<ClCompile Include="ChakraJSIRuntimeHolder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="DesktopTestInstance.h">
Expand All @@ -53,5 +56,8 @@
<ClInclude Include="TestMessageQueueThread.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ChakraJSIRuntimeHolder.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
61 changes: 61 additions & 0 deletions vnext/ReactUWP/Base/ChakraJSIRuntimeHolder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "pch.h"

#include "ChakraJSIRuntimeHolder.h"

#include <Chakra/ChakraJsiRuntimeFactory.h>

using namespace facebook;
using namespace facebook::react;
using namespace facebook::jsi::chakraruntime;

namespace react {
namespace uwp {

std::shared_ptr<facebook::jsi::Runtime> ChakraJSIRuntimeHolder::getRuntime() noexcept {
std::call_once(once_flag_, [this]() { initRuntime(); });

if (!runtime_) std::terminate();

// ChakraJsiRuntime is not thread safe as of now.
if (own_thread_id_ != std::this_thread::get_id()) std::terminate();

return runtime_;
}

void ChakraJSIRuntimeHolder::initRuntime() noexcept {
runtime_ = facebook::jsi::chakraruntime::makeChakraJsiRuntime(std::move(args_));
own_thread_id_ = std::this_thread::get_id();
}

Logger ChakraJSIRuntimeHolder::ChakraRuntimeLoggerFromReactLogger(facebook::react::NativeLoggingHook loggingCallback) noexcept
{
return[loggingCallback = std::move(loggingCallback)](const char* message, LogLevel logLevel) -> void
{
loggingCallback(static_cast<facebook::react::RCTLogLevel>(logLevel), message);
};
}

ChakraJsiRuntimeArgs ChakraJSIRuntimeHolder::RuntimeArgsFromDevSettings(std::shared_ptr<facebook::react::DevSettings> devSettings) noexcept
{
ChakraJsiRuntimeArgs runtimeArgs;

runtimeArgs.debuggerBreakOnNextLine = devSettings->debuggerBreakOnNextLine;
runtimeArgs.debuggerPort = devSettings->debuggerPort;
runtimeArgs.debuggerRuntimeName = devSettings->debuggerRuntimeName;

runtimeArgs.enableDebugging = devSettings->useDirectDebugger;

if (devSettings->loggingCallback) {
runtimeArgs.loggingCallback = ChakraRuntimeLoggerFromReactLogger(devSettings->loggingCallback);
}

runtimeArgs.enableNativePerformanceNow = devSettings->enableNativePerformanceNow;

runtimeArgs.enableJITCompilation = devSettings->useJITCompilation;

runtimeArgs.memoryTracker = devSettings->memoryTracker;

return runtimeArgs;
}

}}
43 changes: 43 additions & 0 deletions vnext/ReactUWP/Base/ChakraJSIRuntimeHolder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <DevSettings.h>

#include <jsi/ScriptStore.h>
#include <jsi/RuntimeHolder.h>

#include <Chakra/ChakraJsiRuntimeArgs.h>

#include <Logging.h>

namespace react {
namespace uwp {

class ChakraJSIRuntimeHolder : public facebook::jsi::RuntimeHolderLazyInit {
public:
std::shared_ptr<facebook::jsi::Runtime> getRuntime() noexcept override;

ChakraJSIRuntimeHolder(std::shared_ptr<facebook::react::DevSettings> devSettings,
std::shared_ptr<facebook::react::MessageQueueThread> jsQueue,
std::unique_ptr<facebook::jsi::ScriptStore>&& scriptStore,
std::unique_ptr<facebook::jsi::PreparedScriptStore>&& preparedScriptStore) noexcept
: args_(RuntimeArgsFromDevSettings(devSettings)) {
args_.jsQueue = std::move(jsQueue);
args_.scriptStore = std::move(scriptStore);
args_.preparedScriptStore = std::move(preparedScriptStore);
}

private:
facebook::jsi::chakraruntime::ChakraJsiRuntimeArgs RuntimeArgsFromDevSettings(std::shared_ptr<facebook::react::DevSettings> devSettings) noexcept;
facebook::jsi::chakraruntime::Logger ChakraRuntimeLoggerFromReactLogger(facebook::react::NativeLoggingHook loggingCallback) noexcept;

void initRuntime() noexcept;

facebook::jsi::chakraruntime::ChakraJsiRuntimeArgs args_;
std::shared_ptr<facebook::jsi::Runtime> runtime_;

std::once_flag once_flag_;
std::thread::id own_thread_id_;

};

}}
7 changes: 6 additions & 1 deletion vnext/ReactUWP/Base/UwpReactInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
#include <cxxreact/CxxNativeModule.h>
#include <cxxreact/Instance.h>

#include "ChakraJSIRuntimeHolder.h"

#include <tuple>

namespace {
Expand Down Expand Up @@ -276,14 +278,17 @@ void UwpReactInstance::Start(const std::shared_ptr<IReactInstance>& spThis, cons
cxxModules.insert(std::end(cxxModules), std::begin(customCxxModules), std::end(customCxxModules));
}

std::shared_ptr<facebook::react::CxxMessageQueue> jsQueue = CreateAndStartJSQueueThread();
devSettings->jsiRuntimeHolder = std::make_shared<ChakraJSIRuntimeHolder>(devSettings, jsQueue, nullptr, nullptr);

try
{
// Create the react instance
m_instanceWrapper = facebook::react::CreateReactInstance(
std::string(), // bundleRootPath
std::move(cxxModules),
m_uiManager,
CreateAndStartJSQueueThread(),
jsQueue,
m_defaultNativeThread,
std::move(devSettings));
}
Expand Down
2 changes: 2 additions & 0 deletions vnext/ReactUWP/ReactUWP.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<ClInclude Include="ABI\HStringHelper.h" />
<ClInclude Include="ABI\Instance_rt.h" />
<ClInclude Include="ABI\ReactControl_rt.h" />
<ClInclude Include="Base\ChakraJSIRuntimeHolder.h" />
<ClInclude Include="Base\UwpReactInstance.h" />
<ClInclude Include="Executors\WebSocketJSExecutorUwp.h" />
<ClInclude Include="Modules\AppStateModuleUwp.h" />
Expand Down Expand Up @@ -225,6 +226,7 @@
<ClCompile Include="ABI\ReactControl_rt.cpp" />
<ClCompile Include="..\Shared\InstanceManager.cpp" />
<ClCompile Include="..\Shared\OInstance.cpp" />
<ClCompile Include="Base\ChakraJSIRuntimeHolder.cpp" />
<ClCompile Include="Base\InstanceFactory.cpp" />
<ClCompile Include="Base\UwpReactInstance.cpp" />
<ClCompile Include="CxxReactUWP\JSBigString.cpp" />
Expand Down
8 changes: 8 additions & 0 deletions vnext/ReactUWP/ReactUWP.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<ClCompile Include="Base\InstanceFactory.cpp">
<Filter>Base</Filter>
</ClCompile>
<ClCompile Include="Base\ChakraJSIRuntimeHolder.cpp">
<Filter>Base</Filter>
</ClCompile>
<ClCompile Include="Utils\LocalBundleReader.cpp">
<Filter>Utils</Filter>
</ClCompile>
Expand Down Expand Up @@ -206,6 +209,7 @@
<ClCompile Include="Views\FlyoutViewManager.cpp">
<Filter>Views</Filter>
</ClCompile>

<ClCompile Include="Views\Impl\ScrollViewUWPImplementation.cpp">
<Filter>Views\Impl</Filter>
</ClCompile>
Expand Down Expand Up @@ -268,6 +272,9 @@
<ClInclude Include="Base\UwpReactInstance.h">
<Filter>Base</Filter>
</ClInclude>
<ClInclude Include="Base\ChakraJSIRuntimeHolder.h">
<Filter>Base</Filter>
</ClInclude>
<ClInclude Include="..\include\ReactUWP\XamlView.h">
<Filter>include</Filter>
</ClInclude>
Expand Down Expand Up @@ -418,6 +425,7 @@
<ClInclude Include="Views\FlyoutViewManager.h">
<Filter>Views</Filter>
</ClInclude>

<ClInclude Include="Views\Impl\ScrollViewUWPImplementation.h">
<Filter>Views\Impl</Filter>
</ClInclude>
Expand Down
Loading

0 comments on commit 761e2e7

Please sign in to comment.