import { WasmInstanceAsync, WasmInstanceSync } from "@cowasm/kernel";
import { bind_methods } from "./util";
import debug from "debug";
const log = debug("python-wasm");
type FileSystemOption = "auto" | "bundle" | "everything" | "stdlib";
export interface Options {
fs?: FileSystemOption;
noReadline?: boolean;
env?: object;
interactive?: boolean;
noStdio?: boolean;
}
export class PythonWasmSync {
kernel: WasmInstanceSync;
python_wasm: string;
constructor(kernel: WasmInstanceSync, python_wasm: string) {
this.kernel = kernel;
this.python_wasm = python_wasm;
bind_methods(this);
}
init(): void {
log("loading python.wasm...");
this.callWithString("cowasm_python_init");
log("done");
}
callWithString(name: string, str?: string | string[], ...args): any {
return this.kernel.callWithString(
{ name, dll: this.python_wasm },
str,
...args
);
}
repr(code: string): string {
log("repr", code);
const s = this.callWithString("cowasm_python_repr", code);
log("result =", s);
return s;
}
exec(code: string): void {
log("exec", code);
const ret = this.callWithString("cowasm_python_exec", code);
log("ret", ret);
if (ret) {
throw Error("exec failed");
}
}
terminal(argv: string[] = []): number {
log("terminal", argv);
const ret = this.callWithString("cowasm_python_terminal", argv);
log("terminal ended and returned ", ret);
return ret;
}
}
export class PythonWasmAsync {
kernel: WasmInstanceAsync;
python_wasm: string;
constructor(kernel: WasmInstanceAsync, python_wasm: string) {
this.kernel = kernel;
this.python_wasm = python_wasm;
bind_methods(this);
}
async init(): Promise<void> {
log("loading and calling cowasm_python_init");
await this.callWithString("cowasm_python_init");
log("done");
}
terminate(): void {
this.kernel.terminate();
}
async callWithString(
name: string,
str?: string | string[],
...args
): Promise<any> {
return await this.kernel.callWithString(
{ name, dll: this.python_wasm },
str,
...args
);
}
async repr(code: string): Promise<string> {
log("repr", code);
const ret = await this.callWithString("cowasm_python_repr", code);
log("done", "ret =", ret);
return ret;
}
async exec(code: string): Promise<void> {
log("exec", code);
const ret = await this.callWithString("cowasm_python_exec", code);
if (ret) {
throw Error("exec failed");
}
}
async terminal(argv: string[] = []): Promise<number> {
log("terminal", argv);
const ret = await this.callWithString(
"cowasm_python_terminal",
argv,
argv.length
);
log("terminal ended and returned ", ret);
return ret;
}
}