Skip to content

Commit

Permalink
feat: implement Into<JsValue> for IntoWasmAbi tsify types
Browse files Browse the repository at this point in the history
  • Loading branch information
Pantamis committed Apr 11, 2024
1 parent 5a10a97 commit 55f5a81
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 105 deletions.
57 changes: 30 additions & 27 deletions tests/expand/borrow.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ const _: () = {
extern crate serde as _serde;
use tsify_next::Tsify;
use wasm_bindgen::{
convert::{
FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi,
RefFromWasmAbi,
},
describe::WasmDescribe, prelude::*,
convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, RefFromWasmAbi},
describe::WasmDescribe,
prelude::*,
};
#[automatically_derived]
///
Expand All @@ -24,13 +22,13 @@ const _: () = {
}
#[automatically_derived]
const _: () = {
use wasm_bindgen::__rt::core;
use wasm_bindgen::convert::TryFromJsValue;
use wasm_bindgen::convert::{IntoWasmAbi, FromWasmAbi};
use wasm_bindgen::convert::{OptionIntoWasmAbi, OptionFromWasmAbi};
use wasm_bindgen::convert::{RefFromWasmAbi, LongRefFromWasmAbi};
use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi};
use wasm_bindgen::convert::{LongRefFromWasmAbi, RefFromWasmAbi};
use wasm_bindgen::convert::{OptionFromWasmAbi, OptionIntoWasmAbi};
use wasm_bindgen::describe::WasmDescribe;
use wasm_bindgen::{JsValue, JsCast, JsObject};
use wasm_bindgen::__rt::core;
use wasm_bindgen::{JsCast, JsObject, JsValue};
impl WasmDescribe for JsType {
fn describe() {
use wasm_bindgen::describe::*;
Expand Down Expand Up @@ -131,19 +129,13 @@ const _: () = {
}
impl JsCast for JsType {
fn instanceof(val: &JsValue) -> bool {
#[cfg(
not(
all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)
)
)]
#[cfg(not(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
unsafe fn __wbg_instanceof_JsType_1641ac20ec916ae7(_: u32) -> u32 {
{
::std::rt::begin_panic(
"cannot check instanceof on non-wasm targets",
);
::std::rt::begin_panic("cannot check instanceof on non-wasm targets");
};
}
unsafe {
Expand Down Expand Up @@ -172,12 +164,14 @@ const _: () = {
}
impl<'a> Tsify for Borrow<'a> {
type JsType = JsType;
const DECL: &'static str = "export interface Borrow {\n raw: string;\n cow: string;\n}";
const SERIALIZATION_CONFIG: tsify_next::SerializationConfig = tsify_next::SerializationConfig {
missing_as_null: false,
hashmap_as_object: false,
large_number_types_as_bigints: false,
};
const DECL: &'static str =
"export interface Borrow {\n raw: string;\n cow: string;\n}";
const SERIALIZATION_CONFIG: tsify_next::SerializationConfig =
tsify_next::SerializationConfig {
missing_as_null: false,
hashmap_as_object: false,
large_number_types_as_bigints: false,
};
}
impl<'a> WasmDescribe for Borrow<'a> {
#[inline]
Expand All @@ -204,6 +198,15 @@ const _: () = {
<JsType as OptionIntoWasmAbi>::none()
}
}
impl<'a> Into<JsValue> for Borrow<'a>
where
Self: _serde::Serialize,
{
#[inline]
fn into(self) -> JsValue {
self.into_js().unwrap_throw().into()
}
}
impl<'a> FromWasmAbi for Borrow<'a>
where
Self: _serde::de::DeserializeOwned,
Expand Down
54 changes: 28 additions & 26 deletions tests/expand/generic_enum.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ const _: () = {
extern crate serde as _serde;
use tsify_next::Tsify;
use wasm_bindgen::{
convert::{
FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi,
RefFromWasmAbi,
},
describe::WasmDescribe, prelude::*,
convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, RefFromWasmAbi},
describe::WasmDescribe,
prelude::*,
};
#[automatically_derived]
///
Expand All @@ -25,13 +23,13 @@ const _: () = {
}
#[automatically_derived]
const _: () = {
use wasm_bindgen::__rt::core;
use wasm_bindgen::convert::TryFromJsValue;
use wasm_bindgen::convert::{IntoWasmAbi, FromWasmAbi};
use wasm_bindgen::convert::{OptionIntoWasmAbi, OptionFromWasmAbi};
use wasm_bindgen::convert::{RefFromWasmAbi, LongRefFromWasmAbi};
use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi};
use wasm_bindgen::convert::{LongRefFromWasmAbi, RefFromWasmAbi};
use wasm_bindgen::convert::{OptionFromWasmAbi, OptionIntoWasmAbi};
use wasm_bindgen::describe::WasmDescribe;
use wasm_bindgen::{JsValue, JsCast, JsObject};
use wasm_bindgen::__rt::core;
use wasm_bindgen::{JsCast, JsObject, JsValue};
impl WasmDescribe for JsType {
fn describe() {
use wasm_bindgen::describe::*;
Expand Down Expand Up @@ -137,19 +135,13 @@ const _: () = {
}
impl JsCast for JsType {
fn instanceof(val: &JsValue) -> bool {
#[cfg(
not(
all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)
)
)]
#[cfg(not(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
unsafe fn __wbg_instanceof_JsType_1641ac20ec916ae7(_: u32) -> u32 {
{
::std::rt::begin_panic(
"cannot check instanceof on non-wasm targets",
);
::std::rt::begin_panic("cannot check instanceof on non-wasm targets");
};
}
unsafe {
Expand Down Expand Up @@ -179,11 +171,12 @@ const _: () = {
impl<T, U> Tsify for GenericEnum<T, U> {
type JsType = JsType;
const DECL: &'static str = "export type GenericEnum<T, U> = \"Unit\" | { NewType: T } | { Seq: [T, U] } | { Map: { x: T; y: U } };";
const SERIALIZATION_CONFIG: tsify_next::SerializationConfig = tsify_next::SerializationConfig {
missing_as_null: false,
hashmap_as_object: false,
large_number_types_as_bigints: false,
};
const SERIALIZATION_CONFIG: tsify_next::SerializationConfig =
tsify_next::SerializationConfig {
missing_as_null: false,
hashmap_as_object: false,
large_number_types_as_bigints: false,
};
}
impl<T, U> WasmDescribe for GenericEnum<T, U> {
#[inline]
Expand All @@ -210,6 +203,15 @@ const _: () = {
<JsType as OptionIntoWasmAbi>::none()
}
}
impl<T, U> Into<JsValue> for GenericEnum<T, U>
where
Self: _serde::Serialize,
{
#[inline]
fn into(self) -> JsValue {
self.into_js().unwrap_throw().into()
}
}
impl<T, U> FromWasmAbi for GenericEnum<T, U>
where
Self: _serde::de::DeserializeOwned,
Expand Down
108 changes: 56 additions & 52 deletions tests/expand/generic_struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ const _: () = {
extern crate serde as _serde;
use tsify_next::Tsify;
use wasm_bindgen::{
convert::{
FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi,
RefFromWasmAbi,
},
describe::WasmDescribe, prelude::*,
convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, RefFromWasmAbi},
describe::WasmDescribe,
prelude::*,
};
#[automatically_derived]
///
Expand All @@ -22,13 +20,13 @@ const _: () = {
}
#[automatically_derived]
const _: () = {
use wasm_bindgen::__rt::core;
use wasm_bindgen::convert::TryFromJsValue;
use wasm_bindgen::convert::{IntoWasmAbi, FromWasmAbi};
use wasm_bindgen::convert::{OptionIntoWasmAbi, OptionFromWasmAbi};
use wasm_bindgen::convert::{RefFromWasmAbi, LongRefFromWasmAbi};
use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi};
use wasm_bindgen::convert::{LongRefFromWasmAbi, RefFromWasmAbi};
use wasm_bindgen::convert::{OptionFromWasmAbi, OptionIntoWasmAbi};
use wasm_bindgen::describe::WasmDescribe;
use wasm_bindgen::{JsValue, JsCast, JsObject};
use wasm_bindgen::__rt::core;
use wasm_bindgen::{JsCast, JsObject, JsValue};
impl WasmDescribe for JsType {
fn describe() {
use wasm_bindgen::describe::*;
Expand Down Expand Up @@ -136,19 +134,13 @@ const _: () = {
}
impl JsCast for JsType {
fn instanceof(val: &JsValue) -> bool {
#[cfg(
not(
all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)
)
)]
#[cfg(not(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
unsafe fn __wbg_instanceof_JsType_1641ac20ec916ae7(_: u32) -> u32 {
{
::std::rt::begin_panic(
"cannot check instanceof on non-wasm targets",
);
::std::rt::begin_panic("cannot check instanceof on non-wasm targets");
};
}
unsafe {
Expand Down Expand Up @@ -178,11 +170,12 @@ const _: () = {
impl<T> Tsify for GenericStruct<T> {
type JsType = JsType;
const DECL: &'static str = "export interface GenericStruct<T> {\n x: T;\n}";
const SERIALIZATION_CONFIG: tsify_next::SerializationConfig = tsify_next::SerializationConfig {
missing_as_null: false,
hashmap_as_object: false,
large_number_types_as_bigints: false,
};
const SERIALIZATION_CONFIG: tsify_next::SerializationConfig =
tsify_next::SerializationConfig {
missing_as_null: false,
hashmap_as_object: false,
large_number_types_as_bigints: false,
};
}
impl<T> WasmDescribe for GenericStruct<T> {
#[inline]
Expand All @@ -209,6 +202,15 @@ const _: () = {
<JsType as OptionIntoWasmAbi>::none()
}
}
impl<T> Into<JsValue> for GenericStruct<T>
where
Self: _serde::Serialize,
{
#[inline]
fn into(self) -> JsValue {
self.into_js().unwrap_throw().into()
}
}
impl<T> FromWasmAbi for GenericStruct<T>
where
Self: _serde::de::DeserializeOwned,
Expand Down Expand Up @@ -257,11 +259,9 @@ const _: () = {
extern crate serde as _serde;
use tsify_next::Tsify;
use wasm_bindgen::{
convert::{
FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi,
RefFromWasmAbi,
},
describe::WasmDescribe, prelude::*,
convert::{FromWasmAbi, IntoWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, RefFromWasmAbi},
describe::WasmDescribe,
prelude::*,
};
#[automatically_derived]
///
Expand All @@ -271,13 +271,13 @@ const _: () = {
}
#[automatically_derived]
const _: () = {
use wasm_bindgen::__rt::core;
use wasm_bindgen::convert::TryFromJsValue;
use wasm_bindgen::convert::{IntoWasmAbi, FromWasmAbi};
use wasm_bindgen::convert::{OptionIntoWasmAbi, OptionFromWasmAbi};
use wasm_bindgen::convert::{RefFromWasmAbi, LongRefFromWasmAbi};
use wasm_bindgen::convert::{FromWasmAbi, IntoWasmAbi};
use wasm_bindgen::convert::{LongRefFromWasmAbi, RefFromWasmAbi};
use wasm_bindgen::convert::{OptionFromWasmAbi, OptionIntoWasmAbi};
use wasm_bindgen::describe::WasmDescribe;
use wasm_bindgen::{JsValue, JsCast, JsObject};
use wasm_bindgen::__rt::core;
use wasm_bindgen::{JsCast, JsObject, JsValue};
impl WasmDescribe for JsType {
fn describe() {
use wasm_bindgen::describe::*;
Expand Down Expand Up @@ -386,19 +386,13 @@ const _: () = {
}
impl JsCast for JsType {
fn instanceof(val: &JsValue) -> bool {
#[cfg(
not(
all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)
)
)]
#[cfg(not(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)))]
unsafe fn __wbg_instanceof_JsType_1641ac20ec916ae7(_: u32) -> u32 {
{
::std::rt::begin_panic(
"cannot check instanceof on non-wasm targets",
);
::std::rt::begin_panic("cannot check instanceof on non-wasm targets");
};
}
unsafe {
Expand Down Expand Up @@ -428,11 +422,12 @@ const _: () = {
impl<T> Tsify for GenericNewtype<T> {
type JsType = JsType;
const DECL: &'static str = "export type GenericNewtype<T> = T;";
const SERIALIZATION_CONFIG: tsify_next::SerializationConfig = tsify_next::SerializationConfig {
missing_as_null: false,
hashmap_as_object: false,
large_number_types_as_bigints: false,
};
const SERIALIZATION_CONFIG: tsify_next::SerializationConfig =
tsify_next::SerializationConfig {
missing_as_null: false,
hashmap_as_object: false,
large_number_types_as_bigints: false,
};
}
impl<T> WasmDescribe for GenericNewtype<T> {
#[inline]
Expand All @@ -459,6 +454,15 @@ const _: () = {
<JsType as OptionIntoWasmAbi>::none()
}
}
impl<T> Into<JsValue> for GenericNewtype<T>
where
Self: _serde::Serialize,
{
#[inline]
fn into(self) -> JsValue {
self.into_js().unwrap_throw().into()
}
}
impl<T> FromWasmAbi for GenericNewtype<T>
where
Self: _serde::de::DeserializeOwned,
Expand Down
7 changes: 7 additions & 0 deletions tsify-next-macros/src/wasm_bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ fn expand_into_wasm_abi(cont: &Container) -> TokenStream {
<JsType as OptionIntoWasmAbi>::none()
}
}

impl #impl_generics Into<JsValue> for #ident #ty_generics #where_clause {
#[inline]
fn into(self) -> JsValue {
self.into_js().unwrap_throw().into()
}
}
}
}

Expand Down

0 comments on commit 55f5a81

Please sign in to comment.