Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/python/python-wasm/src/common.ts
1067 views
1
/*
2
Code that is the same for both the browser and node.
3
*/
4
5
import { WasmInstanceAsync, WasmInstanceSync } from "@cowasm/kernel";
6
import { bind_methods } from "./util";
7
import debug from "debug";
8
9
const log = debug("python-wasm");
10
11
type FileSystemOption = "auto" | "bundle" | "everything" | "stdlib";
12
13
export interface Options {
14
fs?: FileSystemOption;
15
noReadline?: boolean;
16
env?: object; // overrides or sets environment variables
17
interactive?: boolean; // for interactive async terminal or program under nodejs
18
noStdio?: boolean; // for nodejs -- do NOT use process.stdin, process.stdout, and process.stderr. Instead, use the same programatic control of IO as in the browser, i.e., the .kernel has a writeToStdin function and 'stdout' and 'stderr' events. ONLY for async mode.
19
}
20
21
export class PythonWasmSync {
22
kernel: WasmInstanceSync;
23
python_wasm: string;
24
25
constructor(kernel: WasmInstanceSync, python_wasm: string) {
26
this.kernel = kernel;
27
this.python_wasm = python_wasm;
28
bind_methods(this);
29
}
30
31
init(): void {
32
log("loading python.wasm...");
33
this.callWithString("cowasm_python_init");
34
log("done");
35
}
36
37
callWithString(name: string, str?: string | string[], ...args): any {
38
return this.kernel.callWithString(
39
{ name, dll: this.python_wasm },
40
str,
41
...args
42
);
43
}
44
45
repr(code: string): string {
46
log("repr", code);
47
const s = this.callWithString("cowasm_python_repr", code);
48
log("result =", s);
49
return s;
50
}
51
52
exec(code: string): void {
53
log("exec", code);
54
const ret = this.callWithString("cowasm_python_exec", code);
55
log("ret", ret);
56
if (ret) {
57
throw Error("exec failed");
58
}
59
}
60
61
// starts the python REPL
62
terminal(argv: string[] = []): number {
63
log("terminal", argv);
64
// NOTE: when you pass a string[] it actually sends argv.length, argv over to WASM!
65
const ret = this.callWithString("cowasm_python_terminal", argv);
66
log("terminal ended and returned ", ret);
67
return ret;
68
}
69
}
70
71
// Run in a worker
72
export class PythonWasmAsync {
73
kernel: WasmInstanceAsync;
74
python_wasm: string;
75
76
constructor(kernel: WasmInstanceAsync, python_wasm: string) {
77
this.kernel = kernel;
78
this.python_wasm = python_wasm;
79
bind_methods(this);
80
}
81
82
async init(): Promise<void> {
83
log("loading and calling cowasm_python_init");
84
await this.callWithString("cowasm_python_init");
85
log("done");
86
}
87
88
terminate(): void {
89
this.kernel.terminate();
90
}
91
92
async callWithString(
93
name: string,
94
str?: string | string[],
95
...args
96
): Promise<any> {
97
return await this.kernel.callWithString(
98
{ name, dll: this.python_wasm },
99
str,
100
...args
101
);
102
}
103
104
async repr(code: string): Promise<string> {
105
log("repr", code);
106
const ret = await this.callWithString("cowasm_python_repr", code);
107
log("done", "ret =", ret);
108
return ret;
109
}
110
111
async exec(code: string): Promise<void> {
112
log("exec", code);
113
const ret = await this.callWithString("cowasm_python_exec", code);
114
if (ret) {
115
throw Error("exec failed");
116
}
117
}
118
119
async terminal(argv: string[] = []): Promise<number> {
120
log("terminal", argv);
121
const ret = await this.callWithString(
122
"cowasm_python_terminal",
123
argv,
124
argv.length
125
);
126
log("terminal ended and returned ", ret);
127
return ret;
128
}
129
}
130
131