Repository navigation
feat(runtime): add a JavaScript evaluation API with named arguments, keeping the old one with warnings - #673
Merged
Merged
Conversation
route
force-pushed
the
feat/js-api
branch
2 times, most recently
from
October 23, 2026 16:27
c311f8e to
10e6264
Compare
|
This syntax is much more intuitive! One suggestion: I think that semicolons get dropped, so e.g. |
Member
Author
nice find! thank you! |
route
force-pushed
the
feat/js-api
branch
4 times, most recently
from
October 25, 2026 16:31
0b21dda to
84c7c67
Compare
…keeping the old one with warnings
`#evaluate`, `#evaluate_async`, `#execute`, `#evaluate_func` and `#evaluate_on` had four argument conventions and three
return semantics between them, and scripts reached their arguments through `arguments[0]`. This adds one convention for
all of them, without changing what any existing call returns by default.
New: keyword arguments become the script's parameters, `page.evaluate("a + b", a: 1, b: 2)`, and a script returns the
value of its last statement. A script may be a function or arrow declaration, used as written with positional
arguments, and may `await`. Returned promises are awaited, including zone.js and Bluebird ones, bounded by `timeout:`
and raising `ScriptTimeoutError`, now a `TimeoutError`, and a navigation while awaiting doesn't run the script again.
Errors point at the script's own lines, objects shared by several references are converted once, and a node inside an
Array or Hash argument raises. Added `#evaluate_handle`, returning a `Ferrum::RemoteObject`, and
`Node#execute`/`Node#evaluate_handle`. Scripts are read by a small scanner, `Frame::Script`, that skips strings,
template literals, regular expressions and comments.
Compatible: by default, a call that doesn't use any of that runs exactly as before, through `Runtime::Legacy`, which
also keeps `#evaluate_async`, `#evaluate_func` and `#evaluate_on`. Positional arguments keep working. What the new way
does differently is detected and warned about with a rewrite, pointing at the first caller outside installed gems:
keywords read through `arguments`, mixed positional and keyword arguments, scripts of several statements, promises
that will be awaited, and nodes `Node#evaluate` will resolve.
`FERRUM_DEPRECATION_WARNINGS=raise` turns the warnings into errors, `0` silences them.
`FERRUM_JS_API=new` runs every call the new way. 0.21 removes `Runtime::Legacy` and the deprecated methods.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Redesigns the JavaScript evaluation API around named arguments. It lands in one go without breaking anyone's scripts: old-style calls keep their old behavior and warn, and
FERRUM_JS_API=newruns the new behavior ahead of time.FERRUM_JS_API=newruns them the new wayRuntime::Legacy,evaluate_async,evaluate_funcandevaluate_onremoved, no switch backThe old code lives unchanged, apart from warnings, in
Frame::Runtime::Legacy, and the new pipeline is separate. So 0.21 deleteslegacy.rbandutils/deprecate.rb. The switch is an environment variable rather than a browser option, so it's clearly temporary and removing it breaks no code.The new API
arguments[0]and up, but can't be mixed with named ones in the new way.timeout:(seconds, the page timeout by default) is enforced in Ruby by waiting on the CDP response, so pages that replacePromiseor mock timers can't break it. It raisesFerrum::ScriptTimeoutError, which inherits fromFerrum::TimeoutError. A navigation while a promise is awaited doesn't run the script again.SyntaxErroror a thrown error points at the script's own lines, and objects shared by several references are converted once. A node inside an Array or Hash argument raisesArgumentError, pass each one as its own argument.Node#propertyandNode#attributepass the name as an argument, so a name with a quote no longer breaks the script, andpropertyreturns aFerrum::Nodefor a property holding one.Frame::Script, that skips strings, template literals, regular expressions and comments.Adapting your code
execute_scriptcall), the script, and a rewrite. Each warning prints once per call site.FERRUM_JS_API=new. Warnings only cover the scripts and results your suite runs into, and 0.21 has no switch back, so this is the real check.FERRUM_DEPRECATION_WARNINGS=raiseturns warnings into errors.Most code needs nothing: plain expressions and positional arguments,
page.evaluate("document.title")orpage.evaluate("arguments[0] + 1", 1), behave the same in every release.evaluate("arguments[0].foo", foo: 1)1nilevaluate("foo", foo: 1)evaluate("[arguments[0], arguments[1]]", 1, foo: 2)[1, { "foo" => 2 }]ArgumentError{ foo: 2 }in bracesevaluate("a(); b")a()'s value,bnever runsb's valueevaluate("\n window.ran = true\n")nil, nothing runstruereturn, check it's safe to runevaluate("Promise.resolve(1)"){}1voidkeeps one from being awaitednode.evaluate("this.parentNode"){}Ferrum::Nodenode.evaluate("new Date(0)"){}"1970-01-01T00:00:00.000Z"evaluate_async("arguments[0](1)", 5)1NoMethodErrorevaluate("await …", timeout: 5)evaluate_func("function(a) { return a }", 1)1NoMethodErrorevaluate("function(a) { return a }", 1), withon:usenode.evaluateevaluate_on(node: node, expression: "this.id")"main"NoMethodErrornode.evaluate("this.id")evaluate_on(node: link, expression: "this.click()", wait: 0.1)NoMethodErrorpage.wait_for_navigation!(0.1) { link.evaluate("this.click()") }(#679)evaluate_asynccallbacks becomeawait:Besides being simpler, a rejected promise now raises its real error right away, where
evaluate_asynctimed out because nothing called the callback.Libraries built on Ferrum should pass named arguments or a function declaration, which run the new way in both 0.20 and 0.21.
How old-style calls are detected
A call takes the new path, even in legacy mode, if it uses something only the new API has: named arguments that the script doesn't read through
arguments, a function declaration,await, or a new method. So Ferrum's own calls behave the same in both modes. Anything else runs the code frommainunchanged, and is checked for what the new way does differently:arguments, or mixed with positional arguments;or by line breakspromiseNode#evaluateRuntime.getPropertiesfor arraysThe script runs once, and detection never runs code on its result, so getters and proxy traps don't fire. Promises that Chrome doesn't report as
promise, zone.js, Bluebird or any object with athenmethod, aren't warned about, though the new way awaits them.Verified
Legacy mode runs
main's code, deep serialization (#677) and thrown-value handling (#675) included, andmain's runtime specs still pass against it.spec/frame/runtime/legacy_spec.rbpins those results and warnings, andspec/frame/runtime_spec.rbcoversFERRUM_JS_API=new.Replaces #617, #659, #660 and #661.