Skip to content

Commit 2f25893

Browse files
authored
fix(virtualmachine): honor __concat metamethod in CONCAT instruction #8
Replace `table.concat`-based `CONCAT` implementation with use of the `..` operator so `__concat` metamethods are invoked. Fixes concatenation when operands aren't plain strings (e.g., metatable with `__concat` metamethod).
1 parent 6185535 commit 2f25893

1 file changed

Lines changed: 4 additions & 6 deletions

File tree

the-tiny-lua-compiler.lua

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3928,13 +3928,11 @@ function VirtualMachine:executeClosure(...)
39283928
-- OP_CONCAT [A, B, C] R(A) := R(B).. ... ..R(C)
39293929
-- Concatenate a range of registers and store the result in a register.
39303930
elseif opcode == "CONCAT" then
3931-
local values = {}
3932-
for reg = b, c do
3933-
table.insert(values, stack[reg])
3931+
for reg = c - 1, b, -1 do
3932+
-- We cannot use table.concat as it does not call metamethods
3933+
stack[reg] = stack[reg] .. stack[reg + 1]
39343934
end
3935-
3936-
-- Optimization: use table.concat for efficient string concatenation.
3937-
stack[a] = table.concat(values)
3935+
stack[a] = stack[b]
39383936

39393937
-- OP_JMP [A, sBx] pc+=sBx
39403938
-- Jump to a new instruction offset.

0 commit comments

Comments
 (0)