2020-12-26 03:32:21 +03:00
|
|
|
#pragma once
|
|
|
|
|
2020-12-29 09:26:45 +03:00
|
|
|
#include <stdarg.h>
|
2020-12-26 03:32:21 +03:00
|
|
|
#include "kuroko.h"
|
|
|
|
#include "value.h"
|
2020-12-26 08:33:34 +03:00
|
|
|
#include "table.h"
|
2020-12-27 03:33:28 +03:00
|
|
|
#include "object.h"
|
|
|
|
|
|
|
|
#define FRAMES_MAX 64
|
2020-12-26 03:32:21 +03:00
|
|
|
|
|
|
|
typedef struct {
|
2020-12-27 07:02:26 +03:00
|
|
|
KrkClosure * closure;
|
2020-12-26 03:32:21 +03:00
|
|
|
uint8_t * ip;
|
2020-12-27 16:40:35 +03:00
|
|
|
size_t slots;
|
2020-12-27 03:33:28 +03:00
|
|
|
} CallFrame;
|
|
|
|
|
2020-12-27 16:40:35 +03:00
|
|
|
typedef enum {
|
|
|
|
METHOD_INIT,
|
|
|
|
METHOD_STR,
|
2020-12-28 13:01:28 +03:00
|
|
|
METHOD_GET,
|
|
|
|
METHOD_SET,
|
|
|
|
METHOD_CLASS,
|
|
|
|
METHOD_NAME,
|
|
|
|
METHOD_FILE,
|
2020-12-27 16:40:35 +03:00
|
|
|
|
|
|
|
METHOD__MAX,
|
|
|
|
} KrkSpecialMethods;
|
|
|
|
|
2020-12-27 03:33:28 +03:00
|
|
|
typedef struct {
|
|
|
|
CallFrame frames[FRAMES_MAX];
|
|
|
|
size_t frameCount;
|
2020-12-26 03:32:21 +03:00
|
|
|
size_t stackSize;
|
|
|
|
KrkValue * stack;
|
|
|
|
KrkValue * stackTop;
|
2020-12-26 10:53:15 +03:00
|
|
|
KrkTable globals;
|
2020-12-26 08:33:34 +03:00
|
|
|
KrkTable strings;
|
2020-12-27 12:55:52 +03:00
|
|
|
KrkTable modules;
|
2020-12-27 07:02:26 +03:00
|
|
|
KrkUpvalue * openUpvalues;
|
2020-12-26 03:32:21 +03:00
|
|
|
KrkObj * objects;
|
2020-12-27 09:58:32 +03:00
|
|
|
size_t bytesAllocated;
|
|
|
|
size_t nextGC;
|
|
|
|
size_t grayCount;
|
|
|
|
size_t grayCapacity;
|
2020-12-29 07:19:22 +03:00
|
|
|
ssize_t exitOnFrame;
|
2020-12-27 09:58:32 +03:00
|
|
|
KrkObj** grayStack;
|
2020-12-27 16:40:35 +03:00
|
|
|
KrkValue specialMethodNames[METHOD__MAX];
|
2020-12-28 03:08:35 +03:00
|
|
|
|
2020-12-28 13:01:28 +03:00
|
|
|
KrkClass * object_class;
|
|
|
|
KrkInstance * builtins;
|
|
|
|
|
2020-12-29 05:00:12 +03:00
|
|
|
KrkValue currentException;
|
2020-12-28 14:38:26 +03:00
|
|
|
int flags;
|
2020-12-26 03:32:21 +03:00
|
|
|
} KrkVM;
|
|
|
|
|
2020-12-28 14:38:26 +03:00
|
|
|
#define KRK_ENABLE_TRACING (1 << 0)
|
|
|
|
#define KRK_ENABLE_DEBUGGING (1 << 1)
|
|
|
|
#define KRK_ENABLE_SCAN_TRACING (1 << 2)
|
|
|
|
#define KRK_ENABLE_STRESS_GC (1 << 3)
|
|
|
|
|
2020-12-28 16:02:39 +03:00
|
|
|
#define KRK_GC_PAUSED (1 << 10)
|
2020-12-29 05:00:12 +03:00
|
|
|
#define KRK_HAS_EXCEPTION (1 << 11)
|
2020-12-28 16:02:39 +03:00
|
|
|
|
2020-12-26 03:32:21 +03:00
|
|
|
extern KrkVM vm;
|
|
|
|
|
2020-12-28 14:38:26 +03:00
|
|
|
extern void krk_initVM(int flags);
|
2020-12-26 03:32:21 +03:00
|
|
|
extern void krk_freeVM(void);
|
2020-12-28 04:54:25 +03:00
|
|
|
extern KrkValue krk_interpret(const char * src, int newScope, char *, char *);
|
|
|
|
extern KrkValue krk_runfile(const char * fileName, int newScope, char *, char *);
|
2020-12-26 03:32:21 +03:00
|
|
|
extern void krk_push(KrkValue value);
|
|
|
|
extern KrkValue krk_pop(void);
|
2020-12-28 05:11:50 +03:00
|
|
|
extern const char * krk_typeName(KrkValue value);
|
2020-12-28 13:01:28 +03:00
|
|
|
extern void krk_defineNative(KrkTable * table, const char * name, NativeFn function);
|
2020-12-28 13:25:33 +03:00
|
|
|
extern void krk_attachNamedObject(KrkTable * table, const char name[], KrkObj * obj);
|
2020-12-29 09:26:45 +03:00
|
|
|
extern void krk_runtimeError(const char * fmt, ...);
|
2020-12-28 16:02:39 +03:00
|
|
|
|
|
|
|
#define KRK_PAUSE_GC() do { vm.flags |= KRK_GC_PAUSED; } while (0)
|
|
|
|
#define KRK_RESUME_GC() do { vm.flags &= ~(KRK_GC_PAUSED); } while (0)
|