From f942c5366437dd5c407ed7bf1159a422f46edfed Mon Sep 17 00:00:00 2001 From: Will Date: Sun, 24 Dec 2023 10:09:41 +0000 Subject: [PATCH] Added `IndexExprConditions` & `IndexExprImport` test cases. --- tests/Compiler.test.cpp | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/Compiler.test.cpp b/tests/Compiler.test.cpp index f24087bf..28232202 100644 --- a/tests/Compiler.test.cpp +++ b/tests/Compiler.test.cpp @@ -7878,4 +7878,54 @@ RETURN R1 7 )"); } +TEST_CASE("IndexExprConditions") +{ + // An issue with compileExprIndexExpr meant that GETTABLEKS and GETTABLEN were never generated and would always default to a GETTABLE. + + // string indexing, no optimisations + CHECK_EQ("\n" + compileFunction(R"( +local _ = foo["bar"] +)", + 0, 0), + R"( +GETGLOBAL R1 K1 ['foo'] +GETTABLEKS R0 R1 K0 ['bar'] +RETURN R0 0 +)"); + + // number indexing, no optimisations + CHECK_EQ("\n" + compileFunction(R"( +local _ = foo[1] +)", + 0, 0), + R"( +GETGLOBAL R1 K0 ['foo'] +GETTABLEN R0 R1 1 +RETURN R0 0 +)"); +} + +TEST_CASE("IndexExprImport") +{ + // Optimisations O1 and above should generate GETIMPORT for compileExprIndexExpr when a string index is provided + + // index with no whitespace + CHECK_EQ("\n" + compileFunction0(R"( +local _ = foo["bar"] +)"), + R"( +GETIMPORT R0 2 [foo.bar] +RETURN R0 0 +)"); + + // index with whitespace + CHECK_EQ("\n" + compileFunction0(R"( +local _ = foo["bar baz buz"] +)"), + R"( +GETIMPORT R0 2 [foo["bar baz buz"]] +RETURN R0 0 +)"); +} + TEST_SUITE_END();