Skip to content

Lightpanda: the headless browser designed for AI and automation

script: Force pending script evaluation after evalutate finishes

Karl Seguine8e9b0356a8a
diff --git a/src/browser/ScriptManager.zig b/src/browser/ScriptManager.zig
index 96631d005..769d57f45 100644
--- a/src/browser/ScriptManager.zig
+++ b/src/browser/ScriptManager.zig
@@ -157,7 +157,7 @@ fn waitForPreload(self: *ScriptManager, url: [:0]const u8) ?*Script {
const was_evaluating = self.base.is_evaluating;
self.base.is_evaluating = true;
- defer self.base.is_evaluating = was_evaluating;
+ defer self.base.endEvaluationWindow(was_evaluating);
var client = self.base.client;
while (true) {
@@ -333,7 +333,7 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
const was_evaluating = self.base.is_evaluating;
self.base.is_evaluating = true;
- defer self.base.is_evaluating = was_evaluating;
+ defer self.base.endEvaluationWindow(was_evaluating);
if (is_blocking) {
if (self.waitForPreload(url)) |pre| {
@@ -405,8 +405,8 @@ pub fn addFromElement(self: *ScriptManager, comptime from_parser: bool, script_e
const was_evaluating = self.base.is_evaluating;
self.base.is_evaluating = true;
defer {
- self.base.is_evaluating = was_evaluating;
script.deinit();
+ self.base.endEvaluationWindow(was_evaluating);
}
script.eval();
diff --git a/src/browser/ScriptManagerBase.zig b/src/browser/ScriptManagerBase.zig
index f5a672ae5..80d5f22d9 100644
--- a/src/browser/ScriptManagerBase.zig
+++ b/src/browser/ScriptManagerBase.zig
@@ -95,6 +95,10 @@ owner: Owner,
// used to prevent recursive evaluation
is_evaluating: bool,
+// an evaluate() arrived while is_evaluating was set, signal to call evaluate()
+// when is_evaluating becomes false (to prevent reentrency)
+evaluate_pending: bool = false,
+
// Only once this is true can deferred scripts be run
static_scripts_done: bool,
@@ -288,7 +292,7 @@ pub fn waitForImport(self: *ScriptManagerBase, url: [:0]const u8) !ModuleSource
const was_evaluating = self.is_evaluating;
self.is_evaluating = true;
- defer self.is_evaluating = was_evaluating;
+ defer self.endEvaluationWindow(was_evaluating);
var client = self.client;
while (true) {
@@ -357,7 +361,7 @@ pub fn getAsyncImport(self: *ScriptManagerBase, url: [:0]const u8, cb: ImportAsy
// executes a callback.
const was_evaluating = self.is_evaluating;
self.is_evaluating = true;
- defer self.is_evaluating = was_evaluating;
+ defer self.endEvaluationWindow(was_evaluating);
const owner = self.owner;
const session = self.owner.session();
@@ -406,63 +410,97 @@ pub fn scriptCreatedParseDone(self: *ScriptManagerBase) void {
pub fn evaluate(self: *ScriptManagerBase) void {
if (self.is_evaluating) {
// It's possible for a script.eval to cause evaluate to be called again.
+ // signal that this happened so that when we're done the outer-evaluate
+ // we can evaluate() again to catch these that we're gonna skip now.
+ self.evaluate_pending = true;
return;
}
self.is_evaluating = true;
defer self.is_evaluating = false;
- while (self.ready_scripts.popFirst()) |n| {
- var script: *Script = @fieldParentPtr("node", n);
- switch (script.extra) {
- .frame => {
- // Only .async mode reaches ready_scripts (defer stays in
- // defer_scripts, normal is sync and never queued).
- defer script.deinit();
- script.eval();
- },
- .import_async => |ia| {
- if (script.status < 200 or script.status > 299) {
+ while (true) {
+ self.evaluate_pending = false;
+
+ while (self.ready_scripts.popFirst()) |n| {
+ var script: *Script = @fieldParentPtr("node", n);
+ switch (script.extra) {
+ .frame => {
+ // Only .async mode reaches ready_scripts (defer stays in
+ // defer_scripts, normal is sync and never queued).
+ defer script.deinit();
+ script.eval();
+ },
+ .import_async => |ia| {
+ if (script.status < 200 or script.status > 299) {
+ script.deinit();
+ ia.callback(ia.data, error.FailedToLoad);
+ } else {
+ ia.callback(ia.data, .{
+ .shared = false,
+ .script = script,
+ .buffer = script.source.remote,
+ });
+ }
+ },
+ .import => unreachable, // .import doesn't go through ready_scripts
+ .preload => unreachable, // .preload is buffered in the map, never queued
+ }
+ }
+
+ if (self.static_scripts_done) {
+ while (self.defer_scripts.first) |n| {
+ var script: *Script = @fieldParentPtr("node", n);
+ if (script.complete == false) break;
+ defer {
+ _ = self.defer_scripts.popFirst();
script.deinit();
- ia.callback(ia.data, error.FailedToLoad);
- } else {
- ia.callback(ia.data, .{
- .shared = false,
- .script = script,
- .buffer = script.source.remote,
- });
}
- },
- .import => unreachable, // .import doesn't go through ready_scripts
- .preload => unreachable, // .preload is buffered in the map, never queued
+ // Only frame scripts populate defer_scripts.
+ script.eval();
+ }
}
- }
- if (self.static_scripts_done == false) {
- // We can only execute deferred scripts if
- // 1 - all the normal scripts are done
- // 2 - we've finished parsing the HTML and at least queued all the scripts
- // The last one isn't obvious, but it's possible for self.scripts to
- // be empty not because we're done executing all the normal scripts
- // but because we're done executing some (or maybe none), but we're still
- // parsing the HTML.
- return;
- }
+ // A script.eval above may have pumped the http client (module
+ // imports, preloads); anything that completed during the pump had
+ // its evaluate() swallowed by the re-entrancy guard, so re-drain
+ // here rather than relying on a future doneCallback that may never
+ // come.
+ if (self.evaluate_pending) {
+ continue;
+ }
- while (self.defer_scripts.first) |n| {
- var script: *Script = @fieldParentPtr("node", n);
- if (script.complete == false) return;
- defer {
- _ = self.defer_scripts.popFirst();
- script.deinit();
+ if (self.static_scripts_done == false) {
+ // We can only execute deferred scripts if
+ // 1 - all the normal scripts are done
+ // 2 - we've finished parsing the HTML and at least queued all the scripts
+ // The last one isn't obvious, but it's possible for self.scripts to
+ // be empty not because we're done executing all the normal scripts
+ // but because we're done executing some (or maybe none), but we're still
+ // parsing the HTML.
+ return;
}
- // Only frame scripts populate defer_scripts.
- script.eval();
+ if (self.defer_scripts.first != null) {
+ // Head of the defer list is still in flight; its doneCallback
+ // re-enters evaluate.
+ return;
+ }
+ break;
}
// Frame wrapper uses this to fire documentIsLoaded and
// scriptsCompletedLoading. Null for workers.
- if (self.tail_hook) |hook| hook(self);
+ if (self.tail_hook) |hook| {
+ hook(self);
+ }
+}
+
+pub fn endEvaluationWindow(self: *ScriptManagerBase, was_evaluating: bool) void {
+ self.is_evaluating = was_evaluating;
+ if (was_evaluating == false and self.evaluate_pending) {
+ // we have something to evaluate
+ self.evaluate();
+ }
}
pub const Script = struct {