mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 01:11:06 +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
33 lines
800 B
Plaintext
33 lines
800 B
Plaintext
;; @xform table 0 (anyref_owned)
|
|
|
|
(module
|
|
(func $foo (param i32))
|
|
(table (export "func") 0 funcref)
|
|
(elem (i32.const 0) 0)
|
|
(func $alloc (export "__anyref_table_alloc") (result i32)
|
|
i32.const 0)
|
|
(func $dealloc (export "__anyref_table_dealloc") (param i32))
|
|
)
|
|
|
|
(; CHECK-ALL:
|
|
(module
|
|
(type (;0;) (func (result i32)))
|
|
(type (;1;) (func (param i32)))
|
|
(type (;2;) (func (param anyref)))
|
|
(func $closure0 anyref shim (type 2) (param anyref)
|
|
(local i32)
|
|
call $alloc
|
|
local.tee 1
|
|
local.get 0
|
|
table.set 1
|
|
local.get 1
|
|
call $foo)
|
|
(func $alloc (type 0) (result i32)
|
|
i32.const 0)
|
|
(func $foo (type 1) (param i32))
|
|
(table (;0;) 2 funcref)
|
|
(table (;1;) 32 anyref)
|
|
(export "func" (table 0))
|
|
(elem (;0;) (i32.const 0) func $foo $closure0 anyref shim))
|
|
;)
|