-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyodide.loader.ts
123 lines (93 loc) · 3.35 KB
/
pyodide.loader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { loadPyodide } from '/node_modules/pyodide/pyodide.mjs';
import type { PyProxy } from 'pyodide/ffi';
import { loadHistograms, loadOtherWasm, histo_pi_digits, histo_version } from './histo.loader.js';
import { logJS } from './utils.js';
let pyodide;
class HistogramInterop {
num_digits
histogram
}
export class PiAdapter {
seeded: boolean;
constructor(
public piaProxy: PyProxy,
) {
this.piaProxy = piaProxy;
this.seeded = false;
}
destroy_proxy() {
this.piaProxy?.destroy();
}
seed_pi_digits(pi_digits: Int32Array) {
this.piaProxy?.seed_pi_digits(pi_digits);
}
histograms_seed_cache(histograms: HistogramInterop[], force = false): void {
logJS(`histograms_seed_cache([...], force=${force})`);
if (this.piaProxy == null) {
logJS(`histograms_seed_cache([...], force=${force}) - no piaProxy yet ... skipping`);
return;
}
this.seeded = false;
logJS(`histograms_seed_cache([...], force=${force}) ... continuing`);
this.piaProxy?.histograms_seed_cache(histograms);
logJS(`histograms_seed_cache([...], force=${force}) ... done`);
this.seeded = true;
}
histogram(num_digits: number): number[] {
let rc = [];
logJS(`histogram(${num_digits})`);
if (this.piaProxy == null) {
logJS(`histogram(${num_digits}) - - no piaProxy yet ... skipping`);
return rc
}
if (!this.seeded) {
logJS(`histogram(${num_digits}) - cache not seeded or currently seeding - skipping`);
} else {
rc = this.piaProxy?.histogram(num_digits) || [];
}
return rc;
}
pi_digits(num_digits: number, n: number): number[] {
logJS(`pi_digits(${num_digits}, ${n})`);
const rc = this.piaProxy?.pi_digits(num_digits, n) || [];
return rc;
}
version(): string[] {
logJS('version()');
const pyVersions = this.piaProxy?.version() || ['Python is loading...'];
return [histo_version, `pyodide.version: ${pyodide?.version}`, ...pyVersions];
}
}
const loadPiadapter = async (pyodide) => {
// See etc/gen_run_pytests.sh where piadapter.tgz is created. It is called from Containerfile.
let tgzResponse = await fetch('piadapter.tgz');
let tgzBinary = await tgzResponse.arrayBuffer();
await pyodide.unpackArchive(tgzBinary, 'gztar');
const piadapterPkg = pyodide.pyimport('piadapter');
return piadapterPkg.pia;
};
export const loadWasm = async (_load: boolean): Promise<PiAdapter> => {
logJS('loading pyodide');
pyodide = await loadPyodide();
pyodide.setStderr({ batched: console.log });
pyodide.setStdout({ batched: console.log });
logJS('back from loading pyodide', pyodide);
logJS('loading piadapter');
const pia = await loadPiadapter(pyodide);
logJS('back from loading piadapter', pia);
logJS('loading other WASM ...');
await loadOtherWasm();
logJS('loading other WASM ... done');
logJS('The server can be shutdown now. Everything is running in the browser.');
const piAdapter = new PiAdapter(pia);
piAdapter.seed_pi_digits(histo_pi_digits);
logJS('loading histograms');
const histograms: HistogramInterop[] = await loadHistograms(WELL_KNOWN_NUMS);
logJS('back from loading histograms');
piAdapter.histograms_seed_cache(histograms);
logJS('histogram cache seeded');
return piAdapter;
};
export const WELL_KNOWN_NUMS: number[] = [
10, 100, 1000, 1024, 1536, 10000, 20000, 25000, 30000, 35000, 40000, 45000, 50000,
];