Create JavaScript array without using new keyword. (#1987)

* 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
This commit is contained in:
Timothy McCallum
2020-02-07 11:00:15 +10:00
committed by GitHub
parent f507a2a5ff
commit 0f3c53b5a5
5 changed files with 5 additions and 8 deletions

View File

@ -28,5 +28,5 @@
(table (;0;) 2 funcref) (table (;0;) 2 funcref)
(table (;1;) 32 anyref) (table (;1;) 32 anyref)
(export "func" (table 0)) (export "func" (table 0))
(elem (;0;) (i32.const 0) $foo $closure0 anyref shim)) (elem (;0;) (i32.const 0) func $foo $closure0 anyref shim))
;) ;)

View File

@ -744,8 +744,7 @@ impl<'a> Context<'a> {
return; return;
} }
assert!(!self.config.anyref); assert!(!self.config.anyref);
self.global(&format!("const heap = new Array({});", INITIAL_HEAP_OFFSET)); self.global(&format!("const heap = new Array({}).fill(undefined);", INITIAL_HEAP_OFFSET));
self.global("heap.fill(undefined);");
self.global(&format!("heap.push({});", INITIAL_HEAP_VALUES.join(", "))); self.global(&format!("heap.push({});", INITIAL_HEAP_VALUES.join(", ")));
} }

View File

@ -2,7 +2,7 @@
(type (;0;) (func)) (type (;0;) (func))
(import "./reference_test.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0))) (import "./reference_test.js" "__wbindgen_init_anyref_table" (func (;0;) (type 0)))
(table (;0;) 32 anyref) (table (;0;) 32 anyref)
(memory (;0;) 17) (memory (;0;) 16)
(export "memory" (memory 0)) (export "memory" (memory 0))
(export "__wbindgen_export_0" (table 0)) (export "__wbindgen_export_0" (table 0))
(export "__wbindgen_start" (func 0))) (export "__wbindgen_start" (func 0)))

View File

@ -1,3 +1,3 @@
(module (module
(memory (;0;) 17) (memory (;0;) 16)
(export "memory" (memory 0))) (export "memory" (memory 0)))

View File

@ -1,8 +1,6 @@
import * as wasm from './reference_test_bg.wasm'; import * as wasm from './reference_test_bg.wasm';
const heap = new Array(32); const heap = new Array(32).fill(undefined);
heap.fill(undefined);
heap.push(undefined, null, true, false); heap.push(undefined, null, true, false);