Skip to content

Commit

Permalink
avm2: Re-implement RegExp.prototype.toString
Browse files Browse the repository at this point in the history
Also remove the TObject to_string implementation that isn't necessary.
  • Loading branch information
evilpie committed Mar 10, 2024
1 parent 788c3da commit 53aee62
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
29 changes: 27 additions & 2 deletions core/src/avm2/globals/RegExp.as
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,34 @@ package {
}

prototype.toString = function():String {
return this.valueOf();
// Note: This function is not generic and will throw for non-regexps.
var regexp: RegExp = this;

// ECMA-262 Edition 5.1 - RegExp.prototype.toString():
// Return the String value formed by concatenating the Strings "/",
// the String value of the source property of this RegExp object, and "/";
// plus "g" if the global property is true,
// "i" if the ignoreCase property is true,
// and "m" if the multiline property is true.
var string = "/" + regexp.source + "/";
if (regexp.global) {
string += "g";
}
if (regexp.ignoreCase) {
string += "i";
}
if (regexp.multiline) {
string += "m";
}
if (regexp.dotall) {
string += "s";
}
if (regexp.extended) {
string += "x";
}
return string;
}

prototype.setPropertyIsEnumerable("exec", false);
prototype.setPropertyIsEnumerable("test", false);
prototype.setPropertyIsEnumerable("toString", false);
Expand Down
4 changes: 0 additions & 4 deletions core/src/avm2/object/regexp_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ impl<'gc> TObject<'gc> for RegExpObject<'gc> {
self.0.as_ptr() as *const ObjectPtr
}

fn to_string(&self, _activation: &mut Activation<'_, 'gc>) -> Result<Value<'gc>, Error<'gc>> {
Ok(Value::Object(Object::from(*self)))
}

fn value_of(&self, mc: &Mutation<'gc>) -> Result<Value<'gc>, Error<'gc>> {
let read = self.0.read();
let mut s = WString::new();
Expand Down

0 comments on commit 53aee62

Please sign in to comment.