Skip to content

Commit 9c4d55a

Browse files
authored
Merge pull request #296 from rxbryan/bug-fix2
metacall-plugin-local-test working properly
2 parents d33c725 + 512f0d3 commit 9c4d55a

File tree

7 files changed

+131
-6
lines changed

7 files changed

+131
-6
lines changed

source/extensions/plugin_extension/source/plugin_extension.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ static int plugin_extension_get_path(std::string &ext_path)
6767
return 0;
6868
}
6969

70-
int plugin_extension(void *, void *)
70+
int plugin_extension(void *loader, void *context)
7171
{
72+
(void)loader;
7273
std::string ext_path;
7374

7475
if (plugin_extension_get_path(ext_path) != 0)
@@ -102,7 +103,7 @@ int plugin_extension(void *, void *)
102103
{
103104
log_write("metacall", LOG_LEVEL_DEBUG, "Loading extension: %s", dir.path().filename().c_str());
104105

105-
if (metacall_load_from_configuration(dir.path().c_str(), NULL, config_allocator) != 0)
106+
if (metacall_load_from_configuration_ctx(dir.path().c_str(), context, config_allocator) != 0)
106107
{
107108
log_write("metacall", LOG_LEVEL_ERROR, "Failed to load extension: %s", dir.path().c_str());
108109
return 1;

source/loader/include/loader/loader.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ LOADER_API int loader_execution_path(const loader_tag tag, const loader_path pat
6565

6666
LOADER_API int loader_load_from_file(const loader_tag tag, const loader_path paths[], size_t size, void **handle);
6767

68+
LOADER_API int loader_load_from_file_ctx(const loader_tag tag, const loader_path paths[], size_t size, void *ctx);
69+
6870
LOADER_API int loader_load_from_memory(const loader_tag tag, const char *buffer, size_t size, void **handle);
6971

7072
LOADER_API int loader_load_from_package(const loader_tag tag, const loader_path path, void **handle);
7173

72-
LOADER_API int loader_load_from_configuration(const loader_path path, void **handle, void *allocator);
74+
LOADER_API int loader_load_from_configuration(const loader_path path, void **handle, void *ctx, void *allocator);
7375

7476
LOADER_API loader_impl loader_get_impl(const loader_tag tag);
7577

source/loader/include/loader/loader_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ LOADER_API int loader_impl_execution_path(plugin p, loader_impl impl, const load
5959

6060
LOADER_API int loader_impl_load_from_file(plugin_manager manager, plugin p, loader_impl impl, const loader_path paths[], size_t size, void **handle_ptr);
6161

62+
LOADER_API int loader_impl_load_from_file_ctx(plugin_manager manager, plugin p, loader_impl impl, const loader_path paths[], size_t size, void *ctx);
63+
6264
LOADER_API int loader_impl_load_from_memory(plugin_manager manager, plugin p, loader_impl impl, const char *buffer, size_t size, void **handle_ptr);
6365

6466
LOADER_API int loader_impl_load_from_package(plugin_manager manager, plugin p, loader_impl impl, const loader_path path, void **handle_ptr);

source/loader/source/loader.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,26 @@ int loader_load_from_file(const loader_tag tag, const loader_path paths[], size_
302302
return loader_impl_load_from_file(&loader_manager, p, plugin_impl_type(p, loader_impl), paths, size, handle);
303303
}
304304

305+
int loader_load_from_file_ctx(const loader_tag tag, const loader_path paths[], size_t size, void *ctx)
306+
{
307+
if (loader_initialize() == 1)
308+
{
309+
return 1;
310+
}
311+
312+
plugin p = loader_get_impl_plugin(tag);
313+
314+
if (p == NULL)
315+
{
316+
log_write("metacall", LOG_LEVEL_ERROR, "Tried to load %" PRIuS " file(s) from non existent loader (%s): %s", size, tag, paths[0]);
317+
return 1;
318+
}
319+
320+
log_write("metacall", LOG_LEVEL_DEBUG, "Loading %" PRIuS " file(s) (%s) from path(s): %s ...", size, tag, paths[0]);
321+
322+
return loader_impl_load_from_file_ctx(&loader_manager, p, plugin_impl_type(p, loader_impl), paths, size, ctx);
323+
}
324+
305325
int loader_load_from_memory(const loader_tag tag, const char *buffer, size_t size, void **handle)
306326
{
307327
if (loader_initialize() == 1)
@@ -342,7 +362,7 @@ int loader_load_from_package(const loader_tag tag, const loader_path path, void
342362
return loader_impl_load_from_package(&loader_manager, p, plugin_impl_type(p, loader_impl), path, handle);
343363
}
344364

345-
int loader_load_from_configuration(const loader_path path, void **handle, void *allocator)
365+
int loader_load_from_configuration(const loader_path path, void **handle, void *ctx, void *allocator)
346366
{
347367
loader_name config_name;
348368
configuration config;
@@ -467,7 +487,17 @@ int loader_load_from_configuration(const loader_path path, void **handle, void *
467487
}
468488
}
469489

470-
if (loader_load_from_file((const char *)value_to_string(tag), (const loader_path *)paths, size, handle) != 0)
490+
int result = 0;
491+
if (ctx != NULL)
492+
{
493+
result = loader_load_from_file_ctx((const char *)value_to_string(tag), (const loader_path *)paths, size, ctx);
494+
}
495+
else
496+
{
497+
result = loader_load_from_file((const char *)value_to_string(tag), (const loader_path *)paths, size, handle);
498+
}
499+
500+
if (result != 0)
471501
{
472502
log_write("metacall", LOG_LEVEL_ERROR, "Loader load from configuration invalid load from file");
473503

source/loader/source/loader_impl.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,67 @@ int loader_impl_load_from_file(plugin_manager manager, plugin p, loader_impl imp
796796

797797
return 1;
798798
}
799+
int loader_impl_load_from_file_ctx(plugin_manager manager, plugin p, loader_impl impl, const loader_path paths[], size_t size, void *ctx)
800+
{
801+
if (ctx == NULL)
802+
{
803+
log_write("metacall", LOG_LEVEL_ERROR, "Load from file failed, invalid context");
804+
805+
return 1;
806+
}
807+
808+
if (impl != NULL)
809+
{
810+
loader_impl_interface iface = loader_iface(p);
811+
812+
size_t iterator;
813+
814+
for (iterator = 0; iterator < size; ++iterator)
815+
{
816+
log_write("metacall", LOG_LEVEL_DEBUG, "Loading %s", paths[iterator]);
817+
}
818+
819+
if (iface != NULL)
820+
{
821+
loader_handle handle;
822+
loader_path path;
823+
824+
if (loader_impl_initialize(manager, p, impl) != 0)
825+
{
826+
return 1;
827+
}
828+
829+
/* This isn't really necessary*/
830+
if (loader_impl_handle_name(manager, paths[0], path) > 1 && loader_impl_get_handle(impl, path) != NULL)
831+
{
832+
log_write("metacall", LOG_LEVEL_ERROR, "Load from file handle failed, handle with name %s already loaded", path);
833+
834+
return 1;
835+
}
836+
837+
handle = iface->load_from_file(impl, paths, size);
838+
839+
log_write("metacall", LOG_LEVEL_DEBUG, "Loader interface: %p - Loader handle: %p", (void *)iface, (void *)handle);
840+
841+
if (handle != NULL)
842+
{
843+
scope sp = context_scope(ctx);
844+
845+
if (sp != NULL)
846+
{
847+
/* Todo: check for duplicate symbols*/
848+
849+
if (iface->discover(impl, handle, ctx) == 0)
850+
{
851+
return 0;
852+
}
853+
}
854+
}
855+
}
856+
}
857+
858+
return 1;
859+
}
799860

800861
int loader_impl_load_from_memory_name(loader_impl impl, loader_name name, const char *buffer, size_t size)
801862
{

source/metacall/include/metacall/metacall.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,30 @@ METACALL_API int metacall_load_from_package(const char *tag, const char *path, v
309309
*/
310310
METACALL_API int metacall_load_from_configuration(const char *path, void **handle, void *allocator);
311311

312+
/**
313+
* @brief
314+
* Loads a a list of scrips from configuration specified by @path into loader
315+
* with the following format:
316+
* {
317+
* "language_id": "<tag>",
318+
* "path": "<path>",
319+
* "scripts": [ "<script0>", "<script1>", ..., "<scriptN>" ]
320+
* }
321+
*
322+
* @param[in] path
323+
* Path of the configuration
324+
*
325+
* @param[in] context
326+
* Pointer to context
327+
*
328+
* @param[in] allocator
329+
* Pointer to allocator will allocate the configuration
330+
*
331+
* @return
332+
* Zero if success, different from zero otherwise
333+
*/
334+
METACALL_API int metacall_load_from_configuration_ctx(const char *path, void *ctx, void *allocator);
335+
312336
/**
313337
* @brief
314338
* Call a function anonymously by value array @args

source/metacall/source/metacall.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,12 @@ int metacall_load_from_package(const char *tag, const char *path, void **handle)
321321

322322
int metacall_load_from_configuration(const char *path, void **handle, void *allocator)
323323
{
324-
return loader_load_from_configuration(path, handle, allocator);
324+
return loader_load_from_configuration(path, handle, NULL, allocator);
325+
}
326+
327+
int metacall_load_from_configuration_ctx(const char *path, void *ctx, void *allocator)
328+
{
329+
return loader_load_from_configuration(path, NULL, ctx, allocator);
325330
}
326331

327332
void *metacallv(const char *name, void *args[])

0 commit comments

Comments
 (0)