From e003088c2ff66d96697e29a72a164d5c9397a165 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Wed, 20 Nov 2019 17:18:06 +0100 Subject: [PATCH] :art: Update ast --- Makefile | 4 +- ast | 2 +- include/plug.h | 2 +- src/gwion.c | 2 +- src/plug.c | 159 +++++++++++++++++++++++++++++++++---------------- 5 files changed, 112 insertions(+), 57 deletions(-) diff --git a/Makefile b/Makefile index 9e9f6f7e..883f6ae1 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ include config.mk GWION_PACKAGE=gwion CFLAGS += -DGWION_PACKAGE='"${GWION_PACKAGE}"' +CFLAGS += -DGWION_NOLINT GIT_BRANCH=$(shell git branch | grep "*" | cut -d" " -f2) @@ -45,13 +46,12 @@ LDFLAGS += -lm endif ifeq ($(shell uname), Linux) -LDFLAGS += -lrt -rdynamic endif CFLAGS += -DGWION_BUILTIN #GWLIBS = libgwion.a ast/libgwion_ast.a ast/libgwion_grammar.a util/libgwion_util.a -GWLIBS = libgwion.a ast/libgwion_grammar.a ast/libgwion_ast.a util/libgwion_util.a +GWLIBS = libgwion.a ast/libgwion_ast.a util/libgwion_util.a _LDFLAGS = ${GWLIBS} ${LDFLAGS} all: options-show util/libgwion_util.a astlib libgwion.a src/main.o diff --git a/ast b/ast index 6e9203f4..f917f914 160000 --- a/ast +++ b/ast @@ -1 +1 @@ -Subproject commit 6e9203f46d025b4f44af1ec7c51788e0b193970c +Subproject commit f917f9149c3938f7ee4703643f5f16f972b2debd diff --git a/include/plug.h b/include/plug.h index 54185abc..6a81aa66 100644 --- a/include/plug.h +++ b/include/plug.h @@ -13,7 +13,7 @@ typedef struct PlugInfo_ { struct Vector_ vec[GWPLUG_LAST]; } PlugInfo; -ANN PlugInfo* new_plug(MemPool, Vector); +ANN PlugInfo* new_pluginfo(MemPool, Vector); ANN void plug_run(const struct Gwion_*, const Vector); ANN void free_plug(const struct Gwion_*); ANN void* get_module(const struct Gwion_*, const m_str); diff --git a/src/gwion.c b/src/gwion.c index 31a3cdbe..6f067f55 100644 --- a/src/gwion.c +++ b/src/gwion.c @@ -74,7 +74,7 @@ ANN m_bool gwion_ini(const Gwion gwion, Arg* arg) { arg->si = gwion->vm->bbq->si; const m_bool ret = arg_parse(gwion, arg); if(ret) { - gwion->data->plug = new_plug(gwion->mp, &arg->lib); + gwion->data->plug = new_pluginfo(gwion->mp, &arg->lib); gwion->emit->info->memoize = arg->memoize; shreduler_set_loop(gwion->vm->shreduler, arg->loop); if(gwion_audio(gwion) > 0 && gwion_engine(gwion)) { diff --git a/src/plug.c b/src/plug.c index 6f9da98f..bd34041e 100644 --- a/src/plug.c +++ b/src/plug.c @@ -42,72 +42,124 @@ struct Plug_ { #define DLERROR() "plugin" #endif -ANN static void plug_get(MemPool p, PlugInfo* pi, const m_str c) { +struct PlugHandle { + MemPool mp; + PlugInfo* pi; + void *dl; + m_str file; + m_str name; +}; + +ANN static struct Plug_* new_plug(MemPool p, const modini ini) { + struct Plug_ *plug = mp_calloc(p, Plug); + plug->ini = ini; + return plug; +} + +ANN static void plug_import(struct PlugHandle *h) { + const import imp = DLSYM(h->dl, import, GWIMPORT_NAME); + if(imp) + vector_add(&h->pi->vec[GWPLUG_IMPORT], (vtype)imp); +} + +ANN static void plug_module(struct PlugHandle *h) { + const modini ini = DLSYM(h->dl, modini, GWMODINI_NAME); + if(ini) { + struct Plug_ *plug = new_plug(h->mp, ini); + plug->name = h->name; + plug->end = DLSYM(h->dl, modend, GWMODEND_NAME); + vector_add(&h->pi->vec[GWPLUG_MODULE], (vtype)plug); + } +} + +ANN static void plug_driver(struct PlugHandle *h) { + const f_bbqset drv = DLSYM(h->dl, f_bbqset, GWDRIVER_NAME); + if(drv) + map_set(&h->pi->drv, (vtype)h->name, (vtype)drv); +} + +ANN static inline m_str plug_name(struct PlugHandle *h) { + const modstr str = DLSYM(h->dl, modstr, GWMODSTR_NAME); + return str ? str() : NULL; +} + +ANN static void plug_get(struct PlugHandle *h, const m_str c) { void* dl = DLOPEN(c, RTLD_LAZY | RTLD_GLOBAL); if(dl) { - vector_add(&pi->vec[GWPLUG_DL], (vtype)dl); - const import imp = DLSYM(dl, import, GWIMPORT_NAME); - if(imp) - vector_add(&pi->vec[GWPLUG_IMPORT], (vtype)imp); - const modini ini = DLSYM(dl, modini, GWMODINI_NAME); - if(ini) { - struct Plug_ *plug = mp_calloc(p, Plug); - plug->ini = ini; - const modstr str = DLSYM(dl, modstr, GWMODSTR_NAME); - plug->name = str(); - plug->end = DLSYM(dl, modend, GWMODEND_NAME); - vector_add(&pi->vec[GWPLUG_MODULE], (vtype)plug); - } - const f_bbqset drv = DLSYM(dl, f_bbqset, GWDRIVER_NAME); - if(drv) { - const modstr str = DLSYM(dl, modstr, GWMODSTR_NAME); - map_set(&pi->drv, (vtype)str(), (vtype)drv); - } + vector_add(&h->pi->vec[GWPLUG_DL], (vtype)dl); + h->name = plug_name(h); + plug_import(h); + plug_module(h); + plug_driver(h); } else gw_err(_("error in %s."), DLERROR()); } -ANN PlugInfo* new_plug(MemPool p, const Vector list) { - PlugInfo *pi = (PlugInfo*)mp_calloc(p, PlugInfo); - for(m_uint i = 0; i < GWPLUG_LAST; ++i) - vector_init(&pi->vec[i]); - map_init(&pi->drv); - for(m_uint i = 0; i < vector_size(list); i++) { - const m_str dir = (m_str)vector_at(list, i); - char gname[strlen(dir) + 6]; - strcpy(gname, dir); - strcpy(gname + strlen(dir), "/*.so"); +ANN static void plug_get_all(struct PlugHandle *h, const m_str name) { #ifndef BUILD_ON_WINDOWS - glob_t results; - if(glob(gname, 0, NULL, &results)) - continue; - for(m_uint i = 0; i < results.gl_pathc; i++) - plug_get(p, pi, results.gl_pathv[i]); - globfree(& results); + glob_t results; + if(glob(name, 0, NULL, &results)) + return; + for(m_uint i = 0; i < results.gl_pathc; i++) + plug_get(h, results.gl_pathv[i]); + globfree(&results); #else WIN32_FIND_DATA filedata; - HANDLE file = FindFirstFileA(gname,&filedata); + HANDLE file = FindFirstFileA(name,&filedata); if(file == INVALID_HANDLE_VALUE) - continue; - do plug_get(p, pi, filedata.cFileName); + return; + do plug_get(&h, filedata.cFileName); while(FindNextFile(file,&filedata) == 0); FindClose(file); #endif +} + +ANN2(1) static void* pp_ini(const struct Gwion_ *gwion, const Vector v) { + pparg_run(gwion->ppa, v); + return NULL; +} + +ANN static void register_pp(const struct PlugHandle* h) { + struct Plug_ *plug = new_plug(h->mp, pp_ini); + plug->name = "pp"; + vector_add(&h->pi->vec[GWPLUG_MODULE], (vtype)plug); +} + +ANN PlugInfo* new_pluginfo(MemPool p, const Vector list) { + PlugInfo *pi = (PlugInfo*)mp_calloc(p, PlugInfo); + for(m_uint i = 0; i < GWPLUG_LAST; ++i) + vector_init(&pi->vec[i]); + map_init(&pi->drv); + struct PlugHandle h = { .mp=p, .pi=pi }; + register_pp(&h); + for(m_uint i = 0; i < vector_size(list); i++) { + const m_str dir = (m_str)vector_at(list, i); + char name[strlen(dir) + 6]; + sprintf(name, "%s/*.so", dir); + plug_get_all(&h, name); } return pi; } -void free_plug(const struct Gwion_ *gwion) { - PlugInfo *p = gwion->data->plug; - struct Vector_ * const v = p->vec; - for(m_uint i = 0; i < vector_size(&v[GWPLUG_MODULE]); ++i) { - struct Plug_ *plug = (struct Plug_*)vector_at(&v[GWPLUG_MODULE], i); +ANN static void plug_free_module(const struct Gwion_* gwion, const Vector v) { + for(m_uint i = 0; i < vector_size(v); ++i) { + struct Plug_ *plug = (struct Plug_*)vector_at(v, i); if(plug->end) plug->end(gwion, plug->self); mp_free(gwion->mp, Plug, plug); } - for(m_uint i = 0; i < vector_size(&v[GWPLUG_DL]); ++i) - DLCLOSE((void*)vector_at(&v[GWPLUG_DL], i)); +} + +ANN static inline void plug_free_dls(const Vector v) { + for(m_uint i = 0; i < vector_size(v); ++i) + DLCLOSE((void*)vector_at(v, i)); +} + +void free_plug(const struct Gwion_ *gwion) { + PlugInfo *p = gwion->data->plug; + struct Vector_ * const v = p->vec; + plug_free_module(gwion, &v[GWPLUG_MODULE]); + plug_free_dls(&v[GWPLUG_DL]); for(m_uint i = 0; i < GWPLUG_LAST; ++i) vector_release(&v[i]); map_release(&p->drv); @@ -130,23 +182,26 @@ ANN static Vector get_arg(MemPool p, const m_str name, const Vector v) { if(!strncmp(name, str, len)) { vector_rem(v, i-1); const m_str arg = strchr(str, '='); - return arg ? split_args(p, str) : NULL; + return arg ? split_args(p, arg+1) : NULL; } } return NULL; } -void plug_run(const struct Gwion_ *gwion, const Vector args) { +ANN static void plug_free_arg(MemPool p, const Vector v) { + for(m_uint i = 0; i < vector_size(v); ++i) + free_mstr(p, (m_str)vector_at(v, i)); + free_vector(p, v); +} + +ANN void plug_run(const struct Gwion_ *gwion, const Vector args) { const Vector v = &gwion->data->plug->vec[GWPLUG_MODULE]; for(m_uint i = 0; i < vector_size(v); ++i) { struct Plug_ *plug = (struct Plug_*)vector_at(v, i); const Vector arg = get_arg(gwion->mp, plug->name, args); plug->self = plug->ini(gwion, arg); - if(arg) { - for(m_uint i = 0; i < vector_size(arg); ++i) - free_mstr(gwion->mp, (m_str)vector_at(arg, i)); - free_vector(gwion->mp, arg); - } + if(arg) + plug_free_arg(gwion->mp, arg); } } -- 2.43.0