1- use std:: ffi:: { c_char, c_void, CStr } ;
1+ use std:: ffi:: { c_char, c_void, CStr , CString } ;
22use std:: ptr:: null_mut;
33
4+ use anyhow:: Result ;
45use libquickjs_ng_sys as q;
56
67use super :: compile:: compile_module;
78
8- pub type JSModuleLoaderFunc = Box < dyn Fn ( & str , * mut c_void ) -> String > ;
9- pub type JSModuleNormalizeFunc = Box < dyn Fn ( & str , & str , * mut c_void ) -> String > ;
9+ /// Custom module loader function, passes (module_name, opaque) and returns module code
10+ /// If the module code is not found, return None
11+ pub type JSModuleLoaderFunc = Box < dyn Fn ( & str , * mut c_void ) -> Result < String > > ;
12+ /// Custom module normalize function, passes (module_base_name, module_name, opaque)
13+ /// and returns normalized module name (or None if not found)
14+ pub type JSModuleNormalizeFunc = Box < dyn Fn ( & str , & str , * mut c_void ) -> Result < String > > ;
1015
1116pub struct ModuleLoader {
1217 pub loader : JSModuleLoaderFunc ,
@@ -23,17 +28,22 @@ pub unsafe extern "C" fn js_module_loader(
2328 let opaque = wrapper. opaque ;
2429 let loader = & wrapper. loader ;
2530
26- let module_name = CStr :: from_ptr ( module_name) . to_str ( ) . unwrap ( ) ;
27- let module_code = loader ( module_name, opaque) ;
31+ let module_name = CStr :: from_ptr ( module_name) . to_string_lossy ( ) . to_string ( ) ;
32+ let module_code = match loader ( & module_name, opaque) {
33+ Ok ( v) => v,
34+ Err ( err) => {
35+ throw_internal_error ( ctx, & err. to_string ( ) ) ;
36+ return null_mut ( ) as * mut q:: JSModuleDef ;
37+ }
38+ } ;
2839
29- match compile_module ( ctx, & module_code, module_name) {
40+ match compile_module ( ctx, & module_code, & module_name) {
3041 Ok ( v) => {
3142 let module_def = q:: JS_Ext_GetPtr ( v. value ) ;
32- // q::JS_DupValue(wrapper.context, v.value);
3343 module_def as * mut q:: JSModuleDef
3444 }
3545 Err ( e) => {
36- eprintln ! ( "compile module error: {:?}" , e ) ;
46+ throw_internal_error ( ctx , & e . to_string ( ) ) ;
3747 null_mut ( ) as * mut q:: JSModuleDef
3848 }
3949 }
@@ -54,7 +64,13 @@ pub unsafe extern "C" fn js_module_normalize(
5464
5565 if let Some ( module_normalize_func) = normalize {
5666 let mut normalized_module_name =
57- module_normalize_func ( module_base_name, module_name, opaque) ;
67+ match module_normalize_func ( module_base_name, module_name, opaque) {
68+ Ok ( v) => v,
69+ Err ( err) => {
70+ throw_internal_error ( ctx, & err. to_string ( ) ) ;
71+ return null_mut ( ) as * mut c_char ;
72+ }
73+ } ;
5874 normalized_module_name. push ( '\0' ) ;
5975 let m = q:: js_malloc ( ctx, normalized_module_name. len ( ) ) ;
6076 std:: ptr:: copy (
@@ -64,7 +80,15 @@ pub unsafe extern "C" fn js_module_normalize(
6480 ) ;
6581 m as * mut c_char
6682 } else {
67- eprintln ! ( "module normalize func not set" ) ;
83+ log :: warn !( "module normalize func not set" ) ;
6884 null_mut ( ) as * mut c_char
6985 }
7086}
87+
88+ #[ inline]
89+ fn throw_internal_error ( ctx : * mut q:: JSContext , err : & str ) {
90+ let err = CString :: new ( err) . unwrap ( ) ;
91+ unsafe {
92+ q:: JS_ThrowInternalError ( ctx, err. as_ptr ( ) as * const i8 ) ;
93+ }
94+ }
0 commit comments