diff --git a/hogvm/__tests__/__snapshots__/arrays.hoge b/hogvm/__tests__/__snapshots__/arrays.hoge index 7f082d8fdf5..79ab98b96a5 100644 --- a/hogvm/__tests__/__snapshots__/arrays.hoge +++ b/hogvm/__tests__/__snapshots__/arrays.hoge @@ -1,21 +1,24 @@ ["_h", 43, 0, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 2, "print", 1, 35, 33, 1, 32, "2", 33, 3, 43, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 2, 33, 4, 43, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, -33, 5, 43, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 36, 0, 33, 1, 45, 2, "print", 1, 35, 36, 0, 33, 1, 48, 2, -"print", 1, 35, 36, 0, 33, 1, 48, 2, "print", 1, 35, 36, 0, 33, 6, 48, 2, "print", 1, 35, 36, 0, 33, 6, 48, 2, "print", -1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, -33, 1, 45, 33, 1, 45, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 1, 48, -33, 1, 48, 33, 1, 48, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 1, 48, 33, 1, 48, -33, 1, 48, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 6, 48, 33, 3, 48, 33, 1, 48, -2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 6, 48, 33, 3, 48, 33, 1, 48, 2, "print", -1, 35, 33, 1, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 1, 45, 33, 1, 45, 33, 1, 45, 6, 2, "print", 1, -35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 1, 45, 33, 1, 45, 33, 1, 45, 2, "print", 1, 35, 32, -"------", 2, "print", 1, 35, 33, 4, 33, 1, 33, 2, 33, 3, 43, 3, 2, "arrayPushBack", 2, 2, "print", 1, 35, 33, 0, 33, 1, -33, 2, 33, 3, 43, 3, 2, "arrayPushFront", 2, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 2, "arrayPopBack", 1, 2, -"print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 2, "arrayPopFront", 1, 2, "print", 1, 35, 33, 3, 33, 2, 33, 1, 43, 3, 2, -"arraySort", 1, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 2, "arrayReverse", 1, 2, "print", 1, 35, 33, 3, 33, 2, -33, 1, 43, 3, 2, "arrayReverseSort", 1, 2, "print", 1, 35, 32, ",", 33, 1, 33, 2, 33, 3, 43, 3, 2, "arrayStringConcat", -2, 2, "print", 1, 35, 32, "-----", 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 4, 36, 1, 2, "print", 1, 35, 33, -5, 36, 1, 2, "arrayPushBack", 2, 35, 36, 1, 2, "print", 1, 35, 33, 0, 36, 1, 2, "arrayPushFront", 2, 35, 36, 1, 2, -"print", 1, 35, 36, 1, 2, "arrayPopBack", 1, 35, 36, 1, 2, "print", 1, 35, 36, 1, 2, "arrayPopFront", 1, 35, 36, 1, 2, -"print", 1, 35, 36, 1, 2, "arraySort", 1, 35, 36, 1, 2, "print", 1, 35, 36, 1, 2, "arrayReverse", 1, 35, 36, 1, 2, -"print", 1, 35, 36, 1, 2, "arrayReverseSort", 1, 35, 36, 1, 2, "print", 1, 35, 35, 35] +33, 5, 43, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 36, 0, 33, 2, 45, 2, "print", 1, 35, 36, 0, 33, 2, 48, 2, +"print", 1, 35, 36, 0, 33, 2, 48, 2, "print", 1, 35, 36, 0, 33, 7, 48, 2, "print", 1, 35, 36, 0, 33, 7, 48, 2, "print", +1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 33, 2, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, +33, 2, 45, 33, 2, 45, 33, 2, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 2, 48, +33, 2, 48, 33, 2, 48, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 2, 48, 33, 2, 48, +33, 2, 48, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 7, 48, 33, 4, 48, 33, 2, 48, +2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 7, 48, 33, 4, 48, 33, 2, 48, 2, "print", +1, 35, 33, 1, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 2, 45, 33, 2, 45, 33, 2, 45, 6, 2, "print", 1, +35, 33, 1, 33, 2, 33, 3, 33, 4, 43, 2, 43, 2, 33, 5, 43, 3, 33, 2, 45, 33, 2, 45, 33, 2, 45, 2, "print", 1, 35, 32, +"------", 2, "print", 1, 35, 33, 1, 33, 2, 33, 1, 33, 2, 33, 3, 43, 3, 43, 3, 36, 1, 33, 2, 33, 4, 46, 36, 1, 33, 1, 45, +2, "print", 1, 35, 36, 1, 33, 2, 45, 2, "print", 1, 35, 36, 1, 33, 3, 45, 2, "print", 1, 35, 36, 1, 33, 3, 45, 33, 3, +33, 8, 46, 36, 1, 2, "print", 1, 35, 32, "------", 2, "print", 1, 35, 33, 4, 33, 1, 33, 2, 33, 3, 43, 3, 2, +"arrayPushBack", 2, 2, "print", 1, 35, 33, 0, 33, 1, 33, 2, 33, 3, 43, 3, 2, "arrayPushFront", 2, 2, "print", 1, 35, 33, +1, 33, 2, 33, 3, 43, 3, 2, "arrayPopBack", 1, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 2, "arrayPopFront", 1, 2, +"print", 1, 35, 33, 3, 33, 2, 33, 1, 43, 3, 2, "arraySort", 1, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 2, +"arrayReverse", 1, 2, "print", 1, 35, 33, 3, 33, 2, 33, 1, 43, 3, 2, "arrayReverseSort", 1, 2, "print", 1, 35, 32, ",", +33, 1, 33, 2, 33, 3, 43, 3, 2, "arrayStringConcat", 2, 2, "print", 1, 35, 32, "-----", 2, "print", 1, 35, 33, 1, 33, 2, +33, 3, 33, 4, 43, 4, 36, 2, 2, "print", 1, 35, 33, 5, 36, 2, 2, "arrayPushBack", 2, 35, 36, 2, 2, "print", 1, 35, 33, 0, +36, 2, 2, "arrayPushFront", 2, 35, 36, 2, 2, "print", 1, 35, 36, 2, 2, "arrayPopBack", 1, 35, 36, 2, 2, "print", 1, 35, +36, 2, 2, "arrayPopFront", 1, 35, 36, 2, 2, "print", 1, 35, 36, 2, 2, "arraySort", 1, 35, 36, 2, 2, "print", 1, 35, 36, +2, 2, "arrayReverse", 1, 35, 36, 2, 2, "print", 1, 35, 36, 2, 2, "arrayReverseSort", 1, 35, 36, 2, 2, "print", 1, 35, +35, 35, 35] diff --git a/hogvm/__tests__/__snapshots__/arrays.stdout b/hogvm/__tests__/__snapshots__/arrays.stdout index bac9eea0c6d..2476ff3f01f 100644 --- a/hogvm/__tests__/__snapshots__/arrays.stdout +++ b/hogvm/__tests__/__snapshots__/arrays.stdout @@ -17,6 +17,11 @@ null 5 4 ------ +1 +4 +[1, 2, 3] +[1, 4, [1, 2, 8]] +------ [1, 2, 3, 4] [0, 1, 2, 3] [1, 2] diff --git a/hogvm/__tests__/__snapshots__/loops.hoge b/hogvm/__tests__/__snapshots__/loops.hoge index 349f7f4cbb3..a3328220d6b 100644 --- a/hogvm/__tests__/__snapshots__/loops.hoge +++ b/hogvm/__tests__/__snapshots__/loops.hoge @@ -3,20 +3,20 @@ 36, 0, 15, 40, 15, 36, 0, 2, "print", 1, 35, 33, 1, 36, 0, 6, 37, 0, 39, -22, 35, 32, "i", 1, 1, 2, "print", 1, 35, 32, "-- test emptier for loop --", 2, "print", 1, 35, 33, 0, 33, 3, 36, 0, 15, 40, 15, 32, "woo", 2, "print", 1, 35, 33, 1, 36, 0, 6, 37, 0, 39, -22, 32, "hoo", 2, "print", 1, 35, 35, 32, "-- for in loop with arrays --", 2, "print", 1, 35, 33, -1, 33, 2, 33, 3, 43, 3, 36, 0, 36, 1, 2, "values", 1, 33, 0, 36, 2, 2, "length", 1, 31, 36, 4, 36, 3, 15, 40, 22, 36, 2, +1, 33, 2, 33, 3, 43, 3, 36, 0, 36, 1, 2, "values", 1, 33, 1, 36, 2, 2, "length", 1, 31, 36, 4, 36, 3, 16, 40, 22, 36, 2, 36, 3, 45, 37, 5, 36, 5, 2, "print", 1, 35, 36, 3, 33, 1, 6, 37, 3, 39, -29, 35, 35, 35, 35, 35, 35, 32, "-- for in loop with arrays and keys --", 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 43, 3, 36, 0, 36, 1, 2, "keys", 1, 36, -1, 2, "values", 1, 33, 0, 36, 3, 2, "length", 1, 31, 31, 36, 5, 36, 4, 15, 40, 31, 36, 2, 36, 4, 45, 37, 6, 36, 3, 36, +1, 2, "values", 1, 33, 1, 36, 3, 2, "length", 1, 31, 31, 36, 5, 36, 4, 16, 40, 31, 36, 2, 36, 4, 45, 37, 6, 36, 3, 36, 4, 45, 37, 7, 36, 7, 36, 6, 2, "print", 2, 35, 36, 4, 33, 1, 6, 37, 4, 39, -38, 35, 35, 35, 35, 35, 35, 35, 35, 32, -"-- for in loop with tuples --", 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 3, 36, 0, 36, 1, 2, "values", 1, 33, 0, 36, -2, 2, "length", 1, 31, 36, 4, 36, 3, 15, 40, 22, 36, 2, 36, 3, 45, 37, 5, 36, 5, 2, "print", 1, 35, 36, 3, 33, 1, 6, 37, +"-- for in loop with tuples --", 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 3, 36, 0, 36, 1, 2, "values", 1, 33, 1, 36, +2, 2, "length", 1, 31, 36, 4, 36, 3, 16, 40, 22, 36, 2, 36, 3, 45, 37, 5, 36, 5, 2, "print", 1, 35, 36, 3, 33, 1, 6, 37, 3, 39, -29, 35, 35, 35, 35, 35, 35, 32, "-- for in loop with tuples and keys --", 2, "print", 1, 35, 33, 1, 33, 2, 33, -3, 44, 3, 36, 0, 36, 1, 2, "keys", 1, 36, 1, 2, "values", 1, 33, 0, 36, 3, 2, "length", 1, 31, 31, 36, 5, 36, 4, 15, 40, +3, 44, 3, 36, 0, 36, 1, 2, "keys", 1, 36, 1, 2, "values", 1, 33, 1, 36, 3, 2, "length", 1, 31, 31, 36, 5, 36, 4, 16, 40, 31, 36, 2, 36, 4, 45, 37, 6, 36, 3, 36, 4, 45, 37, 7, 36, 7, 36, 6, 2, "print", 2, 35, 36, 4, 33, 1, 6, 37, 4, 39, -38, 35, 35, 35, 35, 35, 35, 35, 35, 32, "-- for in loop with dicts --", 2, "print", 1, 35, 32, "first", 32, "v1", 32, -"second", 32, "v2", 32, "third", 32, "v3", 42, 3, 36, 0, 36, 1, 2, "values", 1, 33, 0, 36, 2, 2, "length", 1, 31, 36, 4, -36, 3, 15, 40, 22, 36, 2, 36, 3, 45, 37, 5, 36, 5, 2, "print", 1, 35, 36, 3, 33, 1, 6, 37, 3, 39, -29, 35, 35, 35, 35, +"second", 32, "v2", 32, "third", 32, "v3", 42, 3, 36, 0, 36, 1, 2, "values", 1, 33, 1, 36, 2, 2, "length", 1, 31, 36, 4, +36, 3, 16, 40, 22, 36, 2, 36, 3, 45, 37, 5, 36, 5, 2, "print", 1, 35, 36, 3, 33, 1, 6, 37, 3, 39, -29, 35, 35, 35, 35, 35, 35, 32, "-- for in loop with dicts and keys --", 2, "print", 1, 35, 32, "first", 32, "v1", 32, "second", 32, "v2", -32, "third", 32, "v3", 42, 3, 36, 0, 36, 1, 2, "keys", 1, 36, 1, 2, "values", 1, 33, 0, 36, 3, 2, "length", 1, 31, 31, -36, 5, 36, 4, 15, 40, 31, 36, 2, 36, 4, 45, 37, 6, 36, 3, 36, 4, 45, 37, 7, 36, 7, 36, 6, 2, "print", 2, 35, 36, 4, 33, +32, "third", 32, "v3", 42, 3, 36, 0, 36, 1, 2, "keys", 1, 36, 1, 2, "values", 1, 33, 1, 36, 3, 2, "length", 1, 31, 31, +36, 5, 36, 4, 16, 40, 31, 36, 2, 36, 4, 45, 37, 6, 36, 3, 36, 4, 45, 37, 7, 36, 7, 36, 6, 2, "print", 2, 35, 36, 4, 33, 1, 6, 37, 4, 39, -38, 35, 35, 35, 35, 35, 35, 35, 35] diff --git a/hogvm/__tests__/__snapshots__/properties.hoge b/hogvm/__tests__/__snapshots__/properties.hoge index c681c4a68f7..39ec0648c40 100644 --- a/hogvm/__tests__/__snapshots__/properties.hoge +++ b/hogvm/__tests__/__snapshots__/properties.hoge @@ -1,17 +1,17 @@ -["_h", 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 6, 44, 4, 42, 1, 43, 3, 36, 0, 33, 2, 45, 32, "d", 45, 33, 1, -45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 6, 44, 4, 42, 1, 43, 3, 36, 0, 33, 2, 45, -32, "d", 45, 33, 2, 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 6, 44, 4, 42, 1, 43, 3, -36, 0, 33, 2, 45, 32, "d", 45, 33, 3, 45, 2, "print", 1, 35, 35, 32, "d", 33, 1, 33, 3, 33, 42, 33, 6, 44, 4, 42, 1, 36, -0, 32, "d", 45, 33, 1, 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, -3, 36, 0, 33, 2, 45, 32, "d", 45, 33, 2, 33, 3, 46, 36, 0, 33, 2, 45, 32, "d", 45, 33, 2, 45, 2, "print", 1, 35, 35, 33, -1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, 3, 36, 0, 33, 2, 45, 32, "d", 45, 33, 2, 33, 3, 46, -36, 0, 33, 2, 45, 32, "d", 45, 33, 2, 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, -4, 42, 1, 43, 3, 36, 0, 33, 2, 45, 32, "c", 33, 666, 43, 1, 46, 36, 0, 33, 2, 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, -32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, 3, 36, 0, 33, 2, 45, 32, "d", 45, 33, 2, 33, 3, 46, 36, 0, 33, -2, 45, 32, "d", 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, 3, 36, -0, 33, 2, 45, 32, "d", 32, "a", 32, "b", 32, "c", 32, "d", 43, 4, 46, 36, 0, 33, 2, 45, 32, "d", 45, 33, 2, 45, 2, -"print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, 3, 32, "d", 36, 0, 33, 2, 45, -36, 1, 32, "a", 32, "b", 32, "c", 32, "d", 43, 4, 46, 36, 0, 33, 2, 45, 32, "d", 45, 33, 2, 45, 2, "print", 1, 35, 35, +["_h", 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 6, 44, 4, 42, 1, 43, 3, 36, 0, 33, 3, 45, 32, "d", 45, 33, 2, +45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 6, 44, 4, 42, 1, 43, 3, 36, 0, 33, 3, 45, +32, "d", 45, 33, 3, 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 6, 44, 4, 42, 1, 43, 3, +36, 0, 33, 3, 45, 32, "d", 45, 33, 4, 45, 2, "print", 1, 35, 35, 32, "d", 33, 1, 33, 3, 33, 42, 33, 6, 44, 4, 42, 1, 36, +0, 32, "d", 45, 33, 2, 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, +3, 36, 0, 33, 3, 45, 32, "d", 45, 33, 3, 33, 3, 46, 36, 0, 33, 3, 45, 32, "d", 45, 33, 3, 45, 2, "print", 1, 35, 35, 33, +1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, 3, 36, 0, 33, 3, 45, 32, "d", 45, 33, 3, 33, 3, 46, +36, 0, 33, 3, 45, 32, "d", 45, 33, 3, 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, +4, 42, 1, 43, 3, 36, 0, 33, 3, 45, 32, "c", 33, 666, 43, 1, 46, 36, 0, 33, 3, 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, +32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, 3, 36, 0, 33, 3, 45, 32, "d", 45, 33, 3, 33, 3, 46, 36, 0, 33, +3, 45, 32, "d", 45, 2, "print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, 3, 36, +0, 33, 3, 45, 32, "d", 32, "a", 32, "b", 32, "c", 32, "d", 43, 4, 46, 36, 0, 33, 3, 45, 32, "d", 45, 33, 3, 45, 2, +"print", 1, 35, 35, 33, 1, 33, 2, 32, "d", 33, 1, 33, 3, 33, 42, 33, 3, 43, 4, 42, 1, 43, 3, 32, "d", 36, 0, 33, 3, 45, +36, 1, 32, "a", 32, "b", 32, "c", 32, "d", 43, 4, 46, 36, 0, 33, 3, 45, 32, "d", 45, 33, 3, 45, 2, "print", 1, 35, 35, 35, 32, "event", 32, "$pageview", 32, "properties", 32, "$browser", 32, "Chrome", 32, "$os", 32, "Windows", 42, 2, 42, 2, 36, 0, 32, "properties", 45, 32, "$browser", 32, "Firefox", 46, 36, 0, 2, "print", 1, 35, 35, 32, "event", 32, "$pageview", 32, "properties", 32, "$browser", 32, "Chrome", 32, "$os", 32, "Windows", 42, 2, 42, 2, 36, 0, 32, diff --git a/hogvm/__tests__/__snapshots__/tuples.hoge b/hogvm/__tests__/__snapshots__/tuples.hoge index 94ca85f4381..027d8ad2663 100644 --- a/hogvm/__tests__/__snapshots__/tuples.hoge +++ b/hogvm/__tests__/__snapshots__/tuples.hoge @@ -1,11 +1,11 @@ ["_h", 2, "tuple", 0, 2, "print", 1, 35, 33, 1, 44, 1, 2, "print", 1, 35, 33, 1, 33, 2, 44, 2, 2, "print", 1, 35, 33, 1, 33, 2, 44, 2, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 3, 2, "print", 1, 35, 33, 1, 32, "2", 33, 3, 44, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 2, 33, 4, 44, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, -33, 5, 44, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 3, 36, 0, 33, 1, 45, 2, "print", 1, 35, 36, 0, 33, 1, 48, 2, -"print", 1, 35, 36, 0, 33, 7, 48, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 1, 45, -33, 1, 45, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 1, 48, 33, 1, 48, -33, 1, 48, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 1, 48, 33, 1, 48, 33, 1, 48, -2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 3, 48, 33, 6, 48, 33, 1, 48, 2, "print", -1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 3, 48, 33, 6, 48, 33, 1, 48, 2, "print", 1, 35, 33, -1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 1, 45, 33, 1, 45, 33, 1, 45, 2, "print", 1, 35, 33, 1, 33, 1, -33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 1, 45, 33, 1, 45, 33, 1, 45, 6, 2, "print", 1, 35, 35] +33, 5, 44, 3, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 44, 3, 36, 0, 33, 2, 45, 2, "print", 1, 35, 36, 0, 33, 2, 48, 2, +"print", 1, 35, 36, 0, 33, 8, 48, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 2, 45, +33, 2, 45, 33, 2, 45, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 2, 48, 33, 2, 48, +33, 2, 48, 2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 2, 48, 33, 2, 48, 33, 2, 48, +2, "print", 1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 4, 48, 33, 7, 48, 33, 2, 48, 2, "print", +1, 35, 33, 1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 4, 48, 33, 7, 48, 33, 2, 48, 2, "print", 1, 35, 33, +1, 33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 2, 45, 33, 2, 45, 33, 2, 45, 2, "print", 1, 35, 33, 1, 33, 1, +33, 2, 33, 3, 33, 4, 44, 2, 44, 2, 33, 5, 44, 3, 33, 2, 45, 33, 2, 45, 33, 2, 45, 6, 2, "print", 1, 35, 35] diff --git a/hogvm/__tests__/arrays.hog b/hogvm/__tests__/arrays.hog index 04cc57153ba..c65f68eabd8 100644 --- a/hogvm/__tests__/arrays.hog +++ b/hogvm/__tests__/arrays.hog @@ -5,19 +5,28 @@ print([1, [2, 3], 4]) print([1, [2, [3, 4]], 5]) let a := [1, 2, 3] -print(a[1]) -print(a?.[1]) -print(a?.1) -print(a?.[6]) -print(a?.6) -print([1, 2, 3][1], ) -print([1, [2, [3, 4], ], 5][1][1][1]) -print([1, [2, [3, 4], ], 5]?.1?.1?.1) -print([1, [2, [3, 4], ], 5]?.[1]?.[1]?.[1]) -print([1, [2, [3, 4], ], 5]?.6?.3?.1) -print([1, [2, [3, 4], ], 5]?.[6]?.[3]?.[1]) -print([1, [2, [3, 4]], 5][1][1][1] + 1) -print([1, [2, [3, 4, ], ], 5, ].1.1.1) +print(a[2]) +print(a?.[2]) +print(a?.2) +print(a?.[7]) +print(a?.7) +print([1, 2, 3][2], ) +print([1, [2, [3, 4], ], 5][2][2][2]) +print([1, [2, [3, 4], ], 5]?.2?.2?.2) +print([1, [2, [3, 4], ], 5]?.[2]?.[2]?.[2]) +print([1, [2, [3, 4], ], 5]?.7?.4?.2) +print([1, [2, [3, 4], ], 5]?.[7]?.[4]?.[2]) +print([1, [2, [3, 4]], 5][2][2][2] + 1) +print([1, [2, [3, 4, ], ], 5, ].2.2.2) + +print('------') +let b := [1, 2, [1,2,3]] +b[2] := 4 +print(b[1]) +print(b[2]) +print(b[3]) +b[3][3] := 8 +print(b) print('------') diff --git a/hogvm/__tests__/properties.hog b/hogvm/__tests__/properties.hog index c3dde6b2748..d882d25b55a 100644 --- a/hogvm/__tests__/properties.hog +++ b/hogvm/__tests__/properties.hog @@ -1,49 +1,49 @@ { let r := [1, 2, {'d': (1, 3, 42, 6)}] - print(r.2.d.1) + print(r.3.d.2) } { let r := [1, 2, {'d': (1, 3, 42, 6)}] - print(r[2].d[2]) + print(r[3].d[3]) } { let r := [1, 2, {'d': (1, 3, 42, 6)}] - print(r.2['d'][3]) + print(r.3['d'][4]) } { let r := {'d': (1, 3, 42, 6)} - print(r.d.1) + print(r.d.2) } { let r := [1, 2, {'d': [1, 3, 42, 3]}] - r.2.d.2 := 3 - print(r.2.d.2) + r.3.d.3 := 3 + print(r.3.d.3) } { let r := [1, 2, {'d': [1, 3, 42, 3]}] - r[2].d[2] := 3 - print(r[2].d[2]) + r[3].d[3] := 3 + print(r[3].d[3]) } { let r := [1, 2, {'d': [1, 3, 42, 3]}] - r[2].c := [666] - print(r[2]) + r[3].c := [666] + print(r[3]) } { let r := [1, 2, {'d': [1, 3, 42, 3]}] - r[2].d[2] := 3 - print(r[2].d) + r[3].d[3] := 3 + print(r[3].d) } { let r := [1, 2, {'d': [1, 3, 42, 3]}] - r.2['d'] := ['a', 'b', 'c', 'd'] - print(r[2].d[2]) + r.3['d'] := ['a', 'b', 'c', 'd'] + print(r[3].d[3]) } { let r := [1, 2, {'d': [1, 3, 42, 3]}] let g := 'd' - r.2[g] := ['a', 'b', 'c', 'd'] - print(r[2].d[2]) + r.3[g] := ['a', 'b', 'c', 'd'] + print(r[3].d[3]) } { let event := { diff --git a/hogvm/__tests__/tuples.hog b/hogvm/__tests__/tuples.hog index 169cb0b1f06..55c3495737e 100644 --- a/hogvm/__tests__/tuples.hog +++ b/hogvm/__tests__/tuples.hog @@ -7,13 +7,13 @@ print((1, '2', 3)) print((1, (2, 3), 4)) print((1, (2, (3, 4)), 5)) let a := (1, 2, 3) -print(a[1]) -print(a?.1) -print(a?.7) -print((1, (2, (3, 4)), 5)[1][1][1]) -print((1, (2, (3, 4)), 5)?.1?.1?.1) -print((1, (2, (3, 4)), 5)?.[1]?.[1]?.[1]) -print((1, (2, (3, 4)), 5)?.3?.6?.1) -print((1, (2, (3, 4)), 5)?.[3]?.[6]?.[1]) -print((1, (2, (3, 4, ), ), 5, ).1.1.1) -print((1, (2, (3, 4)), 5)[1][1][1] + 1) +print(a[2]) +print(a?.2) +print(a?.8) +print((1, (2, (3, 4)), 5)[2][2][2]) +print((1, (2, (3, 4)), 5)?.2?.2?.2) +print((1, (2, (3, 4)), 5)?.[2]?.[2]?.[2]) +print((1, (2, (3, 4)), 5)?.4?.7?.2) +print((1, (2, (3, 4)), 5)?.[4]?.[7]?.[2]) +print((1, (2, (3, 4, ), ), 5, ).2.2.2) +print((1, (2, (3, 4)), 5)[2][2][2] + 1) diff --git a/hogvm/asd.hog b/hogvm/asd.hog new file mode 100644 index 00000000000..46d0997010f --- /dev/null +++ b/hogvm/asd.hog @@ -0,0 +1 @@ +return [1, 2, 3][0] diff --git a/hogvm/asd.hoge b/hogvm/asd.hoge new file mode 100644 index 00000000000..a62fd0c1401 --- /dev/null +++ b/hogvm/asd.hoge @@ -0,0 +1 @@ +["_h", 33, 1, 33, 2, 33, 3, 43, 3, 33, 0, 45, 38] diff --git a/hogvm/python/test/test_execute.py b/hogvm/python/test/test_execute.py index 4d7814439aa..f4e3d31a096 100644 --- a/hogvm/python/test/test_execute.py +++ b/hogvm/python/test/test_execute.py @@ -106,10 +106,10 @@ class TestBytecodeExecute: chain: list[str] = ["properties", "bla"] assert get_nested_value(my_dict, chain) == "hello" - chain = ["properties", "list", 1] + chain = ["properties", "list", 2] assert get_nested_value(my_dict, chain) == "item2" - chain = ["properties", "tuple", 2] + chain = ["properties", "tuple", 3] assert get_nested_value(my_dict, chain) == "item3" def test_errors(self): @@ -634,11 +634,18 @@ class TestBytecodeExecute: assert self._run_program("return [1, [2, 3], 4];") == [1, [2, 3], 4] assert self._run_program("return [1, [2, [3, 4]], 5];") == [1, [2, [3, 4]], 5] - assert self._run_program("let a := [1, 2, 3]; return a[1];") == 2 - assert self._run_program("return [1, 2, 3][1];") == 2 - assert self._run_program("return [1, [2, [3, 4]], 5][1][1][1];") == 4 - assert self._run_program("return [1, [2, [3, 4]], 5][1][1][1] + 1;") == 5 - assert self._run_program("return [1, [2, [3, 4]], 5].1.1.1;") == 4 + assert self._run_program("let a := [1, 2, 3]; return a[2];") == 2 + assert self._run_program("return [1, 2, 3][2];") == 2 + assert self._run_program("return [1, [2, [3, 4]], 5][2][2][2];") == 4 + assert self._run_program("return [1, [2, [3, 4]], 5][2][2][2] + 1;") == 5 + assert self._run_program("return [1, [2, [3, 4]], 5].2.2.2;") == 4 + + try: + self._run_program("return [1, 2, 3][0]") + except Exception as e: + assert str(e) == "Hog arrays start from index 1" + else: + raise AssertionError("Expected Exception not raised") def test_bytecode_tuples(self): # assert self._run_program("return (,);"), () @@ -646,24 +653,24 @@ class TestBytecodeExecute: assert self._run_program("return (1, '2', 3);") == (1, "2", 3) assert self._run_program("return (1, (2, 3), 4);") == (1, (2, 3), 4) assert self._run_program("return (1, (2, (3, 4)), 5);") == (1, (2, (3, 4)), 5) - assert self._run_program("let a := (1, 2, 3); return a[1];") == 2 - assert self._run_program("return (1, (2, (3, 4)), 5)[1][1][1];") == 4 - assert self._run_program("return (1, (2, (3, 4)), 5).1.1.1;") == 4 - assert self._run_program("return (1, (2, (3, 4)), 5)[1][1][1] + 1;") == 5 + assert self._run_program("let a := (1, 2, 3); return a[2];") == 2 + assert self._run_program("return (1, (2, (3, 4)), 5)[2][2][2];") == 4 + assert self._run_program("return (1, (2, (3, 4)), 5).2.2.2;") == 4 + assert self._run_program("return (1, (2, (3, 4)), 5)[2][2][2] + 1;") == 5 def test_bytecode_nested(self): - assert self._run_program("let r := [1, 2, {'d': (1, 3, 42, 6)}]; return r.2.d.1;") == 3 - assert self._run_program("let r := [1, 2, {'d': (1, 3, 42, 6)}]; return r[2].d[2];") == 42 - assert self._run_program("let r := [1, 2, {'d': (1, 3, 42, 6)}]; return r.2['d'][3];") == 6 - assert self._run_program("let r := {'d': (1, 3, 42, 6)}; return r.d.1;") == 3 + assert self._run_program("let r := [1, 2, {'d': (1, 3, 42, 6)}]; return r.3.d.2;") == 3 + assert self._run_program("let r := [1, 2, {'d': (1, 3, 42, 6)}]; return r[3].d[3];") == 42 + assert self._run_program("let r := [1, 2, {'d': (1, 3, 42, 6)}]; return r.3['d'][4];") == 6 + assert self._run_program("let r := {'d': (1, 3, 42, 6)}; return r.d.2;") == 3 def test_bytecode_nested_modify(self): assert ( self._run_program( """ let r := [1, 2, {'d': [1, 3, 42, 3]}]; - r.2.d.2 := 3; - return r.2.d.2; + r.3.d.3 := 3; + return r.3.d.3; """ ) == 3 @@ -673,8 +680,8 @@ class TestBytecodeExecute: self._run_program( """ let r := [1, 2, {'d': [1, 3, 42, 3]}]; - r[2].d[2] := 3; - return r[2].d[2]; + r[3].d[3] := 3; + return r[3].d[3]; """ ) == 3 @@ -683,16 +690,16 @@ class TestBytecodeExecute: assert self._run_program( """ let r := [1, 2, {'d': [1, 3, 42, 3]}]; - r[2].c := [666]; - return r[2]; + r[3].c := [666]; + return r[3]; """ ) == {"d": [1, 3, 42, 3], "c": [666]} assert self._run_program( """ let r := [1, 2, {'d': [1, 3, 42, 3]}]; - r[2].d[2] := 3; - return r[2].d; + r[3].d[3] := 3; + return r[3].d; """ ) == [1, 3, 3, 3] @@ -700,8 +707,8 @@ class TestBytecodeExecute: self._run_program( """ let r := [1, 2, {'d': [1, 3, 42, 3]}]; - r.2['d'] := ['a', 'b', 'c', 'd']; - return r[2].d[2]; + r.3['d'] := ['a', 'b', 'c', 'd']; + return r[3].d[3]; """ ) == "c" @@ -712,8 +719,8 @@ class TestBytecodeExecute: """ let r := [1, 2, {'d': [1, 3, 42, 3]}]; let g := 'd'; - r.2[g] := ['a', 'b', 'c', 'd']; - return r[2].d[2]; + r.3[g] := ['a', 'b', 'c', 'd']; + return r[3].d[3]; """ ) == "c" diff --git a/hogvm/python/utils.py b/hogvm/python/utils.py index 5fd7e35b806..15e9eda4d5b 100644 --- a/hogvm/python/utils.py +++ b/hogvm/python/utils.py @@ -22,9 +22,11 @@ def get_nested_value(obj, chain, nullish=False) -> Any: if nullish and obj is None: return None if isinstance(key, int): - if nullish and len(obj) <= key: + if key <= 0: + raise HogVMException(f"Hog arrays start from index 1") + if nullish and key > len(obj): return None - obj = obj[key] + obj = obj[key - 1] else: obj = obj.get(key, None) return obj @@ -44,7 +46,9 @@ def set_nested_value(obj, chain, value) -> Any: elif isinstance(obj, list): if not isinstance(chain[-1], int): raise HogVMException(f"Invalid index: {chain[-1]}") - obj[chain[-1]] = value + if chain[-1] <= 0: + raise HogVMException(f"Hog arrays start from index 1") + obj[chain[-1] - 1] = value else: raise HogVMException(f'Can not set property "{chain[-1]}" on object of type "{type(obj).__name__}"') diff --git a/hogvm/typescript/src/__tests__/execute.test.ts b/hogvm/typescript/src/__tests__/execute.test.ts index ed836decc27..f8f0f99b285 100644 --- a/hogvm/typescript/src/__tests__/execute.test.ts +++ b/hogvm/typescript/src/__tests__/execute.test.ts @@ -713,7 +713,7 @@ describe('hogvm execute', () => { ]).result ).toEqual([1, [2, [3, 4]], 5]) - // var a := [1, 2, 3]; return a[1]; + // var a := [1, 2, 3]; return a[2]; expect( exec([ '_h', @@ -728,14 +728,14 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, op.POP, ]).result ).toEqual(2) - // return [1, 2, 3][1]; + // return [1, 2, 3][2]; expect( exec([ '_h', @@ -748,13 +748,13 @@ describe('hogvm execute', () => { op.ARRAY, 3, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, ]).result ).toEqual(2) - // return [1, [2, [3, 4]], 5][1][1][1]; + // return [1, [2, [3, 4]], 5][2][2][2]; expect( exec([ '_h', @@ -775,19 +775,19 @@ describe('hogvm execute', () => { op.ARRAY, 3, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, ]).result ).toEqual(4) - // return [1, [2, [3, 4]], 5][1][1][1] + 1; + // return [1, [2, [3, 4]], 5][2][2][2] + 1; expect( exec([ '_h', @@ -810,20 +810,20 @@ describe('hogvm execute', () => { op.ARRAY, 3, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.PLUS, op.RETURN, ]).result ).toEqual(5) - // return [1, [2, [3, 4]], 5].1.1.1; + // return [1, [2, [3, 4]], 5].2.2.2; expect( exec([ '_h', @@ -844,17 +844,22 @@ describe('hogvm execute', () => { op.ARRAY, 3, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, ]).result ).toEqual(4) + + // return [1, 2, 3][0] + expect(() => execSync(['_h', 33, 1, 33, 2, 33, 3, 43, 3, 33, 0, 45, 38])).toThrow( + 'Hog arrays start from index 1' + ) }) test('test bytecode tuples', () => { @@ -912,7 +917,7 @@ describe('hogvm execute', () => { ]).result ).toEqual(tuple([1, tuple([2, tuple([3, 4])]), 5])) - // var a := (1, 2, 3); return a[1]; + // var a := (1, 2, 3); return a[2]; expect( exec([ '_h', @@ -927,14 +932,14 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, op.POP, ]).result ).toEqual(2) - // return (1, (2, (3, 4)), 5)[1][1][1]; + // return (1, (2, (3, 4)), 5)[2][2][2]; expect( exec([ '_h', @@ -955,19 +960,19 @@ describe('hogvm execute', () => { op.TUPLE, 3, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, ]).result ).toEqual(4) - // return (1, (2, (3, 4)), 5).1.1.1; + // return (1, (2, (3, 4)), 5).2.2.2; expect( exec([ '_h', @@ -988,19 +993,19 @@ describe('hogvm execute', () => { op.TUPLE, 3, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, ]).result ).toEqual(4) - // return (1, (2, (3, 4)), 5)[1][1][1] + 1; + // return (1, (2, (3, 4)), 5)[2][2][2] + 1; expect( exec([ '_h', @@ -1023,13 +1028,13 @@ describe('hogvm execute', () => { op.TUPLE, 3, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.PLUS, op.RETURN, @@ -1038,7 +1043,7 @@ describe('hogvm execute', () => { }) test('test bytecode nested', () => { - // var r := [1, 2, {'d': (1, 3, 42, 6)}]; return r.2.d.1; + // var r := [1, 2, {'d': (1, 3, 42, 6)}]; return r.3.d.2; expect( exec([ '_h', @@ -1065,20 +1070,20 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, op.POP, ]).result ).toEqual(3) - // var r := [1, 2, {'d': (1, 3, 42, 6)}]; return r[2].d[2]; + // var r := [1, 2, {'d': (1, 3, 42, 6)}]; return r[3].d[3]; expect( exec([ '_h', @@ -1105,20 +1110,20 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.RETURN, op.POP, ]).result ).toEqual(42) - // var r := [1, 2, {'d': (1, 3, 42, 6)}]; return r.2['d'][3]; + // var r := [1, 2, {'d': (1, 3, 42, 6)}]; return r.3['d'][4]; expect( exec([ '_h', @@ -1145,20 +1150,20 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 3, + 4, op.GET_PROPERTY, op.RETURN, op.POP, ]).result ).toEqual(6) - // var r := {'d': (1, 3, 42, 6)}; return r.d.1; + // var r := {'d': (1, 3, 42, 6)}; return r.d.2; expect( exec([ '_h', @@ -1182,7 +1187,7 @@ describe('hogvm execute', () => { 'd', op.GET_PROPERTY, op.INTEGER, - 1, + 2, op.GET_PROPERTY, op.RETURN, op.POP, @@ -1192,8 +1197,8 @@ describe('hogvm execute', () => { test('test bytecode nested modify', () => { // var r := [1, 2, {'d': [1, 3, 42, 3]}]; - // r.2.d.2 := 3; - // return r.2.d.2; + // r.3.d.3 := 3; + // return r.3.d.3; expect( exec([ '_h', @@ -1220,26 +1225,26 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 2, + 3, op.INTEGER, 3, op.SET_PROPERTY, op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.RETURN, op.POP, @@ -1247,8 +1252,8 @@ describe('hogvm execute', () => { ).toEqual(3) // var r := [1, 2, {'d': [1, 3, 42, 3]}]; - // r[2].d[2] := 3; - // return r[2].d[2]; + // r[3].d[3] := 3; + // return r[3].d[3]; expect( exec([ '_h', @@ -1275,26 +1280,26 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 2, + 3, op.INTEGER, 3, op.SET_PROPERTY, op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.RETURN, op.POP, @@ -1302,8 +1307,8 @@ describe('hogvm execute', () => { ).toEqual(3) // var r := [1, 2, {'d': [1, 3, 42, 3]}]; - // r[2].c := [666]; - // return r[2]; + // r[3].c := [666]; + // return r[3]; expect( exec([ '_h', @@ -1330,7 +1335,7 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'c', @@ -1342,7 +1347,7 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.RETURN, op.POP, @@ -1350,8 +1355,8 @@ describe('hogvm execute', () => { ).toEqual(map({ d: [1, 3, 42, 3], c: [666] })) // var r := [1, 2, {'d': [1, 3, 42, 3]}]; - // r[2].d[2] := 3; - // return r[2].d; + // r[3].d[3] := 3; + // return r[3].d; expect( exec([ '_h', @@ -1378,20 +1383,20 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 2, + 3, op.INTEGER, 3, op.SET_PROPERTY, op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', @@ -1402,8 +1407,8 @@ describe('hogvm execute', () => { ).toEqual([1, 3, 3, 3]) // var r := [1, 2, {'d': [1, 3, 42, 3]}]; - // r.2['d'] := ['a', 'b', 'c', 'd']; - // return r[2].d[2]; + // r.3['d'] := ['a', 'b', 'c', 'd']; + // return r[3].d[3]; expect( exec([ '_h', @@ -1430,7 +1435,7 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', @@ -1448,13 +1453,13 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.RETURN, op.POP, @@ -1463,8 +1468,8 @@ describe('hogvm execute', () => { // var r := [1, 2, {'d': [1, 3, 42, 3]}]; // var g := 'd'; - // r.2[g] := ['a', 'b', 'c', 'd']; - // return r[2].d[2]; + // r.3[g] := ['a', 'b', 'c', 'd']; + // return r[3].d[3]; expect( exec([ '_h', @@ -1493,7 +1498,7 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.GET_LOCAL, 1, @@ -1511,13 +1516,13 @@ describe('hogvm execute', () => { op.GET_LOCAL, 0, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.STRING, 'd', op.GET_PROPERTY, op.INTEGER, - 2, + 3, op.GET_PROPERTY, op.RETURN, op.POP, diff --git a/hogvm/typescript/src/utils.ts b/hogvm/typescript/src/utils.ts index fe1595c8511..6b1d88246da 100644 --- a/hogvm/typescript/src/utils.ts +++ b/hogvm/typescript/src/utils.ts @@ -19,7 +19,10 @@ export function getNestedValue(obj: any, chain: any[], nullish = false): any { if (obj instanceof Map) { obj = obj.get(key) ?? null } else if (typeof key === 'number') { - obj = obj[key] ?? null + if (key <= 0) { + throw new Error(`Hog arrays start from index 1`) + } + obj = obj[key - 1] ?? null } else { obj = obj[key] ?? null } @@ -37,7 +40,10 @@ export function setNestedValue(obj: any, chain: any[], value: any): void { if (obj instanceof Map) { obj = obj.get(key) ?? null } else if (Array.isArray(obj) && typeof key === 'number') { - obj = obj[key] + if (key <= 0) { + throw new Error(`Hog arrays start from index 1`) + } + obj = obj[key - 1] } else { throw new Error(`Can not get ${chain} on element of type ${typeof obj}`) } @@ -46,7 +52,10 @@ export function setNestedValue(obj: any, chain: any[], value: any): void { if (obj instanceof Map) { obj.set(lastKey, value) } else if (Array.isArray(obj) && typeof lastKey === 'number') { - obj[lastKey] = value + if (lastKey <= 0) { + throw new Error(`Hog arrays start from index 1`) + } + obj[lastKey - 1] = value } else { throw new Error(`Can not set ${chain} on element of type ${typeof obj}`) } diff --git a/posthog/hogql/bytecode.py b/posthog/hogql/bytecode.py index 4f7bdf9f1a7..51bca368e14 100644 --- a/posthog/hogql/bytecode.py +++ b/posthog/hogql/bytecode.py @@ -375,7 +375,7 @@ class BytecodeBuilder(Visitor): response.extend([Operation.GET_LOCAL, expr_local, Operation.CALL, "values", 1]) loop_index_local = self._declare_local("__H_index_H__") # 0 - response.extend([Operation.INTEGER, 0]) + response.extend([Operation.INTEGER, 1]) loop_limit_local = self._declare_local("__H_limit_H__") # length of keys response.extend([Operation.GET_LOCAL, expr_values_local, Operation.CALL, "length", 1]) @@ -390,7 +390,7 @@ class BytecodeBuilder(Visitor): response.extend([Operation.NULL]) # check if loop_index < loop_limit - condition = [Operation.GET_LOCAL, loop_limit_local, Operation.GET_LOCAL, loop_index_local, Operation.LT] + condition = [Operation.GET_LOCAL, loop_limit_local, Operation.GET_LOCAL, loop_index_local, Operation.LT_EQ] # set key_var and value_var body: list = []