Browse Source

Fix regexp `toString` method (#3608)

pull/3611/head
raskad 10 months ago committed by GitHub
parent
commit
48062d76f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 33
      core/engine/src/builtins/regexp/mod.rs

33
core/engine/src/builtins/regexp/mod.rs

@ -1268,18 +1268,27 @@ impl RegExp {
/// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.tostring /// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.tostring
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/toString /// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/toString
#[allow(clippy::wrong_self_convention)] #[allow(clippy::wrong_self_convention)]
pub(crate) fn to_string(this: &JsValue, _: &[JsValue], _: &mut Context) -> JsResult<JsValue> { pub(crate) fn to_string(
let (body, flags) = this this: &JsValue,
.as_object() _: &[JsValue],
.and_then(JsObject::downcast_ref::<RegExp>) context: &mut Context,
.map(|rx| (rx.original_source.clone(), rx.original_flags.clone())) ) -> JsResult<JsValue> {
.ok_or_else(|| { // 1. Let R be the this value.
JsNativeError::typ().with_message(format!( // 2. If R is not an Object, throw a TypeError exception.
"Method RegExp.prototype.toString called on incompatible receiver {}", let regexp = this.as_object().ok_or_else(|| {
this.display() JsNativeError::typ()
)) .with_message("RegExp.prototype.toString method called on incompatible value")
})?; })?;
Ok(js_string!(utf16!("/"), &body, utf16!("/"), &flags).into())
// 3. Let pattern be ? ToString(? Get(R, "source")).
let pattern = regexp.get(utf16!("source"), context)?.to_string(context)?;
// 4. Let flags be ? ToString(? Get(R, "flags")).
let flags = regexp.get(utf16!("flags"), context)?.to_string(context)?;
// 5. Let result be the string-concatenation of "/", pattern, "/", and flags.
// 6. Return result.
Ok(js_string!(utf16!("/"), &pattern, utf16!("/"), &flags).into())
} }
/// `RegExp.prototype[ @@matchAll ]( string )` /// `RegExp.prototype[ @@matchAll ]( string )`

Loading…
Cancel
Save