Skip to content

Commit 2aa4b03

Browse files
committed
try to cleanup persisted references
1 parent eed10dd commit 2aa4b03

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/runtime/js.zig

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,16 +1707,17 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
17071707
// Will get passed to ScriptManager and then passed back to us when
17081708
// the src of the module is loaded
17091709
const DynamicModuleResolveState = struct {
1710-
// this is what we're trying to get from the module and resolve
1711-
// on our promise
1712-
namespace: ?v8.Value,
1710+
// The module that we're resolving (we'll actually resolve its
1711+
// namespace)
1712+
module: ?v8.Module,
17131713
context_id: usize,
17141714
js_context: *JsContext,
17151715
specifier: [:0]const u8,
1716-
resolver: v8.PromiseResolver,
1716+
resolver: v8.Persistent(v8.PromiseResolver),
17171717
};
17181718

17191719
fn _dynamicModuleCallback(self: *JsContext, specifier: [:0]const u8) !v8.Promise {
1720+
const isolate = self.isolate;
17201721
const gop = try self.module_cache.getOrPut(self.context_arena, specifier);
17211722
if (gop.found_existing and gop.value_ptr.resolver_promise != null) {
17221723
// This is easy, there's already something responsible
@@ -1725,22 +1726,21 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
17251726
return gop.value_ptr.resolver_promise.?.castToPromise();
17261727
}
17271728

1728-
const isolate = self.isolate;
1729-
17301729
const persistent_resolver = v8.Persistent(v8.PromiseResolver).init(isolate, v8.PromiseResolver.init(self.v8_context));
17311730
try self.persisted_promise_resolvers.append(self.context_arena, persistent_resolver);
17321731
var resolver = persistent_resolver.castToPromiseResolver();
17331732

17341733
const state = try self.context_arena.create(DynamicModuleResolveState);
17351734
state.* = .{
1735+
.module = null,
17361736
.js_context = self,
1737-
.resolver = resolver,
17381737
.specifier = specifier,
17391738
.context_id = self.id,
1740-
.namespace = null,
1739+
.resolver = persistent_resolver,
17411740
};
17421741

1743-
const promise = resolver.getPromise();
1742+
const persisted_promise = PersistentPromise.init(self.isolate, resolver.getPromise());
1743+
const promise = persisted_promise.castToPromise();
17441744

17451745
if (!gop.found_existing) {
17461746
// this module hasn't been seen before. This is the most
@@ -1752,7 +1752,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
17521752
gop.value_ptr.* = ModuleEntry{
17531753
.module = null,
17541754
.module_promise = null,
1755-
.resolver_promise = PersistentPromise.init(self.isolate, .{ .handle = promise.handle }),
1755+
.resolver_promise = persisted_promise,
17561756
};
17571757

17581758
// Next, we need to actually load it.
@@ -1782,7 +1782,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
17821782
// like before, we want to set this up so that if anything else
17831783
// tries to load this module, it can just return our promise
17841784
// since we're going to be doing all the work.
1785-
gop.value_ptr.resolver_promise = PersistentPromise.init(self.isolate, .{ .handle = promise.handle });
1785+
gop.value_ptr.resolver_promise = persisted_promise;
17861786

17871787
// But we can skip direclty to `resolveDynamicModule` which is
17881788
// what the above callback will eventually do.
@@ -1796,7 +1796,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
17961796

17971797
var fetch_result = fetch_result_ catch |err| {
17981798
const error_msg = v8.String.initUtf8(self.isolate, @errorName(err));
1799-
_ = state.resolver.reject(self.v8_context, error_msg.toValue());
1799+
_ = state.resolver.castToPromiseResolver().reject(self.v8_context, error_msg.toValue());
18001800
return;
18011801
};
18021802

@@ -1816,7 +1816,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
18161816
.line = try_catch.sourceLineNumber() orelse 0,
18171817
});
18181818
const error_msg = v8.String.initUtf8(self.isolate, ex);
1819-
_ = state.resolver.reject(self.v8_context, error_msg.toValue());
1819+
_ = state.resolver.castToPromiseResolver().reject(self.v8_context, error_msg.toValue());
18201820
return;
18211821
};
18221822
};
@@ -1826,14 +1826,15 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
18261826

18271827
fn resolveDynamicModule(self: *JsContext, state: *DynamicModuleResolveState, module_entry: ModuleEntry) void {
18281828
const ctx = self.v8_context;
1829+
const isolate = self.isolate;
18291830
const external = v8.External.init(self.isolate, @ptrCast(state));
18301831

18311832
// we can only be here if the module has been evaluated and if
18321833
// we have a resolve loading this asynchronously.
18331834
std.debug.assert(module_entry.module_promise != null);
18341835
std.debug.assert(module_entry.resolver_promise != null);
18351836
std.debug.assert(self.module_cache.contains(state.specifier));
1836-
state.namespace = module_entry.module.?.castToModule().getModuleNamespace();
1837+
state.module = module_entry.module.?.castToModule();
18371838

18381839
// We've gotten the source for the module and are evaluating it.
18391840
// You might think we're done, but the module evaluation is
@@ -1859,7 +1860,8 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
18591860
return;
18601861
}
18611862

1862-
_ = s.resolver.resolve(caller.js_context.v8_context, s.namespace.?);
1863+
const namespace = s.module.?.getModuleNamespace();
1864+
_ = s.resolver.castToPromiseResolver().resolve(caller.js_context.v8_context, namespace);
18631865
}
18641866
}.callback, external);
18651867

@@ -1873,7 +1875,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
18731875
if (s.context_id != caller.js_context.id) {
18741876
return;
18751877
}
1876-
_ = s.resolver.reject(caller.js_context.v8_context, info.getData());
1878+
_ = s.resolver.castToPromiseResolver().reject(caller.js_context.v8_context, info.getData());
18771879
}
18781880
}.callback, external);
18791881

@@ -1882,8 +1884,8 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
18821884
.err = err,
18831885
.specifier = state.specifier,
18841886
});
1885-
const error_msg = v8.String.initUtf8(self.isolate, "Failed to evaluate promise");
1886-
_ = state.resolver.reject(ctx, error_msg.toValue());
1887+
const error_msg = v8.String.initUtf8(isolate, "Failed to evaluate promise");
1888+
_ = state.resolver.castToPromiseResolver().reject(ctx, error_msg.toValue());
18871889
};
18881890
}
18891891

0 commit comments

Comments
 (0)