Skip to content

Commit 10ade16

Browse files
committed
Add metacall_load_from_configuration in node port and improved declartion of functions in node port.
1 parent 2006cad commit 10ade16

File tree

6 files changed

+433
-36
lines changed

6 files changed

+433
-36
lines changed

source/loaders/node_loader/source/node_loader_port.cpp

Lines changed: 175 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
#include <metacall/metacall.h>
3030

31+
#include <preprocessor/preprocessor_concatenation.h>
32+
#include <preprocessor/preprocessor_stringify.h>
33+
3134
#include <cstring>
3235

3336
#include <node_api.h>
@@ -41,7 +44,7 @@ struct promise_context_type
4144

4245
static const loader_tag node_loader_tag = "node";
4346

44-
napi_value node_loader_port_call(napi_env env, napi_callback_info info)
47+
napi_value node_loader_port_metacall(napi_env env, napi_callback_info info)
4548
{
4649
size_t argc = 0;
4750

@@ -118,7 +121,7 @@ napi_value node_loader_port_call(napi_env env, napi_callback_info info)
118121
return result;
119122
}
120123

121-
napi_value node_loader_port_await(napi_env env, napi_callback_info info)
124+
napi_value node_loader_port_metacall_await(napi_env env, napi_callback_info info)
122125
{
123126
size_t argc = 0;
124127

@@ -254,7 +257,7 @@ napi_value node_loader_port_await(napi_env env, napi_callback_info info)
254257
return promise;
255258
}
256259

257-
napi_value node_loader_port_load_from_file(napi_env env, napi_callback_info info)
260+
napi_value node_loader_port_metacall_load_from_file(napi_env env, napi_callback_info info)
258261
{
259262
/* TODO: Detect if input argument types are valid */
260263

@@ -339,7 +342,7 @@ napi_value node_loader_port_load_from_file(napi_env env, napi_callback_info info
339342
return NULL;
340343
}
341344

342-
napi_value node_loader_port_load_from_file_export(napi_env env, napi_callback_info info)
345+
napi_value node_loader_port_metacall_load_from_file_export(napi_env env, napi_callback_info info)
343346
{
344347
/* TODO: Detect if input argument types are valid */
345348

@@ -446,7 +449,7 @@ napi_value node_loader_port_load_from_file_export(napi_env env, napi_callback_in
446449
* @return
447450
* TODO: Not implemented yet
448451
*/
449-
napi_value node_loader_port_load_from_memory(napi_env env, napi_callback_info info)
452+
napi_value node_loader_port_metacall_load_from_memory(napi_env env, napi_callback_info info)
450453
{
451454
const size_t args_size = 2;
452455
size_t argc = args_size, tag_length, script_length, script_size;
@@ -523,7 +526,7 @@ napi_value node_loader_port_load_from_memory(napi_env env, napi_callback_info in
523526
return NULL;
524527
}
525528

526-
napi_value node_loader_port_load_from_memory_export(napi_env env, napi_callback_info info)
529+
napi_value node_loader_port_metacall_load_from_memory_export(napi_env env, napi_callback_info info)
527530
{
528531
const size_t args_size = 2;
529532
size_t argc = args_size, tag_length, script_length, script_size;
@@ -607,8 +610,145 @@ napi_value node_loader_port_load_from_memory_export(napi_env env, napi_callback_
607610
return v_exports;
608611
}
609612

613+
/**
614+
* @brief
615+
* Loads a script from configuration path
616+
*
617+
* @param[in] env
618+
* N-API reference to the enviroment
619+
*
620+
* @param[in] info
621+
* Reference to the call information
622+
*/
623+
napi_value node_loader_port_metacall_load_from_configuration(napi_env env, napi_callback_info info)
624+
{
625+
const size_t args_size = 1;
626+
size_t argc = args_size, path_length;
627+
napi_value argv[args_size];
628+
napi_status status;
629+
char *path;
630+
631+
// Get arguments
632+
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
633+
634+
node_loader_impl_exception(env, status);
635+
636+
// Get tag length
637+
status = napi_get_value_string_utf8(env, argv[0], NULL, 0, &path_length);
638+
639+
node_loader_impl_exception(env, status);
640+
641+
// Allocate path
642+
path = static_cast<char *>(malloc(sizeof(char) * (path_length + 1)));
643+
644+
if (path == NULL)
645+
{
646+
napi_throw_error(env, NULL, "MetaCall could not load from configuration, path allocation failed");
647+
return NULL;
648+
}
649+
650+
// Get path
651+
status = napi_get_value_string_utf8(env, argv[0], path, path_length + 1, &path_length);
652+
653+
node_loader_impl_exception(env, status);
654+
655+
/* Obtain NodeJS loader implementation */
656+
loader_impl impl = loader_get_impl(node_loader_tag);
657+
loader_impl_node node_impl = (loader_impl_node)loader_impl_get(impl);
658+
659+
/* Store current reference of the environment */
660+
node_loader_impl_env(node_impl, env);
661+
662+
// Load script from configuration
663+
struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };
664+
665+
void *allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);
666+
667+
if (metacall_load_from_configuration(path, NULL, allocator) != 0)
668+
{
669+
napi_throw_error(env, NULL, "MetaCall could not load from configuration");
670+
}
671+
672+
metacall_allocator_destroy(allocator);
673+
674+
/* Release current reference of the environment */
675+
// node_loader_impl_env(node_impl, NULL);
676+
677+
free(path);
678+
679+
/* TODO: Return value and logs */
680+
return NULL;
681+
}
682+
683+
napi_value node_loader_port_metacall_load_from_configuration_export(napi_env env, napi_callback_info info)
684+
{
685+
const size_t args_size = 1;
686+
size_t argc = args_size, path_length;
687+
napi_value argv[args_size];
688+
napi_status status;
689+
char *path;
690+
691+
// Get arguments
692+
status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
693+
694+
node_loader_impl_exception(env, status);
695+
696+
// Get tag length
697+
status = napi_get_value_string_utf8(env, argv[0], NULL, 0, &path_length);
698+
699+
node_loader_impl_exception(env, status);
700+
701+
// Allocate path
702+
path = static_cast<char *>(malloc(sizeof(char) * (path_length + 1)));
703+
704+
if (path == NULL)
705+
{
706+
napi_throw_error(env, NULL, "MetaCall could not load from configuration, path allocation failed");
707+
return NULL;
708+
}
709+
710+
// Get path
711+
status = napi_get_value_string_utf8(env, argv[0], path, path_length + 1, &path_length);
712+
713+
node_loader_impl_exception(env, status);
714+
715+
/* Obtain NodeJS loader implementation */
716+
loader_impl impl = loader_get_impl(node_loader_tag);
717+
loader_impl_node node_impl = (loader_impl_node)loader_impl_get(impl);
718+
719+
/* Store current reference of the environment */
720+
node_loader_impl_env(node_impl, env);
721+
722+
// Load script from configuration
723+
void *handle = NULL;
724+
725+
struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };
726+
727+
void *allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);
728+
729+
if (metacall_load_from_configuration(path, &handle, allocator) != 0)
730+
{
731+
napi_throw_error(env, NULL, "MetaCall could not load from configuration");
732+
}
733+
734+
metacall_allocator_destroy(allocator);
735+
736+
/* Release current reference of the environment */
737+
// node_loader_impl_env(node_impl, NULL);
738+
739+
free(path);
740+
741+
void *exports = metacall_handle_export(handle);
742+
743+
napi_value v_exports = node_loader_impl_value_to_napi(node_impl, env, exports);
744+
745+
node_loader_impl_finalizer(env, v_exports, exports);
746+
747+
return v_exports;
748+
}
749+
610750
/* TODO: Add documentation */
611-
napi_value node_loader_port_inspect(napi_env env, napi_callback_info)
751+
napi_value node_loader_port_metacall_inspect(napi_env env, napi_callback_info)
612752
{
613753
napi_value result;
614754

@@ -639,7 +779,7 @@ napi_value node_loader_port_inspect(napi_env env, napi_callback_info)
639779
}
640780

641781
/* TODO: Add documentation */
642-
napi_value node_loader_port_logs(napi_env env, napi_callback_info)
782+
napi_value node_loader_port_metacall_logs(napi_env env, napi_callback_info)
643783
{
644784
struct metacall_log_stdio_type log_stdio = { stdout };
645785

@@ -655,34 +795,33 @@ napi_value node_loader_port_logs(napi_env env, napi_callback_info)
655795
// This functions sets the necessary js functions that could be called in NodeJs
656796
void node_loader_port_exports(napi_env env, napi_value exports)
657797
{
658-
const char function_metacall_str[] = "metacall";
659-
const char function_metacall_await_str[] = "metacall_await";
660-
const char function_load_from_file_str[] = "metacall_load_from_file";
661-
const char function_load_from_file_export_str[] = "metacall_load_from_file_export";
662-
const char function_load_from_memory_str[] = "metacall_load_from_memory";
663-
const char function_load_from_memory_export_str[] = "metacall_load_from_memory_export";
664-
const char function_inspect_str[] = "metacall_inspect";
665-
const char function_logs_str[] = "metacall_logs";
666-
667-
napi_value function_metacall, function_metacall_await, function_load_from_file, function_load_from_file_export, function_load_from_memory, function_load_from_memory_export, function_inspect, function_logs;
668-
669-
napi_create_function(env, function_metacall_str, sizeof(function_metacall_str) - 1, node_loader_port_call, NULL, &function_metacall);
670-
napi_create_function(env, function_metacall_await_str, sizeof(function_metacall_await_str) - 1, node_loader_port_await, NULL, &function_metacall_await);
671-
napi_create_function(env, function_load_from_file_str, sizeof(function_load_from_file_str) - 1, node_loader_port_load_from_file, NULL, &function_load_from_file);
672-
napi_create_function(env, function_load_from_file_export_str, sizeof(function_load_from_file_export_str) - 1, node_loader_port_load_from_file_export, NULL, &function_load_from_file_export);
673-
napi_create_function(env, function_load_from_memory_str, sizeof(function_load_from_memory_str) - 1, node_loader_port_load_from_memory, NULL, &function_load_from_memory);
674-
napi_create_function(env, function_load_from_memory_export_str, sizeof(function_load_from_memory_export_str) - 1, node_loader_port_load_from_memory_export, NULL, &function_load_from_memory_export);
675-
napi_create_function(env, function_inspect_str, sizeof(function_inspect_str) - 1, node_loader_port_inspect, NULL, &function_inspect);
676-
napi_create_function(env, function_logs_str, sizeof(function_logs_str) - 1, node_loader_port_logs, NULL, &function_logs);
677-
678-
napi_set_named_property(env, exports, function_metacall_str, function_metacall);
679-
napi_set_named_property(env, exports, function_metacall_await_str, function_metacall_await);
680-
napi_set_named_property(env, exports, function_load_from_file_str, function_load_from_file);
681-
napi_set_named_property(env, exports, function_load_from_file_export_str, function_load_from_file_export);
682-
napi_set_named_property(env, exports, function_load_from_memory_str, function_load_from_memory);
683-
napi_set_named_property(env, exports, function_load_from_memory_export_str, function_load_from_memory_export);
684-
napi_set_named_property(env, exports, function_inspect_str, function_inspect);
685-
napi_set_named_property(env, exports, function_logs_str, function_logs);
798+
#define NODE_LOADER_PORT_DECL_FUNC(name) \
799+
do \
800+
{ \
801+
const char PREPROCESSOR_CONCAT(function_str_, name)[] = PREPROCESSOR_STRINGIFY(name); \
802+
napi_value PREPROCESSOR_CONCAT(function_, name); \
803+
napi_create_function(env, PREPROCESSOR_CONCAT(function_str_, name), sizeof(PREPROCESSOR_CONCAT(function_str_, name)) - 1, PREPROCESSOR_CONCAT(node_loader_port_, name), NULL, &PREPROCESSOR_CONCAT(function_, name)); \
804+
napi_set_named_property(env, exports, PREPROCESSOR_CONCAT(function_str_, name), PREPROCESSOR_CONCAT(function_, name)); \
805+
\
806+
} while (0)
807+
808+
#define NODE_LOADER_PORT_DECL_X_MACRO(x) \
809+
x(metacall); \
810+
x(metacall_await); \
811+
x(metacall_load_from_file); \
812+
x(metacall_load_from_file_export); \
813+
x(metacall_load_from_memory); \
814+
x(metacall_load_from_memory_export); \
815+
x(metacall_load_from_configuration); \
816+
x(metacall_load_from_configuration_export); \
817+
x(metacall_inspect); \
818+
x(metacall_logs);
819+
820+
/* Declare all the functions */
821+
NODE_LOADER_PORT_DECL_X_MACRO(NODE_LOADER_PORT_DECL_FUNC)
822+
823+
#undef NODE_LOADER_PORT_DECL_FUNC
824+
#undef NODE_LOADER_PORT_DECL_X_MACRO
686825
}
687826

688827
/* TODO: Review documentation */

source/ports/node_port/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ const metacall_load_from_memory_export = (tag, code) => {
138138
return addon.metacall_load_from_memory_export(tag, code);
139139
};
140140

141+
const metacall_load_from_configuration = (path) => {
142+
if (Object.prototype.toString.call(path) !== '[object String]') {
143+
throw Error('Path should be a string indicating the path where the metacall.json is located.');
144+
}
145+
146+
return addon.metacall_load_from_configuration(path);
147+
};
148+
149+
const metacall_load_from_configuration_export = (path) => {
150+
if (Object.prototype.toString.call(path) !== '[object String]') {
151+
throw Error('Path should be a string indicating the path where the metacall.json is located.');
152+
}
153+
154+
return addon.metacall_load_from_configuration_export(path);
155+
};
156+
141157
const metacall_inspect = () => {
142158
const json_data = addon.metacall_inspect();
143159

@@ -182,6 +198,8 @@ const module_exports = {
182198
metacall_load_from_file_export,
183199
metacall_load_from_memory,
184200
metacall_load_from_memory_export,
201+
metacall_load_from_configuration,
202+
metacall_load_from_configuration_export,
185203
metacall_handle,
186204

187205
/* TODO: Remove this from user or provide better ways of configuring logs */

source/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ add_subdirectory(metacall_load_memory_test)
115115
add_subdirectory(metacall_load_configuration_test)
116116
add_subdirectory(metacall_load_configuration_relative_test)
117117
add_subdirectory(metacall_load_configuration_python_node_test)
118+
add_subdirectory(metacall_load_configuration_node_python_test)
118119
add_subdirectory(metacall_duplicated_handle_test)
119120
add_subdirectory(metacall_duplicated_symbols_test)
120121
add_subdirectory(metacall_handle_export_test)

0 commit comments

Comments
 (0)