diff --git a/.gitattributes b/.gitattributes index 07af6686ff4..f4b09cabd49 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,6 +12,7 @@ # Spec Codegen uses LF *Spec.g.h eol=lf /vnext/codegen/** eol=lf +/vnext/Microsoft.ReactNative.IntegrationTests/codegen/** eol=lf /packages/sample-apps/codegen/** eol=lf # Force Visual Studio project files (mostly XML) to CRLF diff --git a/change/@react-native-windows-cli-fa61a3c9-9d92-4acb-827e-63b92770d0f6.json b/change/@react-native-windows-cli-fa61a3c9-9d92-4acb-827e-63b92770d0f6.json new file mode 100644 index 00000000000..b5f1cfbd6ac --- /dev/null +++ b/change/@react-native-windows-cli-fa61a3c9-9d92-4acb-827e-63b92770d0f6.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Enable ability to codegen JSI C++ TurboModule specs", + "packageName": "@react-native-windows/cli", + "email": "30809111+acoates-ms@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/change/@react-native-windows-codegen-cf208af5-cab0-4f53-978d-b7f54c60df9e.json b/change/@react-native-windows-codegen-cf208af5-cab0-4f53-978d-b7f54c60df9e.json new file mode 100644 index 00000000000..8fdedc1746d --- /dev/null +++ b/change/@react-native-windows-codegen-cf208af5-cab0-4f53-978d-b7f54c60df9e.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Enable ability to codegen JSI C++ TurboModule specs", + "packageName": "@react-native-windows/codegen", + "email": "30809111+acoates-ms@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/change/react-native-windows-714c935b-779f-4733-8803-4c3ac23aedfe.json b/change/react-native-windows-714c935b-779f-4733-8803-4c3ac23aedfe.json new file mode 100644 index 00000000000..8ce48d6bb59 --- /dev/null +++ b/change/react-native-windows-714c935b-779f-4733-8803-4c3ac23aedfe.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Update codegen version", + "packageName": "react-native-windows", + "email": "30809111+acoates-ms@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/packages/@react-native-windows/cli/src/codegen.ts b/packages/@react-native-windows/cli/src/codegen.ts index 074b26e463e..880c838dc23 100644 --- a/packages/@react-native-windows/cli/src/codegen.ts +++ b/packages/@react-native-windows/cli/src/codegen.ts @@ -103,6 +103,10 @@ export class CodeGenWindows { ? path.join(this.root, pkgJson.codegenConfig.jsSrcsDir) : this.root; + const generators = pkgJson.codegenConfig.windows.generators ?? [ + 'modulesWindows', + ]; + const jsRootPathRelative = path.relative(process.cwd(), jsRootDir); const options = { files: [ @@ -110,11 +114,14 @@ export class CodeGenWindows { jsRootPathRelative ? '/' : '' }**/*Native*.[jt]s`, ], - namespace: projectNamespace, libraryName: projectName, - outdir: path.join(this.root, 'codegen'), - methodonly: false, - ts: false, + methodOnly: false, + modulesCxx: generators.indexOf('modulesCxx') !== -1, + modulesTypeScriptTypes: + generators.indexOf('modulesTypeScriptTypes') !== -1, + modulesWindows: generators.indexOf('modulesWindows') !== -1, + namespace: projectNamespace, + outputDirectory: path.join(this.root, 'codegen'), test: !!this.options.check, }; diff --git a/packages/@react-native-windows/codegen/src/Cli.ts b/packages/@react-native-windows/codegen/src/Cli.ts index f582c3526bb..377c8e61371 100644 --- a/packages/@react-native-windows/codegen/src/Cli.ts +++ b/packages/@react-native-windows/codegen/src/Cli.ts @@ -18,17 +18,27 @@ const argv = yargs.options({ array: true, describe: 'glob patterns for files which contains specs', }, - ts: { + modulesTypeScriptTypes: { type: 'boolean', describe: 'generate turbo module definition files in TypeScript', default: false, }, - methodonly: { + modulesCxx: { + type: 'boolean', + describe: 'generate C++ JSI turbo module spec files', + default: false, + }, + modulesWindows: { + type: 'boolean', + describe: 'generate turbo module spec files for REACT_MODULE', + default: false, + }, + methodOnly: { type: 'boolean', describe: 'generate only method metadata in C++ turbo module spec', default: false, }, - outdir: { + outputDirectory: { type: 'string', describe: 'output directory', default: 'codegen', diff --git a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts index f49cbc95d5a..e4baf2582cb 100644 --- a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts +++ b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts @@ -44,11 +44,11 @@ struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec { `; export function createNM2Generator({ + methodOnly, namespace, - methodonly, }: { + methodOnly: boolean; namespace: string; - methodonly: boolean; }) { return ( _libraryName: string, @@ -85,7 +85,7 @@ ${methods[0]} // prepare constants const constants = generateValidateConstants(nativeModule, aliases); - if (constants !== undefined && !methodonly) { + if (constants !== undefined && !methodOnly) { tuples = ` static constexpr auto constants = std::tuple{ ${constants[0]} diff --git a/packages/@react-native-windows/codegen/src/index.ts b/packages/@react-native-windows/codegen/src/index.ts index 7199493cdd7..17c4ddfd771 100644 --- a/packages/@react-native-windows/codegen/src/index.ts +++ b/packages/@react-native-windows/codegen/src/index.ts @@ -33,12 +33,14 @@ const schemaValidator = require(path.resolve( interface Options { libraryName: string; - schema: SchemaType; - outputDirectory: string; + methodOnly: boolean; + modulesCxx: boolean; moduleSpecName: string; + modulesTypeScriptTypes: boolean; + modulesWindows: boolean; namespace: string; - methodonly: boolean; - ts: boolean; + outputDirectory: string; + schema: SchemaType; } interface Config { @@ -179,12 +181,14 @@ export function combineSchemas(files: string[]): SchemaType { export function generate( { libraryName, - schema, - outputDirectory, + methodOnly, + modulesCxx, moduleSpecName, + modulesTypeScriptTypes, + modulesWindows, namespace, - methodonly, - ts, + outputDirectory, + schema, }: Options, {/*generators,*/ test}: Config, ): boolean { @@ -204,10 +208,18 @@ export function generate( ); const generateNM2 = createNM2Generator({ + methodOnly, namespace, - methodonly, }); + const generateJsiModuleH = require(path.resolve( + rncodegenPath, + 'lib/generators/modules/GenerateModuleH', + )).generate; + const generateJsiModuleCpp = require(path.resolve( + rncodegenPath, + 'lib/generators/modules/GenerateModuleCpp', + )).generate; const generatorPropsH = require(path.resolve( rncodegenPath, 'lib/generators/components/GeneratePropsH', @@ -237,33 +249,43 @@ export function generate( 'lib/generators/components/GenerateEventEmitterCpp', )).generate; - normalizeFileMap( - generateNM2(libraryName, schema, moduleSpecName), - outputDirectory, - generatedFiles, - ); + const moduleGenerators = []; - if (ts) { - normalizeFileMap( - generateTypeScript(libraryName, schema, moduleSpecName), - outputDirectory, - generatedFiles, - ); + if (modulesWindows) { + moduleGenerators.push(generateNM2); } + if (modulesCxx) { + moduleGenerators.push(generateJsiModuleH); + moduleGenerators.push(generateJsiModuleCpp); + } + + if (modulesTypeScriptTypes) { + moduleGenerators.push(generateTypeScript); + } + + moduleGenerators.forEach(generator => { + const generated: Map = generator( + libraryName, + schema, + moduleSpecName, + ); + normalizeFileMap(generated, outputDirectory, generatedFiles); + }); + if ( Object.keys(schema.modules).some( moduleName => schema.modules[moduleName].type === 'Component', ) ) { const componentGenerators = [ - generatorPropsH, - generatorPropsCPP, - generatorShadowNodeH, - generatorShadowNodeCPP, generatorComponentDescriptorH, - generatorEventEmitterH, generatorEventEmitterCPP, + generatorEventEmitterH, + generatorPropsCPP, + generatorPropsH, + generatorShadowNodeCPP, + generatorShadowNodeH, ]; componentGenerators.forEach(generator => { @@ -287,10 +309,12 @@ export type CodeGenOptions = { file?: string; files?: string[]; libraryName: string; - outdir: string; + methodOnly: boolean; + modulesCxx: boolean; + modulesTypeScriptTypes: boolean; + modulesWindows: boolean; namespace: string; - methodonly: boolean; - ts: boolean; + outputDirectory: string; test: boolean; }; @@ -304,17 +328,25 @@ export function runCodeGen(options: CodeGenOptions): boolean { const libraryName = options.libraryName; const moduleSpecName = 'moduleSpecName'; - const outputDirectory = options.outdir; - const {namespace, methodonly, ts} = options; + const { + methodOnly, + modulesCxx, + modulesTypeScriptTypes, + modulesWindows, + namespace, + outputDirectory, + } = options; return generate( { libraryName, - schema, - outputDirectory, + methodOnly, + modulesCxx, moduleSpecName, + modulesTypeScriptTypes, + modulesWindows, namespace, - methodonly, - ts, + outputDirectory, + schema, }, {generators: [], test: options.test}, ); diff --git a/packages/sample-apps/codegen/NativeMyJsiModuleSpec.g.h b/packages/sample-apps/codegen/NativeMyJsiModuleSpec.g.h new file mode 100644 index 00000000000..7ab0ac102e7 --- /dev/null +++ b/packages/sample-apps/codegen/NativeMyJsiModuleSpec.g.h @@ -0,0 +1,101 @@ + +/* + * This file is auto-generated from a NativeModule spec file in js. + * + * This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules + * in a way that also verifies at compile time that the native module matches the interface required + * by the TurboModule JS spec. + */ +#pragma once + +#include "NativeModules.h" +#include + +namespace SampleLibraryCodegen { + +REACT_STRUCT(MyJsiModuleSpec_Constants) +struct MyJsiModuleSpec_Constants { + REACT_FIELD(const1) + bool const1; + REACT_FIELD(const2) + double const2; + REACT_FIELD(const3) + std::string const3; +}; + +struct MyJsiModuleSpec : winrt::Microsoft::ReactNative::TurboModuleSpec { + static constexpr auto constants = std::tuple{ + TypedConstant{0}, + }; + static constexpr auto methods = std::tuple{ + Method{0, L"voidFunc"}, + SyncMethod{1, L"getBool"}, + SyncMethod{2, L"getNumber"}, + SyncMethod{3, L"getString"}, + SyncMethod<::React::JSValueArray(::React::JSValueArray) noexcept>{4, L"getArray"}, + SyncMethod<::React::JSValue(::React::JSValue) noexcept>{5, L"getObject"}, + SyncMethod<::React::JSValue(double, std::string, ::React::JSValue) noexcept>{6, L"getValue"}, + Method) noexcept>{7, L"getValueWithCallback"}, + Method) noexcept>{8, L"getValueWithPromise"}, + }; + + template + static constexpr void ValidateModule() noexcept { + constexpr auto constantCheckResults = CheckConstants(); + constexpr auto methodCheckResults = CheckMethods(); + + REACT_SHOW_CONSTANT_SPEC_ERRORS( + 0, + "MyJsiModuleSpec_Constants", + " REACT_GET_CONSTANTS(GetConstants) MyJsiModuleSpec_Constants GetConstants() noexcept {/*implementation*/}\n" + " REACT_GET_CONSTANTS(GetConstants) static MyJsiModuleSpec_Constants GetConstants() noexcept {/*implementation*/}\n"); + + REACT_SHOW_METHOD_SPEC_ERRORS( + 0, + "voidFunc", + " REACT_METHOD(voidFunc) void voidFunc() noexcept { /* implementation */ }\n" + " REACT_METHOD(voidFunc) static void voidFunc() noexcept { /* implementation */ }\n"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 1, + "getBool", + " REACT_SYNC_METHOD(getBool) bool getBool(bool arg) noexcept { /* implementation */ }\n" + " REACT_SYNC_METHOD(getBool) static bool getBool(bool arg) noexcept { /* implementation */ }\n"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 2, + "getNumber", + " REACT_SYNC_METHOD(getNumber) double getNumber(double arg) noexcept { /* implementation */ }\n" + " REACT_SYNC_METHOD(getNumber) static double getNumber(double arg) noexcept { /* implementation */ }\n"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 3, + "getString", + " REACT_SYNC_METHOD(getString) std::string getString(std::string arg) noexcept { /* implementation */ }\n" + " REACT_SYNC_METHOD(getString) static std::string getString(std::string arg) noexcept { /* implementation */ }\n"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 4, + "getArray", + " REACT_SYNC_METHOD(getArray) ::React::JSValueArray getArray(::React::JSValueArray && arg) noexcept { /* implementation */ }\n" + " REACT_SYNC_METHOD(getArray) static ::React::JSValueArray getArray(::React::JSValueArray && arg) noexcept { /* implementation */ }\n"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 5, + "getObject", + " REACT_SYNC_METHOD(getObject) ::React::JSValue getObject(::React::JSValue && arg) noexcept { /* implementation */ }\n" + " REACT_SYNC_METHOD(getObject) static ::React::JSValue getObject(::React::JSValue && arg) noexcept { /* implementation */ }\n"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 6, + "getValue", + " REACT_SYNC_METHOD(getValue) ::React::JSValue getValue(double x, std::string y, ::React::JSValue && z) noexcept { /* implementation */ }\n" + " REACT_SYNC_METHOD(getValue) static ::React::JSValue getValue(double x, std::string y, ::React::JSValue && z) noexcept { /* implementation */ }\n"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 7, + "getValueWithCallback", + " REACT_METHOD(getValueWithCallback) void getValueWithCallback(std::function const & callback) noexcept { /* implementation */ }\n" + " REACT_METHOD(getValueWithCallback) static void getValueWithCallback(std::function const & callback) noexcept { /* implementation */ }\n"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 8, + "getValueWithPromise", + " REACT_METHOD(getValueWithPromise) void getValueWithPromise(bool error, ::React::ReactPromise<::React::JSValue> &&result) noexcept { /* implementation */ }\n" + " REACT_METHOD(getValueWithPromise) static void getValueWithPromise(bool error, ::React::ReactPromise<::React::JSValue> &&result) noexcept { /* implementation */ }\n"); + } +}; + +} // namespace SampleLibraryCodegen diff --git a/packages/sample-apps/codegen/SampleAppJSI-generated.cpp b/packages/sample-apps/codegen/SampleAppJSI-generated.cpp new file mode 100644 index 00000000000..28d74229155 --- /dev/null +++ b/packages/sample-apps/codegen/SampleAppJSI-generated.cpp @@ -0,0 +1,110 @@ +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateModuleH.js + */ + +#include "SampleAppJSI.h" + +namespace facebook { +namespace react { + +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getConstants(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getConstants(rt); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_voidFunc(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + static_cast(&turboModule)->voidFunc(rt); + return jsi::Value::undefined(); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getBool(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getBool(rt, args[0].asBool()); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getNumber(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getNumber(rt, args[0].asNumber()); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getString(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getString(rt, args[0].asString(rt)); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getArray(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getArray(rt, args[0].asObject(rt).asArray(rt)); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getObject(rt, args[0].asObject(rt)); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getValue(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getValue(rt, args[0].asNumber(), args[1].asString(rt), args[2].asObject(rt)); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getValueWithCallback(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + static_cast(&turboModule)->getValueWithCallback(rt, args[0].asObject(rt).asFunction(rt)); + return jsi::Value::undefined(); +} +static jsi::Value __hostFunction_NativeMyJsiModuleCxxSpecJSI_getValueWithPromise(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getValueWithPromise(rt, args[0].asBool()); +} + +NativeMyJsiModuleCxxSpecJSI::NativeMyJsiModuleCxxSpecJSI(std::shared_ptr jsInvoker) + : TurboModule("MyJsiModule", jsInvoker) { + methodMap_["getConstants"] = MethodMetadata {0, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getConstants}; + methodMap_["voidFunc"] = MethodMetadata {0, __hostFunction_NativeMyJsiModuleCxxSpecJSI_voidFunc}; + methodMap_["getBool"] = MethodMetadata {1, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getBool}; + methodMap_["getNumber"] = MethodMetadata {1, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getNumber}; + methodMap_["getString"] = MethodMetadata {1, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getString}; + methodMap_["getArray"] = MethodMetadata {1, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getArray}; + methodMap_["getObject"] = MethodMetadata {1, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getObject}; + methodMap_["getValue"] = MethodMetadata {3, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getValue}; + methodMap_["getValueWithCallback"] = MethodMetadata {1, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getValueWithCallback}; + methodMap_["getValueWithPromise"] = MethodMetadata {1, __hostFunction_NativeMyJsiModuleCxxSpecJSI_getValueWithPromise}; +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getConstants(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getConstants(rt); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_voidFunc(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + static_cast(&turboModule)->voidFunc(rt); + return jsi::Value::undefined(); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getBool(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getBool(rt, args[0].asBool()); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getNumber(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getNumber(rt, args[0].asNumber()); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getString(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getString(rt, args[0].asString(rt)); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getArray(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getArray(rt, args[0].asObject(rt).asArray(rt)); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getObject(rt, args[0].asObject(rt)); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getValue(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getValue(rt, args[0].asNumber(), args[1].asString(rt), args[2].asObject(rt)); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getValueWithCallback(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + static_cast(&turboModule)->getValueWithCallback(rt, args[0].asObject(rt).asFunction(rt)); + return jsi::Value::undefined(); +} +static jsi::Value __hostFunction_NativeMyModuleCxxSpecJSI_getValueWithPromise(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getValueWithPromise(rt, args[0].asBool()); +} + +NativeMyModuleCxxSpecJSI::NativeMyModuleCxxSpecJSI(std::shared_ptr jsInvoker) + : TurboModule("MyModule", jsInvoker) { + methodMap_["getConstants"] = MethodMetadata {0, __hostFunction_NativeMyModuleCxxSpecJSI_getConstants}; + methodMap_["voidFunc"] = MethodMetadata {0, __hostFunction_NativeMyModuleCxxSpecJSI_voidFunc}; + methodMap_["getBool"] = MethodMetadata {1, __hostFunction_NativeMyModuleCxxSpecJSI_getBool}; + methodMap_["getNumber"] = MethodMetadata {1, __hostFunction_NativeMyModuleCxxSpecJSI_getNumber}; + methodMap_["getString"] = MethodMetadata {1, __hostFunction_NativeMyModuleCxxSpecJSI_getString}; + methodMap_["getArray"] = MethodMetadata {1, __hostFunction_NativeMyModuleCxxSpecJSI_getArray}; + methodMap_["getObject"] = MethodMetadata {1, __hostFunction_NativeMyModuleCxxSpecJSI_getObject}; + methodMap_["getValue"] = MethodMetadata {3, __hostFunction_NativeMyModuleCxxSpecJSI_getValue}; + methodMap_["getValueWithCallback"] = MethodMetadata {1, __hostFunction_NativeMyModuleCxxSpecJSI_getValueWithCallback}; + methodMap_["getValueWithPromise"] = MethodMetadata {1, __hostFunction_NativeMyModuleCxxSpecJSI_getValueWithPromise}; +} + + +} // namespace react +} // namespace facebook diff --git a/packages/sample-apps/codegen/SampleAppJSI.h b/packages/sample-apps/codegen/SampleAppJSI.h new file mode 100644 index 00000000000..cf55010eb42 --- /dev/null +++ b/packages/sample-apps/codegen/SampleAppJSI.h @@ -0,0 +1,267 @@ +/** + * This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). + * + * Do not edit this file as changes may cause incorrect behavior and will be lost + * once the code is regenerated. + * + * @generated by codegen project: GenerateModuleH.js + */ + +#pragma once + +#include +#include + +namespace facebook { +namespace react { + +class JSI_EXPORT NativeMyJsiModuleCxxSpecJSI : public TurboModule { +protected: + NativeMyJsiModuleCxxSpecJSI(std::shared_ptr jsInvoker); + +public: + virtual jsi::Object getConstants(jsi::Runtime &rt) = 0; + virtual void voidFunc(jsi::Runtime &rt) = 0; + virtual bool getBool(jsi::Runtime &rt, bool arg) = 0; + virtual double getNumber(jsi::Runtime &rt, double arg) = 0; + virtual jsi::String getString(jsi::Runtime &rt, jsi::String arg) = 0; + virtual jsi::Array getArray(jsi::Runtime &rt, jsi::Array arg) = 0; + virtual jsi::Object getObject(jsi::Runtime &rt, jsi::Object arg) = 0; + virtual jsi::Object getValue(jsi::Runtime &rt, double x, jsi::String y, jsi::Object z) = 0; + virtual void getValueWithCallback(jsi::Runtime &rt, jsi::Function callback) = 0; + virtual jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) = 0; + +}; + +template +class JSI_EXPORT NativeMyJsiModuleCxxSpec : public TurboModule { +public: + jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propName) override { + return delegate_.get(rt, propName); + } + +protected: + NativeMyJsiModuleCxxSpec(std::shared_ptr jsInvoker) + : TurboModule("MyJsiModule", jsInvoker), + delegate_(static_cast(this), jsInvoker) {} + +private: + class Delegate : public NativeMyJsiModuleCxxSpecJSI { + public: + Delegate(T *instance, std::shared_ptr jsInvoker) : + NativeMyJsiModuleCxxSpecJSI(std::move(jsInvoker)), instance_(instance) {} + + jsi::Object getConstants(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::getConstants) == 1, + "Expected getConstants(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::getConstants, jsInvoker_, instance_); + } + void voidFunc(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::voidFunc) == 1, + "Expected voidFunc(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::voidFunc, jsInvoker_, instance_); + } + bool getBool(jsi::Runtime &rt, bool arg) override { + static_assert( + bridging::getParameterCount(&T::getBool) == 2, + "Expected getBool(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getBool, jsInvoker_, instance_, std::move(arg)); + } + double getNumber(jsi::Runtime &rt, double arg) override { + static_assert( + bridging::getParameterCount(&T::getNumber) == 2, + "Expected getNumber(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getNumber, jsInvoker_, instance_, std::move(arg)); + } + jsi::String getString(jsi::Runtime &rt, jsi::String arg) override { + static_assert( + bridging::getParameterCount(&T::getString) == 2, + "Expected getString(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getString, jsInvoker_, instance_, std::move(arg)); + } + jsi::Array getArray(jsi::Runtime &rt, jsi::Array arg) override { + static_assert( + bridging::getParameterCount(&T::getArray) == 2, + "Expected getArray(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getArray, jsInvoker_, instance_, std::move(arg)); + } + jsi::Object getObject(jsi::Runtime &rt, jsi::Object arg) override { + static_assert( + bridging::getParameterCount(&T::getObject) == 2, + "Expected getObject(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getObject, jsInvoker_, instance_, std::move(arg)); + } + jsi::Object getValue(jsi::Runtime &rt, double x, jsi::String y, jsi::Object z) override { + static_assert( + bridging::getParameterCount(&T::getValue) == 4, + "Expected getValue(...) to have 4 parameters"); + + return bridging::callFromJs( + rt, &T::getValue, jsInvoker_, instance_, std::move(x), std::move(y), std::move(z)); + } + void getValueWithCallback(jsi::Runtime &rt, jsi::Function callback) override { + static_assert( + bridging::getParameterCount(&T::getValueWithCallback) == 2, + "Expected getValueWithCallback(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getValueWithCallback, jsInvoker_, instance_, std::move(callback)); + } + jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) override { + static_assert( + bridging::getParameterCount(&T::getValueWithPromise) == 2, + "Expected getValueWithPromise(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getValueWithPromise, jsInvoker_, instance_, std::move(error)); + } + + private: + T *instance_; + }; + + Delegate delegate_; +}; + +class JSI_EXPORT NativeMyModuleCxxSpecJSI : public TurboModule { +protected: + NativeMyModuleCxxSpecJSI(std::shared_ptr jsInvoker); + +public: + virtual jsi::Object getConstants(jsi::Runtime &rt) = 0; + virtual void voidFunc(jsi::Runtime &rt) = 0; + virtual bool getBool(jsi::Runtime &rt, bool arg) = 0; + virtual double getNumber(jsi::Runtime &rt, double arg) = 0; + virtual jsi::String getString(jsi::Runtime &rt, jsi::String arg) = 0; + virtual jsi::Array getArray(jsi::Runtime &rt, jsi::Array arg) = 0; + virtual jsi::Object getObject(jsi::Runtime &rt, jsi::Object arg) = 0; + virtual jsi::Object getValue(jsi::Runtime &rt, double x, jsi::String y, jsi::Object z) = 0; + virtual void getValueWithCallback(jsi::Runtime &rt, jsi::Function callback) = 0; + virtual jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) = 0; + +}; + +template +class JSI_EXPORT NativeMyModuleCxxSpec : public TurboModule { +public: + jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propName) override { + return delegate_.get(rt, propName); + } + +protected: + NativeMyModuleCxxSpec(std::shared_ptr jsInvoker) + : TurboModule("MyModule", jsInvoker), + delegate_(static_cast(this), jsInvoker) {} + +private: + class Delegate : public NativeMyModuleCxxSpecJSI { + public: + Delegate(T *instance, std::shared_ptr jsInvoker) : + NativeMyModuleCxxSpecJSI(std::move(jsInvoker)), instance_(instance) {} + + jsi::Object getConstants(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::getConstants) == 1, + "Expected getConstants(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::getConstants, jsInvoker_, instance_); + } + void voidFunc(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::voidFunc) == 1, + "Expected voidFunc(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::voidFunc, jsInvoker_, instance_); + } + bool getBool(jsi::Runtime &rt, bool arg) override { + static_assert( + bridging::getParameterCount(&T::getBool) == 2, + "Expected getBool(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getBool, jsInvoker_, instance_, std::move(arg)); + } + double getNumber(jsi::Runtime &rt, double arg) override { + static_assert( + bridging::getParameterCount(&T::getNumber) == 2, + "Expected getNumber(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getNumber, jsInvoker_, instance_, std::move(arg)); + } + jsi::String getString(jsi::Runtime &rt, jsi::String arg) override { + static_assert( + bridging::getParameterCount(&T::getString) == 2, + "Expected getString(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getString, jsInvoker_, instance_, std::move(arg)); + } + jsi::Array getArray(jsi::Runtime &rt, jsi::Array arg) override { + static_assert( + bridging::getParameterCount(&T::getArray) == 2, + "Expected getArray(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getArray, jsInvoker_, instance_, std::move(arg)); + } + jsi::Object getObject(jsi::Runtime &rt, jsi::Object arg) override { + static_assert( + bridging::getParameterCount(&T::getObject) == 2, + "Expected getObject(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getObject, jsInvoker_, instance_, std::move(arg)); + } + jsi::Object getValue(jsi::Runtime &rt, double x, jsi::String y, jsi::Object z) override { + static_assert( + bridging::getParameterCount(&T::getValue) == 4, + "Expected getValue(...) to have 4 parameters"); + + return bridging::callFromJs( + rt, &T::getValue, jsInvoker_, instance_, std::move(x), std::move(y), std::move(z)); + } + void getValueWithCallback(jsi::Runtime &rt, jsi::Function callback) override { + static_assert( + bridging::getParameterCount(&T::getValueWithCallback) == 2, + "Expected getValueWithCallback(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getValueWithCallback, jsInvoker_, instance_, std::move(callback)); + } + jsi::Value getValueWithPromise(jsi::Runtime &rt, bool error) override { + static_assert( + bridging::getParameterCount(&T::getValueWithPromise) == 2, + "Expected getValueWithPromise(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::getValueWithPromise, jsInvoker_, instance_, std::move(error)); + } + + private: + T *instance_; + }; + + Delegate delegate_; +}; + +} // namespace react +} // namespace facebook diff --git a/packages/sample-apps/index.windows.js b/packages/sample-apps/index.windows.js index 8574a2e21e5..31efb5ed470 100644 --- a/packages/sample-apps/index.windows.js +++ b/packages/sample-apps/index.windows.js @@ -22,6 +22,8 @@ import {MyComp} from './myComp'; import {default as MyModule} from './src/NativeMyModule'; +import {default as MyJsiModule} from './src/NativeMyJsiModule'; + const SampleModuleCS = TurboModuleRegistry.get('SampleModuleCS'); const SampleModuleCpp = TurboModuleRegistry.get('SampleModuleCpp'); @@ -359,6 +361,8 @@ class SampleApp extends Component {