-
-
Notifications
You must be signed in to change notification settings - Fork 401
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
Implement String prototype methods #13
Comments
I have started to write some implementations for concat() and repeat() but have encountered an issue: currently none of the String prototype methods work, even existing charAt and charCodeAt: Using:
Results in the buffer holding only: "Error: undefined" Is this a known issue or needs to be resolved before prototype methods can be added? |
Hey @CallumQuick hmm that’s interesting could you try doing new String(“Hello world”) instead of “Hello world”.charAt() ? I think that may work. When you make a string literal and immediately use a string prototype method on it, you’re using a string object prototype method on a literal string. Engines will box that value into a String object, Boa doesn’t do this yet, so that will most likely be the reason it doesn’t work. There is still some work to do here around boxing and unboxing and the best approach. |
Thanks @jasonwilliams, generating the string via |
On the topic of the boxing of the primitive string, the only current issue with concat(), repeat(), slice() and the other methods that should return strings is that through
Is there an easy way to cause concat, repeat etc to return not just string "values" but string objects? Would this be solved by auto-boxing string literals (#29)? |
Now you're asking the interesting questions! It would be against the spec to return a String instance instead of a string literal.
Basically yes. I think some research would need to be done in this area as i believe there are ways to optimise this operation rather than simply boxing and unboxing again. There is 1 place where i break this rule and just give back the value, that is when calling length on a string literal. https://github.com/jasonwilliams/boa/blob/master/src/lib/js/value.rs#L202-L208 |
@jasonwilliams Thanks for clarifying the spec's position (I had overlooked the subtlety of the concat/repeat/slice methods returning values only). As a heads up, next I will probably take a look at the set of these methods which take a "searchString" argument (so not patterns yet): startsWith(), endsWith(), includes(), indexOf() and lastIndexOf(). |
…type methods (#13) (#31) * New concat() and repeat() String prototype implementations Created new implementations for the JS String prototype which are added in the string.rs file. Currently not tested. * Add additional implementation of slice() * Fix typo which mixed up start and end of slice * Simplify slice() implementation * Implementation of String.prototype.startsWith() * Add implementation of endsWith() * Implementation of String.prototype.includes() * Implementation of String.prototype.{indexOf, lastIndexOf}() * Fix merge conflict missing closing brace
Hey, the links in this issue are out of date due to some code reorg! Current location of String is here |
Added |
@jasonwilliams I believe this list is missing replaceAll. |
- adds string.prototype.normalize - uses `unicode_normalization` crate Closes boa-dev#13
- match method adheres to spec - regexp method simplified to follow spec Closes boa-dev#13
@neeldug there's still a few methods in the check list, should this issue be closed? |
Shouldn’t be closed, only checked off. |
Closing in favour of #1562. |
<!--- Thank you for contributing to Boa! Please fill out the template below, and remove or add any information as you feel neccesary. ---> This Pull Request fixes existing string prototype methods in #13 and adds static methods. It changes the following: - Fix bugs in existing string prototype methods and improve readability (e.g. rename variables to match the names in spec) - Add static methods `String.raw`, `String.fromCharCode`, `String.fromCodePoint` - Fix broken unit tests Co-authored-by: RageKnify <RageKnify@gmail.com>
String prototype methods
These methods would be added to the String prototype object which is implemented here:
https://github.com/boa-dev/boa/blob/master/boa/src/builtins/string/mod.rs
Spec: https://tc39.es/ecma262/#sec-properties-of-the-string-prototype-object
Implementation Example?
See charAt() and charCodeAt() for a good example
The text was updated successfully, but these errors were encountered: