1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:17:34 +00:00

LibJS: Support "hello friends".length

The above snippet is a MemberExpression that necessitates the implicit
construction of a StringObject wrapper around a PrimitiveString.

We then do a property lookup (a "get") on the StringObject, where we
find the "length" property. This is pretty neat! :^)
This commit is contained in:
Andreas Kling 2020-03-11 18:58:19 +01:00
parent 6ec6fa1a02
commit 542108421e
6 changed files with 100 additions and 11 deletions

View file

@ -28,21 +28,22 @@
#include <LibJS/AST.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Object.h>
#include <LibJS/PrimitiveString.h>
#include <LibJS/Value.h>
#include <stdio.h>
#define PROGRAM 2
#define PROGRAM 4
static void build_program(JS::Program&);
static void build_program(JS::Program&, JS::Heap&);
int main()
{
auto program = make<JS::Program>();
build_program(*program);
JS::Interpreter interpreter;
auto program = make<JS::Program>();
build_program(*program, interpreter.heap());
program->dump(0);
JS::Interpreter interpreter;
auto result = interpreter.run(*program);
dbg() << "Interpreter returned " << result;
@ -125,4 +126,13 @@ void build_program(JS::Program& program)
program.append<JS::FunctionDeclaration>("foo", move(block));
program.append<JS::CallExpression>("foo");
}
#elif PROGRAM == 4
void build_program(JS::Program& program, JS::Heap& heap)
{
// "hello friends".length
program.append<JS::MemberExpression>(
make<JS::Literal>(JS::Value(js_string(heap, "hello friends"))),
make<JS::Identifier>("length"));
}
#endif