QuickJS Python Wrapper
QuickJS is a Python wrapper around the QuickJS C library. It enables Python applications to execute JavaScript code and seamlessly translates Python types like str, float, bool, list, and dict to and from JavaScript. The library handles QuickJS's underlying thread-hostile nature by ensuring that all calls to the same JS runtime are managed within the same thread. The current version is 1.19.4, with releases occurring periodically, though the primary GitHub repository is currently archived.
Warnings
- gotcha The `Context` class is thread-hostile, meaning that all interactions with a `Context` instance or objects derived from it must occur within the same Python thread. The `Function` class, however, is designed to be thread-safe.
- breaking The official GitHub repository (PetterS/quickjs) linked by PyPI is marked as 'Archived due to lack of maintenance'. While PyPI still shows recent releases (e.g., 1.19.4 in November 2023), this indicates that the primary development source is no longer actively maintained. Future updates, bug fixes, or compatibility with newer Python/QuickJS versions might be uncertain or originate from unlinked forks.
- gotcha Installation on Windows systems often requires a C++ compiler (e.g., Microsoft C++ Build Tools) if pre-built wheels are not available for your specific Python version and architecture. Without it, `pip install quickjs` may fail during the 'Building wheel' step.
- gotcha QuickJS is a minimalist JavaScript engine and does not include browser-specific APIs like `setTimeout`, `setInterval`, or DOM manipulation functions by default. Attempting to use these will result in `ReferenceError`.
Install
-
pip install quickjs
Imports
- Function
from quickjs import Function
- Context
from quickjs import Context
Quickstart
from quickjs import Function
# Create a callable JavaScript function from Python
f = Function("f", """
function adder(a, b) {
return a + b;
}
function f(a, b) {
return adder(a, b);
}
""")
# Call the JavaScript function from Python
result = f(1, 2)
print(f"Result of JavaScript function call: {result}")
assert result == 3