@@ -1054,16 +1054,59 @@ test "debug interface" {
1054
1054
try lua .protectedCall (1 , 1 , 0 );
1055
1055
}
1056
1056
1057
+ test "debug upvalues" {
1058
+ var lua = try Lua .init (testing .allocator );
1059
+ defer lua .deinit ();
1060
+
1061
+ try lua .doString (
1062
+ \\f = function(x)
1063
+ \\ return function(y)
1064
+ \\ return x + y
1065
+ \\ end
1066
+ \\end
1067
+ \\addone = f(1)
1068
+ );
1069
+ _ = lua .getGlobal ("addone" );
1070
+
1071
+ // index doesn't exist
1072
+ try expectError (Error .Fail , lua .getUpvalue (1 , 2 ));
1073
+
1074
+ // inspect the upvalue (should be x)
1075
+ try expectEqualStrings ("x" , try lua .getUpvalue (-1 , 1 ));
1076
+ try expectEqual (@as (Number , 1 ), try lua .toNumber (-1 ));
1077
+ lua .pop (1 );
1078
+
1079
+ // now make the function an "add five" function
1080
+ lua .pushNumber (5 );
1081
+ _ = try lua .setUpvalue (-2 , 1 );
1082
+
1083
+ // test a bad index (the valid one's result is unpredicable)
1084
+ try expectError (Error .Fail , lua .upvalueId (-1 , 2 ));
1085
+
1086
+ // call the new function (should return 7)
1087
+ lua .pushNumber (2 );
1088
+ try lua .protectedCall (1 , 1 , 0 );
1089
+ try expectEqual (@as (Number , 7 ), try lua .toNumber (-1 ));
1090
+ lua .pop (1 );
1091
+
1092
+ try lua .doString (
1093
+ \\addthree = f(3)
1094
+ );
1095
+
1096
+ _ = lua .getGlobal ("addone" );
1097
+ _ = lua .getGlobal ("addthree" );
1098
+
1099
+ // now addone and addthree share the same upvalue
1100
+ lua .upvalueJoin (-2 , 1 , -1 , 1 );
1101
+ try expect ((try lua .upvalueId (-2 , 1 )) == try lua .upvalueId (-1 , 1 ));
1102
+ }
1103
+
1057
1104
test "refs" {
1058
1105
// temporary test that includes a reference to all functions so
1059
1106
// they will be type-checked
1060
1107
1061
1108
// debug
1062
1109
_ = Lua .getStack ;
1063
- _ = Lua .getUpvalue ;
1064
- _ = Lua .setUpvalue ;
1065
- _ = Lua .upvalueId ;
1066
- _ = Lua .upvalueJoin ;
1067
1110
1068
1111
// auxlib
1069
1112
_ = Lua .argCheck ;
0 commit comments