diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-12-23 20:54:05 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-02-17 14:10:51 +0100 |
commit | 3756806674da909ec6dc10ad25862b592792604e (patch) | |
tree | f2af7e47f8444caaff0a5a33599f381889db24e3 /module.h | |
parent | 77580a893283f2bde7ab46496bd3a3d7b2fc6784 (diff) |
treewide: rewrite ucode interpreter
Replace the former AST walking interpreter implementation with a single pass
bytecode compiler and a corresponding virtual machine.
The rewrite lays the groundwork for a couple of improvements with will be
subsequently implemented:
- Ability to precompile ucode sources into binary byte code
- Strippable debug information
- Reduced runtime memory usage
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'module.h')
-rw-r--r-- | module.h | 51 |
1 files changed, 33 insertions, 18 deletions
@@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Jo-Philipp Wich <jo@mein.io> + * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -17,27 +17,42 @@ #ifndef __MODULE_H_ #define __MODULE_H_ -#include "ast.h" #include "lib.h" +#include "object.h" +#include "vm.h" -struct uc_ops { - bool (*register_function)(struct uc_state *, struct json_object *, const char *, uc_c_fn *); - bool (*register_type)(const char *, struct json_object *, void (*)(void *)); - struct json_object *(*set_type)(struct json_object *, const char *, void *); - void **(*get_type)(struct json_object *, const char *); - struct json_object *(*new_object)(struct json_object *); - struct json_object *(*new_double)(double); - struct json_object *(*invoke)(struct uc_state *, uint32_t, struct json_object *, struct json_object *, struct json_object *); - enum json_type (*cast_number)(struct json_object *, int64_t *, double *); -}; - -extern const struct uc_ops ut; - -#define register_functions(state, ops, functions, scope) \ +#define register_functions(scope, functions) \ if (scope) \ for (int i = 0; i < ARRAY_SIZE(functions); i++) \ - ops->register_function(state, scope, functions[i].name, functions[i].func) + json_object_object_add(scope->header.jso, functions[i].name, \ + ops->value.cfunc(functions[i].name, functions[i].func)) + +#define alloc_prototype(functions) ({ \ + uc_prototype *__proto = uc_object_as_prototype(ops->value.proto(NULL)); \ + register_functions(__proto, functions); \ + __proto; \ +}) + +#define declare_type(name, proto, freefn) \ + ops->ressource.define(name, proto, freefn) + +#define alloc_ressource(data, type) \ + ops->ressource.create(xjs_new_object(), type, data) + +#define register_ressource(scope, key, res) \ + json_object_object_add((scope)->header.jso, key, (res)->header.jso) + +static const uc_ops *ops; + +void uc_module_init(uc_prototype *scope) __attribute__((weak)); + +void uc_module_entry(const uc_ops *_ops, uc_prototype *scope); +void uc_module_entry(const uc_ops *_ops, uc_prototype *scope) +{ + ops = _ops; -void uc_module_init(const struct uc_ops *, struct uc_state *, struct json_object *); + if (uc_module_init) + uc_module_init(scope); +} #endif /* __MODULE_H_ */ |