Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Jan 23, 2025
1 parent dbac81f commit e4ff7de
Show file tree
Hide file tree
Showing 15 changed files with 890 additions and 34 deletions.
14 changes: 14 additions & 0 deletions src/web/vaev-script/agent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <karm-base/res.h>

#include "gc.h"

namespace Vaev::Script {

// https://tc39.es/ecma262/#agent
struct Agent {
Gc::Gc &gc;
};

} // namespace Vaev::Script
29 changes: 0 additions & 29 deletions src/web/vaev-script/cli/main.cpp

This file was deleted.

77 changes: 77 additions & 0 deletions src/web/vaev-script/completion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#pragma once

#include <karm-base/res.h>

#include "value.h"

namespace Vaev::Script {

// https://tc39.es/ecma262/#sec-completion-record-specification-type
struct [[nodiscard]] Completion {
enum struct _Type {
NORMAL,
BREAK,
CONTINUE,
RETURN,
THROW,
};

using enum _Type;

_Type type = _Type::NORMAL;
Value value = UNDEFINED;
String target = u""_s16;

static Completion normal(Value value) {
return {NORMAL, value};
}

static Completion break_(String target) {
return {BREAK, UNDEFINED, target};
}

static Completion continue_(String target) {
return {CONTINUE, UNDEFINED, target};
}

static Completion return_(Value value) {
return {RETURN, value};
}

static Completion throw_(Value value) {
return {THROW, value};
}

bool operator==(_Type t) const {
return type == t;
}

void repr(Io::Emit& e) const {
switch (type) {
case NORMAL:
e("normal({})", value);
break;

case BREAK:
e("break({})", target);
break;

case CONTINUE:
e("continue({})", target);
break;

case RETURN:
e("return({})", value);
break;

case THROW:
e("throw({})", value);
break;
}
}
};

template <typename T = None>
using Except = Res<T, Completion>;

} // namespace Vaev::Script
98 changes: 98 additions & 0 deletions src/web/vaev-script/gc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#pragma once

#include <karm-base/base.h>
#include <karm-base/panic.h>
#include <karm-io/emit.h>

namespace Karm::Gc {

template <typename T>
struct Ref {
T* _ptr = nullptr;

Ref() = default;

Ref(T* ptr) : _ptr{ptr} {
if (not _ptr)
panic("null pointer");
}

T const* operator->() const {
return _ptr;
}

T* operator->() {
return _ptr;
}

T const& operator*() const {
return *_ptr;
}

T& operator*() {
return *_ptr;
}

void repr(Io::Emit& e) const {
e("{}", *_ptr);
}
};

// Nullable reference
template <typename T>
struct Ptr {
T* _ptr = nullptr;

Ptr() = default;

Ptr(T* ptr) : _ptr{ptr} {
}

T const* operator->() const {
if (not _ptr)
panic("trying to dereference a null pointer");
return _ptr;
}

T* operator->() {
if (not _ptr)
panic("trying to dereference a null pointer");
return _ptr;
}

T const& operator*() const {
if (not _ptr)
panic("trying to dereference a null pointer");
return *_ptr;
}

T& operator*() {
if (not _ptr)
panic("trying to dereference a null pointer");
return *_ptr;
}

bool operator==(None) const {
return not _ptr;
}

void repr(Io::Emit& e) const {
if (not _ptr)
e("nullptr");
else
e("{}", *_ptr);
}

explicit operator bool() const {
return _ptr != nullptr;
}
};

struct Gc {
template <typename T, typename... Args>
Ref<T> alloc(Args&&... args) {
return new T{std::forward<Args>(args)...};
}
};

} // namespace Karm::Gc
8 changes: 4 additions & 4 deletions src/web/vaev-script/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,17 @@ struct Token {
}
}

void repr(Io::Emit& e) const;
void repr(Io::Emit &e) const;
};

struct Lexer {
Io::SScan& _scan;
Io::SScan &_scan;

Lexer(Io::SScan& scan)
Lexer(Io::SScan &scan)
: _scan(scan) {
}

Token _next(Io::SScan& scan) const;
Token _next(Io::SScan &scan) const;

Token next() {
return _next(_scan);
Expand Down
25 changes: 25 additions & 0 deletions src/web/vaev-script/main/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <karm-sys/entry.h>
#include <vaev-script/object.h>
#include <vaev-script/realm.h>

using namespace Vaev;

Async::Task<> entryPointAsync(Sys::Context&) {
Gc::Gc gc;

auto agent = gc.alloc<Script::Agent>(gc);
auto realm = gc.alloc<Script::Realm>(agent);

(void)realm->initializeHostDefinedRealm(agent);

auto object1 = Script::Object::create(*agent);
(void)object1->defineOwnProperty(Script::PropertyKey::from(u"foo"_s16), {.value = 42.});

auto object2 = Script::Object::create(*agent, {.prototype = object1});

auto res = object2->get(Script::PropertyKey::from(u"foo"_s16), object2);

Sys::print("object2.foo = {}\n", res);

co_return Ok();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://schemas.cute.engineering/stable/cutekit.manifest.component.v1",
"id": "vaev-script.cli",
"id": "vaev-script.main",
"type": "exe",
"description": "ECMAScript engine testing tool",
"requires": [
Expand Down
Loading

0 comments on commit e4ff7de

Please sign in to comment.