2020-10-28 05:16:30 +00:00
|
|
|
#ifndef CSTATE_H
|
|
|
|
#define CSTATE_H
|
|
|
|
|
|
|
|
#include "cobj.h"
|
2023-02-09 18:32:48 +00:00
|
|
|
#include "cosmo.h"
|
2020-10-28 05:16:30 +00:00
|
|
|
#include "ctable.h"
|
2023-02-09 18:32:48 +00:00
|
|
|
#include "cvalue.h"
|
2020-10-28 05:16:30 +00:00
|
|
|
|
2023-08-29 02:13:00 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
struct CCallFrame
|
|
|
|
{
|
2020-10-28 05:16:30 +00:00
|
|
|
CObjClosure *closure;
|
|
|
|
INSTRUCTION *pc;
|
2023-02-09 18:32:48 +00:00
|
|
|
CValue *base;
|
2021-02-15 22:20:04 +00:00
|
|
|
};
|
2020-10-28 05:16:30 +00:00
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
typedef enum IStringEnum
|
|
|
|
{
|
|
|
|
ISTRING_INIT, // __init
|
|
|
|
ISTRING_TOSTRING, // __tostring
|
|
|
|
ISTRING_TONUMBER, // __tonumber
|
|
|
|
ISTRING_EQUAL, // __equals
|
|
|
|
ISTRING_INDEX, // __index
|
|
|
|
ISTRING_NEWINDEX, // __newindex
|
|
|
|
ISTRING_COUNT, // __count
|
|
|
|
ISTRING_GETTER, // __getter
|
|
|
|
ISTRING_SETTER, // __setter
|
|
|
|
ISTRING_ITER, // __iter
|
|
|
|
ISTRING_NEXT, // __next
|
|
|
|
ISTRING_RESERVED, // __reserved
|
|
|
|
ISTRING_MAX // if this becomes greater than 33, we are out of space in cosmo_Flag. you'll have
|
|
|
|
// to change that to uint64_t
|
2020-11-17 01:58:16 +00:00
|
|
|
} IStringEnum;
|
2020-11-12 22:52:56 +00:00
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
typedef struct ArrayCObj
|
|
|
|
{
|
2020-12-07 20:35:14 +00:00
|
|
|
CObj **array;
|
|
|
|
int count;
|
|
|
|
int capacity;
|
|
|
|
} ArrayCObj;
|
|
|
|
|
2023-08-29 02:13:00 +00:00
|
|
|
typedef struct CPanic
|
|
|
|
{
|
|
|
|
jmp_buf jmp;
|
2023-08-29 21:48:38 +00:00
|
|
|
StkPtr top;
|
|
|
|
int frameCount;
|
2023-08-29 02:13:00 +00:00
|
|
|
struct CPanic *prev;
|
|
|
|
} CPanic;
|
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
struct CState
|
|
|
|
{
|
2020-10-28 05:16:30 +00:00
|
|
|
int freezeGC; // when > 0, GC events will be ignored (for internal use)
|
2021-01-02 01:20:24 +00:00
|
|
|
int frameCount;
|
2023-08-29 02:13:00 +00:00
|
|
|
CPanic *panic;
|
2021-01-02 01:20:24 +00:00
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
CObj *objects; // tracks all of our allocated objects
|
|
|
|
CObj *userRoots; // user definable roots, this holds CObjs that should be considered "roots",
|
|
|
|
// lets the VM know you are holding a reference to a CObj in your code
|
|
|
|
ArrayCObj grayStack; // keeps track of which objects *haven't yet* been traversed in our GC, but
|
|
|
|
// *have been* found
|
2020-10-28 05:16:30 +00:00
|
|
|
size_t allocatedBytes;
|
|
|
|
size_t nextGC; // when allocatedBytes reaches this threshhold, trigger a GC event
|
|
|
|
|
|
|
|
CObjUpval *openUpvalues; // tracks all of our still open (meaning still on the stack) upvalues
|
|
|
|
CTable strings;
|
2021-01-25 22:14:51 +00:00
|
|
|
CObjTable *globals;
|
2020-10-28 05:16:30 +00:00
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
CValue *top; // top of the stack
|
2021-01-08 01:50:36 +00:00
|
|
|
CObjObject *protoObjects[COBJ_MAX]; // proto object for each COBJ type [NULL = no default proto]
|
2023-05-26 00:40:15 +00:00
|
|
|
CObjString *iStrings[ISTRING_MAX]; // strings used internally by the VM, eg. __init, __index
|
|
|
|
CCallFrame callFrame[FRAME_MAX]; // call frames
|
|
|
|
CValue stack[STACK_MAX]; // stack
|
2021-02-15 22:20:04 +00:00
|
|
|
};
|
2020-10-28 05:16:30 +00:00
|
|
|
|
2023-08-29 02:13:00 +00:00
|
|
|
CPanic *cosmoV_newPanic(CState *state);
|
|
|
|
void cosmoV_freePanic(CState *state);
|
|
|
|
|
2020-10-28 05:16:30 +00:00
|
|
|
COSMO_API CState *cosmoV_newState();
|
2023-08-29 02:13:00 +00:00
|
|
|
COSMO_API void cosmoV_freeState(CState *state);
|
|
|
|
|
2020-12-20 03:15:12 +00:00
|
|
|
// expects 2*pairs values on the stack, each pair should consist of 1 key and 1 value
|
2023-02-09 18:32:48 +00:00
|
|
|
COSMO_API void cosmoV_register(CState *state, int pairs);
|
2023-08-29 02:13:00 +00:00
|
|
|
|
2020-10-28 05:16:30 +00:00
|
|
|
COSMO_API void cosmoV_printStack(CState *state);
|
|
|
|
|
2021-01-02 05:06:24 +00:00
|
|
|
#endif
|