Skip to content

avm2: Implement missing AS3 RegExp features #20426

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ rand = { version = "0.8.5", features = ["std", "small_rng"], default-features =
serde = { workspace = true }
serde_json = { version = "1.0", features = ["preserve_order"] }
nellymoser-rs = { git = "https://github.com/ruffle-rs/nellymoser", rev = "073eb48d907201f46dea0c8feb4e8d9a1d92208c", optional = true }
regress = "0.10"
regress = { git = "https://github.com/evilpie/regress", branch = "as3" }
flash-lso = { git = "https://github.com/ruffle-rs/rust-flash-lso", rev = "a5e938d9bb1909095f2340c2435867f6aae930b0" }
lzma-rs = {version = "0.3.0", optional = true }
dasp = { version = "0.11.0", features = ["interpolate", "interpolate-linear", "signal"], optional = true }
Expand Down
39 changes: 25 additions & 14 deletions core/src/avm2/globals/reg_exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,27 +219,38 @@ pub fn exec<'gc>(
.unwrap_or(&Value::Undefined)
.coerce_to_string(activation)?;

let (storage, index) = match re.exec(text) {
Some(matched) => {
let substrings = matched
.groups()
.map(|range| range.map(|r| WString::from(&text[r])));

let storage = ArrayStorage::from_iter(substrings.map(|s| match s {
None => Value::Undefined,
Some(s) => AvmString::new(activation.gc(), s).into(),
}));

(storage, matched.start())
}
let matched = match re.exec(text) {
Some(matched) => matched,
None => return Ok(Value::Null),
};

let substrings = matched
.groups()
.map(|range| range.map(|r| WString::from(&text[r])));

let storage = ArrayStorage::from_iter(substrings.map(|s| match s {
None => Value::Undefined,
Some(s) => AvmString::new(activation.gc(), s).into(),
}));

let object = ArrayObject::from_storage(activation, storage);

for (name, range) in matched.named_groups() {
let string = range.map_or_else(
|| istr!(""),
|range| AvmString::new(activation.gc(), &text[range]),
);

object.set_string_property_local(
AvmString::new_utf8(activation.gc(), name),
string.into(),
activation,
)?;
}

object.set_string_property_local(
istr!("index"),
Value::Number(index as f64),
Value::Number(matched.start() as f64),
activation,
)?;

Expand Down
1 change: 1 addition & 0 deletions core/src/avm2/regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl<'gc> RegExp<'gc> {
icase: self.flags.contains(RegExpFlags::IGNORE_CASE),
multiline: self.flags.contains(RegExpFlags::MULTILINE),
dot_all: self.flags.contains(RegExpFlags::DOTALL),
extended: self.flags.contains(RegExpFlags::EXTENDED),
no_opt: false,
unicode: false,
unicode_sets: false,
Expand Down
31 changes: 31 additions & 0 deletions tests/tests/swfs/avm2/regexp_extended/Test.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package {
import flash.display.Sprite;

public class Test extends Sprite {

function Test() {
function run(url) {
trace("URL: " + url);

var regexp = /(?#comment) ((?P<protocol>[a-zA-Z]+: \/\/) (?P<host>[^:\/]*) (:(?P<port>\d+))?)? (?P<path>[^?]*)? ((?P<query>.*))? /x;
var match = regexp.exec(url);
trace("match: " + match);
trace('match["protocol"]: ' + match["protocol"]);
trace('match["host"]: ' + match["host"]);
trace('match["port"]: ' + match["port"]);
trace('match["path"]: ' + match["path"]);
trace('match["query"]: ' + match["query"]);

trace();
}

run("");
run("http://");
run("http://example.org");
run("http://example.org/abc");
run("http://example.org:80/abc");
run("http://example.org/abc?hey");
}

}
}
48 changes: 48 additions & 0 deletions tests/tests/swfs/avm2/regexp_extended/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
URL:
match: ,,,,,,,,
match["protocol"]:
match["host"]:
match["port"]:
match["path"]:
match["query"]:

URL: http://
match: http://,http://,http://,,,,,,
match["protocol"]: http://
match["host"]:
match["port"]:
match["path"]:
match["query"]:

URL: http://example.org
match: http://example.org,http://example.org,http://,example.org,,,,,
match["protocol"]: http://
match["host"]: example.org
match["port"]:
match["path"]:
match["query"]:

URL: http://example.org/abc
match: http://example.org/abc,http://example.org,http://,example.org,,,/abc,,
match["protocol"]: http://
match["host"]: example.org
match["port"]:
match["path"]: /abc
match["query"]:

URL: http://example.org:80/abc
match: http://example.org:80/abc,http://example.org:80,http://,example.org,:80,80,/abc,,
match["protocol"]: http://
match["host"]: example.org
match["port"]: 80
match["path"]: /abc
match["query"]:

URL: http://example.org/abc?hey
match: http://example.org/abc?hey,http://example.org,http://,example.org,,,/abc,?hey,?hey
match["protocol"]: http://
match["host"]: example.org
match["port"]:
match["path"]: /abc
match["query"]: ?hey

Binary file added tests/tests/swfs/avm2/regexp_extended/test.swf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/tests/swfs/avm2/regexp_extended/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_frames = 1
Loading