jy.runtime#
Runtimes that execute the call strings jy builds, and return their values.
A runtime is any callable (call_text: str) -> value. NodeRuntime
runs the call in a fresh node process loaded with the JS source, and decodes
the JSON-serialized result (Promise results are awaited).
PlaywrightRuntime does the same in a browser page.
>>> from jy import add_js_funcs
>>> from jy.runtime import NodeRuntime
>>> code = "function shout(s, n=1) { return s.toUpperCase() + '!'.repeat(n) }"
>>> js = add_js_funcs(code, runtime=NodeRuntime(code))
>>> js.shout('say "hi"', 2)
'SAY "HI"!!'
Module Attributes
Line prefix marking the JSON result among whatever else the JS prints. |
Classes
|
Execute jy call strings in |
|
Execute jy call strings in a browser page (Playwright), one page per call. |
Exceptions
The JS call failed (non-zero exit); the message carries stderr. |
- exception jy.runtime.JsRuntimeError[source]#
Bases:
RuntimeErrorThe JS call failed (non-zero exit); the message carries stderr.
- class jy.runtime.NodeRuntime(js_code='', *, prelude='', node='node', timeout=60)[source]#
Bases:
objectExecute jy call strings in
node(one process per call, stateless).js_codeis evaluated before each call (preludeis extra code run first, e.g. arequire).nodeis the executable (or an argv prefix),timeoutbounds one call in seconds.
- class jy.runtime.PlaywrightRuntime(js_code='', *, url=None, browser='chromium', headless=True, timeout=60)[source]#
Bases:
objectExecute jy call strings in a browser page (Playwright), one page per call.
js_codeis injected into a blank page (or the page aturl) as a script, then the call runs in the page, so DOM and browser APIs are available. Results are JSON-serialized by Playwright;Promiseresults are awaited.Needs the optional
browserextra of jy (it installsplaywright; then runplaywright install chromium). It is imported only when a call is made.>>> from jy import add_js_funcs >>> from jy.runtime import PlaywrightRuntime >>> code = "function title(s) { return document.title + s }" >>> js = add_js_funcs(code, runtime=PlaywrightRuntime(code)) >>> js.title("!") '!'
- jy.runtime.RESULT_MARKER = '\x00jy-result\x00'#
Line prefix marking the JSON result among whatever else the JS prints.