From 3f93076326b7ec94fa13d06de26e9c50d26a045d Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 9 Jan 2022 00:01:15 +0000 Subject: [PATCH] [TS] Clean up wasm environment exports --- ts/node/crypto.d.ts | 10 ++++++++++ ts/node/index.d.ts | 3 ++- typescript_strings.py | 40 ++++++++++++++++++++++++++++++++-------- 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100755 ts/node/crypto.d.ts diff --git a/ts/node/crypto.d.ts b/ts/node/crypto.d.ts new file mode 100755 index 00000000..f7020d4d --- /dev/null +++ b/ts/node/crypto.d.ts @@ -0,0 +1,10 @@ +// Minimal part of the Node crypto API which we depend on. +// May be (c) Microsoft licensed under the MIT license, however API's are not generally copyrightable per recent precedent. +declare module 'crypto' { + namespace webcrypto { + function getRandomValues(TypedArray): void; + } +} +declare module 'node:crypto' { + export * from 'crypto'; +} diff --git a/ts/node/index.d.ts b/ts/node/index.d.ts index 110c3060..3f2f5c77 100755 --- a/ts/node/index.d.ts +++ b/ts/node/index.d.ts @@ -1,3 +1,4 @@ -// Minimal part of the Node fs API which we depend on. +// Minimal part of the Node API which we depend on. // May be (c) Microsoft licensed under the MIT license, however API's are not generally copyrightable per recent precedent. +/// /// diff --git a/typescript_strings.py b/typescript_strings.py index 8002aa7c..79976674 100644 --- a/typescript_strings.py +++ b/typescript_strings.py @@ -366,21 +366,44 @@ var js_objs: Array> = []; var js_invoke: Function; imports.wasi_snapshot_preview1 = { - "fd_write" : () => { - console.log("ABORT"); + "fd_write": (fd: number, iovec_array_ptr: number, iovec_array_len: number) => { + // This should generally only be used to print panic messages + console.log("FD_WRITE to " + fd + " in " + iovec_array_len + " chunks."); + const ptr_len_view = new Uint32Array(wasm.memory.buffer, iovec_array_ptr, iovec_array_len * 2); + for (var i = 0; i < iovec_array_len; i++) { + const bytes_view = new Uint8Array(wasm.memory.buffer, ptr_len_view[i*2], ptr_len_view[i*2+1]); + console.log(String.fromCharCode(...bytes_view)); + } + return 0; }, - "random_get" : () => { - console.log("RAND GET"); + "random_get": (buf_ptr: number, buf_len: number) => { + const buf = new Uint8Array(wasm.memory.buffer, buf_ptr, buf_len); + crypto.getRandomValues(buf); + return 0; }, - "environ_sizes_get" : () => { + "environ_sizes_get": (environ_var_count_ptr: number, environ_len_ptr: number) => { + // This is called before fd_write to format + print panic messages console.log("wasi_snapshot_preview1:environ_sizes_get"); + const out_count_view = new Uint32Array(wasm.memory.buffer, environ_var_count_ptr, 1); + out_count_view[0] = 1; + const out_len_view = new Uint32Array(wasm.memory.buffer, environ_len_ptr, 1); + out_len_view[0] = "RUST_BACKTRACE=1".length + 1; // Note that string must be NULL-terminated + return 0; + }, + "environ_get": (environ_ptr: number, environ_buf_ptr: number) => { + // This is called before fd_write to format + print panic messages + console.log("wasi_snapshot_preview1:environ_get"); + const out_ptrs = new Uint32Array(wasm.memory.buffer, environ_ptr, 2); + out_ptrs[0] = environ_buf_ptr; + out_ptrs[1] = "RUST_BACKTRACE=1".length; + const out_environ = new Uint8Array(wasm.memory.buffer, environ_buf_ptr, out_ptrs[1]); + for (var i = 0; i < out_ptrs[1]; i++) { out_environ[i] = "RUST_BACKTRACE=1".codePointAt(i); } + out_environ[out_ptrs[1]] = 0; + return 0; }, "proc_exit" : () => { console.log("wasi_snapshot_preview1:proc_exit"); }, - "environ_get" : () => { - console.log("wasi_snapshot_preview1:environ_get"); - }, }; var wasm: any = null; @@ -389,6 +412,7 @@ let isWasmInitialized: boolean = false; if target == Target.NODEJS: res += """import * as fs from 'fs'; +import { webcrypto as crypto } from 'crypto'; export async function initializeWasm(path: string) { const source = fs.readFileSync(path); imports.env["js_invoke_function"] = js_invoke; -- 2.39.5