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

Implement regress #774

Merged
merged 13 commits into from
Oct 6, 2020
Prev Previous commit
Next Next commit
Fix: Compile with regress #686, failing tests
  • Loading branch information
RageKnify authored and neeldug committed Sep 23, 2020
commit 8d6492b08d088a223d0b43242b8026fa25759f2d
75 changes: 38 additions & 37 deletions boa/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,18 @@ impl RegExp {
let mut last_index = this.get_field("lastIndex").to_index(ctx)?;
let result = if let Some(object) = this.as_object() {
let regex = object.as_regexp().unwrap();
let result = if let Some(m) = regex.matcher.find_from(arg_str.as_str(), last_index) {
if regex.use_last_index {
last_index = m.end();
}
true
} else {
if regex.use_last_index {
last_index = 0;
}
false
};
let result =
if let Some(m) = regex.matcher.find_from(arg_str.as_str(), last_index).next() {
if regex.use_last_index {
last_index = m.total().end;
}
true
} else {
if regex.use_last_index {
last_index = 0;
}
false
};
Ok(Value::boolean(result))
} else {
panic!("object is not a regexp")
Expand Down Expand Up @@ -330,35 +331,35 @@ impl RegExp {
let mut last_index = this.get_field("lastIndex").to_index(ctx)?;
let result = if let Some(object) = this.as_object() {
let regex = object.as_regexp().unwrap();
let mut locations = regex.matcher.capture_locations();
let result = if let Some(m) =
regex
.matcher
.captures_read_at(&mut locations, arg_str.as_str(), last_index)
{
if regex.use_last_index {
last_index = m.end();
}
let mut result = Vec::with_capacity(locations.len());
for i in 0..locations.len() {
if let Some((start, end)) = locations.get(i) {
result.push(Value::from(
arg_str.get(start..end).expect("Could not get slice"),
));
} else {
result.push(Value::undefined());
let result = {
if let Some(m) = regex.matcher.find_from(arg_str.as_str(), last_index).next() {
if regex.use_last_index {
last_index = m.total().end;
}
let mut result = Vec::with_capacity(m.captures.len());
for i in 0..m.captures.len() {
if let Some(range) = m.group(i) {
result.push(Value::from(
arg_str.get(range).expect("Could not get slice"),
));
} else {
result.push(Value::undefined());
}
}
}

let result = Value::from(result);
result.set_property("index", Property::default().value(Value::from(m.start())));
result.set_property("input", Property::default().value(Value::from(arg_str)));
result
} else {
if regex.use_last_index {
last_index = 0;
let result = Value::from(result);
result.set_property(
"index",
Property::default().value(Value::from(m.total().start)),
);
result.set_property("input", Property::default().value(Value::from(arg_str)));
result
} else {
if regex.use_last_index {
last_index = 0;
}
Value::null()
}
Value::null()
};
Ok(result)
} else {
Expand Down
31 changes: 22 additions & 9 deletions boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,13 @@ impl String {
}
(Some('&'), _) => {
// $&
let matched = caps.get(0).expect("cannot get matched value");
result.push_str(&primitive_val[matched.unwrap()]);
let matched = caps
.get(0)
.expect("cannot get matched value's Option")
.as_ref()
.expect("cannot get matched value's Range")
.clone();
result.push_str(&primitive_val[matched]);
}
(Some('`'), _) => {
// $`
Expand All @@ -516,8 +521,8 @@ impl String {
}
} else {
let group = match caps.get(nn) {
Some(text) => &primitive_val[text.unwrap()],
None => "",
Some(Some(range)) => &primitive_val[range.clone()],
_ => "",
};
result.push_str(group);
chars.next(); // consume third
Expand All @@ -531,8 +536,8 @@ impl String {
result.push(second);
} else {
let group = match caps.get(n) {
Some(text) => &primitive_val[text.unwrap()],
None => "",
Some(Some(range)) => &primitive_val[range.clone()],
_ => "",
};
result.push_str(group);
}
Expand Down Expand Up @@ -561,14 +566,22 @@ impl String {
// This will return the matched substring first, then captured parenthesized groups later
let mut results: Vec<Value> = caps
.iter()
.map(|capture| Value::from(&primitive_val[capture.unwrap()]))
.map(|option| {
if let Some(range) = option {
Value::from(&primitive_val[range.clone()])
} else {
Value::undefined()
}
})
.collect();

// Returns the starting byte offset of the match
let start = caps
.get(0)
.expect("Unable to get Byte offset from string for match")
.unwrap()
.expect("cannot get matched value's Option")
.as_ref()
.expect("cannot get matched value's Range")
.clone()
.start;
results.push(Value::from(start));
// Push the whole string being examined
Expand Down