LibJS: Create the RegExpExec result's "input" field last

We move the input string into this field to avoid a string copy, so we
must do this step last to avoid using any views into it (note that
match.view here is a view into this string).
This commit is contained in:
Timothy Flynn 2021-11-07 18:49:04 -05:00 committed by Andreas Kling
parent 79fa9765ca
commit 2530b6adf0
Notes: sideshowbarker 2024-07-18 01:23:02 +09:00
2 changed files with 8 additions and 1 deletions

View file

@ -241,8 +241,8 @@ static ThrowCompletionOr<Value> regexp_builtin_exec(GlobalObject& global_object,
}
MUST(array->create_data_property_or_throw(vm.names.index, Value(match_index)));
MUST(array->create_data_property_or_throw(vm.names.input, js_string(vm, move(string))));
MUST(array->create_data_property_or_throw(0, js_string(vm, match.view.u16_view())));
MUST(array->create_data_property_or_throw(vm.names.input, js_string(vm, move(string))));
return array;
}

View file

@ -205,3 +205,10 @@ test("multiline stateful match", () => {
);
expect(res.index).toBe(231);
});
test("string coercion", () => {
let result = /1/.exec(1);
expect(result.length).toBe(1);
expect(result[0]).toBe("1");
expect(result.index).toBe(0);
});