Verified Commit 2875a397 authored by Vladimír Čunát's avatar Vladimír Čunát
Browse files

module API+ABI: remove one level of indirection

... for layers and props.  This breaks C module API+ABI.

It seemed weird to repeatedly call a function that returns a pointer
to a structure in which we find the function we want to actually call.
We've never used changing these functions AFAIK, and the target
functions could easily be written to change their behavior instead
(i.e. move the indirection *inside* the function).

When breaking this, I also removed these two (_layers and _props)
from the dynamic symbols (to be) exported from the C modules.
They always pointed to memory belonging inside the module,
and they seem quite sensible to be set up by the _init symbol instead.
parent e806b5f9
Loading
Loading
Loading
Loading
+3 −4
Original line number Diff line number Diff line
@@ -662,13 +662,13 @@ int engine_init(struct engine *engine, knot_mm_t *pool)
	return ret;
}

/** Unregister a (found) module */
static void engine_unload(struct engine *engine, struct kr_module *module)
{
	/* Unregister module */
	auto_free char *name = strdup(module->name);
	kr_module_unload(module);
	/* Clear in Lua world, but not for embedded modules ('cache' in particular). */
	if (name && !kr_module_embedded(name)) {
	if (name && !kr_module_get_embedded(name)) {
		lua_pushnil(engine->L);
		lua_setglobal(engine->L, name);
	}
@@ -821,8 +821,7 @@ static int register_properties(struct engine *engine, struct kr_module *module)
		REGISTER_MODULE_CALL(engine->L, module, module->config, "config");
	}

	const struct kr_prop *p = module->props == NULL ? NULL : module->props();
	for (; p && p->name; ++p) {
	for (const struct kr_prop *p = module->props; p && p->name; ++p) {
		if (p->cb != NULL) {
			REGISTER_MODULE_CALL(engine->L, module, p->cb, p->name);
		}
+3 −10
Original line number Diff line number Diff line
@@ -246,14 +246,6 @@ static kr_layer_api_t *l_ffi_layer_create(lua_State *L, struct kr_module *module
	return api;
}

/** @internal Retrieve C layer api wrapper. */
static const kr_layer_api_t *l_ffi_layer(struct kr_module *module)
{
	if (module) {
		return (const kr_layer_api_t *)module->data;
	}
	return NULL;
}
#undef LAYER_REGISTER

int ffimodule_register_lua(struct engine *engine, struct kr_module *module, const char *name)
@@ -278,8 +270,9 @@ int ffimodule_register_lua(struct engine *engine, struct kr_module *module, cons
	/* Bake layer API if defined in module */
	lua_getfield(L, -1, "layer");
	if (!lua_isnil(L, -1)) {
		module->layer = &l_ffi_layer;
		module->data = l_ffi_layer_create(L, module);
		module->layer = l_ffi_layer_create(L, module);
		/* most likely not needed, but compatibility for now */
		module->data = (void *)module->layer;
	}
	module->lib = L;
	lua_pop(L, 2); /* Clear the layer + module global */
+6 −5
Original line number Diff line number Diff line
@@ -18,14 +18,15 @@
#include "lib/cache/api.h"

/** Module implementation. */
const kr_layer_api_t *cache_layer(struct kr_module *module)
int cache_init(struct kr_module *self)
{
	static const kr_layer_api_t _layer = {
	static const kr_layer_api_t layer = {
		.produce = &cache_peek,
		.consume = &cache_stash,
	};

	return &_layer;
	self->layer = &layer;
	return kr_ok();
}

KR_MODULE_EXPORT(cache)
KR_MODULE_EXPORT(cache) /* useless for builtin module, but let's be consistent */
+5 −4
Original line number Diff line number Diff line
@@ -1111,17 +1111,18 @@ static int resolve(kr_layer_t *ctx, knot_pkt_t *pkt)
}

/** Module implementation. */
const kr_layer_api_t *iterate_layer(struct kr_module *module)
int iterate_init(struct kr_module *self)
{
	static const kr_layer_api_t _layer = {
	static const kr_layer_api_t layer = {
		.begin = &begin,
		.reset = &reset,
		.consume = &resolve,
		.produce = &prepare_query
	};
	return &_layer;
	self->layer = &layer;
	return kr_ok();
}

KR_MODULE_EXPORT(iterate)
KR_MODULE_EXPORT(iterate) /* useless for builtin module, but let's be consistent */

#undef VERBOSE_MSG
+5 −9
Original line number Diff line number Diff line
@@ -1116,21 +1116,17 @@ static int validate(kr_layer_t *ctx, knot_pkt_t *pkt)
	VERBOSE_MSG(qry, "<= answer valid, OK\n");
	return KR_STATE_DONE;
}

/** Module implementation. */
const kr_layer_api_t *validate_layer(struct kr_module *module)
int validate_init(struct kr_module *self)
{
	static const kr_layer_api_t _layer = {
	static const kr_layer_api_t layer = {
		.consume = &validate,
	};
	/* Store module reference */
	return &_layer;
}

int validate_init(struct kr_module *module)
{
	self->layer = &layer;
	return kr_ok();
}

KR_MODULE_EXPORT(validate)
KR_MODULE_EXPORT(validate) /* useless for builtin module, but let's be consistent */

#undef VERBOSE_MSG
Loading