Skip to content

Commit

Permalink
Bind libraries backward for consistent shadowing
Browse files Browse the repository at this point in the history
  • Loading branch information
muhmuhten authored and nicowilliams committed Feb 27, 2019
1 parent 141bb78 commit abed751
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ EXTRA_DIST = $(DOC_FILES) $(man_MANS) $(TESTS) $(TEST_LOG_COMPILER) \
tests/modules/c/d.jq tests/modules/data.json \
tests/modules/home1/.jq tests/modules/home2/.jq/g.jq \
tests/modules/lib/jq/e/e.jq tests/modules/lib/jq/f.jq \
tests/modules/shadow1.jq tests/modules/shadow2.jq \
tests/modules/syntaxerror/syntaxerror.jq \
tests/modules/test_bind_order.jq \
tests/modules/test_bind_order0.jq \
Expand Down
2 changes: 1 addition & 1 deletion src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ block block_bind_library(block binder, block body, int bindflags, const char *li
matchlen += 2;
}
assert(block_has_only_binders(binder, bindflags));
for (inst *curr = binder.first; curr; curr = curr->next) {
for (inst *curr = binder.last; curr; curr = curr->prev) {
int bindflags2 = bindflags;
char* cname = curr->symbol;
char* tname = jv_mem_alloc(strlen(curr->symbol)+matchlen+1);
Expand Down
6 changes: 5 additions & 1 deletion src/linker.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ static int process_dependencies(jq_state *jq, jv jq_origin, jv lib_origin, block
block bk = *src_block;
int nerrors = 0;

jv_array_foreach(deps, i, dep) {
// XXX This is a backward jv_array_foreach because bindings go in reverse
for (int i = jv_array_length(jv_copy(deps)); i > 0; ) {
i--;
jv dep = jv_array_get(jv_copy(deps), i);

const char *as_str = NULL;
int is_data = jv_get_kind(jv_object_get(jv_copy(dep), jv_string("is_data"))) == JV_KIND_TRUE;
int raw = 0;
Expand Down
20 changes: 20 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ def f(a;b;c;d;e;f): [a+1,b,c,d,e,f]; f(.[0];.[1];.[0];.[0];.[0];.[0])
[1,2]
[2,2,1,1,1,1]

def f: 1; def g: f, def f: 2; def g: 3; f, def f: g; f, g; def f: 4; [f, def f: g; def g: 5; f, g]+[f,g]
null
[4,1,2,3,3,5,4,1,2,3,3]

# Test precedence of 'def' vs '|'
def a: 0; . | a
null
Expand Down Expand Up @@ -1464,6 +1468,18 @@ import "data" as $e; import "data" as $d; [$d[].this,$e[].that,$d::d[].this,$e::
null
"is a test;is too;is a test;is too"

include "shadow1"; e
null
2

include "shadow1"; include "shadow2"; e
null
3

import "shadow1" as f; import "shadow2" as f; import "shadow1" as e; [e::e, f::e]
null
[2,3]

%%FAIL
module (.+1); 0
jq: error: Module metadata must be constant at <top-level>, line 1:
Expand All @@ -1488,6 +1504,10 @@ modulemeta
"c"
{"whatever":null,"deps":[{"as":"foo","is_data":false,"relpath":"a"},{"search":"./","as":"d","is_data":false,"relpath":"d"},{"search":"./","as":"d2","is_data":false,"relpath":"d"},{"search":"./../lib/jq","as":"e","is_data":false,"relpath":"e"},{"search":"./../lib/jq","as":"f","is_data":false,"relpath":"f"},{"as":"d","is_data":true,"relpath":"data"}]}

modulemeta | .deps |= length
"c"
{"whatever":null,"deps":6}

%%FAIL IGNORE MSG
import "syntaxerror" as e; .
jq: error: syntax error, unexpected ';', expecting $end (Unix shell quoting issues?) at /home/nico/ws/jq/tests/modules/syntaxerror/syntaxerror.jq, line 1:
Expand Down
2 changes: 2 additions & 0 deletions tests/modules/shadow1.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def e: 1;
def e: 2;
1 change: 1 addition & 0 deletions tests/modules/shadow2.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def e: 3;
1 change: 1 addition & 0 deletions tests/modules/test_bind_order0.jq
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
def sym0: 0;
def sym1: 0;
2 changes: 1 addition & 1 deletion tests/modules/test_bind_order1.jq
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def sym0: 1;
def sym1: 1;
def sym2: 1;
1 change: 0 additions & 1 deletion tests/modules/test_bind_order2.jq
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
def sym1: 2;
def sym2: 2;

0 comments on commit abed751

Please sign in to comment.