Skip to content

Commit e23c552

Browse files
authored
Fix lots of minor type errors (#14)
* fix: address type errors in platform lookup * fix: address type errors in define(...) call type assertion The define(...) call types say a `number` is returned for u64s, when they should actually be `bigint`. Since this obviously won't work (a u64 is bigger than the largest `Number.MAX_SAFE_INTEGER`), I'm pretty sure this has got to be an error in the typings. I think this is probably worth digging more into at some point (ie, is this actually a thing?), because if it isn't an error in the typings, then it's possible there are cases where there would be data loss sometimes due to truncation. * fix: address s/listError/liftError/ typo * feat: make liftError return a [enumname, variant] pair so that it can be fed into UniffiError I think I must be missing something from the react native bindgen, I think it handles this in a much more elegant way where the error lift step actually returns an Error subclass. Anyway, this is also something else to look into longer term. * fix: add back in missing asyncopts * fix: ignore FinalizationRegistry type errors This isn't defined on all platforms and typescript complains, however there's a guard for this in the code so the type error produced here isn't all that actionable.
1 parent fd98333 commit e23c552

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

templates/macros.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
{{ arg.name() | typescript_var_name }}: {{ arg | typescript_type_name }}
55
{%- if !loop.last %}, {% endif -%}
66
{%- endfor -%}
7+
{%- if func.is_async() -%}
8+
{%- if !func.arguments().is_empty() %}, {% endif -%}
9+
asyncOpts_?: { signal: AbortSignal }
10+
{%- endif -%}
711
{%- endmacro %}
812

913
{%- macro docstring(defn, indent_level) %}

templates/node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
{%- match func_def.throws_type() -%}
130130
{%- when Some(err) -%}
131131
uniffiCaller.rustCallWithError(
132-
/*liftError:*/ {{err | typescript_ffi_converter_name}}.lift.bind({{err | typescript_ffi_converter_name}}),
132+
/*liftError:*/ (buffer) => ["{{err | typescript_type_name}}", {{err | typescript_ffi_converter_name}}.lift(buffer)],
133133
/*caller:*/ (callStatus) => {
134134
{%- else -%}
135135
uniffiCaller.rustCall(
@@ -436,8 +436,8 @@ const {{ object_def.name() | typescript_ffi_object_factory_name }}: UniffiObject
436436
(() => {
437437
/// <reference lib="es2021" />
438438
const registry =
439-
typeof globalThis.FinalizationRegistry !== 'undefined'
440-
? new globalThis.FinalizationRegistry<UnsafeMutableRawPointer>(
439+
typeof (globalThis as any).FinalizationRegistry !== 'undefined'
440+
? new (globalThis as any).FinalizationRegistry(
441441
(heldValue: UnsafeMutableRawPointer) => {
442442
{{ object_def.name() | typescript_ffi_object_factory_name }}.freePointer(heldValue);
443443
}

templates/sys.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
type UniffiByteArray,
2222
UniffiInternalError,
2323
uniffiCreateFfiConverterString,
24+
UniffiError,
2425
} from 'uniffi-bindgen-react-native';
2526

2627
const CALL_SUCCESS = 0, CALL_ERROR = 1, CALL_UNEXPECTED_ERROR = 2, CALL_CANCELLED = 3;
@@ -34,7 +35,7 @@ let libraryLoaded = false;
3435
function _uniffiLoad() {
3536
const library = "lib{{ ci.crate_name() }}";
3637
const { platform } = process;
37-
let ext = { darwin: "dylib", win32: "dll", linux: "so" }[platform];
38+
let ext = { darwin: "dylib", win32: "dll", linux: "so" }[platform as string];
3839
if (!ext) {
3940
console.warn("Unsupported platform:", platform);
4041
ext = "so";
@@ -93,8 +94,8 @@ class UniffiFfiRsRustCaller {
9394
return this.makeRustCall(caller, liftString);
9495
}
9596

96-
rustCallWithError<T>(
97-
liftError: (buffer: UniffiByteArray) => Error,
97+
rustCallWithError<T, ErrorEnumAndVariant extends [string, string]>(
98+
liftError: (buffer: UniffiByteArray) => ErrorEnumAndVariant,
9899
caller: (status: JsExternal) => T,
99100
liftString: (bytes: UniffiByteArray) => string,
100101
): T {
@@ -124,10 +125,10 @@ class UniffiFfiRsRustCaller {
124125
// return status;
125126
}
126127

127-
makeRustCall<T>(
128+
makeRustCall<T, ErrorEnumAndVariant extends [string, string]>(
128129
caller: (status: JsExternal) => T,
129130
liftString: (bytes: UniffiByteArray) => string,
130-
liftError?: (buffer: UniffiByteArray) => Error,
131+
liftError?: (buffer: UniffiByteArray) => ErrorEnumAndVariant,
131132
): T {
132133
_checkUniffiLoaded();
133134

@@ -144,10 +145,10 @@ class UniffiFfiRsRustCaller {
144145
}
145146
}
146147

147-
function uniffiCheckCallStatus(
148+
function uniffiCheckCallStatus<ErrorEnumAndVariant extends [string, string]>(
148149
callStatus: UniffiRustCallStatusStruct,
149150
liftString: (bytes: UniffiByteArray) => string,
150-
listError?: (buffer: UniffiByteArray) => Error,
151+
liftError?: (buffer: UniffiByteArray) => [string, string],
151152
) {
152153
switch (callStatus.code) {
153154
case CALL_SUCCESS:
@@ -160,8 +161,9 @@ function uniffiCheckCallStatus(
160161
const struct = new UniffiRustBufferValue(callStatus.error_buf);
161162
const errorBufBytes = struct.consumeIntoUint8Array();
162163

163-
if (listError) {
164-
throw listError(errorBufBytes);
164+
if (liftError) {
165+
const [enumName, errorVariant] = liftError(errorBufBytes);
166+
throw new UniffiError(enumName, errorVariant);
165167
}
166168
}
167169
throw new UniffiInternalError.UnexpectedRustCallError();
@@ -424,7 +426,7 @@ const FFI_DYNAMIC_LIB = define({
424426
{%- endmatch %}
425427

426428
{%- endfor %}
427-
}) as {
429+
}) as unknown as {
428430
{%- for definition in ci.ffi_definitions() %}
429431
{%- match definition %}
430432

0 commit comments

Comments
 (0)