2017-04-18 02:40:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Calvin Rose
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-03-26 19:44:57 +00:00
|
|
|
#ifndef GST_H_defined
|
|
|
|
#define GST_H_defined
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/* String utils */
|
|
|
|
#define gst_string_raw(s) ((uint32_t *)(s) - 2)
|
|
|
|
#define gst_string_length(s) (gst_string_raw(s)[0])
|
|
|
|
#define gst_string_hash(s) (gst_string_raw(s)[1])
|
|
|
|
|
|
|
|
/* Tuple utils */
|
|
|
|
#define gst_tuple_raw(t) ((uint32_t *)(t) - 2)
|
|
|
|
#define gst_tuple_length(t) (gst_tuple_raw(t)[0])
|
|
|
|
#define gst_tuple_hash(t) (gst_tuple_raw(t)[1])
|
|
|
|
|
2017-04-14 17:41:32 +00:00
|
|
|
/* Struct utils */
|
|
|
|
#define gst_struct_raw(t) ((uint32_t *)(t) - 2)
|
|
|
|
#define gst_struct_length(t) (gst_struct_raw(t)[0])
|
2017-04-15 20:05:59 +00:00
|
|
|
#define gst_struct_capacity(t) (gst_struct_length(t) * 4)
|
2017-04-14 17:41:32 +00:00
|
|
|
#define gst_struct_hash(t) (gst_struct_raw(t)[1])
|
|
|
|
|
2017-03-26 19:44:57 +00:00
|
|
|
/* Memcpy for moving memory */
|
|
|
|
#ifndef gst_memcpy
|
|
|
|
#include <string.h>
|
|
|
|
#define gst_memcpy memcpy
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Allocation */
|
|
|
|
#ifndef gst_raw_alloc
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define gst_raw_alloc malloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Zero allocation */
|
|
|
|
#ifndef gst_raw_calloc
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define gst_raw_calloc calloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Realloc */
|
|
|
|
#ifndef gst_raw_realloc
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define gst_raw_realloc realloc
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Free */
|
|
|
|
#ifndef gst_raw_free
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define gst_raw_free free
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Null */
|
|
|
|
#ifndef NULL
|
|
|
|
#define NULL ((void *)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Stack frame manipulation */
|
|
|
|
|
|
|
|
/* Size of stack frame in number of values */
|
2017-04-25 16:26:58 +00:00
|
|
|
#define GST_FRAME_SIZE 5
|
2017-03-26 19:44:57 +00:00
|
|
|
|
|
|
|
/* Macros for referencing a stack frame given a stack */
|
|
|
|
#define gst_frame_callee(s) (*(s - 1))
|
2017-04-25 16:26:58 +00:00
|
|
|
#define gst_frame_size(s) ((s - 2)->data.dwords[0])
|
|
|
|
#define gst_frame_prevsize(s) ((s - 2)->data.dwords[1])
|
|
|
|
#define gst_frame_args(s) ((s - 3)->data.dwords[0])
|
|
|
|
#define gst_frame_ret(s) ((s - 3)->data.dwords[1])
|
|
|
|
#define gst_frame_pc(s) ((s - 4)->data.u16p)
|
|
|
|
#define gst_frame_env(s) ((s - 5)->data.env)
|
2017-03-26 19:44:57 +00:00
|
|
|
|
|
|
|
/* C function helpers */
|
|
|
|
|
|
|
|
/* Return in a c function */
|
|
|
|
#define gst_c_return(vm, x) do { (vm)->ret = (x); return GST_RETURN_OK; } while (0)
|
|
|
|
|
|
|
|
/* Throw error from a c function */
|
|
|
|
#define gst_c_throw(vm, e) do { (vm)->ret = (e); return GST_RETURN_ERROR; } while (0)
|
|
|
|
|
|
|
|
/* Throw c string error from a c function */
|
2017-04-15 20:05:59 +00:00
|
|
|
#define gst_c_throwc(vm, e) gst_c_throw((vm), gst_string_cv((vm), (e)))
|
2017-03-26 19:44:57 +00:00
|
|
|
|
|
|
|
/* Assert from a c function */
|
|
|
|
#define gst_c_assert(vm, cond, e) do {if (cond) gst_c_throw((vm), (e)); } while (0)
|
|
|
|
|
|
|
|
/* What to do when out of memory */
|
|
|
|
#ifndef GST_OUT_OF_MEMORY
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2017-04-19 13:02:12 +00:00
|
|
|
#define GST_OUT_OF_MEMORY do { printf("out of memory\n"); exit(1); } while (0)
|
2017-03-26 19:44:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Various types */
|
|
|
|
typedef enum GstType {
|
|
|
|
GST_NIL = 0,
|
2017-04-24 20:02:54 +00:00
|
|
|
GST_REAL,
|
|
|
|
GST_INTEGER,
|
2017-03-26 19:44:57 +00:00
|
|
|
GST_BOOLEAN,
|
|
|
|
GST_STRING,
|
|
|
|
GST_ARRAY,
|
|
|
|
GST_TUPLE,
|
2017-04-15 20:05:59 +00:00
|
|
|
GST_STRUCT,
|
2017-03-26 19:44:57 +00:00
|
|
|
GST_THREAD,
|
|
|
|
GST_BYTEBUFFER,
|
|
|
|
GST_FUNCTION,
|
|
|
|
GST_CFUNCTION,
|
2017-04-25 01:00:56 +00:00
|
|
|
GST_TABLE,
|
2017-03-26 19:44:57 +00:00
|
|
|
GST_USERDATA,
|
|
|
|
GST_FUNCENV,
|
|
|
|
GST_FUNCDEF
|
|
|
|
} GstType;
|
|
|
|
|
|
|
|
/* The state of the virtual machine */
|
|
|
|
typedef struct Gst Gst;
|
|
|
|
|
|
|
|
/* A general gst value type */
|
|
|
|
typedef struct GstValue GstValue;
|
|
|
|
|
|
|
|
/* All of the gst types */
|
2017-04-24 20:02:54 +00:00
|
|
|
typedef double GstReal;
|
|
|
|
typedef int64_t GstInteger;
|
|
|
|
typedef int GstBoolean;
|
2017-03-26 19:44:57 +00:00
|
|
|
typedef struct GstFunction GstFunction;
|
|
|
|
typedef struct GstArray GstArray;
|
|
|
|
typedef struct GstBuffer GstBuffer;
|
2017-04-25 01:00:56 +00:00
|
|
|
typedef struct GstTable GstTable;
|
2017-03-26 19:44:57 +00:00
|
|
|
typedef struct GstThread GstThread;
|
|
|
|
typedef int (*GstCFunction)(Gst * vm);
|
|
|
|
|
|
|
|
/* Implementation details */
|
|
|
|
typedef struct GstUserdataHeader GstUserdataHeader;
|
|
|
|
typedef struct GstFuncDef GstFuncDef;
|
|
|
|
typedef struct GstFuncEnv GstFuncEnv;
|
2017-04-12 14:31:50 +00:00
|
|
|
typedef union GstValueUnion GstValueUnion;
|
2017-03-26 19:44:57 +00:00
|
|
|
|
2017-04-13 01:21:46 +00:00
|
|
|
/* API Types */
|
|
|
|
typedef struct GstModuleItem GstModuleItem;
|
|
|
|
|
|
|
|
/* C Api data types */
|
|
|
|
struct GstModuleItem {
|
|
|
|
const char *name;
|
|
|
|
GstCFunction data;
|
|
|
|
};
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* Union datatype */
|
|
|
|
union GstValueUnion {
|
|
|
|
GstBoolean boolean;
|
2017-04-24 20:02:54 +00:00
|
|
|
GstReal real;
|
|
|
|
GstInteger integer;
|
2017-04-12 14:31:50 +00:00
|
|
|
GstArray *array;
|
|
|
|
GstBuffer *buffer;
|
2017-04-25 01:00:56 +00:00
|
|
|
GstTable *table;
|
2017-04-12 14:31:50 +00:00
|
|
|
GstThread *thread;
|
2017-04-15 20:05:59 +00:00
|
|
|
const GstValue *tuple;
|
2017-04-12 14:31:50 +00:00
|
|
|
GstCFunction cfunction;
|
|
|
|
GstFunction *function;
|
|
|
|
GstFuncEnv *env;
|
|
|
|
GstFuncDef *def;
|
2017-04-15 20:05:59 +00:00
|
|
|
const GstValue *st;
|
2017-04-12 14:31:50 +00:00
|
|
|
const uint8_t *string;
|
|
|
|
/* Indirectly used union members */
|
|
|
|
uint16_t *u16p;
|
2017-04-24 20:02:54 +00:00
|
|
|
uint32_t dwords[2];
|
|
|
|
uint16_t words[4];
|
2017-04-12 14:31:50 +00:00
|
|
|
uint8_t bytes[8];
|
|
|
|
void *pointer;
|
2017-04-24 20:02:54 +00:00
|
|
|
const char *cstring;
|
2017-04-12 14:31:50 +00:00
|
|
|
};
|
|
|
|
|
2017-03-26 19:44:57 +00:00
|
|
|
/* The general gst value type. Contains a large union and
|
|
|
|
* the type information of the value */
|
|
|
|
struct GstValue {
|
|
|
|
GstType type;
|
2017-04-12 14:31:50 +00:00
|
|
|
GstValueUnion data;
|
2017-03-26 19:44:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A lightweight thread in gst. Does not correspond to
|
2017-04-19 13:02:12 +00:00
|
|
|
* operating system threads. Used in coroutines and continuations. */
|
2017-03-26 19:44:57 +00:00
|
|
|
struct GstThread {
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t capacity;
|
|
|
|
GstValue *data;
|
2017-04-12 14:31:50 +00:00
|
|
|
GstThread *parent;
|
2017-03-26 19:44:57 +00:00
|
|
|
enum {
|
|
|
|
GST_THREAD_PENDING = 0,
|
|
|
|
GST_THREAD_ALIVE,
|
|
|
|
GST_THREAD_DEAD
|
|
|
|
} status;
|
|
|
|
};
|
|
|
|
|
2017-04-13 01:21:46 +00:00
|
|
|
/* A dynamic array type. */
|
2017-03-26 19:44:57 +00:00
|
|
|
struct GstArray {
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t capacity;
|
|
|
|
GstValue *data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A bytebuffer type. Used as a mutable string or string builder. */
|
|
|
|
struct GstBuffer {
|
|
|
|
uint32_t count;
|
|
|
|
uint32_t capacity;
|
|
|
|
uint8_t *data;
|
|
|
|
};
|
|
|
|
|
2017-04-25 01:00:56 +00:00
|
|
|
/* A mutable associative data type. Backed by a hashtable. */
|
|
|
|
struct GstTable {
|
2017-03-26 19:44:57 +00:00
|
|
|
uint32_t count;
|
|
|
|
uint32_t capacity;
|
2017-04-16 13:39:41 +00:00
|
|
|
uint32_t deleted;
|
|
|
|
GstValue *data;
|
2017-03-26 19:44:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Some function defintion flags */
|
|
|
|
#define GST_FUNCDEF_FLAG_VARARG 1
|
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
/* A function definition. Contains information need to instantiate closures. */
|
2017-03-26 19:44:57 +00:00
|
|
|
struct GstFuncDef {
|
|
|
|
uint32_t locals;
|
|
|
|
uint32_t arity; /* Not including varargs */
|
|
|
|
uint32_t literalsLen;
|
|
|
|
uint32_t byteCodeLen;
|
|
|
|
uint32_t flags;
|
|
|
|
GstValue *literals; /* Contains strings, FuncDefs, etc. */
|
|
|
|
uint16_t *byteCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A fuction environment */
|
|
|
|
struct GstFuncEnv {
|
|
|
|
GstThread *thread; /* When nil, index the local values */
|
|
|
|
uint32_t stackOffset; /* Used as environment size when off stack */
|
|
|
|
GstValue *values;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* A function */
|
|
|
|
struct GstFunction {
|
|
|
|
GstFuncDef *def;
|
|
|
|
GstFuncEnv *env;
|
|
|
|
GstFunction *parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Contains information about userdata */
|
|
|
|
struct GstUserdataHeader {
|
|
|
|
uint32_t size;
|
2017-04-17 22:46:28 +00:00
|
|
|
const GstValue *meta;
|
2017-03-26 19:44:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* VM return status from c function */
|
|
|
|
#define GST_RETURN_OK 0
|
|
|
|
#define GST_RETURN_ERROR 1
|
|
|
|
#define GST_RETURN_CRASH 2
|
|
|
|
|
|
|
|
/* The VM state */
|
|
|
|
struct Gst {
|
|
|
|
/* Garbage collection */
|
|
|
|
void *blocks;
|
|
|
|
uint32_t memoryInterval;
|
|
|
|
uint32_t nextCollection;
|
|
|
|
uint32_t black : 1;
|
2017-04-15 20:05:59 +00:00
|
|
|
/* Immutable value cache */
|
|
|
|
GstValue *cache;
|
|
|
|
uint32_t cache_capacity;
|
|
|
|
uint32_t cache_count;
|
|
|
|
uint32_t cache_deleted;
|
2017-04-18 20:55:03 +00:00
|
|
|
/* Scratch memory (should be marked in gc) */
|
2017-04-15 20:05:59 +00:00
|
|
|
char *scratch;
|
|
|
|
uint32_t scratch_len;
|
2017-04-18 20:55:03 +00:00
|
|
|
/* GC roots */
|
2017-03-26 19:44:57 +00:00
|
|
|
GstThread *thread;
|
2017-04-25 01:00:56 +00:00
|
|
|
GstTable *modules;
|
|
|
|
GstTable *registry;
|
2017-03-26 19:44:57 +00:00
|
|
|
/* Return state */
|
|
|
|
const char *crash;
|
2017-04-19 13:02:12 +00:00
|
|
|
GstValue ret; /* Returned value from gst_start. */
|
2017-03-26 19:44:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Bytecode */
|
|
|
|
enum GstOpCode {
|
|
|
|
GST_OP_FLS, /* Load false */
|
|
|
|
GST_OP_TRU, /* Load true */
|
|
|
|
GST_OP_NIL, /* Load nil */
|
|
|
|
GST_OP_UPV, /* Load upvalue */
|
|
|
|
GST_OP_JIF, /* Jump if */
|
|
|
|
GST_OP_JMP, /* Jump */
|
|
|
|
GST_OP_SUV, /* Set upvalue */
|
|
|
|
GST_OP_CST, /* Load constant */
|
2017-04-24 20:02:54 +00:00
|
|
|
GST_OP_I16, /* Load 16 bit signed integer */
|
2017-03-26 19:44:57 +00:00
|
|
|
GST_OP_I32, /* Load 32 bit signed integer */
|
2017-04-24 20:02:54 +00:00
|
|
|
GST_OP_I64, /* Load 64 bit signed integer */
|
2017-03-26 19:44:57 +00:00
|
|
|
GST_OP_F64, /* Load 64 bit IEEE double */
|
|
|
|
GST_OP_MOV, /* Move value */
|
|
|
|
GST_OP_CLN, /* Create a closure */
|
|
|
|
GST_OP_ARR, /* Create array */
|
|
|
|
GST_OP_DIC, /* Create object */
|
|
|
|
GST_OP_TUP, /* Create tuple */
|
|
|
|
GST_OP_RET, /* Return from function */
|
|
|
|
GST_OP_RTN, /* Return nil */
|
2017-04-19 13:02:12 +00:00
|
|
|
GST_OP_PSK, /* Push stack */
|
|
|
|
GST_OP_PAR, /* Push array or tuple */
|
2017-03-26 19:44:57 +00:00
|
|
|
GST_OP_CAL, /* Call function */
|
2017-04-12 14:31:50 +00:00
|
|
|
GST_OP_TCL, /* Tail call */
|
|
|
|
GST_OP_YLD /* Yield from function */
|
2017-03-26 19:44:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Buffer functions */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
GstBuffer *gst_buffer(Gst *vm, uint32_t capacity);
|
|
|
|
void gst_buffer_ensure(Gst *vm, GstBuffer *buffer, uint32_t capacity);
|
|
|
|
int gst_buffer_get(GstBuffer *buffer, uint32_t index);
|
|
|
|
void gst_buffer_push(Gst *vm, GstBuffer *buffer, uint8_t c);
|
|
|
|
void gst_buffer_append(Gst *vm, GstBuffer *buffer, const uint8_t *string, uint32_t length);
|
|
|
|
const uint8_t *gst_buffer_to_string(Gst *vm, GstBuffer *buffer);
|
|
|
|
|
|
|
|
/* Define a push function for pushing a certain type to the buffer */
|
|
|
|
#define BUFFER_DEFINE(name, type) \
|
|
|
|
static void gst_buffer_push_##name(Gst * vm, GstBuffer * buffer, type x) { \
|
|
|
|
union { type t; uint8_t bytes[sizeof(type)]; } u; \
|
|
|
|
u.t = x; gst_buffer_append(vm, buffer, u.bytes, sizeof(type)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Array functions */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
GstArray *gst_array(Gst *vm, uint32_t capacity);
|
|
|
|
GstValue gst_array_get(GstArray *array, uint32_t index);
|
|
|
|
int gst_array_set(GstArray *array, uint32_t index, GstValue x);
|
|
|
|
void gst_array_ensure(Gst *vm, GstArray *array, uint32_t capacity);
|
|
|
|
void gst_array_push(Gst *vm, GstArray *array, GstValue x);
|
|
|
|
GstValue gst_array_pop(GstArray *array);
|
2017-04-24 17:12:55 +00:00
|
|
|
GstValue gst_array_peek(GstArray *array);
|
2017-03-26 19:44:57 +00:00
|
|
|
|
2017-04-15 20:05:59 +00:00
|
|
|
/****/
|
|
|
|
/* Userdata functions */
|
|
|
|
/****/
|
|
|
|
|
2017-04-17 22:46:28 +00:00
|
|
|
void *gst_userdata(Gst *vm, uint32_t size, const GstValue *meta);
|
2017-04-15 20:05:59 +00:00
|
|
|
|
2017-03-26 19:44:57 +00:00
|
|
|
/****/
|
|
|
|
/* Tuple functions */
|
|
|
|
/****/
|
|
|
|
|
2017-04-15 20:05:59 +00:00
|
|
|
GstValue *gst_tuple_begin(Gst *vm, uint32_t length);
|
|
|
|
const GstValue *gst_tuple_end(Gst *vm, GstValue *tuple);
|
2017-03-26 19:44:57 +00:00
|
|
|
|
|
|
|
/****/
|
|
|
|
/* String functions */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
uint8_t *gst_string_begin(Gst *vm, uint32_t len);
|
|
|
|
const uint8_t *gst_string_end(Gst *vm, uint8_t *str);
|
2017-04-15 20:05:59 +00:00
|
|
|
const uint8_t *gst_string_b(Gst *vm, const uint8_t *buf, uint32_t len);
|
|
|
|
const uint8_t *gst_string_c(Gst *vm, const char *cstring);
|
|
|
|
GstValue gst_string_cv(Gst *vm, const char *string);
|
2017-03-26 19:44:57 +00:00
|
|
|
int gst_string_compare(const uint8_t *lhs, const uint8_t *rhs);
|
|
|
|
|
|
|
|
/****/
|
2017-04-15 20:05:59 +00:00
|
|
|
/* Struct functions */
|
2017-03-26 19:44:57 +00:00
|
|
|
/****/
|
|
|
|
|
2017-04-15 20:05:59 +00:00
|
|
|
GstValue *gst_struct_begin(Gst *vm, uint32_t count);
|
|
|
|
void gst_struct_put(GstValue *st, GstValue key, GstValue value);
|
|
|
|
const GstValue *gst_struct_end(Gst *vm, GstValue *st);
|
|
|
|
GstValue gst_struct_get(const GstValue *st, GstValue key);
|
2017-04-16 13:39:41 +00:00
|
|
|
GstValue gst_struct_next(const GstValue *st, GstValue key);
|
2017-03-26 19:44:57 +00:00
|
|
|
|
|
|
|
/****/
|
2017-04-25 01:00:56 +00:00
|
|
|
/* Table functions */
|
2017-03-26 19:44:57 +00:00
|
|
|
/****/
|
|
|
|
|
2017-04-25 01:00:56 +00:00
|
|
|
GstTable *gst_table(Gst *vm, uint32_t capacity);
|
|
|
|
GstValue gst_table_get(GstTable *t, GstValue key);
|
|
|
|
GstValue gst_table_remove(GstTable *t, GstValue key);
|
|
|
|
void gst_table_put(Gst *vm, GstTable *t, GstValue key, GstValue value);
|
|
|
|
GstValue gst_table_next(GstTable *o, GstValue key);
|
2017-03-26 19:44:57 +00:00
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Threads */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
#define gst_thread_stack(t) ((t)->data + (t)->count)
|
|
|
|
GstThread *gst_thread(Gst *vm, GstValue callee, uint32_t capacity);
|
|
|
|
void gst_thread_ensure_extra(Gst *vm, GstThread *thread, uint32_t extra);
|
|
|
|
void gst_thread_push(Gst *vm, GstThread *thread, GstValue x);
|
|
|
|
void gst_thread_pushnil(Gst *vm, GstThread *thread, uint32_t n);
|
|
|
|
void gst_thread_tuplepack(Gst *vm, GstThread *thread, uint32_t n);
|
|
|
|
GstValue *gst_thread_beginframe(Gst *vm, GstThread *thread, GstValue callee, uint32_t arity);
|
|
|
|
void gst_thread_endframe(Gst *vm, GstThread *thread);
|
|
|
|
GstValue *gst_thread_popframe(Gst *vm, GstThread *thread);
|
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Value manipulation */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
int gst_truthy(GstValue x);
|
|
|
|
int gst_compare(GstValue x, GstValue y);
|
|
|
|
int gst_equals(GstValue x, GstValue y);
|
|
|
|
const char *gst_get(GstValue ds, GstValue key, GstValue *out);
|
|
|
|
const char *gst_set(Gst *vm, GstValue ds, GstValue key, GstValue value);
|
|
|
|
const uint8_t *gst_to_string(Gst *vm, GstValue x);
|
|
|
|
uint32_t gst_hash(GstValue x);
|
2017-04-24 20:02:54 +00:00
|
|
|
GstInteger gst_length(Gst *vm, GstValue x);
|
2017-03-26 19:44:57 +00:00
|
|
|
|
|
|
|
/****/
|
|
|
|
/* Serialization */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data format
|
|
|
|
* State is encoded as a string of unsigned bytes.
|
|
|
|
*
|
|
|
|
* Types:
|
|
|
|
*
|
|
|
|
* Byte 0 to 200: small integer byte - 100
|
|
|
|
* Byte 201: Nil
|
|
|
|
* Byte 202: True
|
|
|
|
* Byte 203: False
|
|
|
|
* Byte 204: Number - double format
|
|
|
|
* Byte 205: String - [u32 length]*[u8... characters]
|
|
|
|
* Byte 206: Symbol - [u32 length]*[u8... characters]
|
|
|
|
* Byte 207: Buffer - [u32 length]*[u8... characters]
|
|
|
|
* Byte 208: Array - [u32 length]*[value... elements]
|
|
|
|
* Byte 209: Tuple - [u32 length]*[value... elements]
|
|
|
|
* Byte 210: Thread - [u8 state][u32 frames]*[[value callee][value env]
|
|
|
|
* [u32 pcoffset][u32 erroffset][u16 ret][u16 errloc][u16 size]*[value ...stack]
|
2017-04-25 01:00:56 +00:00
|
|
|
* Byte 211: Table - [u32 length]*2*[value... kvs]
|
2017-03-26 19:44:57 +00:00
|
|
|
* Byte 212: FuncDef - [u32 locals][u32 arity][u32 flags][u32 literallen]*[value...
|
|
|
|
* literals][u32 bytecodelen]*[u16... bytecode]
|
|
|
|
* Byte 213: FunEnv - [value thread][u32 length]*[value ...upvalues]
|
|
|
|
* (upvalues is not read if thread is a thread object)
|
|
|
|
* Byte 214: Func - [value parent][value def][value env]
|
|
|
|
* (nil values indicate empty)
|
|
|
|
* Byte 215: LUdata - [value meta][u32 length]*[u8... bytes]
|
|
|
|
* Byte 216: CFunc - [u32 length]*[u8... idstring]
|
|
|
|
* Byte 217: Ref - [u32 id]
|
2017-04-24 20:02:54 +00:00
|
|
|
* Byte 218: Integer - [i64 value]
|
2017-03-26 19:44:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
const char *gst_deserialize(
|
|
|
|
Gst *vm,
|
|
|
|
const uint8_t *data,
|
|
|
|
uint32_t len,
|
|
|
|
GstValue *out,
|
|
|
|
const uint8_t *nextData);
|
|
|
|
const char *gst_serialize(Gst *vm, GstBuffer *buffer, GstValue x);
|
|
|
|
|
|
|
|
/****/
|
|
|
|
/* GC */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
#define GST_MEMTAG_STRING 4
|
2017-04-15 20:05:59 +00:00
|
|
|
#define GST_MEMTAG_TUPLE 8
|
|
|
|
#define GST_MEMTAG_STRUCT 16
|
2017-03-26 19:44:57 +00:00
|
|
|
|
2017-04-12 14:31:50 +00:00
|
|
|
void gst_mark_value(Gst *vm, GstValue x);
|
|
|
|
void gst_mark(Gst *vm, GstValueUnion x, GstType type);
|
2017-03-26 19:44:57 +00:00
|
|
|
void gst_sweep(Gst *vm);
|
|
|
|
void *gst_alloc(Gst *vm, uint32_t size);
|
|
|
|
void *gst_zalloc(Gst *vm, uint32_t size);
|
|
|
|
void gst_mem_tag(void *mem, uint32_t tags);
|
|
|
|
void gst_collect(Gst *vm);
|
|
|
|
void gst_maybe_collect(Gst *vm);
|
|
|
|
void gst_clear_memory(Gst *vm);
|
|
|
|
|
|
|
|
/****/
|
|
|
|
/* VM */
|
|
|
|
/****/
|
|
|
|
|
|
|
|
void gst_init(Gst *vm);
|
|
|
|
void gst_deinit(Gst *vm);
|
|
|
|
int gst_run(Gst *vm, GstValue func);
|
|
|
|
int gst_continue(Gst *vm);
|
2017-04-25 16:26:58 +00:00
|
|
|
GstValue gst_arg(Gst *vm, uint32_t index);
|
|
|
|
void gst_set_arg(Gst *vm, uint32_t index, GstValue x);
|
|
|
|
uint32_t gst_count_args(Gst *vm);
|
2017-03-26 19:44:57 +00:00
|
|
|
|
2017-04-19 16:56:29 +00:00
|
|
|
/****/
|
2017-04-13 01:21:46 +00:00
|
|
|
/* C Api */
|
2017-04-19 16:56:29 +00:00
|
|
|
/****/
|
2017-04-13 01:21:46 +00:00
|
|
|
|
2017-04-25 01:00:56 +00:00
|
|
|
GstValue gst_cmodule_table(Gst *vm, const GstModuleItem *mod);
|
2017-04-18 20:55:03 +00:00
|
|
|
GstValue gst_cmodule_struct(Gst *vm, const GstModuleItem *mod);
|
|
|
|
void gst_module_put(Gst *vm, const char *packagename, GstValue mod);
|
|
|
|
GstValue gst_module_get(Gst *vm, const char *packagename);
|
|
|
|
void gst_register_put(Gst *vm, const char *packagename, GstValue mod);
|
|
|
|
GstValue gst_register_get(Gst *vm, const char *name);
|
2017-04-17 22:46:28 +00:00
|
|
|
|
|
|
|
/* Wrap data in GstValue */
|
|
|
|
GstValue gst_wrap_nil();
|
2017-04-24 20:02:54 +00:00
|
|
|
GstValue gst_wrap_real(GstReal x);
|
|
|
|
GstValue gst_wrap_integer(GstInteger x);
|
2017-04-17 22:46:28 +00:00
|
|
|
GstValue gst_wrap_boolean(int x);
|
|
|
|
GstValue gst_wrap_string(const uint8_t *x);
|
|
|
|
GstValue gst_wrap_array(GstArray *x);
|
|
|
|
GstValue gst_wrap_tuple(const GstValue *x);
|
|
|
|
GstValue gst_wrap_struct(const GstValue *x);
|
|
|
|
GstValue gst_wrap_thread(GstThread *x);
|
|
|
|
GstValue gst_wrap_buffer(GstBuffer *x);
|
|
|
|
GstValue gst_wrap_function(GstFunction *x);
|
|
|
|
GstValue gst_wrap_cfunction(GstCFunction x);
|
2017-04-25 01:00:56 +00:00
|
|
|
GstValue gst_wrap_table(GstTable *x);
|
2017-04-17 22:46:28 +00:00
|
|
|
GstValue gst_wrap_userdata(void *x);
|
|
|
|
GstValue gst_wrap_funcenv(GstFuncEnv *x);
|
|
|
|
GstValue gst_wrap_funcdef(GstFuncDef *x);
|
2017-04-13 01:21:46 +00:00
|
|
|
|
2017-04-19 16:56:29 +00:00
|
|
|
/* Check data from arguments */
|
|
|
|
int gst_check_nil(Gst *vm, uint32_t i);
|
2017-04-24 20:02:54 +00:00
|
|
|
int gst_check_real(Gst *vm, uint32_t i, GstReal (*x));
|
|
|
|
int gst_check_integer(Gst *vm, uint32_t i, GstInteger (*x));
|
2017-04-19 16:56:29 +00:00
|
|
|
int gst_check_boolean(Gst *vm, uint32_t i, int (*x));
|
|
|
|
int gst_check_string(Gst *vm, uint32_t i, const uint8_t *(*x));
|
|
|
|
int gst_check_array(Gst *vm, uint32_t i, GstArray *(*x));
|
|
|
|
int gst_check_tuple(Gst *vm, uint32_t i, const GstValue *(*x));
|
|
|
|
int gst_check_struct(Gst *vm, uint32_t i, const GstValue *(*x));
|
|
|
|
int gst_check_thread(Gst *vm, uint32_t i, GstThread *(*x));
|
|
|
|
int gst_check_buffer(Gst *vm, uint32_t i, GstBuffer *(*x));
|
|
|
|
int gst_check_function(Gst *vm, uint32_t i, GstFunction *(*x));
|
|
|
|
int gst_check_cfunction(Gst *vm, uint32_t i, GstCFunction (*x));
|
2017-04-25 01:00:56 +00:00
|
|
|
int gst_check_table(Gst *vm, uint32_t i, GstTable *(*x));
|
2017-04-19 16:56:29 +00:00
|
|
|
int gst_check_userdata(Gst *vm, uint32_t i, void *(*x));
|
|
|
|
int gst_check_funcenv(Gst *vm, uint32_t i, GstFuncEnv *(*x));
|
|
|
|
int gst_check_funcdef(Gst *vm, uint32_t i, GstFuncDef *(*x));
|
|
|
|
|
2017-04-24 17:12:55 +00:00
|
|
|
int gst_seq_view(GstValue seq, const GstValue **data, uint32_t *len);
|
|
|
|
int gst_chararray_view(GstValue str, const uint8_t **data, uint32_t *len);
|
|
|
|
int gst_hashtable_view(GstValue tab, const GstValue **data, uint32_t *cap);
|
|
|
|
|
2017-04-19 16:56:29 +00:00
|
|
|
/****/
|
|
|
|
/* Misc */
|
|
|
|
/****/
|
|
|
|
|
2017-04-24 20:02:54 +00:00
|
|
|
GstReal gst_integer_to_real(GstInteger x);
|
|
|
|
GstInteger gst_real_to_integer(GstReal x);
|
|
|
|
GstInteger gst_startrange(GstInteger raw, uint32_t len);
|
|
|
|
GstInteger gst_endrange(GstInteger raw, uint32_t len);
|
2017-04-19 16:56:29 +00:00
|
|
|
|
2017-03-26 19:44:57 +00:00
|
|
|
#endif // GST_H_defined
|