mherkender / lua.js

An ECMAscript framework to compile and run Lua code, allowing Lua to run in a browser or in Flash
http://blog.brokenfunction.com/
600 stars 73 forks source link

Why is unpack broken? #2

Closed cmwelsh closed 12 years ago

cmwelsh commented 12 years ago

This patch fixes the Lua unpack function by modifying the value passed to the ReturnValues constructor function. It appears that your standard library requires the parameter "count" to be set to 1 or else it does not handle the thrown results exception...

Please weigh in if I'm missing something here. Thanks!

mherkender commented 12 years ago

I misread the Lua documentation. It said that unpack was equivalent to...

return list[i], list[i+1], ···, list[j]

Which I assumed meant something like this...

function test()
  return list[i], list[i+1], ···, list[j]
  -- is equivalent to
  unpack(list)
end

But the actual behavior resembles this...

function test()
  return list[i], list[i+1], ···, list[j]
  -- is equivalent to
  return unpack(list)
end

The reason "count" even exists is because it allows ReturnValue to delay being returned until it passes far enough down the stack, to recreate the first example's behavior. I'm removing it right now. Thank you for noticing this bug.