moonscript icon indicating copy to clipboard operation
moonscript copied to clipboard

How to use lua's `load` function

Open sucrecacao opened this issue 4 years ago • 6 comments

Hi,

How can I use lua's load function in moonscript ? And does it work with a moonscript string only with lua string?

I have found related issue on this topic but it didn't help me solve the problem, maybe their meant to work with older moonscript version.

sucrecacao avatar Jan 25 '21 09:01 sucrecacao

load only works with Lua strings. If you want to load MoonScript strings, you have to use MoonScript's own function.

moonscript = require "moonscript.base"
lua_code   = moonscript.load "code!"

It's all detailed in the reference manual.

daelvn avatar Jan 25 '21 11:01 daelvn

This doesn't work: I got the following error:

lua: main.lua:1: module 'moonscript.base' not found:
        no field package.preload['moonscript.base']
        no file '/usr/local/share/lua/5.3/moonscript/base.lua'
        no file '/usr/local/share/lua/5.3/moonscript/base/init.lua'
        no file '/usr/local/lib/lua/5.3/moonscript/base.lua'
        no file '/usr/local/lib/lua/5.3/moonscript/base/init.lua'
        no file './moonscript/base.lua'
        no file './moonscript/base/init.lua'
        no file '/usr/local/lib/lua/5.3/moonscript/base.so'
        no file '/usr/local/lib/lua/5.3/loadall.so'
        no file './moonscript/base.so'
        no file '/usr/local/lib/lua/5.3/moonscript.so'
        no file '/usr/local/lib/lua/5.3/loadall.so'
        no file './moonscript.so'
stack traceback:
        [C]: in function 'require'
        main.lua:1: in main chunk
        [C]: in ?

But I installed moonscript through lua rocks and the library is located in:

~/.luarocks/share/lua/5.1/moonscript/base.lua

is there a way to solve this issue in a "clean" way? ( without messing with symlink)

sucrecacao avatar Jan 25 '21 14:01 sucrecacao

According to https://stackoverflow.com/a/47267672/1517394 you might need to add eval $(luarocks path --bin) to your bashrc.

qaisjp avatar Jan 25 '21 15:01 qaisjp

Ok I have advanced one step, I still get:

Built main.moon
lua: error loading module 'lpeg' from file '/home/joe/.luarocks/lib/lua/5.1/lpeg.so':
        /home/joe/.luarocks/lib/lua/5.1/lpeg.so: undefined symbol: lua_tointeger
stack traceback:
        [C]: in ?
        [C]: in function 'require'
        /home/joe/.luarocks/share/lua/5.1/moonscript/errors.lua:2: in main chunk
        [C]: in function 'require'
        ...rocks/share/lua/5.1/moonscript/transform/destructure.lua:13: in main chunk
        [C]: in function 'require'
        ...uarocks/share/lua/5.1/moonscript/transform/statement.lua:18: in main chunk
        [C]: in function 'require'
        /home/joe/.luarocks/share/lua/5.1/moonscript/transform.lua:2: in main chunk
        [C]: in function 'require'
        /home/joe/.luarocks/share/lua/5.1/moonscript/compile.lua:3: in main chunk
        [C]: in function 'require'
        /home/joe/.luarocks/share/lua/5.1/moonscript/base.lua:1: in main chunk
        [C]: in function 'require'
        main.lua:1: in main chunk
        [C]: in ?

Apparently lua has change the lua_tointeger function from 5.1 to 5.2, this might be the causes of the bug. I have upgraded from lua5.1 to lua5.3, but I still have the error message.

sucrecacao avatar Jan 25 '21 18:01 sucrecacao

Ok I have solved the issue using lua5.1 instead of lua which was using version 5.3

Does moon-script only work with 5.1?

sucrecacao avatar Jan 25 '21 18:01 sucrecacao

I also had to use loadstring instead of load ( moonscript.loadstring)

Also to use lua load I had to use the old loadstring, so I guess my moonscript is just installed on 5.1 but most of the doc and issue are for 5.3

sucrecacao avatar Jan 25 '21 18:01 sucrecacao