Skip to content

Commit dc7a7fe

Browse files
committed
module: make NEED_MODULE self contained again
register_<Module> is extern "C" so C and D hosts can call it unmangled between das_initialize_modules() and das_initialize_finalize(). Two side effects of that linkage broke every C++ host that pulls modules. The macro can no longer declare the entry point: it expands inside a function body, a linkage specification is only valid at namespace scope, and declaring it without one names a different, C++ mangled symbol that nothing defines. So each host had to add a file scope DECLARE_MODULE per module, boilerplate the macro already has the name for. REGISTER_MODULE and REGISTER_MODULE_IN_NAMESPACE now also emit das_pull_<Module>, a C++ linkage tail call to register_<Module>, and NEED_MODULE declares that instead, which at block scope is both legal and correct. The macro also stopped ending in a semicolon, so two bare uses on consecutive lines spliced into one expression and the second leading * became a multiplication: error: invalid operands to binary expression ('unsigned int' and 'DasThreadLocal<unsigned int, ...>') Put the semicolon back. A call site that writes its own gets an empty statement. Pulling a module from C++ needs no declaration again: void pull_game_das() { NEED_MODULE(SomeModule) } The C side is untouched: register_<Module> and jit_register_<Module> keep C linkage and their unmangled names, so the C API embedding flow works exactly as documented. DECLARE_MODULE and PULL_MODULE keep working for callers that would rather declare at file scope.
1 parent 70e34f4 commit dc7a7fe

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

include/daScript/ast/ast.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,9 @@ namespace das
13031303
} \
13041304
extern "C" DAS_EXPORT_DLL das::Module * jit_register_##ClassName () { \
13051305
return register_##ClassName(); \
1306+
} \
1307+
DAS_EXPORT_DLL das::Module * das_pull_##ClassName () { \
1308+
return register_##ClassName(); \
13061309
}
13071310

13081311
#if DAS_ENABLE_DLL
@@ -1333,6 +1336,9 @@ namespace das
13331336
} \
13341337
extern "C" DAS_EXPORT_DLL das::Module * jit_register_##ClassName () { \
13351338
return register_##ClassName(); \
1339+
} \
1340+
DAS_EXPORT_DLL das::Module * das_pull_##ClassName () { \
1341+
return register_##ClassName(); \
13361342
}
13371343

13381344
using module_pull_t = das::Module*(*)();

include/daScript/daScriptModule.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ DAS_CC_API void register_fusion ();
4646
// modules are declared above. A separately compiled custom module must first be
4747
// declared with DECLARE_MODULE at file scope.
4848
#define NEED_MODULE(ClassName) \
49-
*das::ModuleKarma += unsigned(intptr_t(::register_##ClassName()))
49+
extern DAS_API das::Module * das_pull_##ClassName (); \
50+
*das::ModuleKarma += unsigned(intptr_t(das_pull_##ClassName()));
5051

5152
#define NEED_FUSION \
5253
::register_fusion()

0 commit comments

Comments
 (0)