mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-04-02 18:31:05 +00:00
* Create JavaScript array without using `new` keyword. At present [this line of code](https://github.com/rustwasm/wasm-bindgen/blob/master/crates/cli-support/src/js/mod.rs#L747) creates the heap using JavaScript's new keyword. ``` //Line 747 self.global(&format!("const heap = new Array({});", INITIAL_HEAP_OFFSET)); self.global("heap.fill(undefined);"); ``` Assuming that the `INITIAL_HEAP_OFFSET` is always 32 (because it is set as a constant in the Rust code), below is the equivalent of what this code will produce; an Array Object with 32 items which are all undefined. ``` const heap = new Array(32); //(32) [empty × 32] //Where var zero_element = heap[0]; //undefined var one_element = heap[1]; //undefined ``` I believe that this is the desired outcome for the program. All good. ### Suggestion to consider I am always reminded **not** to use the `new` keyword. Mainly by reading or listening to JavaScript ["The Good Parts"](https://youtu.be/XFTOG895C7c?t=1654). For example if the `INITIAL_HEAP_OFFSET` was ever anything but one number, the heap would be created in a different way. For example if two numbers are passed in, then an array of size 2 would be created; where both items in the array are individual numbers. ``` const heap = new Array(32, 32); var zero_element = heap[0]; var one_element = heap[1]; //32 //32 ``` I know that this is highly unlikely, due to the fact that the `INITIAL_HEAP_OFFSET` is set as a `const` in the Rust. But thought that I would put out the following suggestion for consideration anyway. This comes from a place of just wanting to contribute in a way that could make this already awesome program a little better. :) ### Suggested update The heap array could be created using the following code ``` const heap = []; heap.length = INITIAL_HEAP_OFFSET; heap[0] heap[1] //undefined //undefined ``` This would create a JavaScript Array of length `INITIAL_HEAP_OFFSET`, where are items are `undefined` The new code generates (in raw JavaScript) ``` const heap = []; heap.length = 32; ``` Which produces ``` (32) [empty × 32] ``` In the same way that the original code does. * Add closing parenthesis to close out self.global * Adding files which were altered by the BLESS=1 system variable. Essentially updating generated files that are used for testing. * Adding code generated wat file, by way of running tests using BLESS=1 * Adding table.wat that was generated by running the tests with BLESS=1 set * Update code that creates heap array line 747 mod.rs * Updating files that are automatically generated when using BLESS=1
57 lines
1.0 KiB
JavaScript
57 lines
1.0 KiB
JavaScript
import * as wasm from './reference_test_bg.wasm';
|
|
|
|
const heap = new Array(32).fill(undefined);
|
|
|
|
heap.push(undefined, null, true, false);
|
|
|
|
function getObject(idx) { return heap[idx]; }
|
|
|
|
let heap_next = heap.length;
|
|
|
|
function dropObject(idx) {
|
|
if (idx < 36) return;
|
|
heap[idx] = heap_next;
|
|
heap_next = idx;
|
|
}
|
|
|
|
function takeObject(idx) {
|
|
const ret = getObject(idx);
|
|
dropObject(idx);
|
|
return ret;
|
|
}
|
|
|
|
function addHeapObject(obj) {
|
|
if (heap_next === heap.length) heap.push(heap.length + 1);
|
|
const idx = heap_next;
|
|
heap_next = heap[idx];
|
|
|
|
heap[idx] = obj;
|
|
return idx;
|
|
}
|
|
|
|
function handleError(e) {
|
|
wasm.__wbindgen_exn_store(addHeapObject(e));
|
|
}
|
|
/**
|
|
*/
|
|
export function exported() {
|
|
wasm.exported();
|
|
}
|
|
|
|
export const __wbindgen_object_drop_ref = function(arg0) {
|
|
takeObject(arg0);
|
|
};
|
|
|
|
export const __wbg_foo_8d66ddef0ff279d6 = function() {
|
|
try {
|
|
foo();
|
|
} catch (e) {
|
|
handleError(e)
|
|
}
|
|
};
|
|
|
|
export const __wbindgen_rethrow = function(arg0) {
|
|
throw takeObject(arg0);
|
|
};
|
|
|