Files
JetCore/__include.cpp
T
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

105 lines
4.6 KiB
C++

#include "__include.h"
#include "Exception.h"
#include "File.h"
#include <string>
#include <unordered_map>
#include <sys/stat.h>
namespace jet {
namespace {
struct CachedInclude {
std::string content;
time_t mtime;
};
// Persistent, per-worker-thread cache of <include> file contents,
// keyed by resolved filename. Real content depends on this
// constantly through custom tag bodies that jetc's own compile-
// time <include>-inlining doesn't reach (only the top-level .jet
// source's own includes get compiled in) - mainpagetemplate.temp
// alone pulls in login.menu/menu.bar/opensource.menu/cart.menu/
// sessionvar.menu, all previously re-read from disk on every
// single request even though the files are 100% static between
// edits. Invalidated via mtime comparison (a stat() call, cheap
// relative to a full read()) so a live edit still takes effect on
// the very next request - matching the workflow already relied on
// all session of editing .tag/.temp/.menu content directly and
// seeing it live without a rebuild.
thread_local std::unordered_map<std::string, CachedInclude> includeCache;
}
__include::__include(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local) : Tag(in, parentOut, global, parent, local) {
if(!keywordDefined("file"))
throw coreutils::Exception("file keyword must be specified.");
if(hasContainer)
throw coreutils::Exception("include tag should not have a container.");
hasContainer = true;
coreutils::MString includeFileName = resolveKeyword("file");
std::string fileNameKey = includeFileName.str();
struct stat st;
bool haveStat = (stat(fileNameKey.c_str(), &st) == 0);
auto cached = includeCache.find(fileNameKey);
if(cached != includeCache.end() && haveStat && cached->second.mtime == st.st_mtime) {
container = coreutils::ZString(cached->second.content.data(), cached->second.content.length());
} else {
coreutils::File file(includeFileName);
file.read();
if(haveStat) {
CachedInclude &entry = includeCache[fileNameKey];
entry.content = file.asString();
entry.mtime = st.st_mtime;
container = coreutils::ZString(entry.content.data(), entry.content.length());
} else {
// Couldn't stat the file (unusual - e.g. a race with deletion) -
// don't cache something we can't validate the freshness of,
// just render this one read directly, matching the pre-cache
// behavior exactly for this edge case.
container = file.asZString();
}
}
try {
processContainer(container);
}
catch(coreutils::Exception e) {
// global.errorCursor is whatever cursor was active in whichever
// ZString buffer was being parsed at the moment the ORIGINAL
// exception was thrown - not necessarily this include's own
// `container` buffer at all (e.g. a keyword-resolution error
// happening while interpreting a completely different .temp
// file's own text). setCursor() on a pointer outside container's
// own [data, data+length] range throws "Cursor out of range on
// setCursor." - and since that happens INSIDE this diagnostic-
// only "show a nice line number" block, it was replacing the
// real error (e.text) with that unrelated cursor-range message
// before it ever reached the caller, making every real error
// from inside an <include> impossible to see. This is purely
// diagnostic output - if it can't compute a line number, fall
// back to printing the real error text without one rather than
// losing it entirely.
try {
container.setCursor(global.errorCursor);
container.moveBackToLineStart();
std::cout << "-----------------------------" << std::endl;
std::cout << "Error in included script '" << variables["file"] << "' at line " << container.getLineNumberAtCursor() << std::endl;
std::cout << "Error text: " << e.text << std::endl;
std::cout << "-----------------------------" << std::endl;
std::cout << container.parsed() << std::endl;
std::cout << "******** Error caught: " << e.text << std::endl;
std::cout << container.unparsed() << std::endl;
} catch(coreutils::Exception &) {
std::cout << "-----------------------------" << std::endl;
std::cout << "Error in included script '" << variables["file"] << "' (line number unavailable - error originated outside this include's own content)" << std::endl;
std::cout << "Error text: " << e.text << std::endl;
std::cout << "-----------------------------" << std::endl;
}
global.errorCursor = in.getCursor();
throw coreutils::Exception(e.text);
}
}
}