lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Tim Hill <drtimhill@gmail.com> writes:

>> On May 28, 2015, at 2:42 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> 
>> 2015-05-28 11:21 GMT+02:00 Hans Riekehof <Riekehof@schoeps.de>:
>> 
>>> My idea is to have this for pre compiled lua code because I want to
>>> write a lot of code in Lua and not in C/C++.
>> 
>> Lua compiles to bytecode for a virtual machine. That VM is different
>> for every major Lua release: 5.1, 5.2, 5.3 etc, but the same within minor
>> releases 5.2.1, 5.2.2 etc. However, it is not supposed to portable across
>> different architectures.
>> 
>> I'm not entirely sure I understand what you want. Can you show us
>> what the Lua code would look like in a postulated Lua dialect that has
>> this feature?
>
> The poster wants to execute directly from a bytecode file via
> memory-mapping to save on RAM footprint by avoiding the need to
> copy/load the bytecode into memory before execution. The issue of
> portability isn't a problem here since (presumably) the bytecode and
> Lua interpreter are all embedded in the flash/ROM image at the same
> time.
>
> Given that Lua is designed for embedded systems it’s a bit surprising
> that this isn’t part of the core API.

I'm confused.  Although not part of the core library, luaL_loadbufferx()
exists, and is implemented in a very small amount of code:

  typedef struct LoadS {
    const char *s;
    size_t size;
  } LoadS;
  
  static const char *getS (lua_State *L, void *ud, size_t *size) {
    LoadS *ls = (LoadS *)ud;
    (void)L;  /* not used */
    if (ls->size == 0) return NULL;
    *size = ls->size;
    ls->size = 0;
    return ls->s;
  }
  
  LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
                                   const char *name, const char *mode) {
    LoadS ls;
    ls.s = buff;
    ls.size = size;
    return lua_load(L, getS, &ls, name, mode);
  }

I use luaL_loadbufferx() to compile compiled lua into my binaries (along
with a small script that converts the binary compiled lua file into a C
array).  I feel that I must be missing something about the original
question.

-- 
Michael Welsh Duggan
(mwd@cert.org)