Skip to content

Commit

Permalink
Change metacall_destroy signature, improved bugs with atexit.
Browse files Browse the repository at this point in the history
  • Loading branch information
viferga committed Dec 19, 2024
1 parent 10f72e1 commit ad08aec
Show file tree
Hide file tree
Showing 155 changed files with 183 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,7 @@ int main(int argc, char **argv)

::benchmark::RunSpecifiedBenchmarks();

return metacall_destroy();
metacall_destroy();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,7 @@ int main(int argc, char **argv)
}
#endif /* OPTION_BUILD_LOADERS_NODE */

return metacall_destroy();
metacall_destroy();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,7 @@ int main(int argc, char *argv[])
::benchmark::RunSpecifiedBenchmarks();
::benchmark::Shutdown();

if (metacall_destroy() != 0)
{
return 4;
}
metacall_destroy();

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ BENCHMARK_DEFINE_F(metacall_py_init_bench, destroy)
/* Python */
#if defined(OPTION_BUILD_LOADERS_PY)
{
if (metacall_destroy() != 0)
{
state.SkipWithError("Error destroying MetaCall");
}
metacall_destroy();
}
#endif /* OPTION_BUILD_LOADERS_PY */
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ int main(int argc, char *argv[])
::benchmark::RunSpecifiedBenchmarks();
::benchmark::Shutdown();

if (metacall_destroy() != 0)
{
return 4;
}
metacall_destroy();

return 0;
}
7 changes: 1 addition & 6 deletions source/cli/metacallcli/source/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,7 @@ application::application(int argc, char *argv[]) :

application::~application()
{
int result = metacall_destroy();

if (result != 0)
{
std::cout << "Error while destroying MetaCall, exit code: " << result << std::endl;
}
metacall_destroy();
}

void application::arguments_parse(std::vector<std::string> &arguments)
Expand Down
4 changes: 3 additions & 1 deletion source/examples/metacalllog/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,7 @@ int main(int, char *[])

/* Here you can load some scripts */

return metacall_destroy();
metacall_destroy();

return 0;
}
5 changes: 1 addition & 4 deletions source/metacall/include/metacall/metacall.h
Original file line number Diff line number Diff line change
Expand Up @@ -1488,11 +1488,8 @@ METACALL_API const char *metacall_plugin_path(void);
/**
* @brief
* Destroy MetaCall library
*
* @return
* Zero if success, different from zero otherwise
*/
METACALL_API int metacall_destroy(void);
METACALL_API void metacall_destroy(void);

/**
* @brief
Expand Down
19 changes: 5 additions & 14 deletions source/metacall/source/metacall.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ static loader_path plugin_path = { 0 };
static int metacall_plugin_extension_load(void);
static void *metacallv_method(void *target, const char *name, method_invoke_ptr call, vector v, void *args[], size_t size);
static type_id *metacall_type_ids(void *args[], size_t size);
static void metacall_destructor(void);
static void metacall_detour_destructor(void);

/* -- Costructors -- */
Expand Down Expand Up @@ -114,16 +113,11 @@ portability_constructor(metacall_constructor)
}

/* Register the destructor on exit */
atexit(metacall_destructor);
atexit(metacall_destroy);
}
}
}

void metacall_destructor(void)
{
metacall_destroy();
}

/* -- Methods -- */

const char *metacall_serial(void)
Expand Down Expand Up @@ -200,8 +194,6 @@ int metacall_plugin_extension_load(void)

void metacall_detour_destructor(void)
{
log_write("metacall", LOG_LEVEL_DEBUG, "MetaCall detour destruction");

/* Destroy link */
if (metacall_link_destroy() != 0)
{
Expand Down Expand Up @@ -2364,7 +2356,7 @@ const char *metacall_plugin_path(void)
return plugin_path;
}

int metacall_destroy(void)
void metacall_destroy(void)
{
if (metacall_initialize_flag == 0)
{
Expand All @@ -2374,17 +2366,16 @@ int metacall_destroy(void)
/* Destroy configurations */
configuration_destroy();

metacall_initialize_flag = 1;

/* Print stats from functions, classes, objects and exceptions */
reflect_memory_tracker_debug();

/* Set to null the plugin extension and core plugin handles */
plugin_extension_handle = NULL;
plugin_core_handle = NULL;
}

return 0;
/* Set initialization flag to destroyed */
metacall_initialize_flag = 1;
}
}

const struct metacall_version_type *metacall_version(void)
Expand Down
10 changes: 2 additions & 8 deletions source/metacall/source/metacall_fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,7 @@ NTSTATUS NTAPI metacall_fork_hook(ULONG ProcessFlags,
log_write("metacall", LOG_LEVEL_DEBUG, "MetaCall process fork auto destroy");

/* Destroy metacall before the fork */
if (metacall_destroy() != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "MetaCall fork auto destruction");
}
metacall_destroy();

/* Execute the real fork */
result = metacall_fork_trampoline(ProcessFlags, ProcessSecurityDescriptor, ThreadSecurityDescriptor, DebugPort, ProcessInformation);
Expand Down Expand Up @@ -247,10 +244,7 @@ pid_t metacall_fork_hook(void)
log_write("metacall", LOG_LEVEL_DEBUG, "MetaCall process fork auto destroy");

/* Destroy metacall before the fork */
if (metacall_destroy() != 0)
{
log_write("metacall", LOG_LEVEL_ERROR, "MetaCall fork auto destruction fail");
}
metacall_destroy();

/* Execute the real fork */
pid = metacall_fork_trampoline();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface Bindings extends Library
SizeT metacall_function_size(Pointer func);
int metacall_function_async(Pointer func);

int metacall_destroy();
void metacall_destroy();

//metacall_value.h
Pointer metacall_value_create_int(int i);
Expand Down
4 changes: 2 additions & 2 deletions source/ports/rs_port/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1355,8 +1355,8 @@ unsafe extern "C" {
pub fn metacall_plugin_path() -> *const ::std::os::raw::c_char;
}
unsafe extern "C" {
#[doc = " @brief\n Destroy MetaCall library\n\n @return\n Zero if success, different from zero otherwise"]
pub fn metacall_destroy() -> ::std::os::raw::c_int;
#[doc = " @brief\n Destroy MetaCall library\n"]
pub fn metacall_destroy();
}
unsafe extern "C" {
#[doc = " @brief\n Provide the module version struct\n\n @return\n Static struct containing unpacked version"]
Expand Down
10 changes: 3 additions & 7 deletions source/ports/rs_port/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ use crate::{
bindings::{metacall_destroy, metacall_initialize, metacall_is_initialized},
types::MetaCallInitError,
};
use std::{ffi::c_int, ptr};
use std::ptr;

pub struct MetaCallDestroy(unsafe extern "C" fn() -> c_int);
pub struct MetaCallDestroy(unsafe extern "C" fn());

impl Drop for MetaCallDestroy {
fn drop(&mut self) {
let code = unsafe { self.0() };

if code != 0 {
panic!("MetaCall failed to destroy with code: {}", code)
}
unsafe { self.0() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/ports/zig_port/src/metacall-bindings.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ pub extern fn metacall_deserialize(name: [*c]const u8, buffer: [*c]const u8, siz
pub extern fn metacall_clear(handle: ?*anyopaque) c_int;
pub extern fn metacall_plugin_extension() ?*anyopaque;
pub extern fn metacall_plugin_path() [*c]const u8;
pub extern fn metacall_destroy() c_int;
pub extern fn metacall_destroy() void;
pub extern fn metacall_version() [*c]const struct_metacall_version_type;
pub extern fn metacall_version_hex_make(major: c_uint, minor: c_uint, patch: c_uint) u32;
pub extern fn metacall_version_hex() u32;
Expand Down
9 changes: 2 additions & 7 deletions source/ports/zig_port/src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@ pub fn init() !void {
return error.FailedToInitMetacall;
}
/// Deinitializes MetaCall and returns an error if didn't succeed.
pub fn destroy() !void {
if (mb.metacall_destroy() != 0)
return error.FailedToDeinitMetacall;
}
/// Deinitializes MetaCall.
pub fn deinit() void {
_ = destroy() catch {};
pub fn destroy() void {
mb.metacall_destroy();
}

/// Loads files into MetaCall, strings should be null-terminated.
Expand Down
2 changes: 1 addition & 1 deletion source/ports/zig_port/src/tests/integrated.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ pub fn main() !void {
try std.testing.expect(ret_string.?[0] == 'h');
try std.testing.expect(ret_string.?[1] == 'i');

defer metacall.deinit();
defer metacall.destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ TEST_F(metacall_backtrace_plugin_test, DefaultConstructor)
/* Generate a segmentation fault in order to catch it by backtrace plugin */
EXPECT_DEATH({ badass_function(); }, "");

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ TEST_F(metacall_c_lib_test, DefaultConstructor)

metacall_value_destroy(args_destroy[0]);

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
2 changes: 1 addition & 1 deletion source/tests/metacall_c_test/source/metacall_c_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,5 @@ TEST_F(metacall_c_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,5 @@ TEST_F(metacall_callback_complex_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ TEST_F(metacall_cast_test, DefaultConstructor)
}
#endif /* OPTION_BUILD_LOADERS_PY */

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ TEST_F(metacall_clear_test, DefaultConstructor)
}
#endif /* OPTION_BUILD_LOADERS_PY */

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ TEST_F(metacall_cli_core_plugin_await_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,5 @@ TEST_F(metacall_cli_core_plugin_test, DefaultConstructor)

metacall_allocator_destroy(allocator);

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ TEST_F(metacall_cobol_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ TEST_F(metacall_configuration_default_test, DefaultConstructor)

ASSERT_EQ((int)0, (int)metacall_initialize());

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ TEST_F(metacall_configuration_exec_path_test, DefaultConstructor)
}
#endif /* OPTION_BUILD_LOADERS_PY */

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
2 changes: 1 addition & 1 deletion source/tests/metacall_cs_test/source/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ void environment::SetUp()

void environment::TearDown()
{
EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ TEST_F(metacall_csharp_function_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ TEST_F(metacall_csharp_static_class_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ TEST_F(metacall_depends_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,5 @@ TEST_F(metacall_distributable_test, DefaultConstructor)
}
#endif /* OPTION_BUILD_LOADERS_C */

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,5 @@ TEST_F(metacall_ducktype_test, DefaultConstructor)
}
#endif /* OPTION_BUILD_LOADERS_JS */

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ TEST_F(metacall_duplicated_handle_test, DefaultConstructor)
}
#endif /* OPTION_BUILD_LOADERS_NODE */

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,5 @@ TEST_F(metacall_duplicated_symbols_test, DefaultConstructor)
}
#endif /* OPTION_BUILD_LOADERS_PY + OPTION_BUILD_LOADERS_RB */

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ TEST_F(metacall_ext_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ TEST_F(metacall_file_fail_test, DefaultConstructor)
metacall_allocator_destroy(allocator);
}

EXPECT_EQ((int)0, (int)metacall_destroy());
metacall_destroy();
}
Loading

0 comments on commit ad08aec

Please sign in to comment.