wasm-idle 0.0.1

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.
Files changed (49) hide show
  1. package/README.md +9 -0
  2. package/dist/clang/app.d.ts +24 -0
  3. package/dist/clang/app.js +119 -0
  4. package/dist/clang/apply.d.ts +2 -0
  5. package/dist/clang/apply.js +11 -0
  6. package/dist/clang/color.d.ts +3 -0
  7. package/dist/clang/color.js +3 -0
  8. package/dist/clang/encode.d.ts +3 -0
  9. package/dist/clang/encode.js +13 -0
  10. package/dist/clang/error.d.ts +14 -0
  11. package/dist/clang/error.js +27 -0
  12. package/dist/clang/index.d.ts +30 -0
  13. package/dist/clang/index.js +107 -0
  14. package/dist/clang/memory/index.d.ts +4 -0
  15. package/dist/clang/memory/index.js +4 -0
  16. package/dist/clang/memory/memFs.d.ts +31 -0
  17. package/dist/clang/memory/memFs.js +121 -0
  18. package/dist/clang/memory/memory.d.ts +17 -0
  19. package/dist/clang/memory/memory.js +56 -0
  20. package/dist/clang/memory/tar.d.ts +2 -0
  21. package/dist/clang/memory/tar.js +59 -0
  22. package/dist/clang/url.d.ts +4 -0
  23. package/dist/clang/url.js +4 -0
  24. package/dist/clang/wasm.d.ts +3 -0
  25. package/dist/clang/wasm.js +16 -0
  26. package/dist/index.d.ts +2 -0
  27. package/dist/index.js +2 -0
  28. package/dist/playground/clang.d.ts +20 -0
  29. package/dist/playground/clang.js +89 -0
  30. package/dist/playground/index.d.ts +3 -0
  31. package/dist/playground/index.js +33 -0
  32. package/dist/playground/python.d.ts +20 -0
  33. package/dist/playground/python.js +88 -0
  34. package/dist/playground/sandbox.d.ts +9 -0
  35. package/dist/playground/sandbox.js +1 -0
  36. package/dist/playground/worker/clang.d.ts +1 -0
  37. package/dist/playground/worker/clang.js +46 -0
  38. package/dist/playground/worker/python.d.ts +1 -0
  39. package/dist/playground/worker/python.js +77 -0
  40. package/dist/terminal/Terminal.svelte +130 -0
  41. package/dist/terminal/Terminal.svelte.d.ts +29 -0
  42. package/dist/terminal/index.d.ts +5 -0
  43. package/dist/terminal/index.js +5 -0
  44. package/dist/terminal/plugin/index.d.ts +9 -0
  45. package/dist/terminal/plugin/index.js +19 -0
  46. package/dist/terminal/theme.d.ts +49 -0
  47. package/dist/terminal/theme.js +49 -0
  48. package/package.json +60 -0
  49. package/static/image.jpeg +0 -0
package/README.md ADDED
@@ -0,0 +1,9 @@
1
+ ## wasm-idle
2
+
3
+ ![wasm-idle](static/image.jpeg)
4
+
5
+ Executes C++, Python Code with working stdio.
6
+
7
+ Refer to src/lib/clang.
8
+
9
+ Powered by [wasm-clang](https://github.com/binji/wasm-clang) and Pyodide.
@@ -0,0 +1,24 @@
1
+ import { Memory, type MemFS } from "./memory";
2
+ export default class App {
3
+ ready: Promise<void>;
4
+ mem: Memory;
5
+ memfs: MemFS;
6
+ instance: WebAssembly.Instance;
7
+ exports: any;
8
+ argv: string[];
9
+ environ: {
10
+ [key: string]: string;
11
+ };
12
+ handles: Map<number, any>;
13
+ nextHandle: number;
14
+ constructor(module: WebAssembly.Module, memfs: MemFS, name: string, ...args: string[]);
15
+ run(): Promise<boolean | undefined>;
16
+ proc_exit(code: number): void;
17
+ environ_sizes_get(environ_count_out: number, environ_buf_size_out: number): number;
18
+ environ_get(environ_ptrs: number, environ_buf: number): number;
19
+ args_sizes_get(argc_out: number, argv_buf_size_out: number): number;
20
+ args_get(argv_ptrs: number, argv_buf: number): number;
21
+ random_get(buf: number, buf_len: number): void;
22
+ clock_time_get(): void;
23
+ poll_oneoff(): void;
24
+ }
@@ -0,0 +1,119 @@
1
+ import { bindNew } from "./apply";
2
+ import { Memory } from "./memory";
3
+ import { getInstance } from "./wasm";
4
+ import { NotImplemented, ProcExit } from "./error";
5
+ const ESUCCESS = 0;
6
+ const RAF_PROC_EXIT_CODE = 0xC0C0A;
7
+ export default class App {
8
+ ready;
9
+ mem = null;
10
+ memfs;
11
+ instance = null;
12
+ exports;
13
+ argv;
14
+ environ;
15
+ handles = new Map();
16
+ nextHandle = 0;
17
+ constructor(module, memfs, name, ...args) {
18
+ this.argv = [name, ...args];
19
+ this.environ = { USER: 'jungol' };
20
+ this.memfs = memfs;
21
+ const env = bindNew(this);
22
+ const wasi_unstable = {
23
+ ...bindNew(this, 'proc_exit', 'environ_sizes_get', 'environ_get', 'args_sizes_get', 'args_get', 'random_get', 'clock_time_get', 'poll_oneoff'), ...this.memfs.exports
24
+ };
25
+ this.ready = getInstance(module, { wasi_unstable, env }).then(instance => {
26
+ this.instance = instance;
27
+ this.exports = this.instance.exports;
28
+ this.mem = new Memory(this.exports.memory);
29
+ this.memfs.hostMem = this.mem;
30
+ });
31
+ }
32
+ async run() {
33
+ await this.ready;
34
+ try {
35
+ this.exports._start();
36
+ }
37
+ catch (exn) {
38
+ let writeStack = true;
39
+ if (exn instanceof ProcExit) {
40
+ if (exn.code === RAF_PROC_EXIT_CODE) {
41
+ console.log('Allowing rAF after exit.');
42
+ return true;
43
+ }
44
+ // Don't allow rAF unless you return the right code.
45
+ console.log(`Disallowing rAF since exit code is ${exn.code}.`);
46
+ if (exn.code == 0)
47
+ return false;
48
+ writeStack = false;
49
+ }
50
+ // Write error message.
51
+ let msg = `\x1b[91mError: ${exn.message}`;
52
+ if (writeStack)
53
+ msg = msg + `\n${exn.stack}`;
54
+ msg += '\x1b[0m\n';
55
+ this.memfs.stdout(msg);
56
+ // Propagate error.
57
+ throw exn;
58
+ }
59
+ }
60
+ proc_exit(code) {
61
+ throw new ProcExit(code);
62
+ }
63
+ environ_sizes_get(environ_count_out, environ_buf_size_out) {
64
+ this.mem.check();
65
+ let size = 0;
66
+ const names = Object.getOwnPropertyNames(this.environ);
67
+ for (const name of names) {
68
+ const value = this.environ[name];
69
+ // +2 to account for = and \0 in "name=value\0".
70
+ size += name.length + value.length + 2;
71
+ }
72
+ this.mem.write64(environ_count_out, names.length);
73
+ this.mem.write64(environ_buf_size_out, size);
74
+ return ESUCCESS;
75
+ }
76
+ environ_get(environ_ptrs, environ_buf) {
77
+ this.mem.check();
78
+ const names = Object.getOwnPropertyNames(this.environ);
79
+ for (const name of names) {
80
+ this.mem.write32(environ_ptrs, environ_buf);
81
+ environ_ptrs += 4;
82
+ environ_buf +=
83
+ this.mem.writeStr(environ_buf, `${name}=${this.environ[name]}`);
84
+ }
85
+ this.mem.write32(environ_ptrs, 0);
86
+ return ESUCCESS;
87
+ }
88
+ args_sizes_get(argc_out, argv_buf_size_out) {
89
+ this.mem.check();
90
+ let size = 0;
91
+ for (let arg of this.argv) {
92
+ size += arg.length + 1; // "arg\0".
93
+ }
94
+ this.mem.write64(argc_out, this.argv.length);
95
+ this.mem.write64(argv_buf_size_out, size);
96
+ return ESUCCESS;
97
+ }
98
+ args_get(argv_ptrs, argv_buf) {
99
+ this.mem.check();
100
+ for (let arg of this.argv) {
101
+ this.mem.write32(argv_ptrs, argv_buf);
102
+ argv_ptrs += 4;
103
+ argv_buf += this.mem.writeStr(argv_buf, arg);
104
+ }
105
+ this.mem.write32(argv_ptrs, 0);
106
+ return ESUCCESS;
107
+ }
108
+ random_get(buf, buf_len) {
109
+ const data = new Uint8Array(this.mem.buffer, buf, buf_len);
110
+ for (let i = 0; i < buf_len; ++i)
111
+ data[i] = (Math.random() * 256) | 0;
112
+ }
113
+ clock_time_get() {
114
+ throw new NotImplemented('wasi_unstable', 'clock_time_get');
115
+ }
116
+ poll_oneoff() {
117
+ throw new NotImplemented('wasi_unstable', 'poll_oneoff');
118
+ }
119
+ }
@@ -0,0 +1,2 @@
1
+ export declare function bindNew(obj: any, ...names: string[]): any;
2
+ export declare function bind(obj: any, ...names: string[]): any;
@@ -0,0 +1,11 @@
1
+ export function bindNew(obj, ...names) {
2
+ const result = {};
3
+ for (let name of names)
4
+ result[name] = (obj[name] || (() => 0)).bind(obj);
5
+ return result;
6
+ }
7
+ export function bind(obj, ...names) {
8
+ for (let name of names)
9
+ obj[name] = obj[name].bind(obj);
10
+ return obj;
11
+ }
@@ -0,0 +1,3 @@
1
+ export declare const green = "\u001B[92m";
2
+ export declare const normal = "\u001B[0m";
3
+ export declare const yellow = "\u001B[1;93m";
@@ -0,0 +1,3 @@
1
+ export const green = '\x1b[92m';
2
+ export const normal = '\x1b[0m';
3
+ export const yellow = '\x1b[1;93m';
@@ -0,0 +1,3 @@
1
+ export declare function readStr(u8: Uint8Array, o: number, len?: number): string;
2
+ export declare function readOct(u8: Uint8Array, o: number, len: number): number;
3
+ export declare function readUint8(u8: Uint8Array, o: number, len: number): Uint8Array;
@@ -0,0 +1,13 @@
1
+ export function readStr(u8, o, len = -1) {
2
+ const end = len === -1 ? u8.length : o + len;
3
+ let str = '';
4
+ for (let i = o; i < end && u8[i]; ++i)
5
+ str += String.fromCharCode(u8[i]);
6
+ return str;
7
+ }
8
+ export function readOct(u8, o, len) {
9
+ return parseInt(readStr(u8, o, len), 8);
10
+ }
11
+ export function readUint8(u8, o, len) {
12
+ return new Uint8Array(u8.buffer, o, len);
13
+ }
@@ -0,0 +1,14 @@
1
+ export declare class ProcExit extends Error {
2
+ code: number;
3
+ constructor(code: number);
4
+ }
5
+ export declare class NotImplemented extends Error {
6
+ constructor(modname: any, fieldname: any);
7
+ }
8
+ export declare class AbortError extends Error {
9
+ constructor(msg?: string);
10
+ }
11
+ export declare class AssertError extends Error {
12
+ constructor(msg: string);
13
+ }
14
+ export declare function assert(cond: any): void;
@@ -0,0 +1,27 @@
1
+ export class ProcExit extends Error {
2
+ code;
3
+ constructor(code) {
4
+ super(`process exited with code ${code}.`);
5
+ this.code = code;
6
+ }
7
+ }
8
+ export class NotImplemented extends Error {
9
+ constructor(modname, fieldname) {
10
+ super(`${modname}.${fieldname} not implemented.`);
11
+ }
12
+ }
13
+ export class AbortError extends Error {
14
+ constructor(msg = 'abort') {
15
+ super(msg);
16
+ }
17
+ }
18
+ export class AssertError extends Error {
19
+ constructor(msg) {
20
+ super(msg);
21
+ }
22
+ }
23
+ export function assert(cond) {
24
+ if (!cond) {
25
+ throw new AssertError('assertion failed.');
26
+ }
27
+ }
@@ -0,0 +1,30 @@
1
+ import App from "./app";
2
+ import { MemFS } from "./memory";
3
+ interface APIOption {
4
+ stdin: () => string;
5
+ stdout: (str: string) => void;
6
+ log?: boolean;
7
+ showTiming?: boolean;
8
+ path: string;
9
+ }
10
+ export default class Clang {
11
+ ready: Promise<void>;
12
+ memfs: MemFS;
13
+ stdout: (str: string) => void;
14
+ moduleCache: {
15
+ [key: string]: WebAssembly.Module;
16
+ };
17
+ showTiming: boolean;
18
+ log: boolean;
19
+ lastCode: string;
20
+ path: string;
21
+ constructor(options: APIOption);
22
+ hostLog(message: string): void;
23
+ hostLogAsync(message: string, promise: Promise<any>): Promise<any>;
24
+ getModule(name: string): Promise<any>;
25
+ compile(options: any): Promise<App | null>;
26
+ link(obj: string, wasm: string): Promise<App | null>;
27
+ run(module: WebAssembly.Module, out: boolean, ...args: string[]): Promise<App | null>;
28
+ compileLinkRun(code: string): Promise<App | null>;
29
+ }
30
+ export {};
@@ -0,0 +1,107 @@
1
+ import App from "./app";
2
+ import { MemFS, untar } from "./memory";
3
+ import { green, yellow, normal } from "./color";
4
+ import { compile, readBuffer } from "./wasm";
5
+ import { clangUrl, lldUrl, rootUrl } from "./url";
6
+ const clangCommonArgs = [
7
+ '-disable-free',
8
+ '-isysroot', '/',
9
+ '-internal-isystem', '/include/c++/v1',
10
+ '-internal-isystem', '/include',
11
+ '-internal-isystem', '/lib/clang/8.0.1/include',
12
+ '-ferror-limit', '19',
13
+ '-fmessage-length', '80',
14
+ '-fcolor-diagnostics',
15
+ ];
16
+ export default class Clang {
17
+ ready;
18
+ memfs;
19
+ stdout;
20
+ moduleCache;
21
+ showTiming;
22
+ log;
23
+ lastCode = '';
24
+ path;
25
+ constructor(options) {
26
+ this.moduleCache = {};
27
+ this.stdout = options.stdout;
28
+ this.showTiming = options.showTiming || false;
29
+ this.log = options.log || false;
30
+ this.path = options.path;
31
+ this.memfs = new MemFS({
32
+ stdout: this.stdout,
33
+ stdin: options.stdin,
34
+ path: this.path
35
+ });
36
+ this.ready = this.memfs.ready.then(() => this.hostLogAsync(`Untarring ${rootUrl(this.path)}`, readBuffer(rootUrl(this.path)).then(buffer => untar(buffer, this.memfs))));
37
+ }
38
+ hostLog(message) {
39
+ if (!this.log)
40
+ return;
41
+ const yellowArrow = `${yellow}>${normal} `;
42
+ this.stdout(`${yellowArrow}${message}`);
43
+ }
44
+ async hostLogAsync(message, promise) {
45
+ const start = +new Date();
46
+ this.hostLog(`${message}...`);
47
+ const result = await promise;
48
+ const end = +new Date();
49
+ if (this.log)
50
+ this.stdout(' done.');
51
+ if (this.showTiming)
52
+ this.stdout(` ${green}(${end - start}ms)${normal}\n`);
53
+ if (this.log)
54
+ this.stdout('\n');
55
+ return result;
56
+ }
57
+ async getModule(name) {
58
+ if (this.moduleCache[name])
59
+ return this.moduleCache[name];
60
+ const module = await this.hostLogAsync(`Fetching and compiling ${name}`, compile(name));
61
+ this.moduleCache[name] = module;
62
+ return module;
63
+ }
64
+ async compile(options) {
65
+ const input = options.input;
66
+ const code = options.code;
67
+ const obj = options.obj;
68
+ const opt = options.opt || '2';
69
+ await this.ready;
70
+ this.memfs.addFile(input, code);
71
+ const clang = await this.getModule(clangUrl(this.path));
72
+ return await this.run(clang, this.log, 'clang', '-cc1', '-emit-obj', ...clangCommonArgs, '-O' + opt, '-o', obj, '-x', 'c++', input);
73
+ }
74
+ async link(obj, wasm) {
75
+ const stackSize = 1024 * 1024;
76
+ const libdir = 'lib/wasm32-wasi';
77
+ const crt1 = `${libdir}/crt1.o`;
78
+ await this.ready;
79
+ const lld = await this.getModule(lldUrl(this.path));
80
+ return await this.run(lld, this.log, 'wasm-ld', '--no-threads', '--export-dynamic', // TODO required?
81
+ '-z', `stack-size=${stackSize}`, `-L${libdir}`, crt1, obj, '-lc', '-lc++', '-lc++abi', '-lcanvas', '-o', wasm);
82
+ }
83
+ async run(module, out, ...args) {
84
+ this.memfs.out = out;
85
+ this.hostLog(`${args.join(' ')}\n`);
86
+ const start = +new Date();
87
+ const app = new App(module, this.memfs, args[0], ...args.slice(1));
88
+ const instantiate = +new Date();
89
+ const stillRunning = await app.run();
90
+ const end = +new Date();
91
+ if (this.log)
92
+ this.stdout('\n');
93
+ if (this.showTiming)
94
+ this.stdout(`${green}(${start - instantiate}ms/${end - instantiate}ms)${normal}\n`);
95
+ return stillRunning ? app : null;
96
+ }
97
+ async compileLinkRun(code) {
98
+ const input = `test.cc`, obj = `test.o`, wasm = `test.wasm`;
99
+ if (this.lastCode !== code) {
100
+ await this.compile({ input, code, obj });
101
+ await this.link(obj, wasm);
102
+ }
103
+ this.lastCode = code;
104
+ const testMod = await this.hostLogAsync(`Compiling ${wasm}`, WebAssembly.compile(this.memfs.getFileContents(wasm)));
105
+ return await this.run(testMod, true, wasm);
106
+ }
107
+ }
@@ -0,0 +1,4 @@
1
+ import MemFS from "./memFs";
2
+ import Memory from "./memory";
3
+ import untar from "./tar";
4
+ export { MemFS, Memory, untar };
@@ -0,0 +1,4 @@
1
+ import MemFS from "./memFs";
2
+ import Memory from "./memory";
3
+ import untar from "./tar";
4
+ export { MemFS, Memory, untar };
@@ -0,0 +1,31 @@
1
+ import { Memory } from './';
2
+ interface MemFsOptions {
3
+ stdin: () => string;
4
+ stdout: (str: string) => void;
5
+ stdinStr?: string;
6
+ path: string;
7
+ }
8
+ export default class MemFS {
9
+ ready: Promise<void>;
10
+ mem: Memory;
11
+ hostMem_: Memory;
12
+ stdinStr: string;
13
+ stdin: () => string;
14
+ stdout: (str: string) => void;
15
+ instance: WebAssembly.Instance;
16
+ exports: any;
17
+ out: boolean;
18
+ constructor(options: MemFsOptions);
19
+ set hostMem(mem: Memory);
20
+ setStdinStr(str: string): void;
21
+ addDirectory(path: string): void;
22
+ addFile(path: string, contents: string | ArrayBuffer | Uint8Array): void;
23
+ getFileContents(path: string): Uint8Array;
24
+ abort(): void;
25
+ host_write(fd: number, iovs: number, iovs_len: number, nwritten_out: number): number;
26
+ host_read(fd: number, iovs: number, iovs_len: number, nread: number): number;
27
+ memfs_log(buf: number, len: number): void;
28
+ copy_out(clang_dst: number, memfs_src: number, size: number): void;
29
+ copy_in(memfs_dst: number, clang_src: number, size: number): void;
30
+ }
31
+ export {};
@@ -0,0 +1,121 @@
1
+ import { Memory } from './';
2
+ import { bindNew } from "../apply";
3
+ import { AbortError, assert } from "../error";
4
+ import { compile } from "../wasm";
5
+ import { memfsUrl } from "../url";
6
+ const ESUCCESS = 0;
7
+ export default class MemFS {
8
+ ready;
9
+ mem = null;
10
+ hostMem_ = null;
11
+ stdinStr;
12
+ stdin;
13
+ stdout;
14
+ instance = null;
15
+ exports;
16
+ out = true;
17
+ constructor(options) {
18
+ this.stdin = options.stdin;
19
+ this.stdout = options.stdout;
20
+ this.stdinStr = options.stdinStr || "";
21
+ const env = bindNew(this, 'abort', 'host_write', 'host_read', 'memfs_log', 'copy_in', 'copy_out');
22
+ this.ready = compile(memfsUrl(options.path))
23
+ .then(module => WebAssembly.instantiate(module, { env }))
24
+ .then(instance => {
25
+ this.instance = instance;
26
+ this.exports = instance.exports;
27
+ this.mem = new Memory(this.exports.memory);
28
+ this.exports.init();
29
+ });
30
+ }
31
+ set hostMem(mem) {
32
+ this.hostMem_ = mem;
33
+ }
34
+ setStdinStr(str) {
35
+ this.stdinStr = str;
36
+ }
37
+ addDirectory(path) {
38
+ this.mem.check();
39
+ this.mem.write(this.exports.GetPathBuf(), path);
40
+ this.exports.AddDirectoryNode(path.length);
41
+ }
42
+ addFile(path, contents) {
43
+ const length = contents instanceof ArrayBuffer ? contents.byteLength : contents.length;
44
+ this.mem.check();
45
+ this.mem.write(this.exports.GetPathBuf(), path);
46
+ const inode = this.exports.AddFileNode(path.length, length);
47
+ const addr = this.exports.GetFileNodeAddress(inode);
48
+ this.mem.check();
49
+ this.mem.write(addr, contents);
50
+ }
51
+ getFileContents(path) {
52
+ this.mem.check();
53
+ this.mem.write(this.exports.GetPathBuf(), path);
54
+ const inode = this.exports.FindNode(path.length);
55
+ const addr = this.exports.GetFileNodeAddress(inode);
56
+ const size = this.exports.GetFileNodeSize(inode);
57
+ return new Uint8Array(this.mem.buffer, addr, size);
58
+ }
59
+ abort() {
60
+ throw new AbortError();
61
+ }
62
+ host_write(fd, iovs, iovs_len, nwritten_out) {
63
+ this.hostMem_.check();
64
+ assert(fd <= 2);
65
+ let size = 0;
66
+ let str = '';
67
+ for (let i = 0; i < iovs_len; ++i) {
68
+ const buf = this.hostMem_.read32(iovs);
69
+ iovs += 4;
70
+ const len = this.hostMem_.read32(iovs);
71
+ iovs += 4;
72
+ str += this.hostMem_.readStr(buf, len);
73
+ size += len;
74
+ }
75
+ this.hostMem_.write32(nwritten_out, size);
76
+ if (this.out)
77
+ this.stdout(str);
78
+ return ESUCCESS;
79
+ }
80
+ host_read(fd, iovs, iovs_len, nread) {
81
+ this.hostMem_.check();
82
+ assert(fd === 0);
83
+ let size = 0;
84
+ for (let i = 0; i < iovs_len; ++i) {
85
+ const buf = this.hostMem_.read32(iovs);
86
+ iovs += 4;
87
+ const len = this.hostMem_.read32(iovs);
88
+ iovs += 4;
89
+ if (!this.stdinStr.length)
90
+ this.stdinStr = this.stdin();
91
+ const lenToWrite = Math.min(len, this.stdinStr.length);
92
+ if (lenToWrite === 0)
93
+ break;
94
+ this.hostMem_.write(buf, this.stdinStr.substring(0, lenToWrite));
95
+ this.stdinStr = this.stdinStr.substring(lenToWrite);
96
+ size += lenToWrite;
97
+ if (lenToWrite !== len)
98
+ break;
99
+ }
100
+ this.hostMem_.write32(nread, size);
101
+ return ESUCCESS;
102
+ }
103
+ memfs_log(buf, len) {
104
+ this.mem.check();
105
+ console.log(this.mem.readStr(buf, len));
106
+ }
107
+ copy_out(clang_dst, memfs_src, size) {
108
+ this.hostMem_.check();
109
+ const dst = new Uint8Array(this.hostMem_.buffer, clang_dst, size);
110
+ this.mem.check();
111
+ const src = new Uint8Array(this.mem.buffer, memfs_src, size);
112
+ dst.set(src);
113
+ }
114
+ copy_in(memfs_dst, clang_src, size) {
115
+ this.mem.check();
116
+ const dst = new Uint8Array(this.mem.buffer, memfs_dst, size);
117
+ this.hostMem_.check();
118
+ const src = new Uint8Array(this.hostMem_.buffer, clang_src, size);
119
+ dst.set(src);
120
+ }
121
+ }
@@ -0,0 +1,17 @@
1
+ export default class Memory {
2
+ memory: DataView;
3
+ buffer: ArrayBuffer;
4
+ u8: Uint8Array;
5
+ u32: Uint32Array;
6
+ constructor(memory: DataView);
7
+ check(): void;
8
+ read8(o: number): number;
9
+ read32(o: number): number;
10
+ readStr(o: number, len: number): string;
11
+ write8(o: number, v: number): void;
12
+ write32(o: number, v: number): void;
13
+ write64(o: number, vlo: number, vhi?: number): void;
14
+ writeStr(o: number, str: string): number;
15
+ writeUint8(o: number, arr: ArrayLike<number>): number;
16
+ write(o: number, buf: ArrayBuffer | string | Uint8Array): number;
17
+ }
@@ -0,0 +1,56 @@
1
+ import { readStr } from "../encode";
2
+ export default class Memory {
3
+ memory;
4
+ buffer;
5
+ u8;
6
+ u32;
7
+ constructor(memory) {
8
+ this.memory = memory;
9
+ this.buffer = memory.buffer;
10
+ this.u8 = new Uint8Array(this.buffer);
11
+ this.u32 = new Uint32Array(this.buffer);
12
+ }
13
+ check() {
14
+ if (this.buffer.byteLength === 0) {
15
+ this.buffer = this.memory.buffer;
16
+ this.u8 = new Uint8Array(this.buffer);
17
+ this.u32 = new Uint32Array(this.buffer);
18
+ }
19
+ }
20
+ read8(o) {
21
+ return this.u8[o];
22
+ }
23
+ read32(o) {
24
+ return this.u32[o >> 2];
25
+ }
26
+ readStr(o, len) {
27
+ return readStr(this.u8, o, len);
28
+ }
29
+ write8(o, v) {
30
+ this.u8[o] = v;
31
+ }
32
+ write32(o, v) {
33
+ this.u32[o >> 2] = v;
34
+ }
35
+ write64(o, vlo, vhi = 0) {
36
+ this.write32(o, vlo);
37
+ this.write32(o + 4, vhi);
38
+ }
39
+ writeStr(o, str) {
40
+ o += this.write(o, str);
41
+ this.write8(o, 0);
42
+ return str.length + 1;
43
+ }
44
+ writeUint8(o, arr) {
45
+ new Uint8Array(this.buffer, o, arr.length).set(arr);
46
+ return arr.length;
47
+ }
48
+ write(o, buf) {
49
+ if (buf instanceof ArrayBuffer)
50
+ return this.writeUint8(o, new Uint8Array(buf));
51
+ else if (typeof buf === 'string')
52
+ return this.writeUint8(o, buf.split('').map(x => x.charCodeAt(0)));
53
+ else
54
+ return this.writeUint8(o, buf);
55
+ }
56
+ }
@@ -0,0 +1,2 @@
1
+ import type { MemFS } from "./";
2
+ export default function untar(buffer: ArrayBuffer, memfs: MemFS): void;