Files
JetCore/jetc/JetCodegen.h
T
brad ArantandClaude Opus 5.5 a18d1358b0 Add <bind> bound parameters for <sql> (phase 1)
Implements phase 1 of docs/design-sql-bound-parameters.md: <bind> supplies
the value for the next ? placeholder of the enclosing <sql>, with typed
validation (int, decimal, date, datetime, bool, ident with allow list),
null="empty", and refusal to change the connection character set.

- SqlBind.*: MySQL-free placeholder scanning and binding logic
- __bind.*, __sql, __mysql: runtime wiring
- Tag.cpp, jetc/JetParser.cpp: bind is a void tag in both
- jetc/JetCodegen: emit sized ZString literals for text holding a NUL,
  which previously truncated the compiled page
- tests: unit tests, end-to-end test against MySQL (interpreted and
  jetc-compiled), and the generator for the binary test page

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 19:55:23 -08:00

96 lines
5.0 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 std::string zstr(const std::string &raw); // same, as a sized ZString if it holds a NUL
static const char *staticTypeOp(const std::string &type, const char *rcVar);
};
}
#endif