jetc: add --shared mode to compile templates to a dlopen()-able .so
For the server you're about to build: `jetc --shared template.jet out.cpp`
now emits no main() at all - instead the render logic is exported as
`extern "C" void jetc_render_<template>(jet::Global &global,
coreutils::MString &out)`, one call per request, meant to be dlsym()'d
by a long-running host process instead of spawning a jet-2.0 process (or
even a separate compiled executable) per request. extern "C" here only
fixes the symbol's name mangling for a stable dlsym() target - the
parameter types are still real C++ classes, safe to pass across the
dlopen() boundary since the host and the .so share the same
CoreUtils/JetCore headers and toolchain (a same-ABI plugin boundary, not
a foreign C one). coreutils::Exception propagates out of the call
uncaught, so the host decides how to handle a render failure rather than
the module unilaterally deciding (the executable mode's try/catch prints
to stdout and exits, which is CGI-appropriate but wrong for a persistent
server). The exported symbol name is derived from the input filename
(jetc.cpp's baseNameNoExt+sanitizeIdent) so multiple compiled templates'
.so's stay collision-free even under dlopen(..., RTLD_GLOBAL).
jetc/build-shared.sh builds the .so: JetCore's 22 tag classes are still
force-linked directly (Tag::interpretSpan's fallback path can dispatch
to any of them at runtime, regardless of what a given template
statically references), but CoreUtils objects are compiled -fPIC and
archived into a throwaway libCoreUtils_pic.a instead of being linked
directly - libCoreUtils.a itself isn't position-independent, so linking
it into a .so fails outright ("relocation ... can not be used when
making a shared object"), and force-linking every CoreUtils .cpp
directly (rather than archiving, so the linker only pulls in what's
actually referenced) surfaced an unrelated, pre-existing incomplete
class - IMFMessage's vtable is never emitted anywhere, presumably a
partially-migrated class (see project memory on IMFMultipart/IMFText
still being fd-based) - as an unresolved dlopen()-time symbol, even
though nothing here uses it. A normal archive-based link (like the
existing libCoreUtils.a already does for jet-2.0) never pulls IMFMessage
in at all, so building libCoreUtils_pic.a the same way sidesteps it
without touching the unrelated bug itself.
jetc/host_test.cpp is a minimal stand-in for the real server: dlopen()s
a compiled .so, dlsym()s its entry point, and calls it twice with fresh
jet::Global objects (simulating two separate requests against one loaded
module) to prove the .so is genuinely reusable, not single-use. Verified
both demo templates (demo.jet, demo2.jet - the latter covering nested
if/for, a stateful while loop, and the interpretSpan fallback path) build
as .so's and produce the same byte-identical output as the executable
and interpreter paths, across two calls each. Also re-verified the
existing (non-shared) executable build path still matches jet-2.0's
output after the Codegen::generate() signature change.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d52ca61250
commit
a94fc5e8fb
5 files changed
+214
-9
No files matched your search
Executable → Regular
+13
-1
@@ -172,7 +172,8 @@ namespace jetc {
|
||||
}
|
||||
}
|
||||
|
||||
std::string Codegen::generate(const std::vector<NodePtr> &top, const std::string &sourceFileComment) {
|
||||
std::string Codegen::generate(const std::vector<NodePtr> &top, const std::string &sourceFileComment,
|
||||
bool sharedMode, const std::string &entrySymbol) {
|
||||
out.str("");
|
||||
out.clear();
|
||||
out << "// " << sourceFileComment << "\n";
|
||||
@@ -184,6 +185,17 @@ namespace jetc {
|
||||
out << "#include \"MString.h\"\n";
|
||||
out << "#include \"Exception.h\"\n";
|
||||
out << "#include <iostream>\n\n";
|
||||
|
||||
if (sharedMode) {
|
||||
out << "extern \"C\" void " << entrySymbol << "(jet::Global &global, coreutils::MString &out) {\n";
|
||||
out << " coreutils::ZString jetc_empty(\"\");\n";
|
||||
out << " coreutils::MString jetc_scopeOut;\n";
|
||||
out << " jet::Tag scope(jetc_empty, jetc_scopeOut, global, NULL, NULL);\n";
|
||||
emitBody(top, 1);
|
||||
out << "}\n";
|
||||
return out.str();
|
||||
}
|
||||
|
||||
out << "static void jetc_render(jet::Global &global, coreutils::MString &out) {\n";
|
||||
out << " coreutils::ZString jetc_empty(\"\");\n";
|
||||
out << " coreutils::MString jetc_scopeOut;\n";
|
||||
|
||||
Reference in new issue
Block a user