@@ -687,5 +687,53 @@ pub mod __rt {
687
687
}
688
688
}
689
689
690
- pub fn link_this_library ( ) { }
690
+ /// This is a curious function necessary to get wasm-bindgen working today,
691
+ /// and it's a bit of an unfortunate hack.
692
+ ///
693
+ /// The general problem is that somehow we need the above two symbols to
694
+ /// exist in the final output binary (__wbindgen_malloc and
695
+ /// __wbindgen_free). These symbols may be called by JS for various
696
+ /// bindings, so we for sure need to make sure they're exported.
697
+ ///
698
+ /// The problem arises, though, when what if no Rust code uses the symbols?
699
+ /// For all intents and purposes it looks to LLVM and the linker like the
700
+ /// above two symbols are dead code, so they're completely discarded!
701
+ ///
702
+ /// Specifically what happens is this:
703
+ ///
704
+ /// * The above two symbols are generated into some object file inside of
705
+ /// libwasm_bindgen.rlib
706
+ /// * The linker, LLD, will not load this object file unless *some* symbol
707
+ /// is loaded from the object. In this case, if the Rust code never calls
708
+ /// __wbindgen_malloc or __wbindgen_free then the symbols never get linked
709
+ /// in.
710
+ /// * Later when `wasm-bindgen` attempts to use the symbols they don't
711
+ /// exist, causing an error.
712
+ ///
713
+ /// This function is a weird hack for this problem. We inject a call to this
714
+ /// function in all generated code. Usage of this function should then
715
+ /// ensure that the above two intrinsics are translated.
716
+ ///
717
+ /// Due to how rustc creates object files this function (and anything inside
718
+ /// it) will be placed into the same object file as the two intrinsics
719
+ /// above. That means if this function is called and referenced we'll pull
720
+ /// in the object file and link the intrinsics.
721
+ ///
722
+ /// Note that this is an #[inline] function to remove the function call
723
+ /// overhead we inject in functions, but right now it's unclear how to do
724
+ /// this in a zero-cost fashion. The lowest cost seems to be generating a
725
+ /// store that can't be optimized away (to a global), which is listed below.
726
+ ///
727
+ /// Ideas for how to improve this are most welcome!
728
+ #[ inline]
729
+ pub fn link_mem_intrinsics ( ) {
730
+ // the above symbols only exist with the `std` feature enabled.
731
+ if !cfg ! ( feature = "std" ) {
732
+ return
733
+ }
734
+
735
+ use core:: sync:: atomic:: * ;
736
+ static FOO : AtomicUsize = ATOMIC_USIZE_INIT ;
737
+ FOO . store ( 0 , Ordering :: SeqCst ) ;
738
+ }
691
739
}
0 commit comments