Make writer actually print something

This also fixes some of the GcRoot bugs
This commit is contained in:
Konstantin Nazarov 2024-07-28 23:37:43 +01:00
parent ba433dafe9
commit 4718780d9d
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
5 changed files with 46 additions and 7 deletions

View file

@ -55,11 +55,10 @@ class GcRootList {
GcRootList(GcRootBase* root) : _prev(0), _next(0), _root(root) {} GcRootList(GcRootBase* root) : _prev(0), _next(0), _root(root) {}
void insert(GcRootList* node) { void insert(GcRootList* node) {
GcRootList* next = _next; node->_next = _next;
_next = node;
node->_next = next;
node->_prev = this; node->_prev = this;
if (_next) _next->_prev = node;
_next = node;
} }
void remove() { void remove() {

View file

@ -52,6 +52,7 @@ Result<ByteArray> ByteArray::create(Arena& arena, String& str) {
size += utf8_codepoint_size(TRY(str[i])); size += utf8_codepoint_size(TRY(str[i]));
} }
auto pod = TRY(arena.alloc<PodByteArray>(size * sizeof(char))); auto pod = TRY(arena.alloc<PodByteArray>(size * sizeof(char)));
pod->size = size;
char* res = pod->data; char* res = pod->data;
for (uint64_t i = 0; i < str.size(); i++) { for (uint64_t i = 0; i < str.size(); i++) {
@ -59,7 +60,6 @@ Result<ByteArray> ByteArray::create(Arena& arena, String& str) {
size = utf8_codepoint_size(codepoint); size = utf8_codepoint_size(codepoint);
res = utf8_write_codepoint(res, codepoint); res = utf8_write_codepoint(res, codepoint);
} }
pod->size = size;
return ByteArray(TRY(MkGcRoot(pod, arena))); return ByteArray(TRY(MkGcRoot(pod, arena)));
} }
@ -137,3 +137,13 @@ Result<Value> reverse(Arena& arena, Value& val) {
return res; return res;
} }
Result<void> debug_print(Arena& arena, String& val) {
auto ba = TRY(ByteArray::create(arena, val));
for (uint64_t i = 0; i < ba.size(); i++) {
std::cout << TRY(ba[i]);
}
std::cout << "\n";
return Result<void>();
}

View file

@ -423,6 +423,7 @@ class Value {
} }
Value& operator=(Value&& val) { Value& operator=(Value&& val) {
((Object*)buf)->~Object();
memcpy(buf, val.buf, 24); memcpy(buf, val.buf, 24);
new (val.buf) Nil(); new (val.buf) Nil();
return *this; return *this;
@ -455,3 +456,5 @@ class Value {
Result<Value> syntax_unwrap(Value& val); Result<Value> syntax_unwrap(Value& val);
Result<Value> reverse(Arena& arena, Value& val); Result<Value> reverse(Arena& arena, Value& val);
Result<void> debug_print(Arena& arena, String& val);

View file

@ -7,8 +7,8 @@
template <class T> template <class T>
class Result { class Result {
public: public:
Result(const T& res) : _value(res) {} Result(const T& res) : _value(res), _error(ErrorCode::Success) {}
Result(T&& res) : _value(std::move(res)) {} Result(T&& res) : _value(std::move(res)), _error(ErrorCode::Success) {}
Result(ErrorCode err) : _error(err) {} Result(ErrorCode err) : _error(err) {}
bool has_error() { return _error != ErrorCode::Success; } bool has_error() { return _error != ErrorCode::Success; }
@ -24,6 +24,30 @@ class Result {
ErrorCode _error; ErrorCode _error;
}; };
template <>
class Result<void> {
public:
using ResultType = void;
Result(Result&& res) = default;
Result(ErrorCode err) : err(err) {}
Result() : err(ErrorCode::Success) {}
Result& operator=(Result&& res) = default;
bool has_error() { return err != ErrorCode::Success; }
bool has_value() { return !has_error(); }
void value() {}
void release_value() {}
ErrorCode error() { return err; }
private:
ErrorCode err;
};
#define TRY(m) \ #define TRY(m) \
(({ \ (({ \
auto ___res = (m); \ auto ___res = (m); \

View file

@ -36,5 +36,8 @@ int main() {
auto writer = Writer(arena); auto writer = Writer(arena);
auto s2 = DIEIF(writer.write_one(r)); auto s2 = DIEIF(writer.write_one(r));
std::cout << "size: " << s.size() << "\n";
std::cout << "char: " << (char)DIEIF(s[0]) << "\n";
DIEIF(debug_print(arena, s2));
return 0; return 0;
} }