daurnimator / lua.vm.js

The project is superceded by Fengari. See https://fengari.io/
MIT License
835 stars 101 forks source link

Require doesn't work as expected #14

Closed adrian-castravete closed 10 years ago

adrian-castravete commented 10 years ago

Issue with lua modules and require.

First of all I'd like to say that this is an awesome project. The problem is I may be doing something wrong here, so please correct me if this is not the right way.

I have index.html the page from which I want to load (require) the test.lua module.

The problem is that I'm getting this output in chromium's Console. Which leads me to believe that requiring doesn't work exactly as it should. I have also tried to append to the packages.path variable with no luck. :(

Am I doing something wrong?

For ease of access:

    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8" />
    <title>Simple Lua Require Test</title>
    <script type="text/javascript" src="lua.vm.js"></script>
    <script type="text/lua" src="test.lua"></script>
    </head>
    <body>
        <script type="text/lua">
            test = require("test")
            print(test.test_function("some_value"))
        </script>
    </body>
    </html>
    local function some_function(some_value)
        return 'some_function("' .. some_value .. '")'
    end

    return {
        test_function = some_function
    }
    preload time: 3 ms lua.vm.js:99  
    ERROR: [string "input"]:2: module 'test' not found:     lua.vm.js:96  
        no field package.preload['test']                    lua.vm.js:96  
        no file '/usr/local/share/lua/5.2/test.lua'         lua.vm.js:96  
        no file '/usr/local/share/lua/5.2/test/init.lua'    lua.vm.js:96  
        no file '/usr/local/lib/lua/5.2/test.lua'           lua.vm.js:96  
        no file '/usr/local/lib/lua/5.2/test/init.lua'      lua.vm.js:96  
        no file './test.lua'                                lua.vm.js:96 << 
        no file '/usr/local/lib/lua/5.2/test.so'            lua.vm.js:96  
        no file '/usr/local/lib/lua/5.2/loadall.so'         lua.vm.js:96  
        no file './test.so'                                 lua.vm.js:96
SpiritedDusty commented 10 years ago

I believe you'd have to write it into a file first using the IO API.

adrian-castravete commented 10 years ago

Hello @SpiritedDusty ,
Thanks for the answer! Can you give me an example as to what is happening and how I should make it work? Is it because the files aren't loaded as normal "text/lua" scripts are?

SpiritedDusty commented 10 years ago

First you'd have to load the file from the server using XHR or something. Then you'd pass the file contents to Lua, where it will write it to a file. The file is stored in a temporary filesystem that gets erased after the page closes.

var xhr = new XMLHttpRequest();

// Send a GET request to get the file contents
xhr.open('GET', '/test.lua', true);
xhr.onreadystatechange = function() {
    data = this.responseText;
    // We now get the file contents and write it to a file
    Lua.execute('h = io.open("/test.lua", "w")  h:write(js.global.data) h:close');
    // Now we can require it
    Lua.execute('require("test")')
};

xhr.send();
adrian-castravete commented 10 years ago

Thank you very much, @SpiritedDusty ! :) Had a hunch there would be eval-like stuff.

daurnimator commented 10 years ago

Things are probably now as expected in c35388ef068e7d5acdfcd462bd58dbe20d7be095