small-python 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ju Lin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,53 @@
1
- # Temporary Holding Version
1
+ # small-python
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Run [Small Python](https://github.com/soasme/small-python), a small Python 3.15
4
+ engine written in C, from JavaScript. The engine is compiled to WebAssembly
5
+ and each runtime runs in a Worker thread of its own: code in it sees only the
6
+ functions and modules you give it, cannot touch files, the network, or your
7
+ objects, and can be stopped at any time.
8
+
9
+ ```sh
10
+ npm install small-python
11
+ ```
12
+
13
+ ```js
14
+ import { Runtime } from "small-python";
15
+
16
+ const rt = await Runtime.create({ memoryLimit: 64 << 20, timeout: 5000 });
17
+ await rt.define("fetchUser", async (id) => ({ id, name: "Ada" }));
18
+ console.log(await rt.eval("fetchUser(7)['name'].upper()")); // ADA
19
+ rt.close();
20
+ ```
21
+
22
+ ## Values
23
+
24
+ Plain data is copied in both directions: `null`, booleans, numbers, bigints,
25
+ strings, `Uint8Array`, arrays, `Set`, `Map`, and plain objects. A Python
26
+ `dict` comes back as a `Map`, an `int` beyond 2⁵³ as a bigint. Functions you
27
+ pass in become Python functions; Python functions, instances, and other values
28
+ come back as `PyObject` proxies whose methods (`get`, `call`, `getItem`, ...)
29
+ return promises.
30
+
31
+ Host functions are called with positional arguments. Wrap one with
32
+ `keywords(fn)` to receive Python keyword arguments as a final object. They may
33
+ be `async`; the engine waits.
34
+
35
+ ## Limits and safety
36
+
37
+ | Option | Effect |
38
+ |---|---|
39
+ | `stepLimit` | `RuntimeError` in Python after that many steps per `eval`/`exec` |
40
+ | `memoryLimit` | `MemoryError` in Python beyond that many bytes |
41
+ | `timeout` | terminates the worker after that many milliseconds; rejects with `Timeout` |
42
+ | `messageLimit` | largest value that may cross between JavaScript and Python |
43
+
44
+ A crash or a trap in the engine ends only its Worker: the operation rejects
45
+ with `WorkerError` and the runtime is closed.
46
+
47
+ ## Platforms
48
+
49
+ Node.js 20 or later. In browsers the page must be cross-origin isolated
50
+ (COOP and COEP headers), because the worker uses `SharedArrayBuffer`.
51
+
52
+ The full specification is
53
+ [docs/packaging/javascript.md](https://github.com/soasme/small-python/blob/main/docs/packaging/javascript.md).
package/index.d.ts ADDED
@@ -0,0 +1,81 @@
1
+ /** A value that converts to an engine value. */
2
+ export type PyValue =
3
+ | null | undefined | boolean | number | bigint | string | Uint8Array | Complex | PyObject
4
+ | PyValue[] | Set<PyValue> | Map<PyValue, PyValue> | { [key: string]: PyValue }
5
+ | ((...args: any[]) => unknown);
6
+
7
+ export interface RuntimeOptions {
8
+ /** Register the standard modules (sys, gc, weakref). Default true. */
9
+ std?: boolean;
10
+ /** Evaluation steps per top-level eval or exec; 0 is unlimited. */
11
+ stepLimit?: number;
12
+ /** Bytes the engine may hold; 0 is unlimited. */
13
+ memoryLimit?: number;
14
+ /** Milliseconds one operation may run in the worker. */
15
+ timeout?: number;
16
+ /** Receives printed text; by default lines go to console.log. */
17
+ output?: (text: string) => void | Promise<void>;
18
+ /** Maps a module name to its source, or null. */
19
+ loader?: (name: string) => string | null | undefined | Promise<string | null | undefined>;
20
+ /** Largest message between the runtime and its worker, in bytes. */
21
+ messageLimit?: number;
22
+ }
23
+
24
+ export class Runtime {
25
+ static create(options?: RuntimeOptions): Promise<Runtime>;
26
+ private constructor();
27
+ readonly version: string;
28
+ readonly closed: boolean;
29
+ eval(source: string, filename?: string): Promise<any>;
30
+ exec(source: string, filename?: string): Promise<void>;
31
+ call(target: string | PyObject, args?: PyValue[], kwargs?: { [key: string]: PyValue } | Map<string, PyValue>): Promise<any>;
32
+ get(name: string): Promise<any>;
33
+ set(name: string, value: PyValue): Promise<void>;
34
+ delete(name: string): Promise<void>;
35
+ define<F extends (...args: any[]) => unknown>(name: string, fn: F): Promise<F>;
36
+ registerModule(name: string, members?: { [key: string]: PyValue } | Map<string, PyValue> | null, source?: string | null): Promise<void>;
37
+ collect(): Promise<number>;
38
+ close(): void;
39
+ [Symbol.dispose](): void;
40
+ }
41
+
42
+ export class PyObject {
43
+ private constructor();
44
+ get(name: string): Promise<any>;
45
+ set(name: string, value: PyValue): Promise<void>;
46
+ getItem(key: PyValue): Promise<any>;
47
+ setItem(key: PyValue, value: PyValue): Promise<void>;
48
+ len(): Promise<number>;
49
+ has(item: PyValue): Promise<boolean>;
50
+ call(args?: PyValue[], kwargs?: { [key: string]: PyValue } | Map<string, PyValue>): Promise<any>;
51
+ bool(): Promise<boolean>;
52
+ hash(): Promise<number | bigint>;
53
+ compare(op: "<" | "<=" | "==" | "!=" | ">" | ">=", other: PyValue): Promise<any>;
54
+ repr(): Promise<string>;
55
+ str(): Promise<string>;
56
+ dir(): Promise<string[]>;
57
+ [Symbol.asyncIterator](): AsyncIterator<any>;
58
+ release(): void;
59
+ }
60
+
61
+ export class Complex {
62
+ constructor(real: number, imag: number);
63
+ readonly real: number;
64
+ readonly imag: number;
65
+ }
66
+
67
+ /** Mark a host function as taking keyword arguments: fn(...args, kwargs). */
68
+ export function keywords<F extends (...args: any[]) => unknown>(fn: F): F;
69
+
70
+ export class PythonError extends Error {
71
+ readonly type: string;
72
+ readonly exception: PyObject | null;
73
+ readonly filename: string | null;
74
+ readonly lineno: number | null;
75
+ }
76
+
77
+ export class WorkerError extends Error {
78
+ readonly returncode: number | null;
79
+ }
80
+
81
+ export class Timeout extends WorkerError {}