Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/nitrogen/src/syntax/swift/SwiftEnum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public extension ${enumType.enumName} {
const cppEnumType = type.getCode('c++', { context: 'value' })
const enumMemberValues = enumType.enumMembers.map((member) => member.value)
const enumValues = [...new Set(enumMemberValues)]
const invalidValue = `throw std::invalid_argument("Invalid value for ${enumType.enumName}");`
let jsToSwiftConverterCode: string
let swiftToJSConverterCode: string
let jsCanConvertCode: string
Expand All @@ -102,12 +101,11 @@ public extension ${enumType.enumName} {
`case ${member.value}: return createJSIString<"${member.stringValue}">(runtime);`,
)
jsToSwiftConverterCode = `
if (!value.isString()) { ${invalidValue} }
const auto string = value.getString(runtime).utf8(runtime);
const auto string = value.asString(runtime).utf8(runtime);
switch (hashString(string)) {
${indent(fromJSICases.join('\n'), ' ')}
default: [[unlikely]]
${invalidValue}
throw std::invalid_argument("Cannot convert \\"" + string + "\\" to enum ${enumType.enumName} - invalid value!");
}
`.trim()
jsCanConvertCode = `
Expand Down Expand Up @@ -142,10 +140,11 @@ const auto integer = static_cast<int32_t>(number);
if (number != integer) { ${onInvalid} }
`.trim()
jsToSwiftConverterCode = `
${readInteger(invalidValue)}
const int integer = doubleToInt(value.asNumber());
switch (integer) {
${indent(fromJSICases.join('\n'), ' ')}
default: ${invalidValue}
default: [[unlikely]]
throw std::invalid_argument("Cannot convert " + value.toString(runtime).utf8(runtime) + " to enum ${enumType.enumName} - invalid value!");
}
`.trim()
swiftToJSConverterCode = 'return jsi::Value(value.getRawValue());'
Expand Down Expand Up @@ -202,6 +201,13 @@ return SwiftType::fromRawValue(rawValue);
space: 'system',
},
},
{
language: 'c++',
declaration: {
name: 'NitroModules/NumberUtils.hpp',
space: 'system',
},
},
{
language: 'c++',
declaration: {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/standalone/test-runner/js/arrays.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,13 @@ export const cases = {
);
},

"bounceEnums(...) rejects a number element": (testObject) => {
// @ts-expect-error passing a number instead of a Powertrain
expect(() => testObject.bounceEnums(["gas", 1])).to.throw(
`${testObject.constructor.name}.bounceEnums(...): Value is a number, expected a String`,
);
},

"bounceStructs(...) rejects an element with a missing field": (
testObject,
) => {
Expand Down
6 changes: 3 additions & 3 deletions tests/standalone/test-runner/js/callbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export const cases = {
expect(() =>
// @ts-expect-error intentionally omitting the argument
callback(),
).to.throw(exactly("Function expected 1 arguments, but received 0!"));
).to.throw(exactly("Callback expected 1 arguments, but received 0!"));
},

"getComplexCallback() function throws when called with too many arguments":
Expand All @@ -360,7 +360,7 @@ export const cases = {
expect(() =>
// @ts-expect-error intentionally passing an extra argument
callback(1, 2),
).to.throw(exactly("Function expected 1 arguments, but received 2!"));
).to.throw(exactly("Callback expected 1 arguments, but received 2!"));
},

// TODO: Fix HybridTestObjectSwift.callSumUpNTimes - its `1...Int(n)` range traps for n < 1, so callSumUpNTimes(callback, 0) crashes the app (SIGTRAP); C++ and Kotlin resolve to 0
Expand Down Expand Up @@ -627,7 +627,7 @@ export const cases = {
expect(() =>
// @ts-expect-error intentionally passing an extra argument
callback(1),
).to.throw(exactly("Function expected 0 arguments, but received 1!"));
).to.throw(exactly("Callback expected 0 arguments, but received 1!"));
},

"bounceSyncInt64Callback(...) returns the result of the JS callback": (
Expand Down
17 changes: 6 additions & 11 deletions tests/standalone/test-runner/js/hybrid-objects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,14 @@ export const cases = {
expect(setNumberValue).to.throw("dispose()");
},

"name after dispose() throws": (testObject) => {
"constructor and class name remain available after dispose()": (
testObject,
) => {
const object = testObject.newTestObject();
const constructor = object.constructor;
object.dispose();
expect(() => object.constructor.name).to.throw(
"Cannot get hybrid property `HybridObject.name`",
);
expect(object.constructor).to.equal(constructor);
expect(object.constructor.name).to.equal(testObject.constructor.name);
},

"toString() after dispose() throws": (testObject) => {
Expand Down Expand Up @@ -520,13 +522,6 @@ export const skips = {
{
release: "Existing main exclusion: IS_DEBUG",
},
"HybridObject dispose() / TestObjectCpp / name after dispose() throws": {
release: "Existing main exclusion: IS_DEBUG",
},
"HybridObject dispose() / TestObjectSwiftKotlin / name after dispose() throws":
{
release: "Existing main exclusion: IS_DEBUG",
},
"HybridObject dispose() / TestObjectCpp / toString() after dispose() throws":
{
release: "Existing main exclusion: IS_DEBUG",
Expand Down
Loading
Loading