Skip to content
New issue

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

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

Already on GitHub? # to your account

Add support for static enum methods via TS namespaces #4258

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
336 changes: 286 additions & 50 deletions crates/cli-support/src/js/mod.rs

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions crates/cli/tests/reference/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function option_order(order) {
* A color.
* @enum {0 | 1 | 2}
*/
export const Color = Object.freeze({
export const Color = {
/**
* Green as a leaf.
*/
Expand All @@ -88,25 +88,25 @@ export const Color = Object.freeze({
* Red as a rose.
*/
Red: 2, "2": "Red",
});
};
/**
* @enum {0 | 1 | 42 | 43}
*/
export const ImplicitDiscriminant = Object.freeze({
export const ImplicitDiscriminant = {
A: 0, "0": "A",
B: 1, "1": "B",
C: 42, "42": "C",
D: 43, "43": "D",
});
};
/**
* A C-style enum with negative discriminants.
* @enum {-1 | 0 | 1}
*/
export const Ordering = Object.freeze({
export const Ordering = {
Less: -1, "-1": "Less",
Equal: 0, "0": "Equal",
Greater: 1, "1": "Greater",
});
};

const __wbindgen_enum_ColorName = ["green", "yellow", "red"];

Expand Down
23 changes: 23 additions & 0 deletions crates/cli/tests/reference/namespace-0.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* tslint:disable */
/* eslint-disable */
/**
* C-style enum
*/
export enum ImageFormat {
PNG = 0,
JPEG = 1,
GIF = 2,
}
/**
* String enum
*/
type Status = "success" | "failure";
export namespace ImageFormat {
export function from_str(s: string): ImageFormat;
}
export namespace Status {
/**
* I have documentation.
*/
export function from_bool(success: boolean): Status;
}
121 changes: 121 additions & 0 deletions crates/cli/tests/reference/namespace-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
let wasm;
export function __wbg_set_wasm(val) {
wasm = val;
}


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

let cachedTextDecoder = new lTextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

let cachedUint8ArrayMemory0 = null;

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

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

let WASM_VECTOR_LEN = 0;

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

let cachedTextEncoder = new lTextEncoder('utf-8');

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});

function passStringToWasm0(arg, malloc, realloc) {

if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len, 1) >>> 0;

const mem = getUint8ArrayMemory0();

let offset = 0;

for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}

if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);

offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}

WASM_VECTOR_LEN = offset;
return ptr;
}
/**
* C-style enum
* @enum {0 | 1 | 2}
*/
export const ImageFormat = {
PNG: 0, "0": "PNG",
JPEG: 1, "1": "JPEG",
GIF: 2, "2": "GIF",
};

const __wbindgen_enum_Status = ["success", "failure"];

/**
* @param {string} s
* @returns {ImageFormat}
*/
ImageFormat.from_str = function from_str(s) {
const ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.imageformat_from_str(ptr0, len0);
return ret;
};

export const Status = {};
/**
* I have documentation.
* @param {boolean} success
* @returns {Status}
*/
Status.from_bool = function from_bool(success) {
const ret = wasm.status_from_bool(success);
return __wbindgen_enum_Status[ret];
};

export function __wbindgen_throw(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};

21 changes: 21 additions & 0 deletions crates/cli/tests/reference/namespace-0.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(module $reference_test.wasm
(type (;0;) (func (param i32) (result i32)))
(type (;1;) (func (param i32 i32) (result i32)))
(type (;2;) (func (param i32 i32 i32 i32) (result i32)))
(func $__wbindgen_realloc (;0;) (type 2) (param i32 i32 i32 i32) (result i32))
(func $__wbindgen_malloc (;1;) (type 1) (param i32 i32) (result i32))
(func $imageformat_from_str (;2;) (type 1) (param i32 i32) (result i32))
(func $status_from_bool (;3;) (type 0) (param i32) (result i32))
(func $imageformat_is_lossless (;4;) (type 0) (param i32) (result i32))
(func $status_to_bool (;5;) (type 0) (param i32) (result i32))
(memory (;0;) 17)
(export "memory" (memory 0))
(export "imageformat_from_str" (func $imageformat_from_str))
(export "imageformat_is_lossless" (func $imageformat_is_lossless))
(export "status_from_bool" (func $status_from_bool))
(export "status_to_bool" (func $status_to_bool))
(export "__wbindgen_malloc" (func $__wbindgen_malloc))
(export "__wbindgen_realloc" (func $__wbindgen_realloc))
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

23 changes: 23 additions & 0 deletions crates/cli/tests/reference/namespace-1.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* tslint:disable */
/* eslint-disable */
/**
* C-style enum
*/
export enum ImageFormat {
PNG = 0,
JPEG = 1,
GIF = 2,
}
/**
* String enum
*/
type Status = "success" | "failure";
export namespace ImageFormat {
export function from_str(s: string): ImageFormat;
}
export namespace Status {
/**
* I have documentation.
*/
export function from_bool(success: boolean): Status;
}
141 changes: 141 additions & 0 deletions crates/cli/tests/reference/namespace-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@

let imports = {};
imports['__wbindgen_placeholder__'] = module.exports;
let wasm;
const { TextDecoder, TextEncoder } = require(`util`);

let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

cachedTextDecoder.decode();

let cachedUint8ArrayMemory0 = null;

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

function getStringFromWasm0(ptr, len) {
ptr = ptr >>> 0;
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
}

let WASM_VECTOR_LEN = 0;

let cachedTextEncoder = new TextEncoder('utf-8');

const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
? function (arg, view) {
return cachedTextEncoder.encodeInto(arg, view);
}
: function (arg, view) {
const buf = cachedTextEncoder.encode(arg);
view.set(buf);
return {
read: arg.length,
written: buf.length
};
});

function passStringToWasm0(arg, malloc, realloc) {

if (realloc === undefined) {
const buf = cachedTextEncoder.encode(arg);
const ptr = malloc(buf.length, 1) >>> 0;
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
WASM_VECTOR_LEN = buf.length;
return ptr;
}

let len = arg.length;
let ptr = malloc(len, 1) >>> 0;

const mem = getUint8ArrayMemory0();

let offset = 0;

for (; offset < len; offset++) {
const code = arg.charCodeAt(offset);
if (code > 0x7F) break;
mem[ptr + offset] = code;
}

if (offset !== len) {
if (offset !== 0) {
arg = arg.slice(offset);
}
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
const ret = encodeString(arg, view);

offset += ret.written;
ptr = realloc(ptr, len, offset, 1) >>> 0;
}

WASM_VECTOR_LEN = offset;
return ptr;
}
/**
* C-style enum
* @enum {0 | 1 | 2}
*/
module.exports.ImageFormat = {
PNG: 0, "0": "PNG",
JPEG: 1, "1": "JPEG",
GIF: 2, "2": "GIF",
};

const __wbindgen_enum_Status = ["success", "failure"];

(function(ImageFormat) {
/**
* @param {string} s
* @returns {ImageFormat}
*/
ImageFormat.from_str = function from_str(s) {
const ptr0 = passStringToWasm0(s, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.imageformat_from_str(ptr0, len0);
return ret;
};
})(module.exports.ImageFormat || (module.exports.ImageFormat = {}));

(function(Status) {
/**
* I have documentation.
* @param {boolean} success
* @returns {Status}
*/
Status.from_bool = function from_bool(success) {
const ret = wasm.status_from_bool(success);
return __wbindgen_enum_Status[ret];
};
})(module.exports.Status || (module.exports.Status = {}));

module.exports.__wbindgen_init_externref_table = function() {
const table = wasm.__wbindgen_export_0;
const offset = table.grow(4);
table.set(0, undefined);
table.set(offset + 0, undefined);
table.set(offset + 1, null);
table.set(offset + 2, true);
table.set(offset + 3, false);
;
};

module.exports.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};

const path = require('path').join(__dirname, 'reference_test_bg.wasm');
const bytes = require('fs').readFileSync(path);

const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;

wasm.__wbindgen_start();

Loading
Loading