Rewrite with working async
This commit is contained in:
+16
-38
@@ -1,5 +1,6 @@
|
||||
// Values and objects: the types that every other part uses, the object
|
||||
// header and its tags, the allocator and errors. See docs/model.md
|
||||
// Values: the types that every other part uses, the allocator and errors.
|
||||
// An object is a struct of its class, which the emitted C defines, and every
|
||||
// pointer to one is a real object: undefined and null are values of their own
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
@@ -12,40 +13,24 @@ typedef struct { char _; } null_t;
|
||||
// A string: UTF-16 code units, as JS counts them
|
||||
typedef struct jsrt_string *string_t;
|
||||
|
||||
// The start of every object: the number of its cell, which tells its struct
|
||||
// and, for a fn, its code
|
||||
typedef struct { uint32_t cell; } jsrt_header;
|
||||
|
||||
// A pointer to an object or a string can also be one of these. NULL is null.
|
||||
// They are told apart by their address only
|
||||
extern jsrt_header jsrt_undefined_cell, jsrt_absent_cell;
|
||||
#define JSRT_UNDEFINED ((void *)&jsrt_undefined_cell)
|
||||
#define JSRT_ABSENT ((void *)&jsrt_absent_cell) // a field that an object lacks
|
||||
|
||||
// Whether the objects of each cell are fns, by cell. The emitted C defines it
|
||||
extern const bool jsrt_fn_cells[];
|
||||
|
||||
// A new heap cell of `size` bytes, all zero
|
||||
void *jsrt_alloc(size_t size);
|
||||
void *__rt_alloc(size_t size);
|
||||
|
||||
// `items`, an array's elements of `size` bytes each, with room for at least
|
||||
// `need` of them. `*cap` is the room that it has, and is set to the new room
|
||||
void *jsrt_grow(void *items, int32_t *cap, int32_t need, size_t size);
|
||||
// `need` of them. `*cap` is the room that it has, and is set to the new room.
|
||||
// The new room is all zero
|
||||
void *__rt_grow(void *items, int32_t *cap, int32_t need, size_t size);
|
||||
|
||||
// Throws a TypeError. Exceptions are not supported yet, so it stops the program
|
||||
_Noreturn void jsrt_type_error(void);
|
||||
|
||||
// `p`, a pointer to an object or a string. A sentinel for null, undefined or
|
||||
// absent throws a TypeError
|
||||
void *jsrt_some(void *p);
|
||||
// Throws a TypeError, which says `what`. Exceptions are not supported yet, so
|
||||
// it stops the program
|
||||
_Noreturn void __rt_type_error(const char *what);
|
||||
|
||||
#ifdef JSRT_IMPLEMENTATION
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
jsrt_header jsrt_undefined_cell, jsrt_absent_cell;
|
||||
|
||||
void *jsrt_alloc(size_t size) {
|
||||
void *__rt_alloc(size_t size) {
|
||||
void *p = calloc(1, size);
|
||||
|
||||
if (!p) {
|
||||
@@ -56,7 +41,7 @@ void *jsrt_alloc(size_t size) {
|
||||
return p;
|
||||
}
|
||||
|
||||
void *jsrt_grow(void *items, int32_t *cap, int32_t need, size_t size) {
|
||||
void *__rt_grow(void *items, int32_t *cap, int32_t need, size_t size) {
|
||||
if (need <= *cap) {
|
||||
return items;
|
||||
}
|
||||
@@ -69,21 +54,14 @@ void *jsrt_grow(void *items, int32_t *cap, int32_t need, size_t size) {
|
||||
abort();
|
||||
}
|
||||
|
||||
memset((char *)items + (size_t)*cap * size, 0, (size_t)(room - *cap) * size);
|
||||
*cap = room;
|
||||
return items;
|
||||
}
|
||||
|
||||
_Noreturn void jsrt_type_error(void) {
|
||||
fputs("TypeError\n", stderr);
|
||||
_Noreturn void __rt_type_error(const char *what) {
|
||||
fprintf(stderr, "TypeError: %s\n", what);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void *jsrt_some(void *p) {
|
||||
if (p == NULL || p == JSRT_UNDEFINED || p == JSRT_ABSENT) {
|
||||
jsrt_type_error();
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
#endif // JSRT_IMPLEMENTATION
|
||||
|
||||
Reference in New Issue
Block a user