From c9f3a486cfdad6e134ec88b1e43717508e066796 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Fri, 3 Jul 2026 09:49:00 +0800 Subject: Fix execute arg table layout --- spec/cli/test_execution.sh | 45 ++++++++++++++++++++++++++++++++++++--------- src/yue.cpp | 26 +++++++++++++++++++------- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/spec/cli/test_execution.sh b/spec/cli/test_execution.sh index fedfbea..6572e2e 100755 --- a/spec/cli/test_execution.sh +++ b/spec/cli/test_execution.sh @@ -63,7 +63,34 @@ EOF assert_output_contains "First argument" "first" $YUE_BIN -e "$TMP_DIR/args_test.yue" first second assert_output_contains "Second argument" "second" $YUE_BIN -e "$TMP_DIR/args_test.yue" first second -# Test 7: Test with compiler options via --key=value +# Test 7: Test arg table layout for executed scripts +echo "" +echo "Testing arg table layout..." +cat > "$TMP_DIR/arg_table.yue" << 'EOF' +import arg +print "main=" .. tostring arg[0] +print "first=" .. tostring arg[1] +print "exe=" .. tostring arg[-2] +print "option=" .. tostring arg[-1] +EOF + +assert_output_contains "Yue arg[0]" "main=$TMP_DIR/arg_table.yue" $YUE_BIN -e "$TMP_DIR/arg_table.yue" alpha +assert_output_contains "Yue arg[1]" "first=alpha" $YUE_BIN -e "$TMP_DIR/arg_table.yue" alpha +assert_output_contains "Yue arg[-1]" "option=-e" $YUE_BIN -e "$TMP_DIR/arg_table.yue" alpha +assert_output_contains "Yue --execute arg[0]" "main=$TMP_DIR/arg_table.yue" $YUE_BIN --execute="$TMP_DIR/arg_table.yue" alpha + +cat > "$TMP_DIR/arg_table.lua" << 'EOF' +print("main=" .. tostring(arg[0])) +print("first=" .. tostring(arg[1])) +print("exe=" .. tostring(arg[-2])) +print("option=" .. tostring(arg[-1])) +EOF + +assert_output_contains "Lua arg[0]" "main=$TMP_DIR/arg_table.lua" $YUE_BIN -e "$TMP_DIR/arg_table.lua" alpha +assert_output_contains "Lua arg[1]" "first=alpha" $YUE_BIN -e "$TMP_DIR/arg_table.lua" alpha +assert_output_contains "Lua arg[-1]" "option=-e" $YUE_BIN -e "$TMP_DIR/arg_table.lua" alpha + +# Test 8: Test with compiler options via --key=value echo "" echo "Testing compiler options in execute mode..." cat > "$TMP_DIR/options_test.yue" << 'EOF' @@ -72,12 +99,12 @@ EOF assert_success "Execute with compiler option" $YUE_BIN -e "$TMP_DIR/options_test.yue" --reserve_line_number=true -# Test 8: Execute code with table operations +# Test 9: Execute code with table operations echo "" echo "Testing table operations..." assert_output_contains "Table operations" "3" $YUE_BIN -e 't = {1, 2, 3}; print #t' -# Test 9: Execute code with function definition +# Test 10: Execute code with function definition echo "" echo "Testing function definition..." cat > "$TMP_DIR/func_test.yue" << 'EOF' @@ -87,7 +114,7 @@ EOF assert_output_contains "Function execution" "10" $YUE_BIN -e "$TMP_DIR/func_test.yue" -# Test 10: Execute code with class +# Test 11: Execute code with class echo "" echo "Testing class execution..." cat > "$TMP_DIR/class_test.yue" << 'EOF' @@ -103,7 +130,7 @@ EOF assert_output_contains "Class method execution" "5" $YUE_BIN -e "$TMP_DIR/class_test.yue" -# Test 11: Execute with import +# Test 12: Execute with import echo "" echo "Testing import in execute mode..." cat > "$TMP_DIR/import_test.yue" << 'EOF' @@ -113,7 +140,7 @@ EOF # Note: This test depends on how imports are set up assert_success "Execute with import" $YUE_BIN -e "$TMP_DIR/import_test.yue" --path="$TMP_DIR" -# Test 12: Execute code with error handling +# Test 13: Execute code with error handling echo "" echo "Testing error handling in executed code..." cat > "$TMP_DIR/error_test.yue" << 'EOF' @@ -126,7 +153,7 @@ EOF assert_output_contains "Error handling" "caught:" $YUE_BIN -e "$TMP_DIR/error_test.yue" -# Test 13: Execute with export statement +# Test 14: Execute with export statement echo "" echo "Testing export in execute mode..." cat > "$TMP_DIR/export_test.yue" << 'EOF' @@ -136,7 +163,7 @@ EOF assert_output_contains "Export value" "42" $YUE_BIN -e "$TMP_DIR/export_test.yue" -# Test 14: Execute with macro +# Test 15: Execute with macro echo "" echo "Testing macro in execute mode..." cat > "$TMP_DIR/macro_test.yue" << 'EOF' @@ -148,7 +175,7 @@ EOF assert_output_contains "Macro execution" "42" $YUE_BIN -e "$TMP_DIR/macro_test.yue" -# Test 15: Execute with string literal +# Test 16: Execute with string literal echo "" echo "Testing string literals..." # Use grep without -F to match patterns with newlines diff --git a/src/yue.cpp b/src/yue.cpp index 0493205..4bb4e70 100644 --- a/src/yue.cpp +++ b/src/yue.cpp @@ -580,17 +580,28 @@ int main(int narg, const char** args) { std::cout << lua_tostring(L, -1) << '\n'; return 1; } - lua_newtable(L); i++; - for (int j = i, index = 1; j < narg; j++) { - lua_pushstring(L, args[j]); - lua_rawseti(L, -2, index); - index++; - } - lua_setglobal(L, "arg"); + auto setArgTable = [&](std::string_view scriptName, int tokensBeforeScript) { + lua_newtable(L); + for (int j = i, index = 1; j < narg; j++) { + lua_pushstring(L, args[j]); + lua_rawseti(L, -2, index); + index++; + } + if (!scriptName.empty()) { + lua_pushlstring(L, scriptName.data(), scriptName.size()); + lua_rawseti(L, -2, 0); + for (int j = 0, index = -tokensBeforeScript; j < tokensBeforeScript; j++, index++) { + lua_pushstring(L, args[j]); + lua_rawseti(L, -2, index); + } + } + lua_setglobal(L, "arg"); + }; std::ifstream input(evalStr, std::ios::in); int argNum = 2; if (input) { + setArgTable(evalStr, i - 1); auto ext = fs::path(evalStr).extension().string(); for (auto& ch : ext) ch = std::tolower(ch); if (ext == ".lua") { @@ -630,6 +641,7 @@ int main(int narg, const char** args) { lua_setfield(L, -2, "options"); } } else { + setArgTable({ }, 0); pushYue(L, "loadstring"sv); lua_pushlstring(L, evalStr.c_str(), evalStr.size()); lua_pushliteral(L, "=(eval str)"); -- cgit v1.2.3-55-g6feb