Commit 7b32d151

7b32d151bdfdMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Drop open_memstream from the parser; assemble the AST in memory

open_memstream is POSIX.1-2008, not baseline: it is absent on Solaris, AIX, HP-UX, pre-10.13 macOS, and MinGW. The in-process entry point sp_parse_file_to_text relied on it, with a fragile Windows fallback that round-tripped the AST through a temp file in TEMP/TMP keyed by _getpid().

The AST body already accumulates in a growable lines[] array, so the stream only serialized a short header plus those lines. Replace the FILE* with a small in-memory string builder (SpStrBuf) in the library copy (src/spinel_parse.c): one code path on every platform, no open_memstream and no temp-file dance. Output is byte-identical to the previous open_memstream path (verified via --dump-ast diff across plain, require-using, and line-map inputs).

The legacy standalone (legacy/spinel_parse.c) streams to stdout/fopen, which is already portable; drop its now-dead sp_parse_file_to_text copy and the <process.h> include along with it.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
7b32d151bdfd
Parent
e6525584461d

legacy/spinel_parse.cmodifiedView file

@@ -22,9 +22,6 @@
2222 #include <stdarg.h>
2323 #include <limits.h>
2424 #include <prism.h>
25-#if defined(_WIN32)
26-#include <process.h> /* _getpid -- for a unique temp-file name (no open_memstream on MinGW) */
27-#endif
2825
2926 /* ---- Output buffer ---- */
3027 static char **lines;
@@ -2195,9 +2192,9 @@
21952192 /* ---- Main ---- */
21962193 /* Parse `source_file` and write the text AST to `out`. `argv0` is the
21972194 invoking program path (used to locate the stdlib for plain `require`s).
2198- Returns 0 on success, 1 on read/parse error. Shared by the standalone
2199- `main` and the in-process `sp_parse_file_to_text` entry so the Prism
2200- walk has exactly one home. */
2195+ Returns 0 on success, 1 on read/parse error. This is the standalone CLI;
2196+ the single-binary C compiler links the in-memory variant in
2197+ src/spinel_parse.c instead. */
22012198 static int sp_parse_emit(const char *source_file, const char *argv0, FILE *out) {
22022199 char *source = read_file(source_file);
22032200 if (!source) {
@@ -2334,50 +2331,3 @@
23342331 return rc;
23352332 }
23362333 #endif
2337-
2338-/* In-process entry for the single-binary C compiler: parse `source_file`
2339- and return the text AST as a malloc'd NUL-terminated buffer (caller
2340- frees), or NULL on error. `argv0` locates the stdlib for plain requires.
2341- Avoids any on-disk intermediate by writing to an in-memory stream. */
2342-char *sp_parse_file_to_text(const char *source_file, const char *argv0) {
2343-#if defined(_WIN32)
2344- /* MinGW/MSVCRT has no open_memstream, and tmpfile() opens in the (often
2345- unwritable) drive root on CI. Capture sp_parse_emit's output through a
2346- uniquely-named temp file in TEMP/TMP, then read it back. */
2347- const char *td = getenv("TEMP");
2348- if (!td) td = getenv("TMP");
2349- if (!td) td = ".";
2350- char path[4096];
2351- snprintf(path, sizeof path, "%s\\spinel_ast_%d.tmp", td, (int)_getpid());
2352- FILE *out = fopen(path, "wb+");
2353- if (!out) return NULL;
2354- int rc = sp_parse_emit(source_file, argv0, out);
2355- char *buf = NULL;
2356- if (rc == 0 && fflush(out) == 0 && fseek(out, 0, SEEK_END) == 0) {
2357- long sz = ftell(out);
2358- if (sz >= 0) {
2359- rewind(out);
2360- buf = (char *)malloc((size_t)sz + 1);
2361- if (buf) {
2362- size_t n = fread(buf, 1, (size_t)sz, out);
2363- buf[n] = '\0';
2364- }
2365- }
2366- }
2367- fclose(out);
2368- remove(path);
2369- return buf;
2370-#else
2371- char *buf = NULL;
2372- size_t sz = 0;
2373- FILE *out = open_memstream(&buf, &sz);
2374- if (!out) return NULL;
2375- int rc = sp_parse_emit(source_file, argv0, out);
2376- fclose(out);
2377- if (rc != 0) {
2378- free(buf);
2379- return NULL;
2380- }
2381- return buf;
2382-#endif
2383-}