How do I write a JSI and Codegen based TurboModule? #13985
|
Hi, I have been studying #10909 and the JSI TurboModule stuff in I made a lib using create-react-native-library (#13884 (comment)): npx --yes create-react-native-library@latest --slug rnwturbo --description rnwturbo --author-name "rnwturbo" --author-email rnwturbo@rnwturbo.com --author-url http://example.com --repo-url http://example.com --languages kotlin-objc --type module-new --react-native-version 0.76.0-rc.0 --example test-app rnwturbo
cd rnwturbo
yarn add react-native-windows@0.76.0-preview.1
yarn react-native init-windows --template cpp-lib --overwrite --logging
yarn example windows # twice if you run into #13599Ran react-native-windows-codgen with flag npx react-native-windows-codegen --libraryName "rnwturbo" --file .\src\NativeRnwturbo.ts --outputDirectory .\windows\rnwturbo\codegen-jsi --modulesCxx
Writing windows\rnwturbo\codegen-jsi\.clang-format
Writing windows\rnwturbo\codegen-jsi\rnwturboJSI.h
Writing windows\rnwturbo\codegen-jsi\rnwturboJSI-generated.cppand made a simple implementation. windows\rnwturbo\rnwturbo.{h|cpp}: #include "codegen-jsi/rnwturboJSI.h"
#include <winrt/Microsoft.ReactNative.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <TurboModuleProvider.h>
namespace facebook::react {
class Rnwturbo : react::NativeRnwturboCxxSpecJSI {
public:
Rnwturbo(std::shared_ptr<react::CallInvoker> jsInvoker);
double multiply(jsi::Runtime &rt, double a, double b);
};
struct RnwturboPackageProvider
: winrt::implements<RnwturboPackageProvider, winrt::Microsoft::ReactNative::IReactPackageProvider> {
void CreatePackage(winrt::Microsoft::ReactNative::IReactPackageBuilder const &packageBuilder) noexcept {
winrt::Microsoft::ReactNative::AddTurboModuleProvider<Rnwturbo>(packageBuilder, L"RnwturboCxx");
}
};
}#include "rnwturbo.h"
namespace facebook::react {
Rnwturbo::Rnwturbo(std::shared_ptr<react::CallInvoker> jsInvoker)
: NativeRnwturboCxxSpecJSI(std::move(jsInvoker)) {}
double Rnwturbo::multiply(double a, double b) {
return a * b;
}
}I don't know how to autolink JSI TurboModules, but as a quick hack I changed #include "../../../windows/rnwturbo/rnwturbo.h"
[...]
// RegisterAutolinkedNativeModulePackages(host.PackageProviders());
host.PackageProviders().Append(winrt::make<facebook::react::RnwturboPackageProvider>());Now, everything is set as far as I understand. I also later found the very helpful comments in #13886 confirming my understanding of things. But when running So this is coming from the JSI codegen file that react-native-windows-codegen creates and it's failing to import Meta's JSI file(s). For clarity, the file just looks like normal JSI codegen code: #pragma once
#include <ReactCommon/TurboModule.h>
#include <react/bridging/Bridging.h>
namespace facebook::react {
class JSI_EXPORT NativeRnwturboCxxSpecJSI : public TurboModule {
protected:
NativeRnwturboCxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker);
public:
virtual double multiply(jsi::Runtime &rt, double a, double b) = 0;
};
// etcAny suggestions on how I would fix this? Sidenote: I could not get latest canary 0.0.0-canary.877 or 0.76.0-preview.2 to work. That's why I'm using 0.76.0-preview.1: I've uploaded a repro here: Best |
Replies: 2 comments 2 replies
|
While 0.76 is in preview there have been some packaging gaps, that's why preview.2 didn't work but preview.1 got further. There seems to be a gap with the JSI codegen for cpp/winrt that you're hitting here. So I think this is actually a gap and should be converted into a bug. |
|
0.79.4 node_modules/@react-native-windows/cli/lib-commonjs/commands/codegenWindows/codegenWindows.js.mapconst generators = pkgJson.codegenConfig.windows.generators ?? [\n 'modulesWindows',\n ]so i can config windows.generator: [ "modulesCxx"] to generate JSI glue code |
While 0.76 is in preview there have been some packaging gaps, that's why preview.2 didn't work but preview.1 got further.
There seems to be a gap with the JSI codegen for cpp/winrt that you're hitting here. So I think this is actually a gap and should be converted into a bug.
#13999