@@ -16,6 +16,7 @@ use crate::Local;
1616use crate :: Message ;
1717use crate :: Module ;
1818use crate :: ModuleRequest ;
19+ use crate :: Object ;
1920use crate :: String ;
2021use crate :: UnboundModuleScript ;
2122use crate :: Value ;
@@ -146,6 +147,64 @@ where
146147 }
147148}
148149
150+ // System V ABI
151+ #[ cfg( not( target_os = "windows" ) ) ]
152+ #[ repr( C ) ]
153+ pub struct ResolveSourceCallbackRet ( * const Object ) ;
154+
155+ #[ cfg( not( target_os = "windows" ) ) ]
156+ pub type ResolveSourceCallback < ' a > = extern "C" fn (
157+ Local < ' a , Context > ,
158+ Local < ' a , String > ,
159+ Local < ' a , FixedArray > ,
160+ Local < ' a , Module > ,
161+ ) -> ResolveSourceCallbackRet ;
162+
163+ // Windows x64 ABI: Local<Module> returned on the stack.
164+ #[ cfg( target_os = "windows" ) ]
165+ pub type ResolveSourceCallback < ' a > = extern "C" fn (
166+ * mut * const Object ,
167+ Local < ' a , Context > ,
168+ Local < ' a , String > ,
169+ Local < ' a , FixedArray > ,
170+ Local < ' a , Module > ,
171+ ) -> * mut * const Object ;
172+
173+ impl < ' a , F > MapFnFrom < F > for ResolveSourceCallback < ' a >
174+ where
175+ F : UnitType
176+ + Fn (
177+ Local < ' a , Context > ,
178+ Local < ' a , String > ,
179+ Local < ' a , FixedArray > ,
180+ Local < ' a , Module > ,
181+ ) -> Option < Local < ' a , Object > > ,
182+ {
183+ #[ cfg( not( target_os = "windows" ) ) ]
184+ fn mapping ( ) -> Self {
185+ let f = |context, specifier, import_attributes, referrer| {
186+ ResolveSourceCallbackRet (
187+ ( F :: get ( ) ) ( context, specifier, import_attributes, referrer)
188+ . map ( |r| -> * const Object { & * r } )
189+ . unwrap_or ( null ( ) ) ,
190+ )
191+ } ;
192+ f. to_c_fn ( )
193+ }
194+
195+ #[ cfg( target_os = "windows" ) ]
196+ fn mapping ( ) -> Self {
197+ let f = |ret_ptr, context, specifier, import_attributes, referrer| {
198+ let r = ( F :: get ( ) ) ( context, specifier, import_attributes, referrer)
199+ . map ( |r| -> * const Object { & * r } )
200+ . unwrap_or ( null ( ) ) ;
201+ unsafe { std:: ptr:: write ( ret_ptr, r) } ; // Write result to stack.
202+ ret_ptr // Return stack pointer to the return value.
203+ } ;
204+ f. to_c_fn ( )
205+ }
206+ }
207+
149208extern "C" {
150209 fn v8__Module__GetStatus ( this : * const Module ) -> ModuleStatus ;
151210 fn v8__Module__GetException ( this : * const Module ) -> * const Value ;
@@ -162,6 +221,7 @@ extern "C" {
162221 this : * const Module ,
163222 context : * const Context ,
164223 cb : ResolveModuleCallback ,
224+ source_callback : Option < ResolveSourceCallback > ,
165225 ) -> MaybeBool ;
166226 fn v8__Module__Evaluate (
167227 this : * const Module ,
@@ -323,6 +383,31 @@ impl Module {
323383 self ,
324384 & * scope. get_current_context ( ) ,
325385 callback. map_fn_to ( ) ,
386+ None ,
387+ )
388+ }
389+ . into ( )
390+ }
391+
392+ /// Instantiates the module and its dependencies.
393+ ///
394+ /// Returns an empty Maybe<bool> if an exception occurred during
395+ /// instantiation. (In the case where the callback throws an exception, that
396+ /// exception is propagated.)
397+ #[ must_use]
398+ #[ inline( always) ]
399+ pub fn instantiate_module2 < ' a > (
400+ & self ,
401+ scope : & mut HandleScope ,
402+ callback : impl MapFnTo < ResolveModuleCallback < ' a > > ,
403+ source_callback : impl MapFnTo < ResolveSourceCallback < ' a > > ,
404+ ) -> Option < bool > {
405+ unsafe {
406+ v8__Module__InstantiateModule (
407+ self ,
408+ & * scope. get_current_context ( ) ,
409+ callback. map_fn_to ( ) ,
410+ Some ( source_callback. map_fn_to ( ) ) ,
326411 )
327412 }
328413 . into ( )
0 commit comments