Skip to content

Commit

Permalink
More tests for result type
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored and daxpedda committed Oct 31, 2024
1 parent ff8b683 commit c4b088c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 43 deletions.
3 changes: 0 additions & 3 deletions crates/cli/tests/reference/result-string.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions crates/cli/tests/reference/result-string.rs

This file was deleted.

12 changes: 0 additions & 12 deletions crates/cli/tests/reference/result-string.wat

This file was deleted.

5 changes: 5 additions & 0 deletions crates/cli/tests/reference/result.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* tslint:disable */
/* eslint-disable */
export function result_string(): string;
export function result_void(): void;
export function result_i32(): number;
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ 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 });

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));
}

const heap = new Array(128).fill(undefined);

heap.push(undefined, null, true, false);
Expand Down Expand Up @@ -32,34 +52,14 @@ function takeObject(idx) {
dropObject(idx);
return ret;
}

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));
}
/**
* @returns {string}
*/
export function exported() {
export function result_string() {
let deferred2_0;
let deferred2_1;
try {
const ret = wasm.exported();
const ret = wasm.result_string();
var ptr1 = ret[0];
var len1 = ret[1];
if (ret[3]) {
Expand All @@ -74,6 +74,29 @@ export function exported() {
}
}

export function result_void() {
const ret = wasm.result_void();
if (ret[1]) {
throw takeObject(ret[0]);
}
}

/**
* @returns {number}
*/
export function result_i32() {
const ret = wasm.result_i32();
if (ret[2]) {
throw takeObject(ret[1]);
}
return ret[0];
}

export function __wbindgen_error_new(arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};

export function __wbindgen_number_new(arg0) {
const ret = arg0;
return addHeapObject(ret);
Expand Down
16 changes: 16 additions & 0 deletions crates/cli/tests/reference/result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn result_string() -> Result<String, JsValue> {
Err(JsValue::from(5i32))
}

#[wasm_bindgen]
pub fn result_void() -> Result<(), JsError> {
Err(JsError::new("message"))
}

#[wasm_bindgen]
pub fn result_i32() -> Result<i32, JsError> {
Ok(1)
}
18 changes: 18 additions & 0 deletions crates/cli/tests/reference/result.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(module $reference_test.wasm
(type (;0;) (func (result i32 i32)))
(type (;1;) (func (result i32 i32 i32)))
(type (;2;) (func (result i32 i32 i32 i32)))
(type (;3;) (func (param i32 i32 i32)))
(func $__wbindgen_free (;0;) (type 3) (param i32 i32 i32))
(func $"result_string multivalue shim" (;1;) (type 2) (result i32 i32 i32 i32))
(func $"result_i32 multivalue shim" (;2;) (type 1) (result i32 i32 i32))
(func $"result_void multivalue shim" (;3;) (type 0) (result i32 i32))
(memory (;0;) 17)
(export "memory" (memory 0))
(export "result_string" (func $"result_string multivalue shim"))
(export "result_void" (func $"result_void multivalue shim"))
(export "result_i32" (func $"result_i32 multivalue shim"))
(export "__wbindgen_free" (func $__wbindgen_free))
(@custom "target_features" (after code) "\04+\0amultivalue+\0fmutable-globals+\0freference-types+\08sign-ext")
)

0 comments on commit c4b088c

Please sign in to comment.