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 committed Oct 17, 2024
1 parent 76776ef commit 7347437
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 35 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.

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 @@ -41,35 +61,15 @@ 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 retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.exported(retptr);
wasm.result_string(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
Expand All @@ -89,6 +89,44 @@ export function exported() {
}
}

export function result_void() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.result_void(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
if (r1) {
throw takeObject(r0);
}
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

/**
* @returns {number}
*/
export function result_i32() {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.result_i32(retptr);
var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
if (r2) {
throw takeObject(r1);
}
return r0;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
(type (;0;) (func (param i32)))
(type (;1;) (func (param i32) (result i32)))
(type (;2;) (func (param i32 i32 i32)))
(func $exported (;0;) (type 0) (param i32))
(func $__wbindgen_free (;1;) (type 2) (param i32 i32 i32))
(func $__wbindgen_add_to_stack_pointer (;2;) (type 1) (param i32) (result i32))
(func $result_i32 (;0;) (type 0) (param i32))
(func $result_string (;1;) (type 0) (param i32))
(func $result_void (;2;) (type 0) (param i32))
(func $__wbindgen_free (;3;) (type 2) (param i32 i32 i32))
(func $__wbindgen_add_to_stack_pointer (;4;) (type 1) (param i32) (result i32))
(memory (;0;) 17)
(export "memory" (memory 0))
(export "exported" (func $exported))
(export "result_string" (func $result_string))
(export "result_void" (func $result_void))
(export "result_i32" (func $result_i32))
(export "__wbindgen_add_to_stack_pointer" (func $__wbindgen_add_to_stack_pointer))
(export "__wbindgen_free" (func $__wbindgen_free))
(@custom "target_features" (after code) "\02+\0fmutable-globals+\08sign-ext")
Expand Down

0 comments on commit 7347437

Please sign in to comment.