Skip to content

Commit d227fcd

Browse files
committed
Implement metacallhv, metacallhv_s and metacall_handle_function.
1 parent ba29a2e commit d227fcd

File tree

11 files changed

+373
-13
lines changed

11 files changed

+373
-13
lines changed

source/loader/include/loader/loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ LOADER_API const char * loader_handle_id(void * handle);
9494

9595
LOADER_API void * loader_handle_export(void * handle);
9696

97+
LOADER_API loader_data loader_handle_get(void * handle, const char * name);
98+
9799
LOADER_API value loader_metadata(void);
98100

99101
LOADER_API int loader_clear(void * handle);

source/loader/include/loader/loader_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ LOADER_API const char * loader_impl_handle_id(void * handle);
7070

7171
LOADER_API value loader_impl_handle_export(void * handle);
7272

73+
LOADER_API context loader_impl_handle_context(void * handle);
74+
7375
LOADER_API value loader_impl_metadata(loader_impl impl);
7476

7577
LOADER_API int loader_impl_clear(void * handle);

source/loader/source/loader.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,20 @@ value loader_handle_export(void * handle)
743743
return loader_impl_handle_export(handle);
744744
}
745745

746+
loader_data loader_handle_get(void * handle, const char * name)
747+
{
748+
if (handle != NULL)
749+
{
750+
context ctx = loader_impl_handle_context(handle);
751+
752+
scope sp = context_scope(ctx);
753+
754+
return scope_get(sp, name);
755+
}
756+
757+
return NULL;
758+
}
759+
746760
value loader_metadata_impl(loader_impl impl)
747761
{
748762
loader_naming_tag * tag_ptr = loader_impl_tag(impl);

source/loader/source/loader_impl.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,13 @@ value loader_impl_handle_export(void * handle)
889889
return scope_export(context_scope(handle_impl->ctx));
890890
}
891891

892+
context loader_impl_handle_context(void * handle)
893+
{
894+
loader_handle_impl handle_impl = handle;
895+
896+
return handle_impl->ctx;
897+
}
898+
892899
value loader_impl_metadata_handle_name(loader_handle_impl handle_impl)
893900
{
894901
static const char name[] = "name";

source/metacall/include/metacall/metacall.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,21 @@ METACALL_API void * metacallt_s(const char * name, const enum metacall_value_id
396396
*/
397397
METACALL_API void * metacall_function(const char * name);
398398

399+
/**
400+
* @brief
401+
* Get the function by @name from @handle
402+
*
403+
* @param[in] handle
404+
* Pointer to the handle returned by metacall_load_from_{file, memory, package}
405+
*
406+
* @param[in] name
407+
* Name of the function
408+
*
409+
* @return
410+
* Function reference, null if the function does not exist
411+
*/
412+
METACALL_API void * metacall_handle_function(void * handle, const char * name);
413+
399414
/**
400415
* @brief
401416
* Get the function parameter type id

source/metacall/source/metacall.c

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ int metacall_load_from_configuration(const char * path, void ** handle, void * a
262262
void * metacallv(const char * name, void * args[])
263263
{
264264
value f_val = loader_get(name);
265-
266265
function f = NULL;
266+
267267
if (value_type_id(f_val) == TYPE_FUNCTION)
268268
{
269269
f = value_to_function(f_val);
@@ -287,25 +287,28 @@ void * metacallv_s(const char * name, void * args[], size_t size)
287287

288288
void * metacallhv(void * handle, const char * name, void * args[])
289289
{
290-
(void)handle;
291-
(void)name;
292-
(void)args;
290+
value f_val = loader_handle_get(handle, name);
291+
function f = NULL;
293292

294-
/* TODO */
293+
if (value_type_id(f_val) == TYPE_FUNCTION)
294+
{
295+
f = value_to_function(f_val);
296+
}
295297

296-
return NULL;
298+
return metacallfv(f, args);
297299
}
298300

299301
void * metacallhv_s(void * handle, const char * name, void * args[], size_t size)
300302
{
301-
(void)handle;
302-
(void)name;
303-
(void)args;
304-
(void)size;
303+
value f_val = loader_handle_get(handle, name);
304+
function f = NULL;
305305

306-
/* TODO */
306+
if (value_type_id(f_val) == TYPE_FUNCTION)
307+
{
308+
f = value_to_function(f_val);
309+
}
307310

308-
return NULL;
311+
return metacallfv_s(f, args, size);
309312
}
310313

311314
void * metacall(const char * name, ...)
@@ -613,7 +616,19 @@ void * metacallt_s(const char * name, const enum metacall_value_id ids[], size_t
613616
void * metacall_function(const char * name)
614617
{
615618
value f_val = loader_get(name);
619+
function f = NULL;
616620

621+
if (value_type_id(f_val) == TYPE_FUNCTION)
622+
{
623+
f = value_to_function(f_val);
624+
}
625+
626+
return f;
627+
}
628+
629+
void * metacall_handle_function(void * handle, const char * name)
630+
{
631+
value f_val = loader_handle_get(handle, name);
617632
function f = NULL;
618633

619634
if (value_type_id(f_val) == TYPE_FUNCTION)

source/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ add_subdirectory(metacall_load_configuration_relative_test)
125125
add_subdirectory(metacall_duplicated_handle_test)
126126
add_subdirectory(metacall_duplicated_symbols_test)
127127
add_subdirectory(metacall_handle_export_test)
128+
add_subdirectory(metacall_handle_get_test)
128129
add_subdirectory(metacall_test)
129130
add_subdirectory(metacall_node_test)
130131
add_subdirectory(metacall_node_event_loop_test)
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#
2+
# Setup distributable environment
3+
#
4+
5+
# Check if this loader is enabled
6+
if(NOT OPTION_BUILD_DIST_LIBS OR NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_NODE OR NOT OPTION_BUILD_SCRIPTS OR NOT OPTION_BUILD_SCRIPTS_NODE)
7+
return()
8+
endif()
9+
10+
#
11+
# Executable name and options
12+
#
13+
14+
# Target name
15+
set(target metacall-handle-get-test)
16+
message(STATUS "Test ${target}")
17+
18+
#
19+
# Compiler warnings
20+
#
21+
22+
include(Warnings)
23+
24+
#
25+
# Compiler security
26+
#
27+
28+
include(SecurityFlags)
29+
30+
#
31+
# Sources
32+
#
33+
34+
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
35+
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
36+
37+
set(sources
38+
${source_path}/main.cpp
39+
${source_path}/metacall_handle_get_test.cpp
40+
)
41+
42+
# Group source files
43+
set(header_group "Header Files (API)")
44+
set(source_group "Source Files")
45+
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
46+
${header_group} ${headers})
47+
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
48+
${source_group} ${sources})
49+
50+
#
51+
# Create executable
52+
#
53+
54+
# Build executable
55+
add_executable(${target}
56+
${sources}
57+
)
58+
59+
# Create namespaced alias
60+
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})
61+
62+
#
63+
# Dependecies
64+
#
65+
66+
# Add metacall distributable dependency
67+
add_dependencies(${target}
68+
${META_PROJECT_NAME}::metacall_distributable
69+
)
70+
71+
#
72+
# Project options
73+
#
74+
75+
set_target_properties(${target}
76+
PROPERTIES
77+
${DEFAULT_PROJECT_OPTIONS}
78+
FOLDER "${IDE_FOLDER}"
79+
)
80+
81+
#
82+
# Include directories
83+
#
84+
85+
target_include_directories(${target}
86+
PRIVATE
87+
${DEFAULT_INCLUDE_DIRECTORIES}
88+
${PROJECT_BINARY_DIR}/source/include
89+
)
90+
91+
#
92+
# Libraries
93+
#
94+
95+
target_link_libraries(${target}
96+
PRIVATE
97+
${DEFAULT_LIBRARIES}
98+
99+
GTest
100+
101+
${CMAKE_DL_LIBS}
102+
103+
${META_PROJECT_NAME}::metacall_distributable
104+
)
105+
106+
#
107+
# Compile definitions
108+
#
109+
110+
target_compile_definitions(${target}
111+
PRIVATE
112+
${DEFAULT_COMPILE_DEFINITIONS}
113+
)
114+
115+
#
116+
# Compile options
117+
#
118+
119+
target_compile_options(${target}
120+
PRIVATE
121+
${DEFAULT_COMPILE_OPTIONS}
122+
)
123+
124+
#
125+
# Linker options
126+
#
127+
128+
target_link_libraries(${target}
129+
PRIVATE
130+
${DEFAULT_LINKER_OPTIONS}
131+
)
132+
133+
#
134+
# Define test
135+
#
136+
137+
add_test(NAME ${target}
138+
COMMAND $<TARGET_FILE:${target}>
139+
)
140+
141+
#
142+
# Define dependencies
143+
#
144+
145+
add_dependencies(${target}
146+
node_loader
147+
)
148+
149+
#
150+
# Define test properties
151+
#
152+
153+
set_property(TEST ${target}
154+
PROPERTY LABELS ${target}
155+
)
156+
157+
include(TestEnvironmentVariables)
158+
159+
test_environment_variables(${target}
160+
""
161+
${TESTS_ENVIRONMENT_VARIABLES}
162+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2021 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <gmock/gmock.h>
22+
23+
int main(int argc, char * argv[])
24+
{
25+
::testing::InitGoogleMock(&argc, argv);
26+
27+
return RUN_ALL_TESTS();
28+
}

0 commit comments

Comments
 (0)