Lua C api: How to load lua files defined as modules?

I have the following lua script:

module("modoo",package.seeall)
foo=1
bar={12,34}

Which works fine with cli, for example:

> dofile "mod_modoo.lua"
> =modoo.foo
1
> =modoo
table: 0x86ce058

As far as I understand, it works like a table, but whenever I try to load it as a table, the nil value is pushed onto the stack. Every other table is working fine.

I thought using lua_getglobal would not work with modules, but I could not find the correct way to load it; how should i do this?

+3
source share
2 answers

Download Lua modules using requirehow lua.c. See http://www.lua.org/source/5.1/lua.c.html#dolibrary

+4
source

module dofile Lua 5.1 , script. local script, , . require .

mod_modoo.lua:

return { foo = 1,  bar = { 12, 34 } }

modoo_test.lua:

> local modoo = require "mod_modoo"

, (.. modoo) , . script. :

mod_modoo.lua:

modoo = { foo = 1,  bar = { 12, 34 } }
return modoo

modoo_test.lua:

> require "mod_modoo"
> print(modoo.foo)
1

Lua Critiqued .

+3

Source: https://habr.com/ru/post/1751386/


All Articles