basic VM stuff
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -2,6 +2,6 @@
|
||||
/.zig-cache
|
||||
/zig-out
|
||||
.idea
|
||||
noom
|
||||
noom.*
|
||||
/noom
|
||||
/noom.*
|
||||
Makefile
|
||||
|
||||
@@ -67,6 +67,7 @@ local files = {
|
||||
'src/lexer.c',
|
||||
'src/parser.c',
|
||||
'src/compiler.c',
|
||||
'src/vm.c',
|
||||
'src/main.c',
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ pub fn build(b: *std.Build) void {
|
||||
"src/lexer.c",
|
||||
"src/parser.c",
|
||||
"src/compiler.c",
|
||||
"src/vm.c",
|
||||
"src/main.c",
|
||||
}
|
||||
});
|
||||
|
||||
12
src/noom.h
12
src/noom.h
@@ -3,17 +3,16 @@
|
||||
#ifndef NOOM_H
|
||||
#define NOOM_H
|
||||
|
||||
|
||||
#define STR(x) #x
|
||||
#define XSTR(x) STR(x)
|
||||
#define NN_STR(x) #x
|
||||
#define NN_XSTR(x) NN_STR(x)
|
||||
|
||||
#define NOOM_VERSION_MAJOR 0
|
||||
#define NOOM_VERSION_MINOR 0
|
||||
#define NOOM_VERSION_PATCH 0
|
||||
#if NOOM_VERSION_PATCH==0
|
||||
#define NOOM_VERSION_FULL XSTR(NOOM_VERSION_MAJOR) "." XSTR(NOOM_VERSION_MINOR)
|
||||
#define NOOM_VERSION_FULL NN_XSTR(NOOM_VERSION_MAJOR) "." NN_XSTR(NOOM_VERSION_MINOR)
|
||||
#else
|
||||
#define NOOM_VERSION_FULL XSTR(NOOM_VERSION_MAJOR) "." XSTR(NOOM_VERSION_MINOR) "." XSTR(NOOM_VERSION_PATCH)
|
||||
#define NOOM_VERSION_FULL NN_XSTR(NOOM_VERSION_MAJOR) "." NN_XSTR(NOOM_VERSION_MINOR) "." NN_XSTR(NOOM_VERSION_PATCH)
|
||||
#endif
|
||||
#define NOOM_VERSION_TEXT "Noom " NOOM_VERSION_FULL " (C) 2026 NeoFlock and Noom contributors"
|
||||
|
||||
@@ -59,4 +58,7 @@ typedef struct noom_LuaVM noom_LuaVM;
|
||||
typedef noom_Exit noom_CFunction(noom_LuaVM *vm);
|
||||
typedef noom_Exit noom_KFunction(noom_LuaVM *vm, noom_Exit status, void *ctx);
|
||||
|
||||
noom_LuaVM *noom_createVM(noom_LuaVersion version);
|
||||
void noom_destroyVM(noom_LuaVM *vm);
|
||||
|
||||
#endif
|
||||
|
||||
42
src/vm.c
Normal file
42
src/vm.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "vm.h"
|
||||
#include "helper.h"
|
||||
|
||||
noomV_Object *noomV_allocObj(noom_LuaVM *vm, noomV_ObjTag tag, noom_uint_t size) {
|
||||
noomV_Object *o = noom_alloc(size);
|
||||
if(o == 0) return o;
|
||||
|
||||
o->tag = tag;
|
||||
o->marked = 0;
|
||||
o->next = vm->heap;
|
||||
o->nextGray = 0;
|
||||
vm->heap = o;
|
||||
return o;
|
||||
}
|
||||
|
||||
void noomV_freeObj(noomV_Object *obj) {
|
||||
noom_free(obj);
|
||||
}
|
||||
|
||||
noom_LuaVM *noom_createVM(noom_LuaVersion version) {
|
||||
noom_LuaVM *vm = noom_alloc(sizeof(*vm));
|
||||
if(vm == 0) return 0;
|
||||
// initialize the universe to NULL, handles partial OOMs nicely
|
||||
vm->heap = 0;
|
||||
vm->graySet = 0;
|
||||
vm->globals = 0;
|
||||
vm->registry = 0;
|
||||
vm->mainThread = 0;
|
||||
vm->currentThread = 0;
|
||||
vm->version = version;
|
||||
return vm;
|
||||
}
|
||||
|
||||
void noom_destroyVM(noom_LuaVM *vm) {
|
||||
noomV_Object *iter = vm->heap;
|
||||
while(iter) {
|
||||
noomV_Object *cur = iter;
|
||||
iter = iter->next;
|
||||
noomV_freeObj(cur);
|
||||
}
|
||||
noom_free(vm);
|
||||
}
|
||||
14
src/vm.h
14
src/vm.h
@@ -25,7 +25,7 @@ typedef enum noomV_ObjTag {
|
||||
|
||||
typedef struct noomV_Object {
|
||||
noomV_ObjTag tag;
|
||||
bool marked;
|
||||
noom_bool_t marked;
|
||||
struct noomV_Object *next;
|
||||
struct noomV_Object *nextGray;
|
||||
} noomV_Object;
|
||||
@@ -43,9 +43,9 @@ typedef enum noomV_ValueTag : unsigned char {
|
||||
typedef struct noomV_Value {
|
||||
noomV_ValueTag tag;
|
||||
// for stack slots
|
||||
bool autoclose;
|
||||
noom_bool_t autoclose;
|
||||
// pointer to value
|
||||
bool isptr;
|
||||
noom_bool_t isptr;
|
||||
union {
|
||||
noom_bool_t boolean;
|
||||
noom_int_t integer;
|
||||
@@ -184,7 +184,7 @@ typedef struct noomV_Function {
|
||||
noomV_String *chunkname;
|
||||
noomV_Inst *code;
|
||||
noomV_Value *consts;
|
||||
noomV_Function **protos;
|
||||
struct noomV_Function **protos;
|
||||
noomV_UpvalDesc *upvals;
|
||||
noomV_LocalDesc *locals;
|
||||
unsigned int codesize;
|
||||
@@ -260,7 +260,7 @@ typedef struct noomV_Function {
|
||||
typedef struct noomV_CallFrame {
|
||||
// stack index of function
|
||||
unsigned int funcIdx;
|
||||
bool isC;
|
||||
noom_bool_t isC;
|
||||
union {
|
||||
struct {
|
||||
//
|
||||
@@ -293,6 +293,10 @@ struct noom_LuaVM {
|
||||
noomV_Table *registry;
|
||||
noomV_Thread *mainThread;
|
||||
noomV_Thread *currentThread;
|
||||
noom_LuaVersion version;
|
||||
};
|
||||
|
||||
noomV_Object *noomV_allocObj(noom_LuaVM *vm, noomV_ObjTag tag, noom_uint_t size);
|
||||
void noomV_freeObj(noomV_Object *obj);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user