Skip to content
Closed
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/js/export_module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,27 @@ pub fn exportModule(comptime Module: type, comptime options: anytype) void {
};

var prev_refcount: u32 = 0;
var init_succeeded = false;
if (has_lifecycle) {
prev_refcount = State.env_refcount.fetchAdd(1, .monotonic);
errdefer if (!cleanup_hook_registered) {
_ = State.env_refcount.fetchSub(1, .acq_rel);
};

if (has_init) {
try options.init(prev_refcount);
}
errdefer if (!cleanup_hook_registered) {
if (has_lifecycle) {
const prev_refcount_rollback = State.env_refcount.fetchSub(1, .acq_rel);
std.debug.assert(prev_refcount_rollback > 0);
const new_refcount = prev_refcount_rollback - 1;

if (has_cleanup) {
if (init_succeeded) {
options.cleanup(new_refcount);
}
}
}
};

if (has_init) {
try options.init(prev_refcount);
init_succeeded = true;
}

_ = try registerDecls(Module, env, module, 0);
Expand Down
Loading