@@ -197,6 +197,16 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
197197 isolate .enter ();
198198 errdefer isolate .exit ();
199199
200+ isolate .setHostInitializeImportMetaObjectCallback (struct {
201+ fn callback (c_context : ? * v8.C_Context , c_module : ? * v8.C_Module , c_meta : ? * v8.C_Value ) callconv (.C ) void {
202+ const v8_context = v8.Context {.handle = c_context .? };
203+ const js_context : * JsContext = @ptrFromInt (v8_context .getEmbedderData (1 ).castTo (v8 .BigInt ).getUint64 ());
204+ js_context .initializeImportMeta (v8.Module {.handle = c_module .? }, v8.Object {.handle = c_meta .? }) catch | err | {
205+ log .err (.js , "import meta" , .{ .err = err });
206+ };
207+ }
208+ }.callback );
209+
200210 var temp_scope : v8.HandleScope = undefined ;
201211 v8 .HandleScope .init (& temp_scope , isolate );
202212 defer temp_scope .deinit ();
@@ -726,13 +736,15 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
726736
727737 fn moduleNoCache (self : * JsContext , src : []const u8 , url : []const u8 ) ! void {
728738 const m = try compileModule (self .isolate , src , url );
739+
740+ const arena = self .context_arena ;
741+ const owned_url = try arena .dupe (u8 , url );
742+ try self .module_identifier .putNoClobber (arena , m .getIdentityHash (), owned_url );
743+
729744 const v8_context = self .v8_context ;
730745 if (try m .instantiate (v8_context , resolveModuleCallback ) == false ) {
731746 return error .ModuleInstantiationError ;
732747 }
733- const arena = self .context_arena ;
734- const owned_url = try arena .dupe (u8 , url );
735- try self .module_identifier .putNoClobber (arena , m .getIdentityHash (), owned_url );
736748 _ = try m .evaluate (v8_context );
737749 }
738750
@@ -1279,6 +1291,20 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
12791291 return self .createException (js_value );
12801292 }
12811293
1294+ fn initializeImportMeta (self : * JsContext , m : v8.Module , meta : v8.Object ) ! void {
1295+ const url = self .module_identifier .get (m .getIdentityHash ()) orelse {
1296+ // Shouldn't be possible.
1297+ return error .UnknownModuleReferrer ;
1298+ };
1299+
1300+ const js_key = v8 .String .initUtf8 (self .isolate , "url" );
1301+ const js_value = try self .zigValueToJs (url );
1302+ const res = meta .defineOwnProperty (self .v8_context , js_key .toName (), js_value , 0 ) orelse false ;
1303+ if (! res ) {
1304+ return error .FailedToSet ;
1305+ }
1306+ }
1307+
12821308 // Callback from V8, asking us to load a module. The "specifier" is
12831309 // the src of the module to load.
12841310 fn resolveModuleCallback (
@@ -1335,7 +1361,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
13351361 try_catch .init (self );
13361362 defer try_catch .deinit ();
13371363
1338- const m = compileModule (self .isolate , source , specifier ) catch | err | {
1364+ const m = compileModule (self .isolate , source , normalized_specifier ) catch | err | {
13391365 log .warn (.js , "compile resolved module" , .{
13401366 .specifier = specifier ,
13411367 .stack = try_catch .stack (self .call_arena ) catch null ,
0 commit comments