lua.js icon indicating copy to clipboard operation
lua.js copied to clipboard

How are lua's requires working?

Open caioviel opened this issue 12 years ago • 4 comments

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.

caioviel avatar Aug 31 '12 15:08 caioviel

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.

mherkender avatar Sep 01 '12 17:09 mherkender

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

kyleconroy avatar Oct 08 '12 09:10 kyleconroy

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.

mherkender avatar Oct 08 '12 09:10 mherkender

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?

caioviel avatar Oct 09 '12 14:10 caioviel