LibJS: Implement the ParseScript AO (as JS::Script::parse())

This commit is contained in:
Andreas Kling 2021-09-09 19:02:40 +02:00
parent 35c4a1ebcb
commit d823d7da54
Notes: sideshowbarker 2024-07-18 04:22:08 +09:00
2 changed files with 17 additions and 7 deletions

View file

@ -4,16 +4,26 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/AST.h>
#include <LibJS/Lexer.h>
#include <LibJS/Parser.h>
#include <LibJS/Script.h>
namespace JS {
NonnullRefPtr<Script> Script::create(GlobalObject& global_object, NonnullRefPtr<ASTNode> parse_node)
// 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
NonnullRefPtr<Script> Script::parse(StringView source_text, GlobalObject& global_object)
{
return adopt_ref(*new Script(global_object, move(parse_node)));
// 1. Let body be ParseText(sourceText, Script).
auto body = Parser(Lexer(source_text)).parse_program();
// FIXME: 2. If body is a List of errors, return body.
// 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
return adopt_ref(*new Script(global_object, move(body)));
}
Script::Script(GlobalObject& global_object, NonnullRefPtr<ASTNode> parse_node)
Script::Script(GlobalObject& global_object, NonnullRefPtr<Program> parse_node)
: m_global_object(make_handle(&global_object))
, m_parse_node(move(parse_node))
{

View file

@ -18,16 +18,16 @@ namespace JS {
class Script : public RefCounted<Script> {
public:
~Script();
static NonnullRefPtr<Script> create(GlobalObject&, NonnullRefPtr<ASTNode> parse_node);
static NonnullRefPtr<Script> parse(StringView source_text, GlobalObject&);
GlobalObject& global_object() { return *m_global_object.cell(); }
ASTNode const& parse_node() const { return *m_parse_node; }
Program const& parse_node() const { return *m_parse_node; }
private:
Script(GlobalObject&, NonnullRefPtr<ASTNode>);
Script(GlobalObject&, NonnullRefPtr<Program>);
Handle<GlobalObject> m_global_object;
NonnullRefPtr<ASTNode> m_parse_node;
NonnullRefPtr<Program> m_parse_node;
};
}