@@ -83,6 +83,16 @@ pub enum InstantiationMode {
83
83
Sync ,
84
84
}
85
85
86
+ /// Internal Bindgen calling convention
87
+ enum CallType {
88
+ /// Standard calls - inner function is called directly with parameters
89
+ Standard ,
90
+ /// Exported resource method calls - this is passed as the first argument
91
+ FirstArgIsThis ,
92
+ /// Imported resource method calls - callee is a member of the parameter
93
+ CalleeResourceDispatch ,
94
+ }
95
+
86
96
struct JsBindgen < ' a > {
87
97
local_names : LocalNames ,
88
98
@@ -959,10 +969,9 @@ impl<'a> Instantiator<'a, '_> {
959
969
WorldItem :: Type ( _) => unreachable ! ( ) ,
960
970
} ;
961
971
962
- let callee_name = match func. kind {
963
- FunctionKind :: Freestanding => {
964
- let callee_name = self
965
- . gen
972
+ let ( callee_name, call_type) = match func. kind {
973
+ FunctionKind :: Freestanding => (
974
+ self . gen
966
975
. local_names
967
976
. get_or_create (
968
977
& format ! (
@@ -974,40 +983,39 @@ impl<'a> Instantiator<'a, '_> {
974
983
& func. name ,
975
984
)
976
985
. 0
977
- . to_string ( ) ;
978
- callee_name
979
- }
980
- FunctionKind :: Method ( ty) => format ! (
981
- "{}.prototype.{}.call" ,
982
- match & self . imports_resource_map[ & ty] . data {
983
- ResourceData :: Host { local_name, .. } => {
984
- self . gen . esm_bindgen. ensure_import_binding( local_name) ;
985
- local_name
986
- }
987
- ResourceData :: Guest { .. } => unreachable!( ) ,
988
- } ,
989
- func. item_name( ) . to_lower_camel_case( )
986
+ . to_string ( ) ,
987
+ CallType :: Standard ,
990
988
) ,
991
- FunctionKind :: Static ( ty) => format ! (
992
- "{}.{}" ,
993
- match & self . imports_resource_map[ & ty] . data {
994
- ResourceData :: Host { local_name, .. } => {
995
- self . gen . esm_bindgen. ensure_import_binding( local_name) ;
996
- local_name
997
- }
998
- ResourceData :: Guest { .. } => unreachable!( ) ,
999
- } ,
1000
- func. item_name( ) . to_lower_camel_case( )
989
+ FunctionKind :: Method ( _) => (
990
+ func. item_name ( ) . to_lower_camel_case ( ) ,
991
+ CallType :: CalleeResourceDispatch ,
1001
992
) ,
1002
- FunctionKind :: Constructor ( ty) => format ! (
1003
- "new {}" ,
1004
- match & self . imports_resource_map[ & ty] . data {
1005
- ResourceData :: Host { local_name, .. } => {
1006
- self . gen . esm_bindgen. ensure_import_binding( local_name) ;
1007
- local_name
1008
- }
1009
- ResourceData :: Guest { .. } => unreachable!( ) ,
1010
- } ,
993
+ FunctionKind :: Static ( ty) => (
994
+ format ! (
995
+ "{}.{}" ,
996
+ match & self . imports_resource_map[ & ty] . data {
997
+ ResourceData :: Host { local_name, .. } => {
998
+ self . gen . esm_bindgen. ensure_import_binding( local_name) ;
999
+ local_name
1000
+ }
1001
+ ResourceData :: Guest { .. } => unreachable!( ) ,
1002
+ } ,
1003
+ func. item_name( ) . to_lower_camel_case( )
1004
+ ) ,
1005
+ CallType :: Standard ,
1006
+ ) ,
1007
+ FunctionKind :: Constructor ( ty) => (
1008
+ format ! (
1009
+ "new {}" ,
1010
+ match & self . imports_resource_map[ & ty] . data {
1011
+ ResourceData :: Host { local_name, .. } => {
1012
+ self . gen . esm_bindgen. ensure_import_binding( local_name) ;
1013
+ local_name
1014
+ }
1015
+ ResourceData :: Guest { .. } => unreachable!( ) ,
1016
+ } ,
1017
+ ) ,
1018
+ CallType :: Standard ,
1011
1019
) ,
1012
1020
} ;
1013
1021
@@ -1020,7 +1028,7 @@ impl<'a> Instantiator<'a, '_> {
1020
1028
uwrite ! ( self . src. js, "\n function trampoline{}" , trampoline. as_u32( ) ) ;
1021
1029
self . bindgen (
1022
1030
nparams,
1023
- false ,
1031
+ call_type ,
1024
1032
if import_name. is_empty ( ) {
1025
1033
None
1026
1034
} else {
@@ -1290,7 +1298,7 @@ impl<'a> Instantiator<'a, '_> {
1290
1298
fn bindgen (
1291
1299
& mut self ,
1292
1300
nparams : usize ,
1293
- this_ref : bool ,
1301
+ call_type : CallType ,
1294
1302
module_name : Option < & str > ,
1295
1303
callee : & str ,
1296
1304
opts : & CanonicalOptions ,
@@ -1308,7 +1316,7 @@ impl<'a> Instantiator<'a, '_> {
1308
1316
let mut params = Vec :: new ( ) ;
1309
1317
let mut first = true ;
1310
1318
for i in 0 ..nparams {
1311
- if i == 0 && this_ref {
1319
+ if i == 0 && matches ! ( call_type , CallType :: FirstArgIsThis ) {
1312
1320
params. push ( "this" . into ( ) ) ;
1313
1321
continue ;
1314
1322
}
@@ -1377,6 +1385,7 @@ impl<'a> Instantiator<'a, '_> {
1377
1385
block_storage : Vec :: new ( ) ,
1378
1386
blocks : Vec :: new ( ) ,
1379
1387
callee,
1388
+ callee_resource_dynamic : matches ! ( call_type, CallType :: CalleeResourceDispatch ) ,
1380
1389
memory : memory. as_ref ( ) ,
1381
1390
realloc : realloc. as_ref ( ) ,
1382
1391
tmp : 0 ,
@@ -1706,7 +1715,10 @@ impl<'a> Instantiator<'a, '_> {
1706
1715
let callee = self . core_def ( def) ;
1707
1716
self . bindgen (
1708
1717
func. params . len ( ) ,
1709
- matches ! ( func. kind, FunctionKind :: Method ( _) ) ,
1718
+ match func. kind {
1719
+ FunctionKind :: Method ( _) => CallType :: FirstArgIsThis ,
1720
+ _ => CallType :: Standard ,
1721
+ } ,
1710
1722
if export_name. is_empty ( ) {
1711
1723
None
1712
1724
} else {
0 commit comments