Files
Brad Arant b0cadc3f08 Multi-day accumulated fixes: tag dispatch, session/pooling correctness, SPA rollout, jetc compiler
JetCore engine (Tag.cpp/.h, Global.cpp/.h, __*.cpp tag classes):
- Numerous __mysql.cpp connection-pooling correctness fixes (stale
  result sets, double-indirection sessionid bugs, field caching,
  pool keying) and a session-registration-before-connection-confirmed
  ordering fix (dangling pointer on a failed connect)
- Tag.cpp: large body of fixes to the tag-dispatch/container-scanning
  logic (bare-name fallback, database aliasing, single-quote handling,
  @-prefix session variable resolution, TAGDEF caching, nested split-tag
  scanning for if/while/until/ifrow so an inner one doesn't corrupt an
  outer one's own boundary scan) plus the process-first rendering
  order fix (a <process> now runs before <checksession>/page
  resolution, fixing a real "blank page after successful login" bug)
- Global.h/.cpp: added imapSessions (parallel to the existing __mysql*
  session map) for the new <imap> tag family
- __whiledir.cpp/__include.cpp: caching and directory-name-aliasing
  fixes
- __call.cpp/.h: fixed a dangling-pointer bug
- __write.cpp: fixed an O_WRONLY-only open that couldn't create a new
  file
- __ifrow.cpp/__sql.cpp: fixed the same sessionid double-indirection
  bug __mysql.cpp had (resolveKeyword's result was being used as a map
  key lookup instead of the resolved value itself)

jetc compiler (jetc/JetParser.cpp/.h, JetCodegen.cpp/.h, JetAST.h,
jetc.cpp, build-shared.sh, build-compiled.sh):
- Parser fixes: inline <include>, nested-quote handling, void-tag
  recognition
- Codegen fixes and expanded AST support
- build-shared.sh: atomic rename into place for a freshly-built .jtc
  (a live server dlopen()ing the same path mid-rebuild was corrupting
  its own already-mapped view of the old file) and a staleness check
  so a cached JetCore .o doesn't silently keep serving yesterday's
  fixes forever
- Added -lssl to both build scripts' link lines (needed once the new
  <imap> tag's client-side TLS code existed - previously nothing in
  this codebase used OpenSSL's SSL/TLS client API, only libcrypto)

Site content (jetc/barant-site/): SPA (fetch-and-swap navigation)
rollout across essentially every .page/.jet/.section/.tag/.proc/.method
file - dynamic-content fixes, fragment-mode stripping, native
$[@name] session variables replacing older mechanisms, and this
session's own checkout.page redesign, login start-param redirect fix,
and per-page side-menu (menu.bar/$[PAGE].menu) refresh support.
Completed the earlier main.css/control.css move from assets/ to
assets/images/ (git still showed the old paths as tracked-but-deleted
until now).

Deliberately left out of this commit: dozens of .pre-<description>-
<date> manual backup files (this session's and earlier ones' own
before/after snapshots, not meant for permanent history), compiled
.jtc build outputs, ad hoc test binaries and scripts under jetc/
(login_asan, wcwc_test, optest2_bin, etc.), and jetc/__pycache__/ -
none of these are meant to be tracked; a .gitignore covering them
would be worth adding separately.
2026-09-03 05:20:48 +00:00

95 lines
4.9 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;
// Free functions generated for compiled <tag name="X"> definitions
// (see the TAGDEF case in emitNode) accumulate here, in the order
// encountered, and are spliced into the final output ahead of the
// entry function (see generate()) - C++ doesn't care about
// definition order within one translation unit as long as nothing
// outside of it needs a forward declaration, but keeping them
// grouped together ahead of the entry point makes the generated
// file easier to read/debug than interleaving them positionally.
std::ostringstream tagDefFuncs;
size_t nextId = 0;
std::string id(const std::string &prefix) { return prefix + std::to_string(nextId++) + "_"; }
// target is the stream this node's statements are written to - `out`
// for everything at the top level or nested inside ordinary control
// flow (IF/FOR/WHILE bodies stay in the same stream as their
// enclosing scope), but a TAGDEF's own body is rendered into a
// fresh stream to become a separate generated function (see
// emitNode's TAGDEF case) rather than being inlined in place.
void emitBody(const std::vector<NodePtr> &nodes, int indent, std::ostringstream &target);
void emitNode(const NodePtr &n, int indent, std::ostringstream &target);
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