diff options
author | Linus Groh <mail@linusgroh.de> | 2020-11-11 22:12:51 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-11-12 10:14:57 +0100 |
commit | 7fc98a96a98e02b2d6693cc5858edf6c48689f69 (patch) | |
tree | 8dccad85caab7b9d4648ad7034dcdad09b9e6d18 /Userland | |
parent | 8694d804c7391c9f4215b5c567fed0877317aeb0 (diff) | |
download | serenity-7fc98a96a98e02b2d6693cc5858edf6c48689f69.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/test-js.cpp | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/Userland/test-js.cpp b/Userland/test-js.cpp index 1b817ddc35..42388a8d7a 100644 --- a/Userland/test-js.cpp +++ b/Userland/test-js.cpp @@ -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. |