Skip to content
Myr

Register imported libraries in dependency order (#3620)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

block_mark_referenced walks the joined block backwards and skips the body of any definition not yet marked referenced, so a library must precede its importers. f58787c41 registered a library before walking its dependencies, to have somewhere to keep the `loading` flag, which reversed that order and made block_drop_unreferenced free definitions that are still referenced through bound_by. Keep the libraries being loaded on a stack threaded through the load_library frames instead, so registration can move back after the dependency walk.

authored by itchyny · committed by GitHub · parent e371bfa · Browse files · 7 files+59 added−23 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+59 added−23 removed

  • tests:+27 added−0 removed
  • other:+32 added−23 removed

7 files+59 added−23 removed

modified:src/linker.c+30 added-23 removed
21 unmodified lines
2223struct lib_entry {24 char *name;25 block def;26 int loading;26};27struct loading_lib {28 const char *name;29 struct loading_lib *next;30};31struct lib_loading_state {32 struct lib_entry *entries;33 uint64_t ct;34 struct loading_lib *loading;35};36static int load_library(jq_state *jq, jv lib_path,37 int is_data, int raw, int optional,38 const char *as, block *out_block,
276 unmodified lines
315 break;316 }317318 if (state_idx < lib_state->ct) { // Found315 if (lib_state->entries[state_idx].loading) {316 jq_report_error(jq, jv_string_fmt("jq: error: circular import of %s\n",317 jv_string_value(resolved)));318 jv_free(resolved);319 jv_free(as);320 jv_free(deps);321 jv_free(jq_origin);322 jv_free(lib_origin);323 return 1;324 }319 jv_free(resolved);320 // Bind the library to the program321 bk = block_bind_library(lib_state->entries[state_idx].def, bk, OP_IS_CALL_PSEUDO, as_str);322 } else { // Not found. Add it to the table before binding.
21 unmodified lines
344 int nerrors = 0;345 struct locfile* src = NULL;346 block program;347 jv data;348 for (struct loading_lib *l = lib_state->loading; l; l = l->next) {349 if (strcmp(l->name, jv_string_value(lib_path)) == 0) {350 jq_report_error(jq, jv_string_fmt("jq: error: circular import of %s\n",351 jv_string_value(lib_path)));352 *out_block = gen_noop();353 jv_free(lib_path);354 return 1;355 }356 }357 if (is_data && !raw)358 data = jv_load_file(jv_string_value(lib_path), 0);359 else360 data = jv_load_file(jv_string_value(lib_path), 1);358 int state_idx;361 uint64_t state_idx;362 if (!jv_is_valid(data)) {363 program = gen_noop();364 if (!optional) {365 if (jv_invalid_has_msg(jv_copy(data)))
9 unmodified lines
375 state_idx = lib_state->ct++;376 lib_state->entries = jv_mem_realloc(lib_state->entries, lib_state->ct * sizeof(struct lib_entry));377 lib_state->entries[state_idx].name = strdup(jv_string_value(lib_path));378 lib_state->entries[state_idx].def = program;376 lib_state->entries[state_idx].loading = 0;379 } else {380 // import "foo" as bar;381 src = locfile_init(jq, jv_string_value(lib_path), jv_string_value(data), jv_string_length_bytes(jv_copy(data)));382 nerrors += jq_parse_library(src, &program);383 locfile_free(src);384 if (nerrors == 0) {383 // Register the library before processing its dependencies so that384 // circular imports can be detected.385 state_idx = lib_state->ct++;386 lib_state->entries = jv_mem_realloc(lib_state->entries, lib_state->ct * sizeof(struct lib_entry));387 lib_state->entries[state_idx].name = strdup(jv_string_value(lib_path));388 lib_state->entries[state_idx].def = gen_noop();389 lib_state->entries[state_idx].loading = 1;390385 // Mark the library as being loaded while its dependencies are processed,386 // so that a recursive reference to it is reported as a circular import.387 // The node lives on this frame and is popped before lib_path is freed,388 // so it can borrow the path string.389 struct loading_lib loading = { jv_string_value(lib_path), lib_state->loading };390 lib_state->loading = &loading;391 char *lib_origin = strdup(jv_string_value(lib_path));392 nerrors += process_dependencies(jq, jq_get_jq_origin(jq),393 jv_string(dirname(lib_origin)),394 &program, lib_state);395 free(lib_origin);396 lib_state->loading = loading.next;397 program = block_bind_self(program, OP_IS_CALL_PSEUDO);398 // Register the library only once its dependencies have been processed,399 // to keep lib_state->entries in dependency order. Register it even when400 // its dependencies failed, so that other importers reuse this entry401 // rather than loading and diagnosing the same library again.402 state_idx = lib_state->ct++;403 lib_state->entries = jv_mem_realloc(lib_state->entries, lib_state->ct * sizeof(struct lib_entry));404 lib_state->entries[state_idx].name = strdup(jv_string_value(lib_path));405 lib_state->entries[state_idx].def = program;398 lib_state->entries[state_idx].loading = 0;406 }407 }408 *out_block = program;409 jv_free(lib_path);
31 unmodified lines
441442int load_program(jq_state *jq, struct locfile* src, block *out_block) {443 int nerrors = 0;444 block program;438 struct lib_loading_state lib_state = {0,0};445 struct lib_loading_state lib_state = {0,0,0};446 nerrors = jq_parse(src, &program);447 if (nerrors)448 return nerrors;449
35 unmodified lines
added:tests/modules/deep1.jq+3 added
1import "deep2a" as a;2import "deep2b" as b;3def f: [1, a::f, b::f];
added:tests/modules/deep2a.jq+2 added
1import "deep3" as m;2def f: [21, m::f];
added:tests/modules/deep2b.jq+3 added
1import "deep3" as m;2import "data" as $d;3def f: [22, m::f, ($d | length)];
added:tests/modules/deep3.jq+2 added
1def _f: 3;2def f: _f;
modified:tests/jq.test+17 added
1948 unmodified lines
1949import "shadow1" as f; import "shadow2" as f; import "shadow1" as e; [e::e, f::e]1950null1951[2,3]19521953# A module reached only through another module must be linked before its1954# importers, or dead-code elimination drops the definitions that only the1955# importer's body references (#3597)1956include "deep1"; f1957null1958[1,[21,3],[22,3,1]]19591960# ... including when the shared dependency is pulled in first by an importer1961# whose own definitions end up unused, in either import order1962import "deep2a" as a; import "deep2b" as b; b::f1963null1964[22,3,1]19651966import "deep2b" as b; import "deep2a" as a; b::f1967null1968[22,3,1]19691970%%FAIL1971module (.+1); 01972jq: error: Module metadata must be constant at <top-level>, line 1, column 8:1973 module (.+1); 0
699 unmodified lines
modified:Makefile.am+2 added
222 unmodified lines
223 src/parser.h src/version.h src/builtin.jq scripts/version \224 libjq.pc \225 tests/modules/a.jq tests/modules/b/b.jq tests/modules/c/c.jq \226 tests/modules/c/d.jq tests/modules/data.json \227 tests/modules/deep1.jq tests/modules/deep2a.jq \228 tests/modules/deep2b.jq tests/modules/deep3.jq \229 tests/modules/home1/.jq tests/modules/home2/.jq/g.jq \230 tests/modules/lib/jq/e/e.jq tests/modules/lib/jq/f.jq \231 tests/modules/shadow1.jq tests/modules/shadow2.jq \232 tests/modules/syntaxerror/syntaxerror.jq \
30 unmodified lines