summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMuh Muhten <muh.muhten@gmail.com>2019-02-20 23:05:29 -0500
committerNico Williams <nico@cryptonector.com>2019-02-26 21:57:08 -0600
commitabed751e9669ee716b04a8923413c4bc2734185d (patch)
treebb8740582ffe3e221c0a103390bcd15614fa2efd
parent141bb78e967f0d088b9fda134bb75e23405bd844 (diff)
Bind libraries backward for consistent shadowing
-rw-r--r--Makefile.am1
-rw-r--r--src/compile.c2
-rw-r--r--src/linker.c6
-rw-r--r--tests/jq.test20
-rw-r--r--tests/modules/shadow1.jq2
-rw-r--r--tests/modules/shadow2.jq1
-rw-r--r--tests/modules/test_bind_order0.jq1
-rw-r--r--tests/modules/test_bind_order1.jq2
-rw-r--r--tests/modules/test_bind_order2.jq1
9 files changed, 32 insertions, 4 deletions
diff --git a/Makefile.am b/Makefile.am
index 37940712..542a0eee 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -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 \
diff --git a/src/compile.c b/src/compile.c
index ad632258..dfac30d8 100644
--- a/src/compile.c
+++ b/src/compile.c
@@ -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);
diff --git a/src/linker.c b/src/linker.c
index 5611d512..962bdc14 100644
--- a/src/linker.c
+++ b/src/linker.c
@@ -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;
diff --git a/tests/jq.test b/tests/jq.test
index 28b325f9..f75039c7 100644
--- a/tests/jq.test
+++ b/tests/jq.test
@@ -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
@@ -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:
@@ -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:
diff --git a/tests/modules/shadow1.jq b/tests/modules/shadow1.jq
new file mode 100644
index 00000000..47c31eb0
--- /dev/null
+++ b/tests/modules/shadow1.jq
@@ -0,0 +1,2 @@
+def e: 1;
+def e: 2;
diff --git a/tests/modules/shadow2.jq b/tests/modules/shadow2.jq
new file mode 100644
index 00000000..61b7c007
--- /dev/null
+++ b/tests/modules/shadow2.jq
@@ -0,0 +1 @@
+def e: 3;
diff --git a/tests/modules/test_bind_order0.jq b/tests/modules/test_bind_order0.jq
index 8cb49a99..c2fc0f00 100644
--- a/tests/modules/test_bind_order0.jq
+++ b/tests/modules/test_bind_order0.jq
@@ -1 +1,2 @@
def sym0: 0;
+def sym1: 0;
diff --git a/tests/modules/test_bind_order1.jq b/tests/modules/test_bind_order1.jq
index be72b43c..0d103440 100644
--- a/tests/modules/test_bind_order1.jq
+++ b/tests/modules/test_bind_order1.jq
@@ -1,2 +1,2 @@
-def sym0: 1;
def sym1: 1;
+def sym2: 1;
diff --git a/tests/modules/test_bind_order2.jq b/tests/modules/test_bind_order2.jq
index 86f422bf..8647c514 100644
--- a/tests/modules/test_bind_order2.jq
+++ b/tests/modules/test_bind_order2.jq
@@ -1,2 +1 @@
-def sym1: 2;
def sym2: 2;