Repository navigation
Conversation
…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)`, bound by name when the
script is a function declaration. A script may be a function or arrow declaration, used as-is, and may `await`; promises
it returns are awaited, bounded by `timeout:` and raising `ScriptTimeoutError`, now a `TimeoutError`. 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 or that never run, promises and thenables that will be awaited, thrown non-Errors that will raise, and nodes
`Node#evaluate` will resolve. Detection reads the result and its properties without running code on it.
`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,0for no limit) is enforced in Ruby by waiting on the CDP response, so pages that replacePromiseor mock timers can't break it. It raisesFerrum::ScriptTimeoutError, now aFerrum::TimeoutError.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), keywords read throughargumentsarguments[0]foois a parameterevaluate("options.foo", options: { foo: 1 }), orevaluate("foo", foo: 1)evaluate("a", 1, b: 2), positional mixed with keywordsArgumentErrorevaluate("…", 1, { b: 2 }), or name every argumentevaluate("a(); b"), several statementsa(), skips the restSyntaxErrorevaluate("function() { a(); return b }")evaluate("\n window.x = 1\n"), a line break before the scriptnilevaluate("fetch(url)"), returns a promise{}, not awaitedvoidto not waitthenmethod({ value: object })node.evaluate("this.parentNode"), a node, nodes or aDate{}Ferrum::Node, or the date's ISO 8601 stringevaluate_async(expr, wait, *args)evaluate("await …", timeout: wait)evaluate_func(fn, *args, on: node)evaluate(fn, a: 1), ornode.evaluate(fn)evaluate_on(node:, expression:)node.evaluate(expression)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, a function declaration,
await,timeout:, 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 argumentsRuntime.compileScript, which parses without running, cached per scriptevaluatean ownthenfunction in the serialized result, otherwiseRuntime.getPropertiesNode#evaluateRuntime.getPropertiesfor arraysThe script runs once, and detection never runs code on its result, so getters and proxy traps don't fire. For
evaluatethe thenable check reads the serialized result, so it adds no CDP call.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.