@@ -521,10 +521,12 @@ impl ToTokens for ast::ImportType {
521
521
} ;
522
522
let const_name = format ! ( "__wbg_generated_const_{}" , name) ;
523
523
let const_name = Ident :: new ( & const_name, Span :: call_site ( ) ) ;
524
+ let instanceof_shim = Ident :: new ( & self . instanceof_shim , Span :: call_site ( ) ) ;
524
525
( quote ! {
525
526
#[ allow( bad_style) ]
526
527
#( #attrs) *
527
528
#[ doc = #doc_comment]
529
+ #[ repr( transparent) ]
528
530
#vis struct #name {
529
531
obj: :: wasm_bindgen:: JsValue ,
530
532
}
@@ -533,7 +535,7 @@ impl ToTokens for ast::ImportType {
533
535
const #const_name: ( ) = {
534
536
use wasm_bindgen:: convert:: { IntoWasmAbi , FromWasmAbi , Stack } ;
535
537
use wasm_bindgen:: convert:: { OptionIntoWasmAbi , OptionFromWasmAbi } ;
536
- use wasm_bindgen:: convert:: RefFromWasmAbi ;
538
+ use wasm_bindgen:: convert:: { RefFromWasmAbi , GlobalStack } ;
537
539
use wasm_bindgen:: describe:: WasmDescribe ;
538
540
use wasm_bindgen:: { JsValue , JsCast } ;
539
541
use wasm_bindgen:: __rt:: core:: mem:: ManuallyDrop ;
@@ -594,18 +596,63 @@ impl ToTokens for ast::ImportType {
594
596
}
595
597
}
596
598
599
+ // TODO: remove this on the next major version
597
600
impl From <JsValue > for #name {
598
601
fn from( obj: JsValue ) -> #name {
599
602
#name { obj }
600
603
}
601
604
}
602
605
606
+ impl AsRef <JsValue > for #name {
607
+ fn as_ref( & self ) -> & JsValue { & self . obj }
608
+ }
609
+
610
+ impl AsMut <JsValue > for #name {
611
+ fn as_mut( & mut self ) -> & mut JsValue { & mut self . obj }
612
+ }
613
+
603
614
impl From <#name> for JsValue {
604
615
fn from( obj: #name) -> JsValue {
605
616
obj. obj
606
617
}
607
618
}
608
619
620
+ impl JsCast for #name {
621
+ #[ cfg( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ]
622
+ fn instanceof( val: & JsValue ) -> bool {
623
+ #[ link( wasm_import_module = "__wbindgen_placeholder__" ) ]
624
+ extern {
625
+ fn #instanceof_shim( val: u32 ) -> u32 ;
626
+ }
627
+ unsafe {
628
+ let idx = val. into_abi( & mut GlobalStack :: new( ) ) ;
629
+ #instanceof_shim( idx) != 0
630
+ }
631
+ }
632
+
633
+ #[ cfg( not( all( target_arch = "wasm32" , not( target_os = "emscripten" ) ) ) ) ]
634
+ fn instanceof( val: & JsValue ) -> bool {
635
+ drop( val) ;
636
+ panic!( "cannot check instanceof on non-wasm targets" ) ;
637
+ }
638
+
639
+ fn unchecked_from_js( val: JsValue ) -> Self {
640
+ #name { obj: val }
641
+ }
642
+
643
+ fn unchecked_from_js_ref( val: & JsValue ) -> & Self {
644
+ // Should be safe because `#name` is a transparent
645
+ // wrapper around `val`
646
+ unsafe { & * ( val as * const JsValue as * const #name) }
647
+ }
648
+
649
+ fn unchecked_from_js_mut( val: & mut JsValue ) -> & mut Self {
650
+ // Should be safe because `#name` is a transparent
651
+ // wrapper around `val`
652
+ unsafe { & mut * ( val as * mut JsValue as * mut #name) }
653
+ }
654
+ }
655
+
609
656
( )
610
657
} ;
611
658
} ) . to_tokens ( tokens) ;
0 commit comments