Skip to main content

shape_ext_python/
lib.rs

1//! Shape Python language runtime extension.
2//!
3//! Provides a `shape.language_runtime` capability that embeds CPython
4//! for executing `foreign "python" { ... }` blocks in Shape programs.
5//!
6//! # ABI Exports
7//!
8//! All C ABI exports (`shape_plugin_info`, `shape_abi_version`,
9//! `shape_capability_manifest`, `shape_capability_vtable`,
10//! `shape_language_runtime_vtable`) are generated by the
11//! [`shape_abi_v1::language_runtime_plugin!`] macro below.
12
13pub mod arrow_bridge;
14pub mod error_mapping;
15pub mod marshaling;
16pub mod runtime;
17
18/// Bundled `.shape` module artifact for the `python` namespace.
19///
20/// This source is embedded in the extension binary and registered under the
21/// `"python"` namespace (NOT `"std::core::python"`) when the extension is
22/// loaded. Users import it via `import { eval } from python`.
23const PYTHON_SHAPE_SOURCE: &str = r#"/// @module python
24/// Python interop runtime — provides access to the embedded CPython interpreter.
25///
26/// This module is bundled with the Python language runtime extension and is
27/// only available when the extension is loaded. It does NOT live in `std::*`.
28
29/// Evaluate a Python expression and return its result.
30///
31/// The expression is compiled and executed in the extension's embedded CPython
32/// interpreter. The result is marshalled back to a Shape value.
33pub builtin fn eval(code: string) -> _
34
35/// Import a Python module by name and return it as an opaque handle.
36///
37/// The module is imported in the embedded CPython interpreter. Attribute
38/// access and method calls on the returned handle are forwarded to Python.
39pub builtin fn import(module: string) -> _
40"#;
41
42shape_abi_v1::language_runtime_plugin! {
43    name: c"python",
44    version: c"0.1.0",
45    description: c"Python language runtime for foreign function blocks",
46    shape_source: PYTHON_SHAPE_SOURCE,
47    vtable: {
48        init: runtime::python_init,
49        register_types: runtime::python_register_types,
50        compile: runtime::python_compile,
51        invoke: runtime::python_invoke,
52        dispose_function: runtime::python_dispose_function,
53        language_id: runtime::python_language_id,
54        get_lsp_config: runtime::python_get_lsp_config,
55        free_buffer: runtime::python_free_buffer,
56        drop: runtime::python_drop,
57    }
58}