mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:17:34 +00:00
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:
parent
8694d804c7
commit
7fc98a96a9
1 changed files with 13 additions and 0 deletions
|
@ -106,6 +106,7 @@ private:
|
||||||
virtual const char* class_name() const override { return "TestRunnerGlobalObject"; }
|
virtual const char* class_name() const override { return "TestRunnerGlobalObject"; }
|
||||||
|
|
||||||
JS_DECLARE_NATIVE_FUNCTION(is_strict_mode);
|
JS_DECLARE_NATIVE_FUNCTION(is_strict_mode);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(can_parse_source);
|
||||||
};
|
};
|
||||||
|
|
||||||
class TestRunner {
|
class TestRunner {
|
||||||
|
@ -159,8 +160,10 @@ void TestRunnerGlobalObject::initialize()
|
||||||
JS::GlobalObject::initialize();
|
JS::GlobalObject::initialize();
|
||||||
static FlyString global_property_name { "global" };
|
static FlyString global_property_name { "global" };
|
||||||
static FlyString is_strict_mode_property_name { "isStrictMode" };
|
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_property(global_property_name, this, JS::Attribute::Enumerable);
|
||||||
define_native_function(is_strict_mode_property_name, is_strict_mode);
|
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)
|
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());
|
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()
|
static void cleanup_and_exit()
|
||||||
{
|
{
|
||||||
// Clear the taskbar progress.
|
// Clear the taskbar progress.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue