print('hello' .. ' ' .. 'world!') -- This is Lua!
print(js.run('[0,1,2,3,4,5]')) -- Run JS from Lua
-- Interact with the page using Lua
local screen = js.global.screen
print("you haz " .. (screen.width*screen.height) .. " pixels")
local window = js.global -- global object in JS is the window
window.alert("hello from lua!")
window.setTimeout(function() print('hello from lua callback') end, 2500)
local document = js.global.document
print("this window has title '" .. document.title .. "'")
One of the important aspects of running a VM in another VM is performance, and in this case benchmark tests indicate 50% of the native code, which is on par with other C code compiled to asm.js, and good enough for certain scenarios according to Zakai:
That is too much [performance too low] for some uses cases, but certainly acceptable for others. In particular, remember that the Lua VM is often significantly faster than other dynamic languages like Python and Ruby. These languages are useful in many cases even if they are not super-fast.
Another issue is the size of the library, which in this case is surprisingly small at 200KB g-zipped.
There are some other difficult problems to deal with when running a VM in another VM, as mentioned by Zakai:
This project does not seem to be an indication of Mozilla’s desire to be able to run other VMs in the browser, but rather to show the power of Emscripten and asm.js, which currently are in a race head-to-head with Google PNaCl, another attempt to run native code in the browser. (Mode details can be found in this InfoQ post: Debate: Do We Need a Universal Web Bytecode?)
The problem with both asm.js and PNaCl is that none is supported by other browsers. Theoretically, Chrome can run asm.js code, but the performance is rather poor. For example, Lua VM benchmarks run on Chrome at less than 30% the performance on Firefox Nightly, which is 50% of the native. As it is, nobody will run asm.js in Chrome unless Google optimizes their browser for Firefox’s solution for native code. It may all come down to browser market share and the ability to improve the performance of their respective solutions.
Comprehensive Article on ASM.js performance compared to native code
This is a worthwhile read. ASM.js mostly acquits itself well but only on Firefox. Still, this means that other browsers could follow FF's lead to achieve comparable performance.
Personally I am a bit leary of native code as in Google's NaCl.
Todd Montgomery Dec 19, 2014
Juergen Hoeller,Stéphane Nicoll Dec 18, 2014