summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-09-14 20:51:16 +0200
committerAndreas Kling <kling@serenityos.org>2021-09-14 21:41:51 +0200
commit5fa02b8a9e41f680a7996fee3f5f5f708d0ec638 (patch)
treee46ea116d76204b2d2d4bf5a34cb4a6d86b96ecf /Userland
parent405b8e79152da8049a2bf0a3c1e67486f362ee5d (diff)
downloadserenity-5fa02b8a9e41f680a7996fee3f5f5f708d0ec638.zip
LibJS: Add a barebones SourceTextModule class
This corresponds to "Source Text Module Record" from the spec.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/CMakeLists.txt1
-rw-r--r--Userland/Libraries/LibJS/SourceTextModule.cpp36
-rw-r--r--Userland/Libraries/LibJS/SourceTextModule.h28
3 files changed, 65 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/CMakeLists.txt b/Userland/Libraries/LibJS/CMakeLists.txt
index 82e3714450..35330bfdba 100644
--- a/Userland/Libraries/LibJS/CMakeLists.txt
+++ b/Userland/Libraries/LibJS/CMakeLists.txt
@@ -190,6 +190,7 @@ set(SOURCES
Runtime/WeakSetConstructor.cpp
Runtime/WeakSetPrototype.cpp
Script.cpp
+ SourceTextModule.cpp
SyntaxHighlighter.cpp
Token.cpp
)
diff --git a/Userland/Libraries/LibJS/SourceTextModule.cpp b/Userland/Libraries/LibJS/SourceTextModule.cpp
new file mode 100644
index 0000000000..1e6235f132
--- /dev/null
+++ b/Userland/Libraries/LibJS/SourceTextModule.cpp
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibJS/SourceTextModule.h>
+
+namespace JS {
+
+// 16.2.1.6.1 ParseModule ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parsemodule
+Result<NonnullRefPtr<SourceTextModule>, Vector<Parser::Error>> SourceTextModule::parse(StringView source_text, Realm& realm, [[maybe_unused]] StringView filename)
+{
+ // 1. Let body be ParseText(sourceText, Module).
+ auto parser = Parser(Lexer(source_text, filename), Program::Type::Module);
+ auto body = parser.parse_program();
+
+ // 2. If body is a List of errors, return body.
+ if (parser.has_errors())
+ return parser.errors();
+
+ // FIXME: Implement the rest of ParseModule.
+ return adopt_ref(*new SourceTextModule(realm, move(body)));
+}
+
+SourceTextModule::SourceTextModule(Realm& realm, NonnullRefPtr<Program> program)
+ : Module(realm)
+ , m_ecmascript_code(move(program))
+{
+}
+
+SourceTextModule::~SourceTextModule()
+{
+}
+
+}
diff --git a/Userland/Libraries/LibJS/SourceTextModule.h b/Userland/Libraries/LibJS/SourceTextModule.h
new file mode 100644
index 0000000000..ac3cba673f
--- /dev/null
+++ b/Userland/Libraries/LibJS/SourceTextModule.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibJS/AST.h>
+#include <LibJS/Forward.h>
+#include <LibJS/Module.h>
+#include <LibJS/Parser.h>
+
+namespace JS {
+
+// 16.2.1.6 Source Text Module Records, https://tc39.es/ecma262/#sec-source-text-module-records
+class SourceTextModule final : public Module {
+public:
+ static Result<NonnullRefPtr<SourceTextModule>, Vector<Parser::Error>> parse(StringView source_text, Realm&, StringView filename = {});
+ virtual ~SourceTextModule();
+
+private:
+ explicit SourceTextModule(Realm&, NonnullRefPtr<Program>);
+
+ NonnullRefPtr<Program> m_ecmascript_code; // [[ECMAScriptCode]]
+};
+
+}