Commit ea1e379a
ea1e379aa794Merged
Chris Hasiński <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
fix(parser): dedup plain require too, and grow the table dynamically
- Apply the same canonical-path dedup in resolve_plain_requires that
resolve_requires already uses, so a project that mixes plain
requireandrequire_relativefor the same physical file does not re-inline it (and re-emit struct definitions). - Replace the static 256-entry sp_included_paths table with a dynamic one that doubles on demand. Eliminates the silent drop-on-overflow.
- Add sp_includes_free() and call it at the end of main so the strdup'd entries don't leak; doesn't matter for the short-lived process itself but keeps leak checkers and embedders happy.
Addresses Gemini review on #28.
Details
- Commit
- ea1e379aa794
- Parent
- 55ac5f222251
spinel_parse.cmodifiedView file
@@ -938,13 +938,26 @@
938938
snprintf(lib_path, sizeof(lib_path), "%s/%s", lib_dir, lib_name);939939
if (!strstr(lib_path, ".rb")) strcat(lib_path, ".rb");940940
941-
char *content = read_file(lib_path);942-
if (!content) {943-
content = strdup("# require not resolved");941+
/* Same dedup as resolve_requires: a file pulled in via plain `require`942+
must not be re-inlined if a previous `require` or `require_relative`943+
already pulled it. Otherwise mixing the two forms for the same lib944+
still produces struct-redefinition errors. */945+
char *canonical = sp_canonical_path(lib_path);946+
char *content;947+
if (sp_path_already_included(canonical)) {948+
content = strdup("# require skipped (already included)");949+
free(canonical);944950
} else {945-
char *resolved = resolve_requires(content, lib_path);946-
free(content);947-
content = resolved;951+
sp_mark_path_included(canonical);952+
free(canonical);953+
content = read_file(lib_path);954+
if (!content) {955+
content = strdup("# require not resolved");956+
} else {957+
char *resolved = resolve_requires(content, lib_path);958+
free(content);959+
content = resolved;960+
}948961
}949962
950963
size_t line_len = (line_end - pos) + ((*line_end == '\n') ? 1 : 0);@@ -1119,5 +1132,6 @@
11191132
pm_node_destroy(&parser, root);11201133
pm_parser_free(&parser);11211134
free(source);1135+
sp_includes_free();11221136
return 0;11231137
}