diff --git a/change/react-native-windows-codegen-2020-04-22-13-49-54-codegennm2.json b/change/react-native-windows-codegen-2020-04-22-13-49-54-codegennm2.json new file mode 100644 index 00000000000..6d75f195f59 --- /dev/null +++ b/change/react-native-windows-codegen-2020-04-22-13-49-54-codegennm2.json @@ -0,0 +1,8 @@ +{ + "type": "patch", + "comment": "Initial react-native-windows-codegen", + "packageName": "react-native-windows-codegen", + "email": "acoates@microsoft.com", + "dependentChangeType": "patch", + "date": "2020-04-22T20:49:54.580Z" +} diff --git a/packages/microsoft-reactnative-sampleapps/NativeMyModule.js b/packages/microsoft-reactnative-sampleapps/NativeMyModule.js new file mode 100644 index 00000000000..bb647a3e3d1 --- /dev/null +++ b/packages/microsoft-reactnative-sampleapps/NativeMyModule.js @@ -0,0 +1,32 @@ +/** + * Copyright (c) Microsoft Corporation. + * Licensed under the MIT License. + * + * @flow + * @format + */ + +'use strict'; + +import type {TurboModule} from 'react-native/Libraries/TurboModule/RCTExport'; +import * as TurboModuleRegistry from 'react-native/Libraries/TurboModule/TurboModuleRegistry'; + +export interface Spec extends TurboModule { + // Exported methods. + +getConstants: () => {| + const1: boolean, + const2: number, + const3: string, + |}; + +voidFunc: () => void; + +getBool: (arg: boolean) => boolean; + +getNumber: (arg: number) => number; + +getString: (arg: string) => string; + +getArray: (arg: Array) => Array; + +getObject: (arg: Object) => Object; + +getValue: (x: number, y: string, z: Object) => Object; + +getValueWithCallback: (callback: (value: string) => void) => void; + +getValueWithPromise: (error: boolean) => Promise; +} + +export default (TurboModuleRegistry.getEnforcing('MyModule'): Spec); diff --git a/packages/microsoft-reactnative-sampleapps/codegen/NativeModules.g.h b/packages/microsoft-reactnative-sampleapps/codegen/NativeModules.g.h new file mode 100644 index 00000000000..22d7b869d01 --- /dev/null +++ b/packages/microsoft-reactnative-sampleapps/codegen/NativeModules.g.h @@ -0,0 +1,83 @@ + +/** + * This file is auto-generated from a NativeModule spec file in js. + */ + +#pragma once + +#include "NativeModules.h" +#include + +namespace SampleLibraryCpp { + +/* + * 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. + */ +struct MyModuleSpec : winrt::Microsoft::ReactNative::TurboModuleSpec { + static constexpr auto methods = std::tuple{ + Method{0, L"voidFunc"}, + SyncMethod{1, L"getBool"}, + SyncMethod{2, L"getNumber"}, + SyncMethod{3, L"getString"}, + SyncMethod{4, L"getArray"}, + SyncMethod{5, L"getObject"}, + SyncMethod{6, L"getValue"}, + Method) noexcept>{7, L"getValueWithCallback"}, + Method) noexcept>{8, L"getValueWithPromise"}, + }; + + template + static constexpr void ValidateModule() noexcept { + constexpr auto methodCheckResults = CheckMethods(); + + REACT_SHOW_METHOD_SPEC_ERRORS( + 0, + "voidFunc", + " REACT_METHOD(voidFunc) void voidFunc() noexcept { /* implementation */ }}", + " REACT_METHOD(voidFunc) static void voidFunc() noexcept { /* implementation */ }}"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 1, + "getBool", + " REACT_SYNC_METHOD(getBool) bool getBool(bool arg) noexcept { /* implementation */ }}", + " REACT_SYNC_METHOD(getBool) static bool getBool(bool arg) noexcept { /* implementation */ }}"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 2, + "getNumber", + " REACT_SYNC_METHOD(getNumber) double getNumber(double arg) noexcept { /* implementation */ }}", + " REACT_SYNC_METHOD(getNumber) static double getNumber(double arg) noexcept { /* implementation */ }}"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 3, + "getString", + " REACT_SYNC_METHOD(getString) std::string getString(std::string arg) noexcept { /* implementation */ }}", + " REACT_SYNC_METHOD(getString) static std::string getString(std::string arg) noexcept { /* implementation */ }}"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 4, + "getArray", + " REACT_SYNC_METHOD(getArray) React::JSValueArray getArray(React::JSValueArray && arg) noexcept { /* implementation */ }}", + " REACT_SYNC_METHOD(getArray) static React::JSValueArray getArray(React::JSValueArray && arg) noexcept { /* implementation */ }}"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 5, + "getObject", + " REACT_SYNC_METHOD(getObject) React::JSValueObject getObject(React::JSValueObject && arg) noexcept { /* implementation */ }}", + " REACT_SYNC_METHOD(getObject) static React::JSValueObject getObject(React::JSValueObject && arg) noexcept { /* implementation */ }}"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 6, + "getValue", + " REACT_SYNC_METHOD(getValue) React::JSValueObject getValue(double x, std::string y, React::JSValueObject && z) noexcept { /* implementation */ }}", + " REACT_SYNC_METHOD(getValue) static React::JSValueObject getValue(double x, std::string y, React::JSValueObject && z) noexcept { /* implementation */ }}"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 7, + "getValueWithCallback", + " REACT_METHOD(getValueWithCallback) void getValueWithCallback(std::function const & callback) noexcept { /* implementation */ }}", + " REACT_METHOD(getValueWithCallback) static void getValueWithCallback(std::function const & callback) noexcept { /* implementation */ }}"); + REACT_SHOW_METHOD_SPEC_ERRORS( + 8, + "getValueWithPromise", + " REACT_METHOD(getValueWithPromise) void getValueWithPromise(bool error, React::ReactPromise &&result) noexcept { /* implementation */ }}", + " REACT_METHOD(getValueWithPromise) static void getValueWithPromise(bool error, React::ReactPromise &&result) noexcept { /* implementation */ }}"); + } +}; + +} // namespace SampleLibraryCpp diff --git a/packages/microsoft-reactnative-sampleapps/index.windows.js b/packages/microsoft-reactnative-sampleapps/index.windows.js index 8256f7c6253..6adf5cdaf99 100644 --- a/packages/microsoft-reactnative-sampleapps/index.windows.js +++ b/packages/microsoft-reactnative-sampleapps/index.windows.js @@ -20,6 +20,8 @@ import { NativeModules, NativeEventEmitter } from 'react-native'; import {MyComp} from './myComp'; +import {default as MyModule} from './NativeMyModule'; + // Creating event emitters const SampleModuleCSEmitter = new NativeEventEmitter(NativeModules.SampleModuleCS); const SampleModuleCppEmitter = new NativeEventEmitter(NativeModules.SampleModuleCpp); @@ -278,6 +280,8 @@ class SampleApp extends Component { This app consumes custom Native Modules and View Managers. +