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

Fix: String.prototype.replace substitutions, closes #610 #629

Merged
merged 9 commits into from
Sep 1, 2020

Conversation

RageKnify
Copy link
Member

@RageKnify RageKnify commented Aug 13, 2020

This Pull Request fixes/closes #610 .

It changes the following:

@codecov
Copy link

codecov bot commented Aug 13, 2020

Codecov Report

Merging #629 into master will increase coverage by 0.64%.
The diff coverage is 80.76%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #629      +/-   ##
==========================================
+ Coverage   72.50%   73.14%   +0.64%     
==========================================
  Files         179      192      +13     
  Lines       13376    14005     +629     
==========================================
+ Hits         9698    10244     +546     
- Misses       3678     3761      +83     
Impacted Files Coverage Δ
boa/src/builtins/string/mod.rs 50.95% <72.22%> (+5.13%) ⬆️
boa/src/builtins/string/tests.rs 100.00% <100.00%> (ø)
...x/parser/expression/primary/function_expression.rs 64.70% <0.00%> (-25.30%) ⬇️
boa/src/syntax/parser/expression/mod.rs 78.48% <0.00%> (-10.92%) ⬇️
boa/src/builtins/object/mod.rs 30.35% <0.00%> (-8.82%) ⬇️
boa/src/exec/switch/mod.rs 59.45% <0.00%> (-8.55%) ⬇️
boa/src/exec/new/mod.rs 66.66% <0.00%> (-8.34%) ⬇️
boa/src/syntax/parser/expression/assignment/mod.rs 62.06% <0.00%> (-7.94%) ⬇️
boa/src/lib.rs 73.91% <0.00%> (-7.91%) ⬇️
boa/src/exec/block/mod.rs 61.11% <0.00%> (-7.64%) ⬇️
... and 89 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 14d7791...54df208. Read the comment docs.

RageKnify added a commit to RageKnify/boa that referenced this pull request Aug 13, 2020
Multi-line description of commit,
feel free to be detailed.
boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
@Razican Razican added this to the v0.10.0 milestone Aug 14, 2020
@Razican Razican added bug Something isn't working builtins PRs and Issues related to builtins/intrinsics labels Aug 14, 2020
@RageKnify
Copy link
Member Author

I'm not gonna be working for a week, but I found what I think was an old implementation of the method from V8 in js.
The main change is that we need to go through the replacement string left to right copying characters to the result string and for $ we check if it is part of one of the patterns described in the table (there's a link to the spec in a nearby comment), and if so replace it accordingly.

From a quick glance I think we need to invoke chars on the result string to iterate over them and then peek ahead when we spot $?

@RageKnify
Copy link
Member Author

I've found another bug in the implementation of `String.prototype.replace', copied from Chrome:

> let str = 'boa';
> str.replace(/.*/, "??$&??")
"??boa??"
> str.replace(".*", "??$&??")
"boa"

While on boa both would return "??boa??" because the string ".*" is converted to a RegEx pattern, I'm gonna try to make String.prototype.replace follow the spec more closely.

@RageKnify
Copy link
Member Author

RageKnify commented Aug 24, 2020

I ended up not fixing the other bug, my intention was to follow the spec, but it requires RegExp.prototype [ @@replace ] to be implemented for invocations of String.prototype.replace to work and right now I don't believe it is defined/implemented I think we have an approximation of the implementation here in String.prototype.replace.

I think symbol_properties are working fine, we're just missing the implementation of the function and the initialization of it at startup.

Another thing to keep in mind is regress, which was mentioned in #478 I believe regress's API is lacking right now but it would allow boa not to worry about implementing RegEx. Currently regress isn't very active, but we could try to get it going, probably try to identify which methods we would need and go from there.

@RageKnify
Copy link
Member Author

RageKnify commented Aug 24, 2020

When I get back I'll implement tests for $$, $&, $', $nn, '$`'

PS: Just now realized that I could use tuple pattern matching instead of using matchs inside matchs. I'll try to use that, it'll make it more understandable and shorter.

@RageKnify RageKnify requested a review from Razican August 24, 2020 16:49
@RageKnify RageKnify changed the title Fix: String.prototype.replace for multiple capture groups, closes #610 Fix: String.prototype.replace substitutions, closes #610 Aug 24, 2020
Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks pretty good :) check my comments, they are mostly for my understanding. Also, in the future, we will want to move to regress for regular expressions, as in #478, and it might in fact prove to be easier to implement with it.

boa/src/builtins/string/mod.rs Show resolved Hide resolved
boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
Introduce early return if `regexp|substr` isn't found in `this`
Add test case covering this situation
@RageKnify RageKnify requested a review from Razican August 25, 2020 11:51
Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good to know from my side now!

@RageKnify
Copy link
Member Author

@jasonwilliams @HalidOdat @Lan2u sorry to tag, but it's been waiting for a 2nd review for a week

Copy link
Member

@HalidOdat HalidOdat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good! check my comments to see on how we might improve it :)

Sorry for the long delay I haven't had that much free time this week.

boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/string/mod.rs Outdated Show resolved Hide resolved
RageKnify added a commit to RageKnify/boa that referenced this pull request Sep 1, 2020
@HalidOdat HalidOdat merged commit be20b65 into boa-dev:master Sep 1, 2020
@RageKnify RageKnify deleted the fix/string-replace branch September 1, 2020 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working builtins PRs and Issues related to builtins/intrinsics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error in RegExp
3 participants