Make writer actually print something
This also fixes some of the GcRoot bugs
This commit is contained in:
parent
ba433dafe9
commit
4718780d9d
5 changed files with 46 additions and 7 deletions
|
@ -55,11 +55,10 @@ class GcRootList {
|
|||
|
||||
GcRootList(GcRootBase* root) : _prev(0), _next(0), _root(root) {}
|
||||
void insert(GcRootList* node) {
|
||||
GcRootList* next = _next;
|
||||
|
||||
_next = node;
|
||||
node->_next = next;
|
||||
node->_next = _next;
|
||||
node->_prev = this;
|
||||
if (_next) _next->_prev = node;
|
||||
_next = node;
|
||||
}
|
||||
|
||||
void remove() {
|
||||
|
|
|
@ -52,6 +52,7 @@ Result<ByteArray> ByteArray::create(Arena& arena, String& str) {
|
|||
size += utf8_codepoint_size(TRY(str[i]));
|
||||
}
|
||||
auto pod = TRY(arena.alloc<PodByteArray>(size * sizeof(char)));
|
||||
pod->size = size;
|
||||
|
||||
char* res = pod->data;
|
||||
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);
|
||||
res = utf8_write_codepoint(res, codepoint);
|
||||
}
|
||||
pod->size = size;
|
||||
|
||||
return ByteArray(TRY(MkGcRoot(pod, arena)));
|
||||
}
|
||||
|
@ -137,3 +137,13 @@ Result<Value> reverse(Arena& arena, Value& val) {
|
|||
|
||||
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>();
|
||||
}
|
||||
|
|
|
@ -423,6 +423,7 @@ class Value {
|
|||
}
|
||||
|
||||
Value& operator=(Value&& val) {
|
||||
((Object*)buf)->~Object();
|
||||
memcpy(buf, val.buf, 24);
|
||||
new (val.buf) Nil();
|
||||
return *this;
|
||||
|
@ -455,3 +456,5 @@ class Value {
|
|||
Result<Value> syntax_unwrap(Value& val);
|
||||
|
||||
Result<Value> reverse(Arena& arena, Value& val);
|
||||
|
||||
Result<void> debug_print(Arena& arena, String& val);
|
||||
|
|
|
@ -7,8 +7,8 @@
|
|||
template <class T>
|
||||
class Result {
|
||||
public:
|
||||
Result(const T& res) : _value(res) {}
|
||||
Result(T&& res) : _value(std::move(res)) {}
|
||||
Result(const T& res) : _value(res), _error(ErrorCode::Success) {}
|
||||
Result(T&& res) : _value(std::move(res)), _error(ErrorCode::Success) {}
|
||||
Result(ErrorCode err) : _error(err) {}
|
||||
|
||||
bool has_error() { return _error != ErrorCode::Success; }
|
||||
|
@ -24,6 +24,30 @@ class Result {
|
|||
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) \
|
||||
(({ \
|
||||
auto ___res = (m); \
|
||||
|
|
|
@ -36,5 +36,8 @@ int main() {
|
|||
auto writer = Writer(arena);
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue