lua-users home
lua-l archive

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

No, my comment was that if you wish to inject additional script code into a
chunk using a ChunkReader function passed to lua_load you may have to know
whether to inject strings or bytecodes.  The reason I say this is that
lua_load automatically detects the format of the code, but I do not know if
it can handle a switch in mid-load.

for instance if you write the following (correcting any errors in it)...

typedef struct _ReadState
    const char *Prefix;
    const char *Suffix;
    FILE *File;
    int Stage;
} ReadState;

const char *MyChunkReader(lua_State *L, void *data, size_t *Size)
    ReadState *State = (ReadState *)data;
    switch (State->Stage)
        case 0:
            if (State->Prefix)
                *Size = strlen(State->Prefix);
                return State->Prefix;
        case 1:
            /* insert boring code here to read from the file */

        case 2:
            if (State->Suffix)
                *Size = strlen(State->Suffix);
                return State->Prefix;
            *Size = 0;
            return NULL;

    ReadState MyData;
    MyData.Prefix = "local SomeLocal;";
    MyData.Suffix = NULL;
    MyData.Stage = 0;
    MyData.File = fopen("SomeScript.lua", "r");
    lua_load(L, MyChunkReader, &MyData, "This is an example");

Then the string "local SomeLocal;" will be injected into the chunk before
the contents of the file "SomeScript.lua".  If "SomeScript.lua" contains
ANSI textual Lua code then this is no problem.  If, however, it contains
precompiled byte code then lua_load might become confused when the chunk
switches from ANSI to byte code in mid-stream.

-----Original Message-----
[]On Behalf Of D Burgess
Sent: Thursday, August 21, 2003 5:24 PM
To: Lua list
Subject: Re: Manipulating locals

>Virgil Smith said
> <other stuff> side issue here is that
>you may have to restrict the string/file/whatever to only contain textual
>(i.e. non-precompiled) script code, otherwise you will have to figure out
>how to generate bytecode that isn't already chunked.

Are we saying that compiled script is scoped differently?