lua-users home
lua-l archive

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


Sorry to repeat again, but I'm unsure if my former mail address is still in use ( lua@bazar2.conectiva.com.br )

Helle there,

We are using Lua for protocol templates in our serial field-bus analyzers (for interest, see here https://iftools.com/analyzer/news/topic/index.en.php )

I'm trying to compare the binary chunk of two functions for equality.

The background:

I have to know if the body of a given function was changed. If so, the function must new applied to the recorded data.
The difficulty:  Changed comments or the insertion of some syntax irrelevant white spaces must ignored!
Otherwise every additional line feed or corrected comment forces a new call and redisplay of the protocol data (which sometimes is a time consuming process).

My first attempt was to create the chunk via (here a excerpt from my C++ test program):

typedef vector<char> binary_vector;

int main()
{
    string s1( "function f()\n--comment1\n print(\"hello1\") end");

    string s2( "function f()\n--comment2\n print(\"hello1\") end");

    lua_State *L = luaL_newstate();

    luaL_openlibs( L );

    binary_vector bin1;

    luaL_loadstring( L, s1.c_str() );

    lua_dump( L, writer, &bin1 );

    binary_vector bin2;

    luaL_loadstring( L, s2.c_str() );

    lua_dump( L, writer, &bin2 );

    if( bin1 == bin2 ) {

        cout << "EQUAL" << endl;

    }
}

My writer function iterates over all bytes in the chunk (I hope so) and build a vector of the data


int writer( lua_State* L, const void* p, size_t sz, void* ud )
{
    char* cp = (char*)( p );

    binary_vector* pv = static_cast<binary_vector*>( ud );

    for( int i = 0; i < sz; i++ ) {

        pv->push_back( *cp++ );

    }
    return 0;
}

Unfortunatelly it seems that different white spaces or different comments lead to a different chunk (or byte code).
My hope was that comparing only the chunk gets me rid of removing all comments and spare white spaces by hand.

I also checked the result of the luac program with two identical function and only different comments. Here also the produced binaries are unequal.

I'm not very familiar with the Lua byte code. I just assumed that the byte code of a block only differs when the functional code is different but not when only the comments or white spaces  in the block are unequal.
Maybe I was wrong here - or (better) I have missed something.

I would really appreciate it someone is able to put some light on this matter.

Thanks a lot and best regards

Joachim Bürmann