lua-users home
lua-l archive

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


> I need to exchange code between a normal Intel Lua VM, and an integral VM
> where numbers are long ints.

You mean, precompiled Lua code? In this case, the simplest route is
to modify LoadNumber in lundump.c to handle the conversion. You'll
also have to change LoadHeader to perform a finer compatibility test.
Something along the line of the code below (untested). If you only need
to load one type into the other, the code can be much simpler because
you don't need the S->conversion field, just two slightly different
LoadNumber at each end. I hope this helps.

static lua_Number LoadNumber(LoadState* S)
{
 if (S->conversion==DOUBLE_INTO_LONG)
 {
  double x;
  LoadVar(S,x);
  return (lua_Number)x;
 }
 else if (S->conversion==LONG_INTO_DOUBLE)
 {
  long x;
  LoadVar(S,x);
  return (lua_Number)x;
 }
 else
 {
  lua_Number x;
  LoadVar(S,x);
  return x;
 }
}

static void LoadHeader(LoadState* S)
{
 char h[LUAC_HEADERSIZE];
 char s[LUAC_HEADERSIZE];
 luaU_header(h);
 LoadBlock(S,s,LUAC_HEADERSIZE);
 S->conversion=NO_CONVERSION;
 if (memcmp(h,s,LUAC_HEADERSIZE)==0) return;
 if (memcmp(h,s,LUAC_HEADERSIZE-2)!=0, "bad header");
 if (h[LUAC_HEADERSIZE-3]==sizeof(double) && h[LUAC_HEADERSIZE-3]==0)
	S->conversion=DOUBLE_INTO_LONG;
 if (h[LUAC_HEADERSIZE-3]==sizeof(long) && h[LUAC_HEADERSIZE-3]==1)
	S->conversion=LONG_INTO_DOUBLE;
}