mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:47:35 +00:00
LibJS: Implement RegExp.prototype.replace with RegExpExec abstraction
Also rename the 'rx' variable to 'regexp_object' to match other RegExp methods.
This commit is contained in:
parent
6c53475143
commit
ec898a3370
2 changed files with 42 additions and 12 deletions
|
@ -330,8 +330,8 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
||||||
auto string_value = vm.argument(0);
|
auto string_value = vm.argument(0);
|
||||||
auto replace_value = vm.argument(1);
|
auto replace_value = vm.argument(1);
|
||||||
|
|
||||||
auto rx = regexp_object_from(vm, global_object);
|
auto* regexp_object = regexp_object_from(vm, global_object);
|
||||||
if (!rx)
|
if (!regexp_object)
|
||||||
return {};
|
return {};
|
||||||
auto string = string_value.to_string(global_object);
|
auto string = string_value.to_string(global_object);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
|
@ -347,26 +347,21 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto global_value = rx->get(vm.names.global);
|
auto global_value = regexp_object->get(vm.names.global);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
bool global = global_value.to_boolean();
|
bool global = global_value.to_boolean();
|
||||||
if (global) {
|
if (global) {
|
||||||
rx->set(vm.names.lastIndex, Value(0), true);
|
regexp_object->set(vm.names.lastIndex, Value(0), true);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Implement and use RegExpExec - https://tc39.es/ecma262/#sec-regexpexec
|
|
||||||
auto* exec = Value(rx).get_method(global_object, vm.names.exec);
|
|
||||||
if (!exec)
|
|
||||||
return {};
|
|
||||||
|
|
||||||
MarkedValueList results(vm.heap());
|
MarkedValueList results(vm.heap());
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
auto result = vm.call(*exec, rx, string_value);
|
auto result = regexp_exec(global_object, *regexp_object, string);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
if (result.is_null())
|
if (result.is_null())
|
||||||
|
@ -390,13 +385,13 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_replace)
|
||||||
if (match_str.is_empty()) {
|
if (match_str.is_empty()) {
|
||||||
// FIXME: Implement AdvanceStringIndex to take Unicode code points into account - https://tc39.es/ecma262/#sec-advancestringindex
|
// FIXME: Implement AdvanceStringIndex to take Unicode code points into account - https://tc39.es/ecma262/#sec-advancestringindex
|
||||||
// Once implemented, step (8a) of the @@replace algorithm must also be implemented.
|
// Once implemented, step (8a) of the @@replace algorithm must also be implemented.
|
||||||
auto last_index = rx->get(vm.names.lastIndex);
|
auto last_index = regexp_object->get(vm.names.lastIndex);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
auto this_index = last_index.to_length(global_object);
|
auto this_index = last_index.to_length(global_object);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
rx->set(vm.names.lastIndex, Value(this_index + 1), true);
|
regexp_object->set(vm.names.lastIndex, Value(this_index + 1), true);
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
|
@ -197,3 +197,38 @@ test("search value is coerced to a string", () => {
|
||||||
expect(newString).toBe("abc");
|
expect(newString).toBe("abc");
|
||||||
expect(coerced).toBe("x");
|
expect(coerced).toBe("x");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("override exec with function", () => {
|
||||||
|
let calls = 0;
|
||||||
|
|
||||||
|
let re = /test/;
|
||||||
|
let oldExec = re.exec.bind(re);
|
||||||
|
re.exec = function (...args) {
|
||||||
|
++calls;
|
||||||
|
return oldExec(...args);
|
||||||
|
};
|
||||||
|
|
||||||
|
expect("test".replace(re, "x")).toBe("x");
|
||||||
|
expect(calls).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("override exec with bad function", () => {
|
||||||
|
let calls = 0;
|
||||||
|
|
||||||
|
let re = /test/;
|
||||||
|
re.exec = function (...args) {
|
||||||
|
++calls;
|
||||||
|
return 4;
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
"test".replace(re, "x");
|
||||||
|
}).toThrow(TypeError);
|
||||||
|
expect(calls).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("override exec with non-function", () => {
|
||||||
|
let re = /test/;
|
||||||
|
re.exec = 3;
|
||||||
|
expect("test".replace(re, "x")).toBe("x");
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue