mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:58:11 +00:00
LibJS: Add a barebones SourceTextModule class
This corresponds to "Source Text Module Record" from the spec.
This commit is contained in:
parent
405b8e7915
commit
5fa02b8a9e
3 changed files with 65 additions and 0 deletions
|
@ -190,6 +190,7 @@ set(SOURCES
|
||||||
Runtime/WeakSetConstructor.cpp
|
Runtime/WeakSetConstructor.cpp
|
||||||
Runtime/WeakSetPrototype.cpp
|
Runtime/WeakSetPrototype.cpp
|
||||||
Script.cpp
|
Script.cpp
|
||||||
|
SourceTextModule.cpp
|
||||||
SyntaxHighlighter.cpp
|
SyntaxHighlighter.cpp
|
||||||
Token.cpp
|
Token.cpp
|
||||||
)
|
)
|
||||||
|
|
36
Userland/Libraries/LibJS/SourceTextModule.cpp
Normal file
36
Userland/Libraries/LibJS/SourceTextModule.cpp
Normal file
|
@ -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()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
28
Userland/Libraries/LibJS/SourceTextModule.h
Normal file
28
Userland/Libraries/LibJS/SourceTextModule.h
Normal file
|
@ -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]]
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue