Skip to content

Commit 287df42

Browse files
committed
log module specifier on dynamic import stages
1 parent 06e514c commit 287df42

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

src/runtime/js.zig

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -767,9 +767,8 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
767767
pub fn module(self: *JsContext, src: []const u8, url: []const u8, cacheable: bool) !?v8.Promise {
768768
const arena = self.context_arena;
769769

770-
if (cacheable) {
771-
const value = self.module_cache.get(url);
772-
if (value != null) return null;
770+
if (cacheable and self.module_cache.contains(url)) {
771+
return null;
773772
}
774773
errdefer _ = self.module_cache.remove(url);
775774

@@ -780,7 +779,11 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
780779
errdefer _ = self.module_identifier.remove(m.getIdentityHash());
781780

782781
if (cacheable) {
783-
try self.module_cache.put(arena, owned_url, PersistentModule.init(self.isolate, m));
782+
try self.module_cache.putNoClobber(
783+
arena,
784+
owned_url,
785+
PersistentModule.init(self.isolate, m),
786+
);
784787
}
785788

786789
// resolveModuleCallback loads module's dependencies.
@@ -1552,14 +1555,14 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
15521555
}
15531556

15541557
fn _dynamicModuleCallback(
1555-
context: *JsContext,
1558+
self: *JsContext,
15561559
specifier: []const u8,
15571560
resolver: *const v8.PromiseResolver,
15581561
) !void {
1559-
const iso = context.isolate;
1560-
const ctx = context.v8_context;
1562+
const iso = self.isolate;
1563+
const ctx = self.v8_context;
15611564

1562-
const module_loader = context.module_loader;
1565+
const module_loader = self.module_loader;
15631566
const source = module_loader.func(module_loader.ptr, specifier) catch {
15641567
const error_msg = v8.String.initUtf8(iso, "Failed to load module");
15651568
_ = resolver.reject(ctx, error_msg.toValue());
@@ -1571,34 +1574,36 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
15711574
};
15721575

15731576
var try_catch: TryCatch = undefined;
1574-
try_catch.init(context);
1577+
try_catch.init(self);
15751578
defer try_catch.deinit();
15761579

1577-
const maybe_promise = context.module(source, specifier, true) catch {
1580+
const maybe_promise = self.module(source, specifier, true) catch {
15781581
log.err(.js, "module compilation failed", .{
15791582
.specifier = specifier,
1580-
.exception = try_catch.exception(context.call_arena) catch "unknown error",
1581-
.stack = try_catch.stack(context.call_arena) catch null,
1583+
.exception = try_catch.exception(self.call_arena) catch "unknown error",
1584+
.stack = try_catch.stack(self.call_arena) catch null,
15821585
.line = try_catch.sourceLineNumber() orelse 0,
15831586
});
15841587
const error_msg = if (try_catch.hasCaught()) blk: {
1585-
const exception_str = try_catch.exception(context.call_arena) catch "Evaluation error";
1588+
const exception_str = try_catch.exception(self.call_arena) catch "Evaluation error";
15861589
break :blk v8.String.initUtf8(iso, exception_str orelse "Evaluation error");
15871590
} else v8.String.initUtf8(iso, "Module evaluation failed");
15881591
_ = resolver.reject(ctx, error_msg.toValue());
15891592
return;
15901593
};
1591-
const new_module = context.module_cache.get(specifier).?.castToModule();
1594+
const new_module = self.module_cache.get(specifier).?.castToModule();
15921595

15931596
if (maybe_promise) |promise| {
15941597
// This means we must wait for the evaluation.
15951598
const EvaluationData = struct {
1599+
specifier: []const u8,
15961600
module: v8.Persistent(v8.Module),
15971601
resolver: v8.Persistent(v8.PromiseResolver),
15981602
};
15991603

1600-
const ev_data = try context.context_arena.create(EvaluationData);
1604+
const ev_data = try self.context_arena.create(EvaluationData);
16011605
ev_data.* = .{
1606+
.specifier = specifier,
16021607
.module = v8.Persistent(v8.Module).init(iso, new_module),
16031608
.resolver = v8.Persistent(v8.PromiseResolver).init(iso, resolver.*),
16041609
};
@@ -1614,10 +1619,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
16141619
const cb_resolver = data.resolver.castToPromiseResolver();
16151620

16161621
const namespace = cb_module.getModuleNamespace();
1617-
log.info(.js, "dynamic import complete", .{
1618-
.module = cb_module,
1619-
.namespace = namespace,
1620-
});
1622+
log.info(.js, "dynamic import complete", .{ .specifier = data.specifier });
16211623
_ = cb_resolver.resolve(cb_context, namespace);
16221624
}
16231625
}.callback, external);
@@ -1627,12 +1629,9 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
16271629
const cb_info = v8.FunctionCallbackInfo{ .handle = info.? };
16281630
const cb_context = cb_info.getIsolate().getCurrentContext();
16291631
const data: *EvaluationData = @ptrCast(@alignCast(cb_info.getExternalValue()));
1630-
const cb_module = data.module.castToModule();
16311632
const cb_resolver = data.resolver.castToPromiseResolver();
16321633

1633-
log.err(.js, "dynamic import failed", .{
1634-
.module = cb_module,
1635-
});
1634+
log.err(.js, "dynamic import failed", .{ .specifier = data.specifier });
16361635
_ = cb_resolver.reject(cb_context, cb_info.getData());
16371636
}
16381637
}.callback, external);

0 commit comments

Comments
 (0)