Coder Social home page Coder Social logo

Comments (14)

neeldug avatar neeldug commented on April 20, 2024 1

@jasonwilliams I believe this list is missing replaceAll.

from boa.

calluw avatar calluw commented on April 20, 2024

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:

let a = "Hello world".chatAt(2);
console.log(a);

Results in the buffer holding only: "Error: undefined"

Is this a known issue or needs to be resolved before prototype methods can be added?

from boa.

jasonwilliams avatar jasonwilliams commented on April 20, 2024

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.

https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch3.md#boxing-wrappers

from boa.

calluw avatar calluw commented on April 20, 2024

Thanks @jasonwilliams, generating the string via new String(..) is working with charAt(), charCodeAt(). Will now test with my new implementations.

from boa.

calluw avatar calluw commented on April 20, 2024

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 to_value() they come out as "string literals" again. This means you can't do the following:

let a = new String("Hello world");
a.concat("! ").repeat(2);

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)?

from boa.

jasonwilliams avatar jasonwilliams commented on April 20, 2024

Now you're asking the interesting questions!
We can't do this because the return type of those methods are string types not objects.
Those strings would need to be re-boxed again (yep this is why boxing/un-boxing is a big performance hit and engines often try to find ways around it).

It would be against the spec to return a String instance instead of a string literal.
https://tc39.es/ecma262/#sec-string.prototype.concat

Would this be solved by auto-boxing string literals (#29)?

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.
In theory we can detect a method being called on a string, so at that point we could convert into a String instance call the method then throw away the instance (wasteful).

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

from boa.

calluw avatar calluw commented on April 20, 2024

@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().

from boa.

brian-gavin avatar brian-gavin commented on April 20, 2024

Hey, the links in this issue are out of date due to some code reorg!

Current location of String is here

from boa.

Razican avatar Razican commented on April 20, 2024

Hey, the links in this issue are out of date due to some code reorg!

Current location of String is here

Good catch! I updated the links, but note that some of this is probably blocked until #419 lands :)

from boa.

HalidOdat avatar HalidOdat commented on April 20, 2024

@joshwd36 has implemented String.prototype[@@Symbol.iterator] in #704 , keeping track who has implemented what :)

from boa.

RageKnify avatar RageKnify commented on April 20, 2024

Added split(), being worked on in #1026

from boa.

RageKnify avatar RageKnify commented on April 20, 2024

@neeldug there's still a few methods in the check list, should this issue be closed?

from boa.

neeldug avatar neeldug commented on April 20, 2024

Shouldn’t be closed, only checked off.

from boa.

jedel1043 avatar jedel1043 commented on April 20, 2024

Closing in favour of #1562.

from boa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.