Skip to content

Commit 910187f

Browse files
authored
Add ref t into wasm (#63)
Derive IntoWasmAbi for &T Previously `IntoWasmAbi T` was derived, but not `IntoWasmAbi &T`
1 parent 3955491 commit 910187f

File tree

8 files changed

+112
-10
lines changed

8 files changed

+112
-10
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* tslint:disable */
2+
/* eslint-disable */
3+
export function accept_ref_point(point: Point): void;
4+
export interface Point {
5+
x: number;
6+
y: number;
7+
}
8+

tests-e2e/test5/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "test5"
3+
publish = false
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
[dependencies]
8+
wasm-bindgen = "0.2"
9+
tsify = { path = "../..", version = "*" }
10+
serde = { version = "1.0", features = ["derive"] }
11+
serde_json = "1.0"
12+
13+
[dev-dependencies]
14+
wasm-bindgen-test = "0.3"
15+
16+
[lib]
17+
path = "entry_point.rs"
18+
crate-type = ["cdylib"]
19+
20+
[build-dependencies]
21+
wasm-bindgen-cli = "0.2"

tests-e2e/test5/entry_point.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use serde::{Deserialize, Serialize};
2+
use tsify::Tsify;
3+
use wasm_bindgen::prelude::*;
4+
5+
#[derive(Tsify, Serialize, Deserialize)]
6+
#[tsify(into_wasm_abi, from_wasm_abi)]
7+
pub struct Point {
8+
x: i32,
9+
y: i32,
10+
}
11+
12+
#[wasm_bindgen]
13+
extern "C" {
14+
#[wasm_bindgen]
15+
pub fn call_js(point: &Point);
16+
}
17+
18+
#[wasm_bindgen]
19+
pub fn accept_ref_point(point: &Point) {}

tests/expand/borrow.expanded.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const _: () = {
4747
}
4848
}
4949
#[automatically_derived]
50-
impl<'a> IntoWasmAbi for Borrow<'a>
50+
impl<'a> IntoWasmAbi for &Borrow<'a>
5151
where
5252
Borrow<'a>: _serde::Serialize,
5353
{
@@ -85,6 +85,17 @@ const _: () = {
8585
}
8686
}
8787
#[automatically_derived]
88+
impl<'a> IntoWasmAbi for Borrow<'a>
89+
where
90+
Borrow<'a>: _serde::Serialize,
91+
{
92+
type Abi = <JsType as IntoWasmAbi>::Abi;
93+
#[inline]
94+
fn into_abi(self) -> Self::Abi {
95+
(&self).into_abi()
96+
}
97+
}
98+
#[automatically_derived]
8899
impl<'a> OptionIntoWasmAbi for Borrow<'a>
89100
where
90101
Borrow<'a>: _serde::Serialize,

tests/expand/generic_enum.expanded.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const _: () = {
4848
}
4949
}
5050
#[automatically_derived]
51-
impl<T, U> IntoWasmAbi for GenericEnum<T, U>
51+
impl<T, U> IntoWasmAbi for &GenericEnum<T, U>
5252
where
5353
GenericEnum<T, U>: _serde::Serialize,
5454
{
@@ -86,6 +86,17 @@ const _: () = {
8686
}
8787
}
8888
#[automatically_derived]
89+
impl<T, U> IntoWasmAbi for GenericEnum<T, U>
90+
where
91+
GenericEnum<T, U>: _serde::Serialize,
92+
{
93+
type Abi = <JsType as IntoWasmAbi>::Abi;
94+
#[inline]
95+
fn into_abi(self) -> Self::Abi {
96+
(&self).into_abi()
97+
}
98+
}
99+
#[automatically_derived]
89100
impl<T, U> OptionIntoWasmAbi for GenericEnum<T, U>
90101
where
91102
GenericEnum<T, U>: _serde::Serialize,

tests/expand/generic_struct.expanded.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const _: () = {
4545
}
4646
}
4747
#[automatically_derived]
48-
impl<T> IntoWasmAbi for GenericStruct<T>
48+
impl<T> IntoWasmAbi for &GenericStruct<T>
4949
where
5050
GenericStruct<T>: _serde::Serialize,
5151
{
@@ -83,6 +83,17 @@ const _: () = {
8383
}
8484
}
8585
#[automatically_derived]
86+
impl<T> IntoWasmAbi for GenericStruct<T>
87+
where
88+
GenericStruct<T>: _serde::Serialize,
89+
{
90+
type Abi = <JsType as IntoWasmAbi>::Abi;
91+
#[inline]
92+
fn into_abi(self) -> Self::Abi {
93+
(&self).into_abi()
94+
}
95+
}
96+
#[automatically_derived]
8697
impl<T> OptionIntoWasmAbi for GenericStruct<T>
8798
where
8899
GenericStruct<T>: _serde::Serialize,
@@ -284,7 +295,7 @@ const _: () = {
284295
}
285296
}
286297
#[automatically_derived]
287-
impl<T> IntoWasmAbi for GenericNewtype<T>
298+
impl<T> IntoWasmAbi for &GenericNewtype<T>
288299
where
289300
GenericNewtype<T>: _serde::Serialize,
290301
{
@@ -322,6 +333,17 @@ const _: () = {
322333
}
323334
}
324335
#[automatically_derived]
336+
impl<T> IntoWasmAbi for GenericNewtype<T>
337+
where
338+
GenericNewtype<T>: _serde::Serialize,
339+
{
340+
type Abi = <JsType as IntoWasmAbi>::Abi;
341+
#[inline]
342+
fn into_abi(self) -> Self::Abi {
343+
(&self).into_abi()
344+
}
345+
}
346+
#[automatically_derived]
325347
impl<T> OptionIntoWasmAbi for GenericNewtype<T>
326348
where
327349
GenericNewtype<T>: _serde::Serialize,

tests/wasm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ struct SimpleData {
3131

3232
#[wasm_bindgen(inline_js = r#"
3333
function validate(value, validation) {
34-
validation(value);
34+
validation(value);
3535
3636
// Validate twice to make sure the value is not moved in any way to rust
37-
validation(value);
37+
validation(value);
3838
}
3939
4040
function validateArray(value, validation) {
4141
validation(value);
4242
4343
// Validate twice to make sure the value is not moved in any way to rust
44-
validation(value);
44+
validation(value);
4545
}
4646
4747
function noop(value) {}
@@ -59,7 +59,7 @@ extern "C" {
5959

6060
#[wasm_bindgen(catch, js_name = "validate")]
6161
pub fn validate_simple_data_ref(
62-
value: SimpleData,
62+
value: &SimpleData,
6363
validation: &dyn Fn(&SimpleData),
6464
) -> Result<(), JsValue>;
6565

@@ -98,7 +98,7 @@ fn test_convert_simple_value_type() {
9898
})
9999
.unwrap_throw();
100100

101-
validate_simple_data_ref(first_value.clone(), &|val_after| {
101+
validate_simple_data_ref(&first_value, &|val_after| {
102102
assert_eq!(val_after, &first_value);
103103
})
104104
.unwrap_throw();

tsify-macros/src/wasm_bindgen.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn expand_into_wasm_abi(cont: &Container) -> TokenStream {
107107

108108
quote! {
109109
#[automatically_derived]
110-
impl #impl_generics IntoWasmAbi for #ident #ty_generics #where_clause {
110+
impl #impl_generics IntoWasmAbi for &#ident #ty_generics #where_clause {
111111
type Abi = <JsType as IntoWasmAbi>::Abi;
112112

113113
#[inline]
@@ -129,6 +129,16 @@ fn expand_into_wasm_abi(cont: &Container) -> TokenStream {
129129
}
130130
}
131131

132+
#[automatically_derived]
133+
impl #impl_generics IntoWasmAbi for #ident #ty_generics #where_clause {
134+
type Abi = <JsType as IntoWasmAbi>::Abi;
135+
136+
#[inline]
137+
fn into_abi(self) -> Self::Abi {
138+
(&self).into_abi()
139+
}
140+
}
141+
132142
#[automatically_derived]
133143
impl #impl_generics OptionIntoWasmAbi for #ident #ty_generics #where_clause {
134144
#[inline]

0 commit comments

Comments
 (0)