Skip to content
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

Plans for array prototype methods? #64

Closed
boogie opened this issue Mar 30, 2023 · 5 comments
Closed

Plans for array prototype methods? #64

boogie opened this issue Mar 30, 2023 · 5 comments

Comments

@boogie
Copy link

boogie commented Mar 30, 2023

As far as I see, only push, length and [num] getters are currently supported for arrays. What is the plan about adding the basics like pop, shift, unshift? Maybe some more?

@boogie boogie changed the title Plans for array operations? Plans for array prototype methods? Mar 30, 2023
@coder-mike
Copy link
Owner

The short answer is that there's a long-term plan to add them but no short-term plan, but you can add them yourself in your particular app if you need them.

The longer answer is this:

  • prototype methods like push make the snapshot larger because the prototype is almost always reachable and so can't be garbage collected prior to taking the snapshot. This is true of most of the JS built-ins.

  • I have a proof-of-concept optimizer algorithm that is able to eliminate unused prototypes and prototype methods, among other things, but I don't have time to work on it at the moment to finish it off. My focus is currently on a WASM package of the Microvium runtime and then async/await.

  • They could be added as optional libraries, so they don't add to the default snapshot size, but this just hasn't been my focus at the moment. As a rule of thumb, I'm focusing more on the syntactic features of the language, since users can add their own functions and methods as-needed but they can't add their own syntax.

Having said that, if someone in the Microvium community wanted to create a library of these methods for other people to use, that would be great.

@boogie
Copy link
Author

boogie commented Apr 3, 2023

I see that these make it larger, but hard to implement pop(), as it both returns the popped value and modifies the array. I'm happy to add them on my own and share the code, can you point me to how to do that? I would implement it in C, to not grow the bytecode significantly, but no idea how to add it to the object.

@coder-mike
Copy link
Owner

Probably something like this would work, although I haven't tried it.

const arrayProto = [].__proto__;
arrayProto.pop = function() {
  const len = this.length;
  const last = this[len - 1];
  this.length = this.length - 1;
  return last;
}

Let me know if you have issues with that.

@boogie
Copy link
Author

boogie commented Apr 7, 2023

Looks good and works well, thanks. :)

@boogie
Copy link
Author

boogie commented Apr 14, 2023

Let me close this issue, I think it is already works well enough.

@boogie boogie closed this as completed Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants