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

How are lua's requires working? #13

Open caioviel opened 12 years ago

caioviel commented 12 years ago

Hello,

This project is really great!

I'm using it in WebNCL, a web-based implementation of NCL presentation machine. NCL is a multimedia language and one of its features is support fo Lua scripts.

We'd had great success in using the lua.js in your project so far, but I'm facing a problem with requires. I really don't how they work.

mherkender commented 12 years ago

EDIT: I must've been tired or something when I wrote this. Cleaned it up a bit.

Sorry for the slow response, busy week for me. require should work in a similar way to regular Lua, although modules must be set up manually before use.

A module needs to call module to set itself up, or can be set up more directly in Javascript by using lua_createmodule, if you prefer.

If you're having any other problems, I'd be happy to help.

kyleconroy commented 11 years ago

Lua's module function has design flaws and shouldn't be used. I create modules using the table method.

-- mymodule.lua
local M = {} -- public interface

-- private
local x = 1
local function baz() print 'test' end

function M.foo() print("foo", x) end

function M.bar()
  M.foo()
  baz()
  print "bar"
end

return M

We should make sure that these style of modules also work with require

mherkender commented 11 years ago

I did design for it, although looking at it, it could be improved.

Basically when you return like that, it overrides what's originally returned (_G) in the generated JS function. Since initializing modules is currently done manually in lua.js, the table method just replaces using the return value of the generated script and lua_createmodule.

caioviel commented 11 years ago

I'm working in another project right now, so my NCL/Lua player is kinda inactive.

I was trying to run a NCL/Lua sample that uses multiple Lua files (the description is in portuguese).

I could not "import" the other files used in the sample. Is it not implemented yet, right?