-
-
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
Add String.search(regexp) #887
Conversation
Codecov Report
@@ Coverage Diff @@
## master #887 +/- ##
==========================================
- Coverage 59.28% 59.19% -0.10%
==========================================
Files 155 165 +10
Lines 9924 10397 +473
==========================================
+ Hits 5883 6154 +271
- Misses 4041 4243 +202
Continue to review full report at Codecov.
|
@flix477 you made this branch over an old commit of the |
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.
Looking good, but needs some fixes. I also don't think your implementation of String.prototype.search
is spec-compliant, rather than building a RegExp
object regardless you should check for the @@search
property. I don't think GetMethod
is currently defined, but getting the property and checking whether it is a function is enough for now, probably leave a TODO in a comment mentioning it.
if !this.is_object() { | ||
panic!("Not an object"); | ||
} |
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.
According to point 2. of the spec this should throw a TypeError
.
As an example to test this: RegExp.prototype[Symbol.search].call(0, 'test')
in Chrome throws: TypeError: Method RegExp.prototype.@@search called on incompatible receiver 0
.
I'm not sure .prototype
is accessible in boa
so I don't know if we can test this.
/// | ||
/// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype-@@search | ||
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/@@search | ||
pub(crate) fn search(this: &Value, arg_str: String, ctx: &mut Context) -> Result<Value> { |
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.
Someone else can confirm, but I'm pretty sure the signature needs to be (this: &Value, args: &[Value], ctx: &mut Context) -> Result<Value>
, all builin functions have the same signature.
This also needs to be declared as a method of the RegExp
global object, you can see some examples here:
boa/boa/src/builtins/regexp/mod.rs
Lines 81 to 83 in 2cb2442
.method(Self::test, "test", 1) | |
.method(Self::exec, "exec", 1) | |
.method(Self::to_string, "toString", 0) |
Note that after the algorithm definition the spec mentions the
"name"
of the function should is "[Symbol.search]"
.
/// [regex]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions | ||
pub(crate) fn search(this: &Value, args: &[Value], ctx: &mut Context) -> Result<Value> { | ||
let re = match args.get(0) { | ||
Some(arg) if !arg.is_null() && !arg.is_undefined() => RegExp::constructor( |
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.
Can use just 1 function to check for both cases of null
and undefined
:
Lines 311 to 313 in 2cb2442
pub fn is_null_or_undefined(&self) -> bool { | |
matches!(self, Self::Null | Self::Undefined) | |
} |
Hi @flix477, please, let us know the state of the changes proposed in this PR :) Check the comments by @HalidOdat and let us know if you have any doubts or want any help! |
Hey @flix477 how is this going? |
We should close this PR. Both |
This Pull Request fixes/closes #117 .
It changes the following:
This is my first PR here, comments are welcome!