[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Load module/package from string
- From: Peter Cawley <lua@...>
- Date: Thu, 31 Mar 2011 15:55:07 +0100
On Thu, Mar 31, 2011 at 10:41 AM, <jose.camacho@ifm.com> wrote:
>
> Hi everyone,
>
> this is a long message, sorry for the verbosity. We tried different
> solutions as found in the manual/google, but none of them worked. Here ge go
>
> We are working with LUA in a distributed architecture. Modules and scripts
> should be got over the network as a string (char sequence) and then, loaded
> in a Lua environment
>
> For example, module 'defs.lua' and script 'test.lua'
> File defs.lua
> -- Starts here
> module(..., package.seeall)
>
> description = 'Module description'
> -- Ends here
>
> File test.lua
> -- Starts here
> require 'defs'
>
> print (defs.description)
> -- Ends here
>
> The script 'test.lua' should be called from a C program. For testing, we use
> the Lua command line interpreter, with both files saved in different
> folders. The script is in the same folder where Lua starts and the module is
> in a sub-folder called 'libs'. These are the outputs we got in different
> tries.
>
> Test 1: call loadfile and dofile
> J:\>lua
> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
>> assert (loadfile ('.\\lib\\defs.lua'))
>> dofile ('test.lua')
> test.lua:1: module 'defs' not found:
> no field package.preload['defs']
> no file '.\defs.lua'
> no file 'C:\Programme\Lua_5.1\lua\defs.lua'
> no file 'C:\Programme\Lua_5.1\lua\defs\init.lua'
> no file 'C:\Programme\Lua_5.1\defs.lua'
> no file 'C:\Programme\Lua_5.1\defs\init.lua'
> no file 'C:\Programme\Lua_5.1\lua\defs.luac'
> no file '.\defs.dll'
> no file '.\defs51.dll'
> no file 'C:\Programme\Lua_5.1\defs.dll'
> no file 'C:\Programme\Lua_5.1\defs51.dll'
> no file 'C:\Programme\Lua_5.1\clibs\defs.dll'
> no file 'C:\Programme\Lua_5.1\clibs\defs51.dll'
> no file 'C:\Programme\Lua_5.1\loadall.dll'
> no file 'C:\Programme\Lua_5.1\clibs\loadall.dll'
> stack traceback:
> [C]: in function 'require'
> test.lua:1: in main chunk
> [C]: in function 'dofile'
> stdin:1: in main chunk
> [C]: ?
>
> Test 2: desesperate use of package.loaded
> J:\>lua
> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
>> assert (loadfile ('lib\\defs.lua'))
>> package.loaded['defs'] =loadfile ('lib\\defs.lua')
>> dofile ('test.lua')
> test.lua:3: attempt to index global 'defs' (a nil value)
> stack traceback:
> test.lua:3: in main chunk
> [C]: in function 'dofile'
> stdin:1: in main chunk
> [C]: ?
>
> Test 3: calling the returned loaded function
> J:\>lua
> Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
>> f = assert (loadfile ('lib\\defs.lua'))
>> f()
> lib\defs.lua:1: bad argument #1 to 'module' (string expected, got nil)
> stack traceback:
> [C]: in function 'module'
> lib\defs.lua:1: in function 'f'
> stdin:1: in main chunk
> [C]: ?
>
> And then, we run out of ideas...
>
> Regards,
>
> José Camacho
I suspect that you want to insert things into package.preload, or add
a custom searcher function to the package.loaders array.
E:\Lua\ifm\b>lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> package.preload["defs"] = assert(loadfile([[E:\Lua\ifm\a\defs.lua]]))
> dofile [[test.lua]]
Module description