Python code running on Elide can import elide without installing anything.
| Name | Effect |
|---|---|
elide.js(source) | Evaluates a JavaScript string and returns its value. Needs --interop. |
elide.bind(fn) | Shares fn under its __name__; returns fn, so it works as a decorator. |
elide.poly(name=…) | Decorator that shares a function under name. |
elide.telemetry | Telemetry controls; see OpenTelemetry. |
elide.js
# calls.py
from elide import js
slug = js("(s) => s.trim().toLowerCase().replaceAll(' ', '-')")
print(slug(" Hello Polyglot World "))
point = js("({ x: 1, y: 2, tags: ['a', 'b'] })")
print(point.x + point.y, point.tags[0], len(point.tags))elide --interop python calls.pyhello-polyglot-world
3 a 2JavaScript functions are callable, objects expose their properties as attributes, and undefined becomes None.
Build a polyglot app lists every value conversion.
| Input | Result |
|---|---|
Run without --interop | ValueError: polyglot language 'js' not found |
| JavaScript that throws or fails to parse | RuntimeError: JSException: SyntaxError: <elide. |
| A non-string argument | TypeError: elide. |
elide.bind and elide.poly
Both share a Python function by name. Python reads shared values with polyglot.import_value, and the
REPL with /import NAME.
# shared.py
import polyglot
from elide import bind, poly
@bind
def add(a, b):
return a + b
@poly(name="greet")
def say_hello(name):
return f"hello {name}"
print(add(2, 3), say_hello("elide"))
print(polyglot.import_value("add")(2, 3))
print(polyglot.import_value("greet")("elide"))elide python shared.py5 hello elide
5
hello elide- Both decorators return the original function, so it stays callable from Python.
elide.poly(name=1)raisesTypeError: elide.poly() argument name must be str, not int.- JavaScript files have no
Polyglotglobal, so they cannot read shared values. To call Python from JavaScript, import the.pymodule instead; see Languages and interop.