Skip to content

Commit 24f9a4e

Browse files
committed
Implement loading of core plugins.
1 parent eca656e commit 24f9a4e

File tree

4 files changed

+59
-11
lines changed

4 files changed

+59
-11
lines changed

source/extensions/plugin_extension/source/plugin_extension.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ void *plugin_load_from_path(size_t argc, void *args[], void *data)
8585
(config.substr(0, m_begins.size()) == m_begins &&
8686
config.substr(config.size() - m_ends.size()) == m_ends))
8787
{
88-
log_write("metacall", LOG_LEVEL_DEBUG, "Loading extension: %s", config.c_str());
89-
9088
std::string dir_path = dir.path().string();
9189

90+
log_write("metacall", LOG_LEVEL_DEBUG, "Loading plugin: %s", dir_path.c_str());
91+
9292
if (metacall_load_from_configuration(dir_path.c_str(), handle_ptr, config_allocator) != 0)
9393
{
94-
log_write("metacall", LOG_LEVEL_ERROR, "Failed to load extension: %s", dir_path.c_str());
94+
log_write("metacall", LOG_LEVEL_ERROR, "Failed to load plugin: %s", dir_path.c_str());
9595
return metacall_value_create_int(4);
9696
}
9797

source/loader/include/loader/loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ LOADER_API int loader_register(const char *name, loader_register_invoke invoke,
6161

6262
LOADER_API int loader_register_impl(void *impl, void *ctx, const char *name, loader_register_invoke invoke, type_id return_type, size_t arg_size, type_id args_type_id[]);
6363

64+
LOADER_API const char *loader_library_path(void);
65+
6466
LOADER_API int loader_execution_path(const loader_tag tag, const loader_path path);
6567

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

source/loader/source/loader.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ loader_impl loader_get_impl(const loader_tag tag)
262262
return plugin_impl_type(p, loader_impl);
263263
}
264264

265+
const char *loader_library_path(void)
266+
{
267+
return plugin_manager_library_path(&loader_manager);
268+
}
269+
265270
int loader_execution_path(const loader_tag tag, const loader_path path)
266271
{
267272
if (loader_initialize() == 1)

source/metacall/source/metacall.c

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static void *plugin_extension_handle = NULL;
6464

6565
/* -- Private Methods -- */
6666

67+
static int metacall_plugin_extension_load(void);
6768
static void *metacallv_method(void *target, const char *name, method_invoke_ptr call, vector v, void *args[], size_t size);
6869
static type_id *metacall_type_ids(void *args[], size_t size);
6970

@@ -86,12 +87,57 @@ void metacall_flags(int flags)
8687
metacall_config_flags = flags;
8788
}
8889

89-
int metacall_initialize(void)
90+
int metacall_plugin_extension_load(void)
9091
{
9192
static const char *ext_scripts[] = {
9293
"plugin_extension"
9394
};
95+
static const char plugin_suffix[] = "plugins";
96+
const char *library_path = loader_library_path();
97+
loader_path plugin_path;
98+
size_t plugin_path_size;
99+
void *args[2];
100+
void *ret;
101+
int result = 1;
102+
103+
/* Load the plugin extension */
104+
if (metacall_load_from_file("ext", ext_scripts, sizeof(ext_scripts) / sizeof(ext_scripts[0]), &plugin_extension_handle) != 0)
105+
{
106+
goto plugin_extension_error;
107+
}
108+
109+
/* Get the plugin path */
110+
plugin_path_size = portability_path_join(library_path, strnlen(library_path, PORTABILITY_PATH_SIZE) + 1, plugin_suffix, sizeof(plugin_suffix), plugin_path, PORTABILITY_PATH_SIZE);
111+
112+
/* Load core plugins into plugin extension handle */
113+
args[0] = metacall_value_create_string(plugin_path, plugin_path_size - 1);
114+
args[1] = metacall_value_create_ptr(&plugin_extension_handle);
115+
ret = metacallhv_s(plugin_extension_handle, "plugin_load_from_path", args, sizeof(args) / sizeof(args[0]));
94116

117+
if (ret == NULL)
118+
{
119+
goto plugin_load_from_path_error;
120+
}
121+
122+
if (metacall_value_id(ret) != METACALL_INT)
123+
{
124+
goto plugin_load_from_path_type_error;
125+
}
126+
127+
/* Retrieve the result value */
128+
result = metacall_value_to_int(ret);
129+
130+
plugin_load_from_path_type_error:
131+
metacall_value_destroy(ret);
132+
plugin_load_from_path_error:
133+
metacall_value_destroy(args[0]);
134+
metacall_value_destroy(args[1]);
135+
plugin_extension_error:
136+
return result;
137+
}
138+
139+
int metacall_initialize(void)
140+
{
95141
memory_allocator allocator;
96142

97143
/* Initialize logs by default to stdout if none has been defined */
@@ -193,16 +239,11 @@ int metacall_initialize(void)
193239
return 1;
194240
}
195241

196-
/* Load plugin extension */
197-
if (metacall_load_from_file("ext", ext_scripts, sizeof(ext_scripts) / sizeof(ext_scripts[0]), &plugin_extension_handle) != 0)
242+
/* Load core plugins */
243+
if (metacall_plugin_extension_load() != 0)
198244
{
199245
log_write("metacall", LOG_LEVEL_WARNING, "MetaCall Plugin Extension could not be loaded");
200246
}
201-
else
202-
{
203-
/* TODO: Load core extensions */
204-
/* ... */
205-
}
206247

207248
metacall_initialize_flag = 0;
208249

0 commit comments

Comments
 (0)