Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added type annotations for a few glue code functions #4189

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 77 additions & 17 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,10 @@ __wbg_set_wasm(wasm);"
// the linked list of heap slots that are free.
self.global(&format!(
"
/**
* @param {{number}} idx
* @returns {{void}}
*/
function dropObject(idx) {{
if (idx < {}) return;
heap[idx] = heap_next;
Expand Down Expand Up @@ -1307,10 +1311,14 @@ __wbg_set_wasm(wasm);"
// a `SharedArrayBuffer` is in use.
let shared = self.module.memories.get(memory).shared;

let js_doc =
"/** @type {(arg: string, view: Uint8Array) => TextEncoderEncodeIntoResult} */";

match self.config.encode_into {
EncodeInto::Always if !shared => {
self.global(&format!(
"
{js_doc}
const encodeString = {};
",
encode_into
Expand All @@ -1319,6 +1327,7 @@ __wbg_set_wasm(wasm);"
EncodeInto::Test if !shared => {
self.global(&format!(
"
{js_doc}
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? {}
: {});
Expand All @@ -1329,6 +1338,7 @@ __wbg_set_wasm(wasm);"
_ => {
self.global(&format!(
"
{js_doc}
const encodeString = {};
",
encode
Expand Down Expand Up @@ -1372,7 +1382,14 @@ __wbg_set_wasm(wasm);"
);

self.global(&format!(
"function {name}(arg, malloc, realloc) {{
"
/**
* @param {{string}} arg
* @param {{(size: number, align: number) => number}} malloc
* @param {{(ptr: number, oldSize: number, newSize: number, align: number) => number}} [realloc]
* @returns {{number}}
*/
function {name}(arg, malloc, realloc) {{
{debug}
{ascii}
if (offset !== len) {{
Expand All @@ -1389,7 +1406,8 @@ __wbg_set_wasm(wasm);"

WASM_VECTOR_LEN = offset;
return ptr;
}}",
}}
",
name = ret,
debug = debug,
ascii = encode_as_ascii,
Expand Down Expand Up @@ -1451,6 +1469,11 @@ __wbg_set_wasm(wasm);"
let add = self.expose_add_to_externref_table(table, alloc)?;
self.global(&format!(
"
/**
* @param {{ArrayLike<unknown>}} array
* @param {{(size: number, align: number) => number}} malloc
* @returns {{number}}
*/
function {}(array, malloc) {{
const ptr = malloc(array.length * 4, 4) >>> 0;
const mem = {}();
Expand All @@ -1468,6 +1491,11 @@ __wbg_set_wasm(wasm);"
self.expose_add_heap_object();
self.global(&format!(
"
/**
* @param {{ArrayLike<unknown>}} array
* @param {{(size: number, align: number) => number}} malloc
* @returns {{number}}
*/
function {}(array, malloc) {{
const ptr = malloc(array.length * 4, 4) >>> 0;
const mem = {}();
Expand Down Expand Up @@ -1501,6 +1529,11 @@ __wbg_set_wasm(wasm);"
self.expose_wasm_vector_len();
self.global(&format!(
"
/**
* @param {{ArrayLike<number>}} arg
* @param {{(size: number, align: number) => number}} malloc
* @returns {{number}}
*/
function {}(arg, malloc) {{
const ptr = malloc(arg.length * {size}, {size}) >>> 0;
{}().set(arg, ptr / {size});
Expand Down Expand Up @@ -1558,6 +1591,7 @@ __wbg_set_wasm(wasm);"
args: &str,
init: Option<&str>,
) -> Result<(), Error> {
let js_doc = format!("/** @type {{{s}}} */\n");
match &self.config.mode {
OutputMode::Node { .. } => {
let name = self.import_name(&JsImport {
Expand All @@ -1567,25 +1601,23 @@ __wbg_set_wasm(wasm);"
},
fields: Vec::new(),
})?;
self.global(&format!("let cached{} = new {}{};", s, name, args));
self.global(&format!("{js_doc}let cached{} = new {}{};", s, name, args));
}
OutputMode::Bundler {
browser_only: false,
} => {
self.global(&format!(
"
const l{0} = typeof {0} === 'undefined' ? \
(0, module.require)('util').{0} : {0};\
",
s
));
self.global(&format!("let cached{0} = new l{0}{1};", s, args));
// this is a mix between the web and node version.
// we check if Text{En,De}coder is available (web) and if not, we require it (node).
let type_expr = format!(
"typeof {s} === 'undefined' ? (0, module.require)('util').{s} : {s}"
);
self.global(&format!("{js_doc}let cached{s} = new ({type_expr}){args};"));
}
OutputMode::Deno
| OutputMode::Web
| OutputMode::NoModules { .. }
| OutputMode::Bundler { browser_only: true } => {
self.global(&format!("const cached{0} = (typeof {0} !== 'undefined' ? new {0}{1} : {{ {2}: () => {{ throw Error('{0} not available') }} }} );", s, args, op))
self.global(&format!("{js_doc}const cached{0} = (typeof {0} !== 'undefined' ? new {0}{1} : {{ {2}: () => {{ throw Error('{0} not available') }} }} );", s, args, op))
}
};

Expand Down Expand Up @@ -1633,6 +1665,11 @@ __wbg_set_wasm(wasm);"

self.global(&format!(
"
/**
* @param {{number}} ptr
* @param {{number}} len
* @returns {{string}}
*/
function {}(ptr, len) {{
ptr = ptr >>> 0;
return cachedTextDecoder.decode({}().{}(ptr, ptr + len));
Expand Down Expand Up @@ -1804,6 +1841,10 @@ __wbg_set_wasm(wasm);"
}
self.global(&format!(
"
/**
* @param {{number}} ptr
* @param {{number}} len
*/
function {name}(ptr, len) {{
ptr = ptr >>> 0;
return {mem}().subarray(ptr / {size}, ptr / {size} + len);
Expand Down Expand Up @@ -1892,10 +1933,10 @@ __wbg_set_wasm(wasm);"
format!("{cache}.byteLength === 0", cache = cache)
};

self.global(&format!("let {cache} = null;\n"));

self.global(&format!(
"
/** @type {{{kind} | null}} */
let {cache} = null;
function {name}() {{
if ({cache} === null || {resized_check}) {{
{cache} = new {kind}(wasm.{mem}.buffer);
Expand Down Expand Up @@ -1940,11 +1981,14 @@ __wbg_set_wasm(wasm);"
}
self.global(
"
/**
* @param {unknown} instance
* @param {Function} klass
*/
function _assertClass(instance, klass) {
if (!(instance instanceof klass)) {
throw new Error(`expected instance of ${klass.name}`);
}
return instance.ptr;
}
",
);
Expand Down Expand Up @@ -1987,6 +2031,10 @@ __wbg_set_wasm(wasm);"
self.expose_drop_ref();
self.global(
"
/**
* @param {number} idx
* @returns {any}
*/
function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
Expand Down Expand Up @@ -2018,6 +2066,10 @@ __wbg_set_wasm(wasm);"
// one more slot and use that.
self.global(&format!(
"
/**
* @param {{unknown}} obj
* @returns {{number}}
*/
function addHeapObject(obj) {{
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
Expand Down Expand Up @@ -2177,6 +2229,10 @@ __wbg_set_wasm(wasm);"
}
self.global(
"
/**
* @param {unknown} x
* @returns {x is undefined | null}
*/
function isLikeNone(x) {
return x === undefined || x === null;
}
Expand Down Expand Up @@ -3911,7 +3967,11 @@ __wbg_set_wasm(wasm);"

self.global(
"
function debugString(val) {
/**
* @param {any} val
* @returns {string}
*/
function debugString(val) {
// primitive types
const type = typeof val;
if (type == 'number' || type == 'boolean' || val == null) {
Expand Down Expand Up @@ -3952,7 +4012,7 @@ __wbg_set_wasm(wasm);"
// Test for built-in
const builtInMatches = /\\[object ([^\\]]+)\\]/.exec(toString.call(val));
let className;
if (builtInMatches.length > 1) {
if (builtInMatches && builtInMatches.length > 1) {
className = builtInMatches[1];
} else {
// Failed to match the standard '[object ClassName]'
Expand Down
14 changes: 9 additions & 5 deletions crates/cli/tests/reference/anyref-import-catch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ export function __wbg_set_wasm(val) {
}


const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
/** @type {TextDecoder} */
let cachedTextDecoder = new (typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder)('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

/** @type {Uint8Array | null} */
let cachedUint8ArrayMemory0 = null;

function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}

/**
* @param {number} ptr
* @param {number} len
* @returns {string}
*/
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
Expand All @@ -39,8 +43,8 @@ function handleError(f, args) {
}
}

/** @type {DataView | null} */
let cachedDataViewMemory0 = null;

function getDataViewMemory0() {
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
Expand Down
12 changes: 8 additions & 4 deletions crates/cli/tests/reference/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ export function __wbg_set_wasm(val) {
}


const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
/** @type {TextDecoder} */
let cachedTextDecoder = new (typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder)('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

/** @type {Uint8Array | null} */
let cachedUint8ArrayMemory0 = null;

function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}

/**
* @param {number} ptr
* @param {number} len
* @returns {string}
*/
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
Expand Down
12 changes: 8 additions & 4 deletions crates/cli/tests/reference/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ export function __wbg_set_wasm(val) {
}


const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
/** @type {TextDecoder} */
let cachedTextDecoder = new (typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder)('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

/** @type {Uint8Array | null} */
let cachedUint8ArrayMemory0 = null;

function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}

/**
* @param {number} ptr
* @param {number} len
* @returns {string}
*/
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
Expand Down
16 changes: 12 additions & 4 deletions crates/cli/tests/reference/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ export function __wbg_set_wasm(val) {
}


const lTextDecoder = typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder;

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });
/** @type {TextDecoder} */
let cachedTextDecoder = new (typeof TextDecoder === 'undefined' ? (0, module.require)('util').TextDecoder : TextDecoder)('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

/** @type {Uint8Array | null} */
let cachedUint8ArrayMemory0 = null;

function getUint8ArrayMemory0() {
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8ArrayMemory0;
}

/**
* @param {number} ptr
* @param {number} len
* @returns {string}
*/
function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
Expand All @@ -32,6 +36,10 @@ export function enum_echo(color) {
return ret;
}

/**
* @param {unknown} x
* @returns {x is undefined | null}
*/
function isLikeNone(x) {
return x === undefined || x === null;
}
Expand Down
Loading
Loading