react-native-windows/vnext/Microsoft.ReactNative.Managed/ReactSyncMethodInfo.cs

56 строки
2.1 KiB
C#
Исходник Постоянная ссылка Обычный вид История

// Copyright (c) Microsoft Corporation.
Native Modules in C# and C++ (#3403) This prototype is built on top of @jonthysell and @micahl prototypes. It has two main points: - Simplify native module creation by supporting plain C++ and C# with custom attributes for the module and its members. - Make efficient data sending between native modules and the ReactNative DLL by using IJSValueReader and IJSValueWriter interfaces that allow to avoid data boxing and thus to avoid extra memory allocations. This is an example for the C# native modules with custom attributes: [ReactModule] class CsStrings { [ReactMethod] public int Length(string value) { return value.Length; } [ReactMethod] public string Concat(string x, string y) { return x + y; } [ReactMethod] public void Substr(string value, int length, ReactCallback<string> resolve, ReactCallback<string> reject) { if (length <= value.Length) resolve(value.Substring(0, length)); else reject("length must be less or equal to string length"); } } We use here ReactModule and ReaactMethod attributes. We also have ReactConstant and ReactEvent attributes to mark a field or event to be a constant or an event. The ReactConstantProvider can be added to a method that provides a set of constants. In C++ we have similar syntax: REACT_MODULE(MyModule); struct MyModule { REACT_CONSTANT(m_fieldConst); const int m_fieldConst = 42; REACT_CONSTANT_JSNAME(m_fldConst, "fldConst"); const int m_fldConst = 43; REACT_CONST_PROVIDER(SimpleConstants); void SimpleConstants( const winrt::Microsoft::ReactNative::Bridge::IJSValueWriter &writer) noexcept { ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst1", 5); ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst2", "World"); } REACT_EVENT(OnChanged); std::function<void(int)> OnChanged; REACT_METHOD(Add); int Add(int x, int y) noexcept { OnChanged(x + y); return x + y; } We use macros as custom attributes. The related docs are in the vnext\docs\NativeModules.md file. From @jonthysell's commit message: * Replaced NativeModuleBase with INativeModule * Added Microsoft.ReactNative.Managed project * Removed Microsoft.ReactNative solution, added projects into ReactWindows-Universal #3237 * Moved sample native modules from sample apps into sample libraries #3226, #3223 * Created C# native module sample with all method return types, #3207, #3218, #3231, #3230, #3232 * SampleApp(CS/CPP) get native modules from SampleLibrary(CS/CPP) #3227 * Created FancyMathABI native module on top of INativeModule #3224 * Removed marshaling of native method parameters via json #3331 Squash comments: * Initial import from prototype * Replaced NativeModuleBase with INativeModule * FancyMath now works E2E * Initial NativeModules.md docs * Update NativeModules.md * Started View Manager documentation * Cleaned up ViewManagers, updated docs * Update NativeModules.md * Added stubs for IVM.NativeProps * number fix * Added new C# SampleModule * Addresses #3230, #3231, #3232 * Starts #3226 * Removed C++/WinRT modules from app #3226 added new SampleLibraryCPP #3223 * Fixed solution files * SampleApp package manifest updates * Fixed issue with having an app with 0 native modules * Adding SampleModuleABI to SampleLibraryCPP * Clangify * Added some links to docs * Fixed compilation issue for C++ code. * Added Reader/Writer interfaces and DynamicReader. * Added DynamicWriter * Added NativeModuleBuilder implementation * Added Microsoft copyright notices * Added the INativeModulePackage for the new native modules. * Added C# shared code to support native module creation * Added shared C++ project to support native module development. * Added cpp FancyMathABI stub * clangify * SampleLibraryCPP work * clangify * lintify * Change files * Enable MSBuild - SampleApps in CI loop * Enabled run of Calculator methods from SampleLibraryCPP in SampleAppCPP * Enabled C++ module to be used from C# project * Switch to x86 tools for SampleApps build * Align sampleapp windows min versions * Fix SampleAppCPP dependencies * Enabled calling C++ and C# modules from C++ code * Remove SampleAppCPP and SampleLibraryCPP from build * Fix propertysheets * Removed Microsoft.ReactNative solution, added projects to ReactWindows-Universal * Added JSI projects to SampleApps * Disabled SampleApps build * Fixed issue that C# called resolve callback instead of reject * Fixed a few spelling issues * Improved the Debug message output * Provide better error info for C# code * Implemented Error object message for C++ and C# code * Enabled modules events and added a test for it. * Renamed attribute macros to start with REACT_ * Added MyCtorModule to register module members in constructor. * Initial changes to NativeModules.md documentation * Changed ReactModule attribute to use public property setters * Updated the documentation with the example how to use the new native modules. * Removed JSON marshaling, issue #3331 * clangify * Added docs for the C++ code. * Started to work on the new IReactPackageProvider interface * Work in progress: new package providers * Implemented IReactPackageProvider * Fixed event registration in JS * Make sure that C# test app works * Added REACT_MODULE macro-attribute for C++ code * Changed C++ macros to use variable set of parameters * Fixed code after the merge * Fixed code after merge with master branch * Extracted JSValueReader and JSValueWriter into separate files. * Changed REACT_CONSTANT to accept variable set of parameters * Changed REACT_EVENT macro to accept variable parameters * Renamed INativeModuleBuilder to IReactModuleBuilder * Removed SetName method * Renamed AddConstantWriter to AddConstantProvider * Renamed AddEventRegister to AddEventSetter * Renaming Event setting related delegates * Removed C++ native module registration that can be done from constructor. * Fixed file code formatting
2019-10-15 01:58:23 +03:00
// Licensed under the MIT License.
using System;
using System.Reflection;
using System.Threading;
using static Microsoft.ReactNative.Managed.JSValueGenerator;
using static Microsoft.ReactNative.Managed.JSValueReaderGenerator;
using static Microsoft.ReactNative.Managed.JSValueWriterGenerator;
Native Modules in C# and C++ (#3403) This prototype is built on top of @jonthysell and @micahl prototypes. It has two main points: - Simplify native module creation by supporting plain C++ and C# with custom attributes for the module and its members. - Make efficient data sending between native modules and the ReactNative DLL by using IJSValueReader and IJSValueWriter interfaces that allow to avoid data boxing and thus to avoid extra memory allocations. This is an example for the C# native modules with custom attributes: [ReactModule] class CsStrings { [ReactMethod] public int Length(string value) { return value.Length; } [ReactMethod] public string Concat(string x, string y) { return x + y; } [ReactMethod] public void Substr(string value, int length, ReactCallback<string> resolve, ReactCallback<string> reject) { if (length <= value.Length) resolve(value.Substring(0, length)); else reject("length must be less or equal to string length"); } } We use here ReactModule and ReaactMethod attributes. We also have ReactConstant and ReactEvent attributes to mark a field or event to be a constant or an event. The ReactConstantProvider can be added to a method that provides a set of constants. In C++ we have similar syntax: REACT_MODULE(MyModule); struct MyModule { REACT_CONSTANT(m_fieldConst); const int m_fieldConst = 42; REACT_CONSTANT_JSNAME(m_fldConst, "fldConst"); const int m_fldConst = 43; REACT_CONST_PROVIDER(SimpleConstants); void SimpleConstants( const winrt::Microsoft::ReactNative::Bridge::IJSValueWriter &writer) noexcept { ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst1", 5); ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst2", "World"); } REACT_EVENT(OnChanged); std::function<void(int)> OnChanged; REACT_METHOD(Add); int Add(int x, int y) noexcept { OnChanged(x + y); return x + y; } We use macros as custom attributes. The related docs are in the vnext\docs\NativeModules.md file. From @jonthysell's commit message: * Replaced NativeModuleBase with INativeModule * Added Microsoft.ReactNative.Managed project * Removed Microsoft.ReactNative solution, added projects into ReactWindows-Universal #3237 * Moved sample native modules from sample apps into sample libraries #3226, #3223 * Created C# native module sample with all method return types, #3207, #3218, #3231, #3230, #3232 * SampleApp(CS/CPP) get native modules from SampleLibrary(CS/CPP) #3227 * Created FancyMathABI native module on top of INativeModule #3224 * Removed marshaling of native method parameters via json #3331 Squash comments: * Initial import from prototype * Replaced NativeModuleBase with INativeModule * FancyMath now works E2E * Initial NativeModules.md docs * Update NativeModules.md * Started View Manager documentation * Cleaned up ViewManagers, updated docs * Update NativeModules.md * Added stubs for IVM.NativeProps * number fix * Added new C# SampleModule * Addresses #3230, #3231, #3232 * Starts #3226 * Removed C++/WinRT modules from app #3226 added new SampleLibraryCPP #3223 * Fixed solution files * SampleApp package manifest updates * Fixed issue with having an app with 0 native modules * Adding SampleModuleABI to SampleLibraryCPP * Clangify * Added some links to docs * Fixed compilation issue for C++ code. * Added Reader/Writer interfaces and DynamicReader. * Added DynamicWriter * Added NativeModuleBuilder implementation * Added Microsoft copyright notices * Added the INativeModulePackage for the new native modules. * Added C# shared code to support native module creation * Added shared C++ project to support native module development. * Added cpp FancyMathABI stub * clangify * SampleLibraryCPP work * clangify * lintify * Change files * Enable MSBuild - SampleApps in CI loop * Enabled run of Calculator methods from SampleLibraryCPP in SampleAppCPP * Enabled C++ module to be used from C# project * Switch to x86 tools for SampleApps build * Align sampleapp windows min versions * Fix SampleAppCPP dependencies * Enabled calling C++ and C# modules from C++ code * Remove SampleAppCPP and SampleLibraryCPP from build * Fix propertysheets * Removed Microsoft.ReactNative solution, added projects to ReactWindows-Universal * Added JSI projects to SampleApps * Disabled SampleApps build * Fixed issue that C# called resolve callback instead of reject * Fixed a few spelling issues * Improved the Debug message output * Provide better error info for C# code * Implemented Error object message for C++ and C# code * Enabled modules events and added a test for it. * Renamed attribute macros to start with REACT_ * Added MyCtorModule to register module members in constructor. * Initial changes to NativeModules.md documentation * Changed ReactModule attribute to use public property setters * Updated the documentation with the example how to use the new native modules. * Removed JSON marshaling, issue #3331 * clangify * Added docs for the C++ code. * Started to work on the new IReactPackageProvider interface * Work in progress: new package providers * Implemented IReactPackageProvider * Fixed event registration in JS * Make sure that C# test app works * Added REACT_MODULE macro-attribute for C++ code * Changed C++ macros to use variable set of parameters * Fixed code after the merge * Fixed code after merge with master branch * Extracted JSValueReader and JSValueWriter into separate files. * Changed REACT_CONSTANT to accept variable set of parameters * Changed REACT_EVENT macro to accept variable parameters * Renamed INativeModuleBuilder to IReactModuleBuilder * Removed SetName method * Renamed AddConstantWriter to AddConstantProvider * Renamed AddEventRegister to AddEventSetter * Renaming Event setting related delegates * Removed C++ native module registration that can be done from constructor. * Fixed file code formatting
2019-10-15 01:58:23 +03:00
namespace Microsoft.ReactNative.Managed
{
internal class ReactSyncMethodInfo
Native Modules in C# and C++ (#3403) This prototype is built on top of @jonthysell and @micahl prototypes. It has two main points: - Simplify native module creation by supporting plain C++ and C# with custom attributes for the module and its members. - Make efficient data sending between native modules and the ReactNative DLL by using IJSValueReader and IJSValueWriter interfaces that allow to avoid data boxing and thus to avoid extra memory allocations. This is an example for the C# native modules with custom attributes: [ReactModule] class CsStrings { [ReactMethod] public int Length(string value) { return value.Length; } [ReactMethod] public string Concat(string x, string y) { return x + y; } [ReactMethod] public void Substr(string value, int length, ReactCallback<string> resolve, ReactCallback<string> reject) { if (length <= value.Length) resolve(value.Substring(0, length)); else reject("length must be less or equal to string length"); } } We use here ReactModule and ReaactMethod attributes. We also have ReactConstant and ReactEvent attributes to mark a field or event to be a constant or an event. The ReactConstantProvider can be added to a method that provides a set of constants. In C++ we have similar syntax: REACT_MODULE(MyModule); struct MyModule { REACT_CONSTANT(m_fieldConst); const int m_fieldConst = 42; REACT_CONSTANT_JSNAME(m_fldConst, "fldConst"); const int m_fldConst = 43; REACT_CONST_PROVIDER(SimpleConstants); void SimpleConstants( const winrt::Microsoft::ReactNative::Bridge::IJSValueWriter &writer) noexcept { ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst1", 5); ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst2", "World"); } REACT_EVENT(OnChanged); std::function<void(int)> OnChanged; REACT_METHOD(Add); int Add(int x, int y) noexcept { OnChanged(x + y); return x + y; } We use macros as custom attributes. The related docs are in the vnext\docs\NativeModules.md file. From @jonthysell's commit message: * Replaced NativeModuleBase with INativeModule * Added Microsoft.ReactNative.Managed project * Removed Microsoft.ReactNative solution, added projects into ReactWindows-Universal #3237 * Moved sample native modules from sample apps into sample libraries #3226, #3223 * Created C# native module sample with all method return types, #3207, #3218, #3231, #3230, #3232 * SampleApp(CS/CPP) get native modules from SampleLibrary(CS/CPP) #3227 * Created FancyMathABI native module on top of INativeModule #3224 * Removed marshaling of native method parameters via json #3331 Squash comments: * Initial import from prototype * Replaced NativeModuleBase with INativeModule * FancyMath now works E2E * Initial NativeModules.md docs * Update NativeModules.md * Started View Manager documentation * Cleaned up ViewManagers, updated docs * Update NativeModules.md * Added stubs for IVM.NativeProps * number fix * Added new C# SampleModule * Addresses #3230, #3231, #3232 * Starts #3226 * Removed C++/WinRT modules from app #3226 added new SampleLibraryCPP #3223 * Fixed solution files * SampleApp package manifest updates * Fixed issue with having an app with 0 native modules * Adding SampleModuleABI to SampleLibraryCPP * Clangify * Added some links to docs * Fixed compilation issue for C++ code. * Added Reader/Writer interfaces and DynamicReader. * Added DynamicWriter * Added NativeModuleBuilder implementation * Added Microsoft copyright notices * Added the INativeModulePackage for the new native modules. * Added C# shared code to support native module creation * Added shared C++ project to support native module development. * Added cpp FancyMathABI stub * clangify * SampleLibraryCPP work * clangify * lintify * Change files * Enable MSBuild - SampleApps in CI loop * Enabled run of Calculator methods from SampleLibraryCPP in SampleAppCPP * Enabled C++ module to be used from C# project * Switch to x86 tools for SampleApps build * Align sampleapp windows min versions * Fix SampleAppCPP dependencies * Enabled calling C++ and C# modules from C++ code * Remove SampleAppCPP and SampleLibraryCPP from build * Fix propertysheets * Removed Microsoft.ReactNative solution, added projects to ReactWindows-Universal * Added JSI projects to SampleApps * Disabled SampleApps build * Fixed issue that C# called resolve callback instead of reject * Fixed a few spelling issues * Improved the Debug message output * Provide better error info for C# code * Implemented Error object message for C++ and C# code * Enabled modules events and added a test for it. * Renamed attribute macros to start with REACT_ * Added MyCtorModule to register module members in constructor. * Initial changes to NativeModules.md documentation * Changed ReactModule attribute to use public property setters * Updated the documentation with the example how to use the new native modules. * Removed JSON marshaling, issue #3331 * clangify * Added docs for the C++ code. * Started to work on the new IReactPackageProvider interface * Work in progress: new package providers * Implemented IReactPackageProvider * Fixed event registration in JS * Make sure that C# test app works * Added REACT_MODULE macro-attribute for C++ code * Changed C++ macros to use variable set of parameters * Fixed code after the merge * Fixed code after merge with master branch * Extracted JSValueReader and JSValueWriter into separate files. * Changed REACT_CONSTANT to accept variable set of parameters * Changed REACT_EVENT macro to accept variable parameters * Renamed INativeModuleBuilder to IReactModuleBuilder * Removed SetName method * Renamed AddConstantWriter to AddConstantProvider * Renamed AddEventRegister to AddEventSetter * Renaming Event setting related delegates * Removed C++ native module registration that can be done from constructor. * Fixed file code formatting
2019-10-15 01:58:23 +03:00
{
public ReactSyncMethodInfo(MethodInfo methodInfo, ReactSyncMethodAttribute methodAttribute)
Native Modules in C# and C++ (#3403) This prototype is built on top of @jonthysell and @micahl prototypes. It has two main points: - Simplify native module creation by supporting plain C++ and C# with custom attributes for the module and its members. - Make efficient data sending between native modules and the ReactNative DLL by using IJSValueReader and IJSValueWriter interfaces that allow to avoid data boxing and thus to avoid extra memory allocations. This is an example for the C# native modules with custom attributes: [ReactModule] class CsStrings { [ReactMethod] public int Length(string value) { return value.Length; } [ReactMethod] public string Concat(string x, string y) { return x + y; } [ReactMethod] public void Substr(string value, int length, ReactCallback<string> resolve, ReactCallback<string> reject) { if (length <= value.Length) resolve(value.Substring(0, length)); else reject("length must be less or equal to string length"); } } We use here ReactModule and ReaactMethod attributes. We also have ReactConstant and ReactEvent attributes to mark a field or event to be a constant or an event. The ReactConstantProvider can be added to a method that provides a set of constants. In C++ we have similar syntax: REACT_MODULE(MyModule); struct MyModule { REACT_CONSTANT(m_fieldConst); const int m_fieldConst = 42; REACT_CONSTANT_JSNAME(m_fldConst, "fldConst"); const int m_fldConst = 43; REACT_CONST_PROVIDER(SimpleConstants); void SimpleConstants( const winrt::Microsoft::ReactNative::Bridge::IJSValueWriter &writer) noexcept { ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst1", 5); ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst2", "World"); } REACT_EVENT(OnChanged); std::function<void(int)> OnChanged; REACT_METHOD(Add); int Add(int x, int y) noexcept { OnChanged(x + y); return x + y; } We use macros as custom attributes. The related docs are in the vnext\docs\NativeModules.md file. From @jonthysell's commit message: * Replaced NativeModuleBase with INativeModule * Added Microsoft.ReactNative.Managed project * Removed Microsoft.ReactNative solution, added projects into ReactWindows-Universal #3237 * Moved sample native modules from sample apps into sample libraries #3226, #3223 * Created C# native module sample with all method return types, #3207, #3218, #3231, #3230, #3232 * SampleApp(CS/CPP) get native modules from SampleLibrary(CS/CPP) #3227 * Created FancyMathABI native module on top of INativeModule #3224 * Removed marshaling of native method parameters via json #3331 Squash comments: * Initial import from prototype * Replaced NativeModuleBase with INativeModule * FancyMath now works E2E * Initial NativeModules.md docs * Update NativeModules.md * Started View Manager documentation * Cleaned up ViewManagers, updated docs * Update NativeModules.md * Added stubs for IVM.NativeProps * number fix * Added new C# SampleModule * Addresses #3230, #3231, #3232 * Starts #3226 * Removed C++/WinRT modules from app #3226 added new SampleLibraryCPP #3223 * Fixed solution files * SampleApp package manifest updates * Fixed issue with having an app with 0 native modules * Adding SampleModuleABI to SampleLibraryCPP * Clangify * Added some links to docs * Fixed compilation issue for C++ code. * Added Reader/Writer interfaces and DynamicReader. * Added DynamicWriter * Added NativeModuleBuilder implementation * Added Microsoft copyright notices * Added the INativeModulePackage for the new native modules. * Added C# shared code to support native module creation * Added shared C++ project to support native module development. * Added cpp FancyMathABI stub * clangify * SampleLibraryCPP work * clangify * lintify * Change files * Enable MSBuild - SampleApps in CI loop * Enabled run of Calculator methods from SampleLibraryCPP in SampleAppCPP * Enabled C++ module to be used from C# project * Switch to x86 tools for SampleApps build * Align sampleapp windows min versions * Fix SampleAppCPP dependencies * Enabled calling C++ and C# modules from C++ code * Remove SampleAppCPP and SampleLibraryCPP from build * Fix propertysheets * Removed Microsoft.ReactNative solution, added projects to ReactWindows-Universal * Added JSI projects to SampleApps * Disabled SampleApps build * Fixed issue that C# called resolve callback instead of reject * Fixed a few spelling issues * Improved the Debug message output * Provide better error info for C# code * Implemented Error object message for C++ and C# code * Enabled modules events and added a test for it. * Renamed attribute macros to start with REACT_ * Added MyCtorModule to register module members in constructor. * Initial changes to NativeModules.md documentation * Changed ReactModule attribute to use public property setters * Updated the documentation with the example how to use the new native modules. * Removed JSON marshaling, issue #3331 * clangify * Added docs for the C++ code. * Started to work on the new IReactPackageProvider interface * Work in progress: new package providers * Implemented IReactPackageProvider * Fixed event registration in JS * Make sure that C# test app works * Added REACT_MODULE macro-attribute for C++ code * Changed C++ macros to use variable set of parameters * Fixed code after the merge * Fixed code after merge with master branch * Extracted JSValueReader and JSValueWriter into separate files. * Changed REACT_CONSTANT to accept variable set of parameters * Changed REACT_EVENT macro to accept variable parameters * Renamed INativeModuleBuilder to IReactModuleBuilder * Removed SetName method * Renamed AddConstantWriter to AddConstantProvider * Renamed AddEventRegister to AddEventSetter * Renaming Event setting related delegates * Removed C++ native module registration that can be done from constructor. * Fixed file code formatting
2019-10-15 01:58:23 +03:00
{
MethodName = methodAttribute.MethodName ?? methodInfo.Name;
MethodImpl = new Lazy<ReactSyncMethodImpl>(() => MakeMethod(methodInfo), LazyThreadSafetyMode.PublicationOnly);
}
private ReactSyncMethodImpl MakeMethod(MethodInfo methodInfo)
Native Modules in C# and C++ (#3403) This prototype is built on top of @jonthysell and @micahl prototypes. It has two main points: - Simplify native module creation by supporting plain C++ and C# with custom attributes for the module and its members. - Make efficient data sending between native modules and the ReactNative DLL by using IJSValueReader and IJSValueWriter interfaces that allow to avoid data boxing and thus to avoid extra memory allocations. This is an example for the C# native modules with custom attributes: [ReactModule] class CsStrings { [ReactMethod] public int Length(string value) { return value.Length; } [ReactMethod] public string Concat(string x, string y) { return x + y; } [ReactMethod] public void Substr(string value, int length, ReactCallback<string> resolve, ReactCallback<string> reject) { if (length <= value.Length) resolve(value.Substring(0, length)); else reject("length must be less or equal to string length"); } } We use here ReactModule and ReaactMethod attributes. We also have ReactConstant and ReactEvent attributes to mark a field or event to be a constant or an event. The ReactConstantProvider can be added to a method that provides a set of constants. In C++ we have similar syntax: REACT_MODULE(MyModule); struct MyModule { REACT_CONSTANT(m_fieldConst); const int m_fieldConst = 42; REACT_CONSTANT_JSNAME(m_fldConst, "fldConst"); const int m_fldConst = 43; REACT_CONST_PROVIDER(SimpleConstants); void SimpleConstants( const winrt::Microsoft::ReactNative::Bridge::IJSValueWriter &writer) noexcept { ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst1", 5); ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst2", "World"); } REACT_EVENT(OnChanged); std::function<void(int)> OnChanged; REACT_METHOD(Add); int Add(int x, int y) noexcept { OnChanged(x + y); return x + y; } We use macros as custom attributes. The related docs are in the vnext\docs\NativeModules.md file. From @jonthysell's commit message: * Replaced NativeModuleBase with INativeModule * Added Microsoft.ReactNative.Managed project * Removed Microsoft.ReactNative solution, added projects into ReactWindows-Universal #3237 * Moved sample native modules from sample apps into sample libraries #3226, #3223 * Created C# native module sample with all method return types, #3207, #3218, #3231, #3230, #3232 * SampleApp(CS/CPP) get native modules from SampleLibrary(CS/CPP) #3227 * Created FancyMathABI native module on top of INativeModule #3224 * Removed marshaling of native method parameters via json #3331 Squash comments: * Initial import from prototype * Replaced NativeModuleBase with INativeModule * FancyMath now works E2E * Initial NativeModules.md docs * Update NativeModules.md * Started View Manager documentation * Cleaned up ViewManagers, updated docs * Update NativeModules.md * Added stubs for IVM.NativeProps * number fix * Added new C# SampleModule * Addresses #3230, #3231, #3232 * Starts #3226 * Removed C++/WinRT modules from app #3226 added new SampleLibraryCPP #3223 * Fixed solution files * SampleApp package manifest updates * Fixed issue with having an app with 0 native modules * Adding SampleModuleABI to SampleLibraryCPP * Clangify * Added some links to docs * Fixed compilation issue for C++ code. * Added Reader/Writer interfaces and DynamicReader. * Added DynamicWriter * Added NativeModuleBuilder implementation * Added Microsoft copyright notices * Added the INativeModulePackage for the new native modules. * Added C# shared code to support native module creation * Added shared C++ project to support native module development. * Added cpp FancyMathABI stub * clangify * SampleLibraryCPP work * clangify * lintify * Change files * Enable MSBuild - SampleApps in CI loop * Enabled run of Calculator methods from SampleLibraryCPP in SampleAppCPP * Enabled C++ module to be used from C# project * Switch to x86 tools for SampleApps build * Align sampleapp windows min versions * Fix SampleAppCPP dependencies * Enabled calling C++ and C# modules from C++ code * Remove SampleAppCPP and SampleLibraryCPP from build * Fix propertysheets * Removed Microsoft.ReactNative solution, added projects to ReactWindows-Universal * Added JSI projects to SampleApps * Disabled SampleApps build * Fixed issue that C# called resolve callback instead of reject * Fixed a few spelling issues * Improved the Debug message output * Provide better error info for C# code * Implemented Error object message for C++ and C# code * Enabled modules events and added a test for it. * Renamed attribute macros to start with REACT_ * Added MyCtorModule to register module members in constructor. * Initial changes to NativeModules.md documentation * Changed ReactModule attribute to use public property setters * Updated the documentation with the example how to use the new native modules. * Removed JSON marshaling, issue #3331 * clangify * Added docs for the C++ code. * Started to work on the new IReactPackageProvider interface * Work in progress: new package providers * Implemented IReactPackageProvider * Fixed event registration in JS * Make sure that C# test app works * Added REACT_MODULE macro-attribute for C++ code * Changed C++ macros to use variable set of parameters * Fixed code after the merge * Fixed code after merge with master branch * Extracted JSValueReader and JSValueWriter into separate files. * Changed REACT_CONSTANT to accept variable set of parameters * Changed REACT_EVENT macro to accept variable parameters * Renamed INativeModuleBuilder to IReactModuleBuilder * Removed SetName method * Renamed AddConstantWriter to AddConstantProvider * Renamed AddEventRegister to AddEventSetter * Renaming Event setting related delegates * Removed C++ native module registration that can be done from constructor. * Fixed file code formatting
2019-10-15 01:58:23 +03:00
{
// Generate code that looks like:
//
// (object module, IJSValueReader inputReader, IJSValueWriter outputWriter) =>
// {
// inputReader.ReadArgs(out ArgType0 arg0, out ArgType1 arg1)
// outputWriter.WriteArgs((module as ModuleType).SyncMethod(arg0, arg1));
// }
return CompileLambda<ReactSyncMethodImpl>(
Parameter(typeof(object), out var module),
Parameter(typeof(IJSValueReader), out var inputReader),
Parameter(typeof(IJSValueWriter), out var outputWriter),
MethodArgs(methodInfo.GetParameters(), out var argTypes, out var args),
inputReader.CallExt(ReadArgsOf(argTypes), args),
outputWriter.CallExt(WriteValueOf(methodInfo.ReturnType),
module.CastTo(methodInfo.DeclaringType).Call(methodInfo, args)));
Native Modules in C# and C++ (#3403) This prototype is built on top of @jonthysell and @micahl prototypes. It has two main points: - Simplify native module creation by supporting plain C++ and C# with custom attributes for the module and its members. - Make efficient data sending between native modules and the ReactNative DLL by using IJSValueReader and IJSValueWriter interfaces that allow to avoid data boxing and thus to avoid extra memory allocations. This is an example for the C# native modules with custom attributes: [ReactModule] class CsStrings { [ReactMethod] public int Length(string value) { return value.Length; } [ReactMethod] public string Concat(string x, string y) { return x + y; } [ReactMethod] public void Substr(string value, int length, ReactCallback<string> resolve, ReactCallback<string> reject) { if (length <= value.Length) resolve(value.Substring(0, length)); else reject("length must be less or equal to string length"); } } We use here ReactModule and ReaactMethod attributes. We also have ReactConstant and ReactEvent attributes to mark a field or event to be a constant or an event. The ReactConstantProvider can be added to a method that provides a set of constants. In C++ we have similar syntax: REACT_MODULE(MyModule); struct MyModule { REACT_CONSTANT(m_fieldConst); const int m_fieldConst = 42; REACT_CONSTANT_JSNAME(m_fldConst, "fldConst"); const int m_fldConst = 43; REACT_CONST_PROVIDER(SimpleConstants); void SimpleConstants( const winrt::Microsoft::ReactNative::Bridge::IJSValueWriter &writer) noexcept { ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst1", 5); ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst2", "World"); } REACT_EVENT(OnChanged); std::function<void(int)> OnChanged; REACT_METHOD(Add); int Add(int x, int y) noexcept { OnChanged(x + y); return x + y; } We use macros as custom attributes. The related docs are in the vnext\docs\NativeModules.md file. From @jonthysell's commit message: * Replaced NativeModuleBase with INativeModule * Added Microsoft.ReactNative.Managed project * Removed Microsoft.ReactNative solution, added projects into ReactWindows-Universal #3237 * Moved sample native modules from sample apps into sample libraries #3226, #3223 * Created C# native module sample with all method return types, #3207, #3218, #3231, #3230, #3232 * SampleApp(CS/CPP) get native modules from SampleLibrary(CS/CPP) #3227 * Created FancyMathABI native module on top of INativeModule #3224 * Removed marshaling of native method parameters via json #3331 Squash comments: * Initial import from prototype * Replaced NativeModuleBase with INativeModule * FancyMath now works E2E * Initial NativeModules.md docs * Update NativeModules.md * Started View Manager documentation * Cleaned up ViewManagers, updated docs * Update NativeModules.md * Added stubs for IVM.NativeProps * number fix * Added new C# SampleModule * Addresses #3230, #3231, #3232 * Starts #3226 * Removed C++/WinRT modules from app #3226 added new SampleLibraryCPP #3223 * Fixed solution files * SampleApp package manifest updates * Fixed issue with having an app with 0 native modules * Adding SampleModuleABI to SampleLibraryCPP * Clangify * Added some links to docs * Fixed compilation issue for C++ code. * Added Reader/Writer interfaces and DynamicReader. * Added DynamicWriter * Added NativeModuleBuilder implementation * Added Microsoft copyright notices * Added the INativeModulePackage for the new native modules. * Added C# shared code to support native module creation * Added shared C++ project to support native module development. * Added cpp FancyMathABI stub * clangify * SampleLibraryCPP work * clangify * lintify * Change files * Enable MSBuild - SampleApps in CI loop * Enabled run of Calculator methods from SampleLibraryCPP in SampleAppCPP * Enabled C++ module to be used from C# project * Switch to x86 tools for SampleApps build * Align sampleapp windows min versions * Fix SampleAppCPP dependencies * Enabled calling C++ and C# modules from C++ code * Remove SampleAppCPP and SampleLibraryCPP from build * Fix propertysheets * Removed Microsoft.ReactNative solution, added projects to ReactWindows-Universal * Added JSI projects to SampleApps * Disabled SampleApps build * Fixed issue that C# called resolve callback instead of reject * Fixed a few spelling issues * Improved the Debug message output * Provide better error info for C# code * Implemented Error object message for C++ and C# code * Enabled modules events and added a test for it. * Renamed attribute macros to start with REACT_ * Added MyCtorModule to register module members in constructor. * Initial changes to NativeModules.md documentation * Changed ReactModule attribute to use public property setters * Updated the documentation with the example how to use the new native modules. * Removed JSON marshaling, issue #3331 * clangify * Added docs for the C++ code. * Started to work on the new IReactPackageProvider interface * Work in progress: new package providers * Implemented IReactPackageProvider * Fixed event registration in JS * Make sure that C# test app works * Added REACT_MODULE macro-attribute for C++ code * Changed C++ macros to use variable set of parameters * Fixed code after the merge * Fixed code after merge with master branch * Extracted JSValueReader and JSValueWriter into separate files. * Changed REACT_CONSTANT to accept variable set of parameters * Changed REACT_EVENT macro to accept variable parameters * Renamed INativeModuleBuilder to IReactModuleBuilder * Removed SetName method * Renamed AddConstantWriter to AddConstantProvider * Renamed AddEventRegister to AddEventSetter * Renaming Event setting related delegates * Removed C++ native module registration that can be done from constructor. * Fixed file code formatting
2019-10-15 01:58:23 +03:00
}
public delegate void ReactSyncMethodImpl(object module, IJSValueReader inputReader, IJSValueWriter outputWriter);
Native Modules in C# and C++ (#3403) This prototype is built on top of @jonthysell and @micahl prototypes. It has two main points: - Simplify native module creation by supporting plain C++ and C# with custom attributes for the module and its members. - Make efficient data sending between native modules and the ReactNative DLL by using IJSValueReader and IJSValueWriter interfaces that allow to avoid data boxing and thus to avoid extra memory allocations. This is an example for the C# native modules with custom attributes: [ReactModule] class CsStrings { [ReactMethod] public int Length(string value) { return value.Length; } [ReactMethod] public string Concat(string x, string y) { return x + y; } [ReactMethod] public void Substr(string value, int length, ReactCallback<string> resolve, ReactCallback<string> reject) { if (length <= value.Length) resolve(value.Substring(0, length)); else reject("length must be less or equal to string length"); } } We use here ReactModule and ReaactMethod attributes. We also have ReactConstant and ReactEvent attributes to mark a field or event to be a constant or an event. The ReactConstantProvider can be added to a method that provides a set of constants. In C++ we have similar syntax: REACT_MODULE(MyModule); struct MyModule { REACT_CONSTANT(m_fieldConst); const int m_fieldConst = 42; REACT_CONSTANT_JSNAME(m_fldConst, "fldConst"); const int m_fldConst = 43; REACT_CONST_PROVIDER(SimpleConstants); void SimpleConstants( const winrt::Microsoft::ReactNative::Bridge::IJSValueWriter &writer) noexcept { ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst1", 5); ::Microsoft::ReactNative::WriteProperty(writer, "simpleConst2", "World"); } REACT_EVENT(OnChanged); std::function<void(int)> OnChanged; REACT_METHOD(Add); int Add(int x, int y) noexcept { OnChanged(x + y); return x + y; } We use macros as custom attributes. The related docs are in the vnext\docs\NativeModules.md file. From @jonthysell's commit message: * Replaced NativeModuleBase with INativeModule * Added Microsoft.ReactNative.Managed project * Removed Microsoft.ReactNative solution, added projects into ReactWindows-Universal #3237 * Moved sample native modules from sample apps into sample libraries #3226, #3223 * Created C# native module sample with all method return types, #3207, #3218, #3231, #3230, #3232 * SampleApp(CS/CPP) get native modules from SampleLibrary(CS/CPP) #3227 * Created FancyMathABI native module on top of INativeModule #3224 * Removed marshaling of native method parameters via json #3331 Squash comments: * Initial import from prototype * Replaced NativeModuleBase with INativeModule * FancyMath now works E2E * Initial NativeModules.md docs * Update NativeModules.md * Started View Manager documentation * Cleaned up ViewManagers, updated docs * Update NativeModules.md * Added stubs for IVM.NativeProps * number fix * Added new C# SampleModule * Addresses #3230, #3231, #3232 * Starts #3226 * Removed C++/WinRT modules from app #3226 added new SampleLibraryCPP #3223 * Fixed solution files * SampleApp package manifest updates * Fixed issue with having an app with 0 native modules * Adding SampleModuleABI to SampleLibraryCPP * Clangify * Added some links to docs * Fixed compilation issue for C++ code. * Added Reader/Writer interfaces and DynamicReader. * Added DynamicWriter * Added NativeModuleBuilder implementation * Added Microsoft copyright notices * Added the INativeModulePackage for the new native modules. * Added C# shared code to support native module creation * Added shared C++ project to support native module development. * Added cpp FancyMathABI stub * clangify * SampleLibraryCPP work * clangify * lintify * Change files * Enable MSBuild - SampleApps in CI loop * Enabled run of Calculator methods from SampleLibraryCPP in SampleAppCPP * Enabled C++ module to be used from C# project * Switch to x86 tools for SampleApps build * Align sampleapp windows min versions * Fix SampleAppCPP dependencies * Enabled calling C++ and C# modules from C++ code * Remove SampleAppCPP and SampleLibraryCPP from build * Fix propertysheets * Removed Microsoft.ReactNative solution, added projects to ReactWindows-Universal * Added JSI projects to SampleApps * Disabled SampleApps build * Fixed issue that C# called resolve callback instead of reject * Fixed a few spelling issues * Improved the Debug message output * Provide better error info for C# code * Implemented Error object message for C++ and C# code * Enabled modules events and added a test for it. * Renamed attribute macros to start with REACT_ * Added MyCtorModule to register module members in constructor. * Initial changes to NativeModules.md documentation * Changed ReactModule attribute to use public property setters * Updated the documentation with the example how to use the new native modules. * Removed JSON marshaling, issue #3331 * clangify * Added docs for the C++ code. * Started to work on the new IReactPackageProvider interface * Work in progress: new package providers * Implemented IReactPackageProvider * Fixed event registration in JS * Make sure that C# test app works * Added REACT_MODULE macro-attribute for C++ code * Changed C++ macros to use variable set of parameters * Fixed code after the merge * Fixed code after merge with master branch * Extracted JSValueReader and JSValueWriter into separate files. * Changed REACT_CONSTANT to accept variable set of parameters * Changed REACT_EVENT macro to accept variable parameters * Renamed INativeModuleBuilder to IReactModuleBuilder * Removed SetName method * Renamed AddConstantWriter to AddConstantProvider * Renamed AddEventRegister to AddEventSetter * Renaming Event setting related delegates * Removed C++ native module registration that can be done from constructor. * Fixed file code formatting
2019-10-15 01:58:23 +03:00
public string MethodName { get; private set; }
public Lazy<ReactSyncMethodImpl> MethodImpl { get; private set; }
public void AddToModuleBuilder(IReactModuleBuilder moduleBuilder, object module)
{
moduleBuilder.AddSyncMethod(MethodName, (IJSValueReader inputReader, IJSValueWriter outputWriter) =>
{
MethodImpl.Value(module, inputReader, outputWriter);
});
}
}
}