mherkender/lua.js — pull request #10 by ghoulsblade (2012-04-27 17:50:29+00:00)
Title: more detailed error messages

since i'm often running into not-yet-implemented api stuff and finding out what's missing is a pain, i started experimenting with error messages.

example 1

local myobj = {}
myobj:MissingMethod(1,2,3)
-- luajs master : TypeError: Cannot read property 'metatable' of null : TypeError: Cannot read property 'metatable' of null
-- lua native : attempt to call method 'MissingMethod' (a nil value)
-- luajs changed (lualib.js only) : Error: attempt to call method 'MissingMethod' (a nil value)

example 2

local myfun1 = nil
myfun1()
-- luajs master : TypeError: Cannot read property 'metatable' of null
-- lua native : attempt to call local 'myfun1' (a nil value)
-- luajs changed (lualib.js only) : Error: attempt to call nil (undefined)
-- luajs changed (lualib.js + bison(see below)) : Error: attempt to call nil (_myfun1_)

example 3

local love = { graphics = {} }
love.graphics.setDefaultImageFilter('linear','nearest')
-- luajs master : TypeError: Cannot read property 'metatable' of null
-- lua native : attempt to call field 'setDefaultImageFilter' (a nil value)
-- luajs changed (lualib.js only) : Error: attempt to call nil (undefined)
-- luajs changed (lualib.js + bison(see below)) : Error: attempt to call nil (lua_tableget(lua_tableget(_love_33, 'graphics'), 'setDefaultImageFilter)

bison :
i don't know how to work with bison, so what i did was replace

case 91: this.$ = "lua_call( /*bla!*/ " + $$[$0-1].single + ", " + getTempDecl($$[$0]) + " )";

by

case 91: this.$ = "lua_call( /*bla!*/ " + $$[$0-1].single + ", " + getTempDecl($$[$0]) + ", "+longStringToString($$[$0-1].single)+")";

in the generated js code. ugly _ for example 2, and ugly js bulkwork around "setDefaultImageFilter" for example 3, but it already shows useful info and makes debugging a lot easier if you ask me.
i'm pretty sure it's in src/lua.jison Line 598 ff:

functioncall
: prefixexp args { $$ = "lua_call(" + $1.single + ", " + getTempDecl($2) + ")"; }
| prefixexp ":" NAME args { $$ = "lua_mcall(" + $1.single + ", '" + $3 + "', " + getTempDecl($4) + ")"; }
;

any idea how to generate a useful 3rd "origin" parameter for lua_call here ? prefixexp already works (see example1), but i don't even know how to set up node.js/jison/... , so i cannot try it, even IF i knew what i was doing ;)