diff --git a/docs/12.EXT-REFERENCE-MODULE.md b/docs/12.EXT-REFERENCE-MODULE.md index 67dbf4ded..e0790a32d 100644 --- a/docs/12.EXT-REFERENCE-MODULE.md +++ b/docs/12.EXT-REFERENCE-MODULE.md @@ -37,8 +37,8 @@ resolved using the native JerryScript module resolver `jerryx_module_native_reso `jerryx_module_resolve()`. Native modules are registered during application startup and by calling `dlopen()` by means of library constructors, support for which can be turned on using the `FEATURE_INIT_FINI` build flag. In the absence of such a flag, the module registration and unregistration functions are exposed as global symbols which can be called -explicitly. Note: `FEATURE_INIT_FINI` build flag isn't supported on Windows, because Microsoft Visual C/C++ Compiler -doesn't support library constructors and destructors. +explicitly. Note: On windows, `FEATURE_INIT_FINI` build flag only supported with GNU toolchain or Microsoft Visual C/C++ Compiler +2008 and upper. ## jerryx_module_resolve diff --git a/jerry-ext/include/jerryscript-ext/module.h b/jerry-ext/include/jerryscript-ext/module.h index 681ac13ba..6c5365039 100644 --- a/jerry-ext/include/jerryscript-ext/module.h +++ b/jerry-ext/include/jerryscript-ext/module.h @@ -42,16 +42,56 @@ typedef struct jerryx_native_module_t */ #ifdef ENABLE_INIT_FINI #ifdef _MSC_VER -#error "`FEATURE_INIT_FINI` build flag isn't supported on Windows, because Microsoft Visual C/C++ Compiler \ -doesn't support library constructors and destructors." +/** + * Only Visual Studio 2008 and upper version support for __pragma keyword + * refer to https://msdn.microsoft.com/en-us/library/d9x1s805(v=vs.90).aspx + */ +#if _MSC_VER >= 1500 +#ifdef _WIN64 +#define JERRYX_MSVC_INCLUDE_SYM(s) comment (linker, "/include:" #s) +#else /* !_WIN64 */ +#define JERRYX_MSVC_INCLUDE_SYM(s) comment (linker, "/include:_" #s) +#endif /* _WIN64 */ + +#ifdef __cplusplus +#define JERRYX_MSCV_EXTERN_C extern "C" +#else /* !__cplusplus */ +#define JERRYX_MSCV_EXTERN_C +#endif /* __cplusplus */ + +#pragma section(".CRT$XCU", read) +#pragma section(".CRT$XTU", read) + +#define JERRYX_MSVC_FUNCTION_ON_SECTION(sec_name, f) \ + static void f (void); \ + __pragma (JERRYX_MSVC_INCLUDE_SYM (f##_section)) __declspec(allocate (sec_name)) \ + JERRYX_MSCV_EXTERN_C void (*f##_section) (void) = f; \ + static void f (void) + +#define JERRYX_MODULE_CONSTRUCTOR(f) JERRYX_MSVC_FUNCTION_ON_SECTION (".CRT$XCU", f) +#define JERRYX_MODULE_DESTRUCTOR(f) JERRYX_MSVC_FUNCTION_ON_SECTION (".CRT$XTU", f) +#else /* !(_MSC_VER >= 1500) */ +#error "Only Visual Studio 2008 and upper version are supported." +#endif /* _MSC_VER >= 1500 */ +#elif defined(__GNUC__) +#define JERRYX_MODULE_CONSTRUCTOR(f) \ + static void f (void) __attribute__ ((constructor)); \ + static void f (void) + +#define JERRYX_MODULE_DESTRUCTOR(f) \ + static void f (void) __attribute__ ((destructor)); \ + static void f (void) +#else /* __GNUC__ */ +#error "`FEATURE_INIT_FINI` build flag isn't supported on this compiler" #endif /* _MSC_VER */ -#define JERRYX_MODULE_CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor)) -#define JERRYX_MODULE_DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor)) -#define JERRYX_MODULE_REGISTRATION_QUALIFIER static #else /* !ENABLE_INIT_FINI */ -#define JERRYX_MODULE_CONSTRUCTOR_ATTRIBUTE -#define JERRYX_MODULE_DESTRUCTOR_ATTRIBUTE -#define JERRYX_MODULE_REGISTRATION_QUALIFIER +#define JERRYX_MODULE_CONSTRUCTOR(f) \ + void f (void); \ + void f (void) + +#define JERRYX_MODULE_DESTRUCTOR(f) \ + void f (void); \ + void f (void) #endif /* ENABLE_INIT_FINI */ /** @@ -59,21 +99,19 @@ doesn't support library constructors and destructors." */ #define JERRYX_NATIVE_MODULE(module_name, on_resolve_cb) JERRYX_NATIVE_MODULE_IMPLEM (module_name, on_resolve_cb) -#define JERRYX_NATIVE_MODULE_IMPLEM(module_name, on_resolve_cb) \ - static jerryx_native_module_t _##module_name##_definition = { .name_p = (jerry_char_t *) #module_name, \ - .on_resolve_p = (on_resolve_cb), \ - .next_p = NULL }; \ - \ - JERRYX_MODULE_REGISTRATION_QUALIFIER void module_name##_register (void) JERRYX_MODULE_CONSTRUCTOR_ATTRIBUTE; \ - JERRYX_MODULE_REGISTRATION_QUALIFIER void module_name##_register (void) \ - { \ - jerryx_native_module_register (&_##module_name##_definition); \ - } \ - \ - JERRYX_MODULE_REGISTRATION_QUALIFIER void module_name##_unregister (void) JERRYX_MODULE_DESTRUCTOR_ATTRIBUTE; \ - JERRYX_MODULE_REGISTRATION_QUALIFIER void module_name##_unregister (void) \ - { \ - jerryx_native_module_unregister (&_##module_name##_definition); \ +#define JERRYX_NATIVE_MODULE_IMPLEM(module_name, on_resolve_cb) \ + static jerryx_native_module_t _##module_name##_definition = { .name_p = (jerry_char_t *) #module_name, \ + .on_resolve_p = (on_resolve_cb), \ + .next_p = NULL }; \ + \ + JERRYX_MODULE_CONSTRUCTOR (module_name##_register) \ + { \ + jerryx_native_module_register (&_##module_name##_definition); \ + } \ + \ + JERRYX_MODULE_DESTRUCTOR (module_name##_unregister) \ + { \ + jerryx_native_module_unregister (&_##module_name##_definition); \ } /** diff --git a/tools/run-tests.py b/tools/run-tests.py index c09f61ed2..369db7de5 100755 --- a/tools/run-tests.py +++ b/tools/run-tests.py @@ -68,10 +68,7 @@ JERRY_UNITTESTS_OPTIONS = [ OPTIONS_COMMON + OPTIONS_DOCTESTS + OPTIONS_PROFILE_ES51), Options('unittests-es5.1-init-fini', OPTIONS_COMMON + OPTIONS_UNITTESTS + OPTIONS_PROFILE_ES51 - + ['--cmake-param=-DFEATURE_INIT_FINI=ON'], - skip=skip_if((sys.platform == 'win32'), 'FEATURE_INIT_FINI build flag isn\'t supported on Windows,' + - ' because Microsoft Visual C/C++ Compiler doesn\'t support' + - ' library constructors and destructors.')), + + ['--cmake-param=-DFEATURE_INIT_FINI=ON']), Options('unittests-es5.1-math', OPTIONS_COMMON + OPTIONS_UNITTESTS + OPTIONS_PROFILE_ES51 + ['--jerry-math=on']),