mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 22:47:44 +00:00
LibJS: Implement RegExp.prototype.test with RegExpExec abstraction
This commit is contained in:
parent
a90d5cb622
commit
6c53475143
2 changed files with 38 additions and 21 deletions
|
@ -260,9 +260,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::exec)
|
||||||
// 22.2.5.15 RegExp.prototype.test ( S ), https://tc39.es/ecma262/#sec-regexp.prototype.test
|
// 22.2.5.15 RegExp.prototype.test ( S ), https://tc39.es/ecma262/#sec-regexp.prototype.test
|
||||||
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
|
JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
|
||||||
{
|
{
|
||||||
// FIXME: This should try using dynamic properties for 'exec' first,
|
auto* regexp_object = regexp_object_from(vm, global_object);
|
||||||
// before falling back to builtin_exec.
|
|
||||||
auto regexp_object = regexp_object_from(vm, global_object);
|
|
||||||
if (!regexp_object)
|
if (!regexp_object)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
|
@ -270,27 +268,11 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::test)
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
// RegExps without "global" and "sticky" always start at offset 0.
|
auto match = regexp_exec(global_object, *regexp_object, str);
|
||||||
if (!regexp_object->regex().options().has_flag_set((ECMAScriptFlags)regex::AllFlags::Internal_Stateful)) {
|
|
||||||
regexp_object->set(vm.names.lastIndex, Value(0), true);
|
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto last_index = regexp_object->get(vm.names.lastIndex);
|
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
regexp_object->regex().start_offset = last_index.to_length(global_object);
|
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
auto result = do_match(regexp_object->regex(), str);
|
return Value(!match.is_null());
|
||||||
|
|
||||||
regexp_object->set(vm.names.lastIndex, Value(regexp_object->regex().start_offset), true);
|
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
return Value(result.success);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 22.2.5.16 RegExp.prototype.toString ( ), https://tc39.es/ecma262/#sec-regexp.prototype.tostring
|
// 22.2.5.16 RegExp.prototype.toString ( ), https://tc39.es/ecma262/#sec-regexp.prototype.tostring
|
||||||
|
|
|
@ -56,3 +56,38 @@ test("flag and options", () => {
|
||||||
Function("/foo/x");
|
Function("/foo/x");
|
||||||
}).toThrowWithMessage(SyntaxError, "Invalid RegExp flag 'x'");
|
}).toThrowWithMessage(SyntaxError, "Invalid RegExp flag '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(re.test("test")).toBe(true);
|
||||||
|
expect(calls).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("override exec with bad function", () => {
|
||||||
|
let calls = 0;
|
||||||
|
|
||||||
|
let re = /test/;
|
||||||
|
re.exec = function (...args) {
|
||||||
|
++calls;
|
||||||
|
return 4;
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
re.test("test");
|
||||||
|
}).toThrow(TypeError);
|
||||||
|
expect(calls).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("override exec with non-function", () => {
|
||||||
|
let re = /test/;
|
||||||
|
re.exec = 3;
|
||||||
|
expect(re.test("test")).toBe(true);
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue