test-js: Add canParseSource() native function

This allows us to check code for syntax errors without relying on
Function(), which can lead to false negatives as certain things are
valid in a function context, but not outside one.
This commit is contained in:
Linus Groh 2020-11-11 22:12:51 +00:00 committed by Andreas Kling
parent 8694d804c7
commit 7fc98a96a9
Notes: sideshowbarker 2024-07-19 01:26:06 +09:00

View file

@ -106,6 +106,7 @@ private:
virtual const char* class_name() const override { return "TestRunnerGlobalObject"; }
JS_DECLARE_NATIVE_FUNCTION(is_strict_mode);
JS_DECLARE_NATIVE_FUNCTION(can_parse_source);
};
class TestRunner {
@ -159,8 +160,10 @@ void TestRunnerGlobalObject::initialize()
JS::GlobalObject::initialize();
static FlyString global_property_name { "global" };
static FlyString is_strict_mode_property_name { "isStrictMode" };
static FlyString can_parse_source_property_name { "canParseSource" };
define_property(global_property_name, this, JS::Attribute::Enumerable);
define_native_function(is_strict_mode_property_name, is_strict_mode);
define_native_function(can_parse_source_property_name, can_parse_source);
}
JS_DEFINE_NATIVE_FUNCTION(TestRunnerGlobalObject::is_strict_mode)
@ -168,6 +171,16 @@ JS_DEFINE_NATIVE_FUNCTION(TestRunnerGlobalObject::is_strict_mode)
return JS::Value(vm.in_strict_mode());
}
JS_DEFINE_NATIVE_FUNCTION(TestRunnerGlobalObject::can_parse_source)
{
auto source = vm.argument(0).to_string(global_object);
if (vm.exception())
return {};
auto parser = JS::Parser(JS::Lexer(source));
parser.parse_program();
return JS::Value(!parser.has_errors());
}
static void cleanup_and_exit()
{
// Clear the taskbar progress.