-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create JavaScript array without using new
keyword.
#1987
Conversation
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.
Thanks for the PR! I'm always a fan of following better idioms, and the change looks great to me! I think the test failures here are legitimate, though, mind rerunning them locally to auto-regenerate the expected output? I think you'll need to set |
Hi @alexcrichton, I ran the following test command with the new code from this PR (using BLESS=1 as you suggested).
The above, produced the following result
I have created a gist of the whole procedure i.e. running tests on:
The reason that I used the Please let me know if you need anything else. Kind regards |
Hm yeah that should be the right command, but did files change locally for you after running that? You should just need to add a new commit with the changes that |
Thanks @alexcrichton, mod.rs
import-catch.js
empty.wat
anyref-import-catch.wat
anyref-empty.wat
Please note: I believe that the following lines are not related to my code changes. Reason being, they are there before I make the changes to mode.rs (and of course are also still there afterwards).
I would really appreciate it if you or someone else could explain (or point to some documentation) facts about the I will go ahead and add a new commit with the changes that BLESS=1 made, as you suggested. Chat soon |
…tially updating generated files that are used for testing.
Hi @alexcrichton |
I appreciate the attempt to help, but I don't really like this change. When using Also, using Crockford has never said that And all of this is auto-generated internal code, so there's no real chance of making a mistake. Another minor consideration is that |
Hi @Pauan, The original code and new code both achieve the task in two separate steps
Happy to leave the code as is, I agree that this is in an area which is auto-generated internal code. As mentioned in the initial PR comment 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. Kind regards |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good to me, but let's hold off on merging until @Pauan's concerns are addressed.
Awesome! |
@tpmccallum Yes, I have read Crockford's books, and also spoken to him many years ago. I have 14 years of experience with JavaScript, so I'm well aware of the JS idioms and best practices. In his book, he is recommending to use As I said, Crockford created JSLint in order to enforce his advice. JSLint rejects The code would still need const heap = [];
heap.length = 32;
heap.fill(undefined); This doesn't seem clearer to me than using However, one thing that could be done to make the code clearer would be to change it to this: const heap = new Array(32).fill(undefined); So I would gladly accept a change like that. |
Thanks @Pauan
|
All the tests have passed, looks like we are good to go! |
Thanks! |
At present this line of code creates the heap using JavaScript's built-in array constructor which uses. JavaScripts
new
keyword (to create the array).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.I originally read this in the wasm-bindgen documentation section js-objects-in-rust
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".Here is an example, in a very broad context, of why it would be best not to use the
new
keyword. If theINITIAL_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.I know that this is highly unlikely, due to the fact that the
INITIAL_HEAP_OFFSET
is set as aconst
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
This would create a JavaScript Array of length
INITIAL_HEAP_OFFSET
, where are items areundefined
The new code generates (in raw JavaScript)
Which produces
In the same way that the original code does.