Skip to content

Commit eca656e

Browse files
committed
Refactor plugin_extension.
1 parent aa1a9fa commit eca656e

File tree

9 files changed

+121
-56
lines changed

9 files changed

+121
-56
lines changed

source/extensions/plugin_extension/source/plugin_extension.cpp

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,46 @@
2020

2121
#include <plugin_extension/plugin_extension.h>
2222

23-
#include <environment/environment_variable_path.h>
2423
#include <log/log.h>
2524
#include <metacall/metacall.h>
2625

2726
#include <filesystem>
2827
#include <string>
2928

30-
#define METACALL_PLUGIN_PATH "METACALL_PLUGIN_PATH" /* Environment variable for plugin path */
31-
3229
namespace fs = std::filesystem;
3330

34-
static int plugin_extension_get_path(std::string &ext_path)
31+
void *plugin_load_from_path(size_t argc, void *args[], void *data)
3532
{
36-
/* Initialize the library path */
37-
const char name[] = "metacall"
38-
#if (!defined(NDEBUG) || defined(DEBUG) || defined(_DEBUG) || defined(__DEBUG) || defined(__DEBUG__))
39-
"d"
40-
#endif
41-
;
42-
43-
dynlink_library_path_str lib_path;
44-
size_t length = 0;
45-
46-
/* The order of precedence is:
47-
* 1) Environment variable
48-
* 2) Dynamic link library path of the host library
49-
*/
50-
if (dynlink_library_path(name, lib_path, &length) != 0)
33+
/* TODO: Improve return values with throwable in the future */
34+
(void)data;
35+
36+
if (argc != 1 && argc != 2)
5137
{
52-
return 1;
38+
log_write("metacall", LOG_LEVEL_ERROR, "Invalid number of arguments passed to plugin_load_from_path: %" PRIuS, argc);
39+
return metacall_value_create_int(1);
5340
}
5441

55-
char *env_path = environment_variable_path_create(METACALL_PLUGIN_PATH, lib_path, length + 1, NULL);
56-
57-
if (env_path == NULL)
42+
if (metacall_value_id(args[0]) != METACALL_STRING)
5843
{
59-
return 1;
44+
log_write("metacall", LOG_LEVEL_ERROR, "Invalid first parameter passed to plugin_load_from_path, it requires string");
45+
return metacall_value_create_int(2);
6046
}
6147

62-
fs::path plugin_path(env_path);
63-
environment_variable_path_destroy(env_path);
64-
plugin_path /= "plugins";
65-
ext_path = plugin_path.string();
66-
67-
return 0;
68-
}
69-
70-
int plugin_extension(void *loader, void *handle, void *context)
71-
{
72-
std::string ext_path;
48+
if (argc == 2)
49+
{
50+
if (metacall_value_id(args[1]) != METACALL_PTR)
51+
{
52+
log_write("metacall", LOG_LEVEL_ERROR, "Invalid second parameter passed to plugin_load_from_path, it requires pointer");
53+
return metacall_value_create_int(3);
54+
}
55+
}
7356

74-
(void)loader;
75-
(void)context;
57+
std::string ext_path(metacall_value_to_string(args[0]));
58+
void **handle_ptr = NULL;
7659

77-
if (plugin_extension_get_path(ext_path) != 0)
60+
if (argc == 2)
7861
{
79-
log_write("metacall", LOG_LEVEL_ERROR, "Define the extension path with the environment variable " METACALL_PLUGIN_PATH);
80-
return 1;
62+
handle_ptr = static_cast<void **>(metacall_value_to_ptr(args[1]));
8163
}
8264

8365
static std::string m_begins = "metacall-";
@@ -107,10 +89,10 @@ int plugin_extension(void *loader, void *handle, void *context)
10789

10890
std::string dir_path = dir.path().string();
10991

110-
if (metacall_load_from_configuration(dir_path.c_str(), &handle, config_allocator) != 0)
92+
if (metacall_load_from_configuration(dir_path.c_str(), handle_ptr, config_allocator) != 0)
11193
{
11294
log_write("metacall", LOG_LEVEL_ERROR, "Failed to load extension: %s", dir_path.c_str());
113-
return 1;
95+
return metacall_value_create_int(4);
11496
}
11597

11698
i.pop();
@@ -123,5 +105,12 @@ int plugin_extension(void *loader, void *handle, void *context)
123105

124106
metacall_allocator_destroy(config_allocator);
125107

126-
return 0;
108+
return metacall_value_create_int(0);
109+
}
110+
111+
int plugin_extension(void *loader, void *handle, void *context)
112+
{
113+
enum metacall_value_id arg_types[] = { METACALL_STRING, METACALL_PTR };
114+
(void)handle;
115+
return metacall_register_loaderv(loader, context, "plugin_load_from_path", plugin_load_from_path, METACALL_INT, sizeof(arg_types) / sizeof(arg_types[0]), arg_types);
127116
}

source/metacall/include/metacall/metacall.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,15 @@ METACALL_API void *metacall_deserialize(const char *name, const char *buffer, si
13961396
*/
13971397
METACALL_API int metacall_clear(void *handle);
13981398

1399+
/**
1400+
* @brief
1401+
* Get the plugin extension handle to be used for loading plugins
1402+
*
1403+
* @return
1404+
* Pointer to the extension handle, or null if it failed to load
1405+
*/
1406+
METACALL_API void *metacall_plugin_extension(void);
1407+
13991408
/**
14001409
* @brief
14011410
* Destroy MetaCall library

source/metacall/source/metacall.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static int metacall_log_null_flag = 1;
6060
static int metacall_config_flags = 0;
6161
static int metacall_initialize_argc = 0;
6262
static char **metacall_initialize_argv = NULL;
63+
static void *plugin_extension_handle = NULL;
6364

6465
/* -- Private Methods -- */
6566

@@ -87,6 +88,10 @@ void metacall_flags(int flags)
8788

8889
int metacall_initialize(void)
8990
{
91+
static const char *ext_scripts[] = {
92+
"plugin_extension"
93+
};
94+
9095
memory_allocator allocator;
9196

9297
/* Initialize logs by default to stdout if none has been defined */
@@ -188,6 +193,17 @@ int metacall_initialize(void)
188193
return 1;
189194
}
190195

196+
/* Load plugin extension */
197+
if (metacall_load_from_file("ext", ext_scripts, sizeof(ext_scripts) / sizeof(ext_scripts[0]), &plugin_extension_handle) != 0)
198+
{
199+
log_write("metacall", LOG_LEVEL_WARNING, "MetaCall Plugin Extension could not be loaded");
200+
}
201+
else
202+
{
203+
/* TODO: Load core extensions */
204+
/* ... */
205+
}
206+
191207
metacall_initialize_flag = 0;
192208

193209
return 0;
@@ -2147,6 +2163,11 @@ int metacall_clear(void *handle)
21472163
return loader_clear(handle);
21482164
}
21492165

2166+
void *metacall_plugin_extension(void)
2167+
{
2168+
return plugin_extension_handle;
2169+
}
2170+
21502171
int metacall_destroy(void)
21512172
{
21522173
if (metacall_initialize_flag == 0)
@@ -2170,6 +2191,9 @@ int metacall_destroy(void)
21702191
{
21712192
log_write("metacall", LOG_LEVEL_WARNING, "MetaCall backtrace could not be destroyed");
21722193
}
2194+
2195+
/* Set to null the plugin extension */
2196+
plugin_extension_handle = NULL;
21732197
}
21742198

21752199
return 0;

source/tests/metacall_plugin_extension_destroy_order_test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ target_link_libraries(${target}
9595
target_compile_definitions(${target}
9696
PRIVATE
9797
${DEFAULT_COMPILE_DEFINITIONS}
98+
99+
METACALL_PLUGIN_PATH="${PROJECT_OUTPUT_DIR}/plugins/cli"
98100
)
99101

100102
#

source/tests/metacall_plugin_extension_destroy_order_test/source/metacall_plugin_extension_destroy_order_test.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,26 @@ TEST_F(metacall_plugin_destroy_order_test, DefaultConstructor)
3535
ASSERT_EQ((int)0, (int)metacall_initialize());
3636

3737
/* Extension */
38-
const char *ext_scripts[] = {
39-
"plugin_extension"
38+
void *handle = metacall_plugin_extension();
39+
40+
ASSERT_NE((void *)NULL, (void *)handle);
41+
42+
void *args[] = {
43+
metacall_value_create_string(METACALL_PLUGIN_PATH, sizeof(METACALL_PLUGIN_PATH) - 1),
44+
metacall_value_create_ptr(&handle)
4045
};
4146

42-
void *handle = NULL;
47+
void *result = metacallhv_s(handle, "plugin_load_from_path", args, sizeof(args) / sizeof(args[0]));
48+
49+
ASSERT_NE((void *)NULL, (void *)result);
50+
51+
EXPECT_EQ((enum metacall_value_id)METACALL_INT, (enum metacall_value_id)metacall_value_id(result));
52+
53+
EXPECT_EQ((int)0, (int)metacall_value_to_int(result));
4354

44-
ASSERT_EQ((int)0, (int)metacall_load_from_file("ext", ext_scripts, sizeof(ext_scripts) / sizeof(ext_scripts[0]), &handle));
55+
metacall_value_destroy(args[0]);
56+
metacall_value_destroy(args[1]);
57+
metacall_value_destroy(result);
4558

4659
/* Print inspect information */
4760
{

source/tests/metacall_plugin_extension_local_test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ target_link_libraries(${target}
9595
target_compile_definitions(${target}
9696
PRIVATE
9797
${DEFAULT_COMPILE_DEFINITIONS}
98+
99+
METACALL_PLUGIN_PATH="${CMAKE_CURRENT_SOURCE_DIR}/plugins"
98100
)
99101

100102
#
@@ -147,5 +149,4 @@ include(TestEnvironmentVariables)
147149
test_environment_variables(${target}
148150
""
149151
${TESTS_ENVIRONMENT_VARIABLES}
150-
"METACALL_PLUGIN_PATH=${CMAKE_CURRENT_SOURCE_DIR}"
151152
)

source/tests/metacall_plugin_extension_local_test/source/metacall_plugin_extension_local_test.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,26 @@ TEST_F(metacall_plugin_extension_local_test, DefaultConstructor)
3535
ASSERT_EQ((int)0, (int)metacall_initialize());
3636

3737
/* Extension */
38-
const char *ext_scripts[] = {
39-
"plugin_extension"
38+
void *handle = metacall_plugin_extension();
39+
40+
ASSERT_NE((void *)NULL, (void *)handle);
41+
42+
void *args[] = {
43+
metacall_value_create_string(METACALL_PLUGIN_PATH, sizeof(METACALL_PLUGIN_PATH) - 1),
44+
metacall_value_create_ptr(&handle)
4045
};
4146

42-
void *handle = NULL;
47+
void *result = metacallhv_s(handle, "plugin_load_from_path", args, sizeof(args) / sizeof(args[0]));
48+
49+
ASSERT_NE((void *)NULL, (void *)result);
50+
51+
EXPECT_EQ((enum metacall_value_id)METACALL_INT, (enum metacall_value_id)metacall_value_id(result));
52+
53+
EXPECT_EQ((int)0, (int)metacall_value_to_int(result));
4354

44-
ASSERT_EQ((int)0, (int)metacall_load_from_file("ext", ext_scripts, sizeof(ext_scripts) / sizeof(ext_scripts[0]), &handle));
55+
metacall_value_destroy(args[0]);
56+
metacall_value_destroy(args[1]);
57+
metacall_value_destroy(result);
4558

4659
/* Python */
4760
#if defined(OPTION_BUILD_LOADERS_PY)

source/tests/metacall_plugin_extension_test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ target_link_libraries(${target}
9595
target_compile_definitions(${target}
9696
PRIVATE
9797
${DEFAULT_COMPILE_DEFINITIONS}
98+
99+
METACALL_PLUGIN_PATH="${CMAKE_CURRENT_SOURCE_DIR}/plugins"
98100
)
99101

100102
#
@@ -147,5 +149,4 @@ include(TestEnvironmentVariables)
147149
test_environment_variables(${target}
148150
""
149151
${TESTS_ENVIRONMENT_VARIABLES}
150-
"METACALL_PLUGIN_PATH=${CMAKE_CURRENT_SOURCE_DIR}"
151152
)

source/tests/metacall_plugin_extension_test/source/metacall_plugin_extension_test.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,24 @@ TEST_F(metacall_plugin_extension_test, DefaultConstructor)
3535
ASSERT_EQ((int)0, (int)metacall_initialize());
3636

3737
/* Extension */
38-
const char *ext_scripts[] = {
39-
"plugin_extension"
38+
void *handle = metacall_plugin_extension();
39+
40+
ASSERT_NE((void *)NULL, (void *)handle);
41+
42+
void *args[] = {
43+
metacall_value_create_string(METACALL_PLUGIN_PATH, sizeof(METACALL_PLUGIN_PATH) - 1)
4044
};
4145

42-
ASSERT_EQ((int)0, (int)metacall_load_from_file("ext", ext_scripts, sizeof(ext_scripts) / sizeof(ext_scripts[0]), NULL));
46+
void *result = metacallhv_s(handle, "plugin_load_from_path", args, sizeof(args) / sizeof(args[0]));
47+
48+
ASSERT_NE((void *)NULL, (void *)result);
49+
50+
EXPECT_EQ((enum metacall_value_id)METACALL_INT, (enum metacall_value_id)metacall_value_id(result));
51+
52+
EXPECT_EQ((int)0, (int)metacall_value_to_int(result));
53+
54+
metacall_value_destroy(args[0]);
55+
metacall_value_destroy(result);
4356

4457
/* Python */
4558
#if defined(OPTION_BUILD_LOADERS_PY)

0 commit comments

Comments
 (0)