Skip to content

Commit abbc6eb

Browse files
rubennortefacebook-github-bot
authored andcommitted
Modify codegen to throw JS errors instead of crashing when passing non-number arguments where RootTags are expected
Summary: Changelog: [General][Fixed] Fixed crash when passing non-numeric values where RootTag is expected to methods in native modules using codegen ## Context Right now, if you have a native module using the codegen with a method like this: ``` someMethod(value: RootTag): void; ``` And you call it like this: ``` NativeModule.someMethod(''); ``` The app crashes. This happens because we cast the JS value to a C++ value using the method that asserts (`toNumber`) instead of the one that throws a JS error (`asNumber`). ## Changes This fixes the crash by using `asNumber` instead of `toNumber`. Reviewed By: RSNara Differential Revision: D54206288 fbshipit-source-id: 9398112667e0f26edaf4f8f3b32e79fa8aafde62
1 parent fadcf7b commit abbc6eb

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

packages/react-native-codegen/e2e/__tests__/modules/__snapshots__/GenerateModuleCpp-test.js.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getAlias(jsi:
349349
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
350350
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getRootTag(
351351
rt,
352-
args[0].getNumber()
352+
args[0].asNumber()
353353
);
354354
}
355355
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValue(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
@@ -550,7 +550,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleNullableCxxSpecJSI_getAl
550550
static jsi::Value __hostFunction_NativeSampleTurboModuleNullableCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
551551
auto result = static_cast<NativeSampleTurboModuleNullableCxxSpecJSI *>(&turboModule)->getRootTag(
552552
rt,
553-
args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].getNumber())
553+
args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].asNumber())
554554
);
555555
return result ? jsi::Value(std::move(*result)) : jsi::Value::null();
556556
}
@@ -656,7 +656,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleNullableAndOptionalCxxSp
656656
static jsi::Value __hostFunction_NativeSampleTurboModuleNullableAndOptionalCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
657657
auto result = static_cast<NativeSampleTurboModuleNullableAndOptionalCxxSpecJSI *>(&turboModule)->getRootTag(
658658
rt,
659-
count <= 0 || args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].getNumber())
659+
count <= 0 || args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].asNumber())
660660
);
661661
return result ? jsi::Value(std::move(*result)) : jsi::Value::null();
662662
}
@@ -756,7 +756,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleOptionalCxxSpecJSI_getAl
756756
static jsi::Value __hostFunction_NativeSampleTurboModuleOptionalCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
757757
return static_cast<NativeSampleTurboModuleOptionalCxxSpecJSI *>(&turboModule)->getRootTag(
758758
rt,
759-
count <= 0 || args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].getNumber())
759+
count <= 0 || args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].asNumber())
760760
);
761761
}
762762
static jsi::Value __hostFunction_NativeSampleTurboModuleOptionalCxxSpecJSI_getValue(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
@@ -1170,7 +1170,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getAlias(jsi:
11701170
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
11711171
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getRootTag(
11721172
rt,
1173-
args[0].getNumber()
1173+
args[0].asNumber()
11741174
);
11751175
}
11761176
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValue(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
@@ -1371,7 +1371,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleNullableCxxSpecJSI_getAl
13711371
static jsi::Value __hostFunction_NativeSampleTurboModuleNullableCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
13721372
auto result = static_cast<NativeSampleTurboModuleNullableCxxSpecJSI *>(&turboModule)->getRootTag(
13731373
rt,
1374-
args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].getNumber())
1374+
args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].asNumber())
13751375
);
13761376
return result ? jsi::Value(std::move(*result)) : jsi::Value::null();
13771377
}
@@ -1477,7 +1477,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleNullableAndOptionalCxxSp
14771477
static jsi::Value __hostFunction_NativeSampleTurboModuleNullableAndOptionalCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
14781478
auto result = static_cast<NativeSampleTurboModuleNullableAndOptionalCxxSpecJSI *>(&turboModule)->getRootTag(
14791479
rt,
1480-
count <= 0 || args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].getNumber())
1480+
count <= 0 || args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].asNumber())
14811481
);
14821482
return result ? jsi::Value(std::move(*result)) : jsi::Value::null();
14831483
}
@@ -1577,7 +1577,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleOptionalCxxSpecJSI_getAl
15771577
static jsi::Value __hostFunction_NativeSampleTurboModuleOptionalCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
15781578
return static_cast<NativeSampleTurboModuleOptionalCxxSpecJSI *>(&turboModule)->getRootTag(
15791579
rt,
1580-
count <= 0 || args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].getNumber())
1580+
count <= 0 || args[0].isNull() || args[0].isUndefined() ? std::nullopt : std::make_optional(args[0].asNumber())
15811581
);
15821582
}
15831583
static jsi::Value __hostFunction_NativeSampleTurboModuleOptionalCxxSpecJSI_getValue(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {

packages/react-native-codegen/src/generators/modules/GenerateModuleCpp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function serializeArg(
147147
case 'ReservedTypeAnnotation':
148148
switch (realTypeAnnotation.name) {
149149
case 'RootTag':
150-
return wrap(val => `${val}.getNumber()`);
150+
return wrap(val => `${val}.asNumber()`);
151151
default:
152152
(realTypeAnnotation.name: empty);
153153
throw new Error(

packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getObject(jsi
559559
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getRootTag(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
560560
return static_cast<NativeSampleTurboModuleCxxSpecJSI *>(&turboModule)->getRootTag(
561561
rt,
562-
args[0].getNumber()
562+
args[0].asNumber()
563563
);
564564
}
565565
static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getValue(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {

0 commit comments

Comments
 (0)