1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

LibJS: Defer Value construction until a Literal is executed

Remove the need to construct a full Value during parsing. This means
we don't have to worry about plumbing the heap into the parser.

The Literal ASTNode now has a bunch of subclasses that synthesize a
Value on demand.
This commit is contained in:
Andreas Kling 2020-03-12 12:19:11 +01:00
parent 4d942cc1d0
commit 425fd3ce51
3 changed files with 73 additions and 8 deletions

View file

@ -27,6 +27,7 @@
#include <LibJS/AST.h>
#include <LibJS/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/PrimitiveString.h>
#include <LibJS/Value.h>
#include <stdio.h>
@ -262,10 +263,22 @@ void CallExpression::dump(int indent) const
printf("%s '%s'\n", class_name(), name().characters());
}
void Literal::dump(int indent) const
void StringLiteral::dump(int indent) const
{
print_indent(indent);
printf("Literal _%s_\n", m_value.to_string().characters());
printf("StringLiteral \"%s\"\n", m_value.characters());
}
void NumericLiteral::dump(int indent) const
{
print_indent(indent);
printf("NumberLiteral %g\n", m_value);
}
void BooleanLiteral::dump(int indent) const
{
print_indent(indent);
printf("BooleanLiteral %s\n", m_value ? "true" : "false");
}
void FunctionDeclaration::dump(int indent) const
@ -415,4 +428,19 @@ Value MemberExpression::execute(Interpreter& interpreter) const
return object_result.as_object()->get(property_name);
}
Value StringLiteral::execute(Interpreter& interpreter) const
{
return Value(js_string(interpreter.heap(), m_value));
}
Value NumericLiteral::execute(Interpreter&) const
{
return Value(m_value);
}
Value BooleanLiteral::execute(Interpreter&) const
{
return Value(m_value);
}
}