module: Re-implement using library constructors/destructors (#2018)

By using constructors/destructors we unify the case of static linking with
that of dynamic linking, and we reuse the build flag FEATURE_INIT_FINI. Using
constructors/destructors also allows us to cover the case where library
constructor/destructor functionality is unavailable, because we can expose the
module registration/unregistration functions as global symbols, to be called
explicitly from within the application.

Fixes https://github.com/jerryscript-project/jerryscript/issues/1952

JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof gabriel.schulhof@intel.com
This commit is contained in:
Gabriel "_|Nix|_" Schulhof
2017-09-22 13:35:38 +03:00
committed by Zoltan Herczeg
parent 8d916a44f1
commit 81952f3cd0
11 changed files with 172 additions and 124 deletions
+4
View File
@@ -18,6 +18,10 @@ project (${JERRYX_MODULE_UNITTEST_NAME} C)
file(GLOB JERRYX_MODULE_UNIT_TEST_SOURCES *.c)
if (FEATURE_INIT_FINI)
set(DEFINES_JERRYX_MODULE_UNITTEST ${DEFINES_JERRYX_MODULE_UNITTEST} ENABLE_INIT_FINI)
endif()
add_executable(${JERRYX_MODULE_UNITTEST_NAME} ${JERRYX_MODULE_UNIT_TEST_SOURCES})
set_property(TARGET ${JERRYX_MODULE_UNITTEST_NAME} PROPERTY LINK_FLAGS "${LINKER_FLAGS_COMMON}")
target_link_libraries(${JERRYX_MODULE_UNITTEST_NAME} jerry-ext jerry-core jerry-port-default-minimal)
+10 -4
View File
@@ -19,8 +19,6 @@
#include "test-common.h"
#include "jerryscript-ext/module.h"
#ifdef JERRYX_NATIVE_MODULES_SUPPORTED
/* Load a module. */
const char eval_string1[] = "require ('my_custom_module');";
@@ -138,6 +136,11 @@ eval_one (const char *the_string, double expected_result)
jerry_release_value (js_eval_result);
} /* eval_one */
#ifndef ENABLE_INIT_FINI
extern void my_broken_module_register (void);
extern void my_custom_module_register (void);
#endif /* !ENABLE_INIT_FINI */
int
main (int argc, char **argv)
{
@@ -145,6 +148,11 @@ main (int argc, char **argv)
(void) argv;
jerry_value_t js_global = 0, js_function = 0, js_property_name = 0;
#ifndef ENABLE_INIT_FINI
my_broken_module_register ();
my_custom_module_register ();
#endif /* !ENABLE_INIT_FINI */
jerry_init (JERRY_INIT_EMPTY);
js_global = jerry_get_global_object ();
@@ -164,5 +172,3 @@ main (int argc, char **argv)
jerry_cleanup ();
} /* main */
#endif /* JERRYX_NATIVE_MODULES_SUPPORTED */
+3 -3
View File
@@ -16,9 +16,9 @@
#include "jerryscript.h"
#include "jerryscript-ext/module.h"
#ifdef JERRYX_NATIVE_MODULES_SUPPORTED
#define MODULE_NAME my_broken_module
/*
* A broken module to test that the loader complains about the absence of on_resolve ()
*/
JERRYX_NATIVE_MODULE (my_broken_module, NULL)
#endif /* JERRYX_NATIVE_MODULES_SUPPORTED */
JERRYX_NATIVE_MODULE (MODULE_NAME, NULL)
+2 -4
View File
@@ -16,7 +16,7 @@
#include "jerryscript.h"
#include "jerryscript-ext/module.h"
#ifdef JERRYX_NATIVE_MODULES_SUPPORTED
#define MODULE_NAME my_custom_module
static jerry_value_t
my_custom_module_on_resolve (void)
@@ -24,6 +24,4 @@ my_custom_module_on_resolve (void)
return jerry_create_number (42);
} /* my_custom_module_on_resolve */
JERRYX_NATIVE_MODULE (my_custom_module, my_custom_module_on_resolve)
#endif /* JERRYX_NATIVE_MODULES_SUPPORTED */
JERRYX_NATIVE_MODULE (MODULE_NAME, my_custom_module_on_resolve)