Purely a naming-convention change per user request, not a format
change - the output is still a real ELF shared object (-shared -fPIC),
still dlopen()-able exactly as before. .jtc ("jet compiled") marks it as
an APIServer-loadable content module rather than a generic library.
build-shared.sh: usage text and STEM derivation
(basename "$OUTPUT" .so -> .jtc) both updated - the STEM change is the
one functional piece here, everything else in this commit is comments/
usage strings. Doc comments in jetc.cpp/JetCodegen.h/host_test.cpp/
api_test.cpp updated to match.
Verified live: rebuilt jetc and all five demo*.jtc modules, reran
api_test/host_test against demo3.jtc (still resolves jetc_api_demo3/
jetc_render_demo3 and renders correctly) - the extension is purely
cosmetic to dlopen(), as expected.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
78 lines
3.8 KiB
C++
78 lines
3.8 KiB
C++
#ifndef __JetCodegen_h__
|
|
#define __JetCodegen_h__
|
|
|
|
#include "JetAST.h"
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <vector>
|
|
|
|
namespace jetc {
|
|
|
|
// One `#api METHOD /path` directive from the top of the source template
|
|
// (see jetc.cpp) - a compile-time-known route this template wants to
|
|
// serve once loaded as a .jtc. Purely descriptive data handed to Codegen;
|
|
// jetc itself never interprets the method/path beyond storing them.
|
|
struct ApiRoute {
|
|
std::string method;
|
|
std::string path;
|
|
};
|
|
|
|
// Emits a standalone .cpp translation unit that renders a compiled JET
|
|
// template. The static tag/text structure becomes real C++ control flow;
|
|
// anything genuinely dynamic (variable values, expr/value comparisons,
|
|
// loop bounds) still calls into the existing JetCore/CoreUtils runtime
|
|
// (Tag::getVariable/resolveKeyword/assignVariable, jet::Operand) exactly
|
|
// as the interpreter does - compiling doesn't try to eliminate that, only
|
|
// the repeated re-parsing of the template's own structure.
|
|
class Codegen {
|
|
public:
|
|
// sharedMode == false (default): emits a static jetc_render() plus a
|
|
// main() that runs it once, catches coreutils::Exception, and writes
|
|
// the result to stdout - the existing CGI-style standalone program.
|
|
// apiRoutes/apiSymbol are ignored in this mode (there's no host to
|
|
// dlsym them).
|
|
//
|
|
// sharedMode == true: emits no main() at all. Instead the render
|
|
// logic is exported as `extern "C" void <entrySymbol>(jet::Global
|
|
// &global, coreutils::MString &out)`, meant to be built into a .jtc
|
|
// (see build-shared.sh) and dlsym()'d by a long-running host server -
|
|
// one call renders one request into the caller-owned `out`, using
|
|
// whatever Global the host passes in (its lifecycle, cgi/session
|
|
// setup etc. is entirely the host's business, not this module's).
|
|
// extern "C" only affects name mangling here, not the parameter
|
|
// types - both sides are compiled from the same CoreUtils/JetCore
|
|
// headers with the same toolchain, so passing C++ class types (and
|
|
// letting coreutils::Exception propagate to the caller) across the
|
|
// dlopen() boundary is safe; this is a same-ABI plugin boundary, not
|
|
// a foreign C ABI one. entrySymbol should be unique per compiled
|
|
// template (see jetc.cpp) so multiple .jtc's can coexist even if a
|
|
// host ever dlopen()s them RTLD_GLOBAL instead of keeping separate
|
|
// handles.
|
|
//
|
|
// If apiRoutes is non-empty, sharedMode also emits a second exported
|
|
// function `extern "C" const JetApiRoute *<apiSymbol>(int *count)`
|
|
// returning a static array describing every `#api` directive the
|
|
// source declared - a host's directory watcher/loader dlsym()'s this
|
|
// (by the same filename-derived naming convention as entrySymbol) to
|
|
// learn what to register, without any separate manifest file that
|
|
// could drift out of sync with the render function it describes.
|
|
std::string generate(const std::vector<NodePtr> &top, const std::string &sourceFileComment,
|
|
bool sharedMode = false, const std::string &entrySymbol = "jetc_render",
|
|
const std::vector<ApiRoute> &apiRoutes = {}, const std::string &apiSymbol = "jetc_api");
|
|
|
|
private:
|
|
std::ostringstream out;
|
|
size_t nextId = 0;
|
|
std::string id(const std::string &prefix) { return prefix + std::to_string(nextId++) + "_"; }
|
|
|
|
void emitBody(const std::vector<NodePtr> &nodes, int indent);
|
|
void emitNode(const NodePtr &n, int indent);
|
|
static std::string ind(int indent);
|
|
static std::string cstr(const std::string &raw); // C++ string-literal escape
|
|
static const char *staticTypeOp(const std::string &type, const char *rcVar);
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|