react-native-windows
react-native-windows copied to clipboard
IReactPackageBuilder AddMethod created methods don't work correctly
Problem Description
Native Module method created by custom IReactPackageProvider are causing application fatal crash when callback function is given as a parameter. AddSyncMethod works correctly (in release). Adding initializer as well as consts via IReactPackageProvider also works correctly.
IReactPackageProvider is missing SetName() method, (had to comment it out) from Native Modules (advanced) tutorial but I don't think that's the reason for the crash.
Exactly the same methods created using [ReactMethod] Attribute called in exactly the same way are working correctly.
When debbuging AddMethod MethodDelegate I noticed that resolve delegate is in invalid state.
Debug build (Method = <Internal Error evaluating expression>):

Release build gave me few more details (Method = Evaluation of method System.Delegate.System.Delegate.get_Method() requires use of the static field Internal.Runtime.TypeLoader.CallConverterThunk.s_thunkPoolHeap, which is not available in this context.):

Tested both on RN 0.66.4 & RNW 0.66.25 as well as on RN 0.70.0 & RNW 0.70.0
Steps To Reproduce
Follow step-by-step Native Modules (advanced): https://microsoft.github.io/react-native-windows/docs/native-modules-advanced
Or
clone https://github.com/MaciejNiklasinski/ModuleBuilderMethodsDontWork yarn yarn start yarn windows
Expected Results
When "FancyMath" button is pressed to call the native module function, function returns result correctly When "FancyMathNoAttr - Async" button is pressed to call the native module function application will crash. When "FancyMath" button is pressed (in release) to call the native module function, function returns result correctly.
CLI version
6.4.0
Environment
System:
OS: Windows 10 10.0.19044
CPU: (32) x64 AMD Ryzen 9 5950X 16-Core Processor
Memory: 51.35 GB / 63.91 GB
Binaries:
Node: 16.16.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
npm: 8.11.0 - C:\Program Files\nodejs\npm.CMD
Watchman: Not Found
SDKs:
Android SDK: Not Found
Windows SDK:
AllowDevelopmentWithoutDevLicense: Enabled
AllowAllTrustedApps: Enabled
Versions: 10.0.16299.0, 10.0.17134.0, 10.0.17763.0, 10.0.18362.0, 10.0.19041.0, 10.0.22000.0
IDEs:
Android Studio: Not Found
Visual Studio: 16.11.32802.440 (Visual Studio Community 2019)
Languages:
Java: Not Found
npmPackages:
@react-native-community/cli: Not Found
react: 17.0.2 => 17.0.2
react-native: 0.66.4 => 0.66.4
react-native-windows: 0.66.25 => 0.66.25
npmGlobalPackages:
*react-native*: Not Found
Target Platform Version
10.0.19041
Target Device(s)
Desktop
Visual Studio Version
Visual Studio 2019
Build Configuration
Debug
Snack, code example, screenshot, or link to a repository
https://github.com/MaciejNiklasinski/ModuleBuilderMethodsDontWork
@vmoroz fyi
Appears to be a C# only issue. @jonthysell can you take a look?
The sample is incorrect. When calling the resolve callback, it needs the outputWriter to contain the input for a callback, i.e. a serialized array of argument values.
So you need to warp the result in an array:
outputWriter.WriteArrayBegin();
outputWriter.WriteDouble(result);
outputWriter.WriteArrayEnd();
Alternatively, you can use the generic helper and call outputWriter.WriteArgs(result); instead.
I will update the sample code in the docs.