Rework the main API"s that implement the calling convention. Provide utility methods to get arguments that were supplied, and provide return values. Add a VM interface to clarify the relationship between the VM, things that call the VM, and things that are called by the VM. Make the code more closely aligned with the C++ version.

This commit is contained in:
James Roseborough
2007-07-24 05:06:10 +00:00
parent 56f33b373d
commit 8bf4c82a12
17 changed files with 424 additions and 862 deletions

View File

@@ -4,7 +4,7 @@ import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import lua.StackState;
import lua.VM;
import lua.value.LBoolean;
import lua.value.LDouble;
import lua.value.LInteger;
@@ -44,15 +44,14 @@ public class LoadState {
private int luacSizeofLuaNumber;
private boolean luacIsNumberIntegral;
/** The lua state that is loading the code */
private StackState L;
/** input stream from which we are loading */
private DataInputStream is;
/** Name of what is being loaded? */
String name;
/** The VM doing the loading */
VM L;
private static final int LUA_TNONE = (-1);
@@ -65,76 +64,11 @@ public class LoadState {
private static final int LUA_TFUNCTION = 6;
private static final int LUA_TUSERDATA = 7;
private static final int LUA_TTHREAD = 8;
// /*
// ** $Id$
// ** load precompiled Lua chunks
// ** See Copyright Notice in lua.h
// */
//
// #include <string.h>
//
// #define lundump_c
// #define LUA_CORE
//
// #include "lua.h"
//
// #include "ldebug.h"
// #include "ldo.h"
// #include "lfunc.h"
// #include "lmem.h"
// #include "lobject.h"
// #include "lstring.h"
// #include "lundump.h"
// #include "lzio.h"
//
// typedef struct {
// lua_State* L;
// ZIO* Z;
// Mbuffer* b;
// const char* name;
// } LoadState;
//
// #ifdef LUAC_TRUST_BINARIES
// #define IF(c,s)
// #else
// #define IF(c,s) if (c) error(S,s)
//
// static void error(LoadState* S, const char* why)
// {
// luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
// luaD_throw(S->L,LUA_ERRSYNTAX);
// }
// #endif
//
// #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
// #define LoadByte(S) (lu_byte)LoadChar(S)
// #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
// #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
//
// static void LoadBlock(LoadState* S, void* b, size_t size)
// {
// size_t r=luaZ_read(S->Z,b,size);
// IF (r!=0, "unexpected end");
// }
//
// static int LoadChar(LoadState* S)
// {
// char x;
// LoadVar(S,x);
// return x;
// }
int loadByte() throws IOException {
return is.readUnsignedByte();
}
//
// static int LoadInt(LoadState* S)
// {
// int x;
// LoadVar(S,x);
// IF (x<0, "bad integer");
// return x;
// }
int loadInt() throws IOException {
if ( this.luacLittleEndian ) {
int a = is.readUnsignedByte();
@@ -158,27 +92,7 @@ public class LoadState {
}
return (((long)b)<<32) | (((long)a)&0xffffffffL);
}
//
// static lua_Number LoadNumber(LoadState* S)
// {
// lua_Number x;
// LoadVar(S,x);
// return x;
// }
//
// static TString* LoadString(LoadState* S)
// {
// size_t size;
// LoadVar(S,size);
// if (size==0)
// return NULL;
// else
// {
// char* s=luaZ_openspace(S->L,S->b,size);
// LoadBlock(S,s,size);
// return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
// }
// }
LString loadString() throws IOException {
int size = loadInt();
if ( size == 0 )
@@ -218,14 +132,7 @@ public class LoadState {
return longBitsToLuaNumber( loadInt64() );
}
}
//
// static void LoadCode(LoadState* S, Proto* f)
// {
// int n=LoadInt(S);
// f->code=luaM_newvector(S->L,n,Instruction);
// f->sizecode=n;
// LoadVector(S,f->code,n,sizeof(Instruction));
// }
public void loadCode( Proto f ) throws IOException {
int n = loadInt();
int[] code = new int[n];
@@ -233,45 +140,7 @@ public class LoadState {
code[i] = loadInt();
f.code = code;
}
//
// static Proto* LoadFunction(LoadState* S, TString* p);
//
// static void LoadConstants(LoadState* S, Proto* f)
// {
// int i,n;
// n=LoadInt(S);
// f->k=luaM_newvector(S->L,n,TValue);
// f->sizek=n;
// for (i=0; i<n; i++) setnilvalue(&f->k[i]);
// for (i=0; i<n; i++)
// {
// TValue* o=&f->k[i];
// int t=LoadChar(S);
// switch (t)
// {
// case LUA_TNIL:
// setnilvalue(o);
// break;
// case LUA_TBOOLEAN:
// setbvalue(o,LoadChar(S));
// break;
// case LUA_TNUMBER:
// setnvalue(o,LoadNumber(S));
// break;
// case LUA_TSTRING:
// setsvalue2n(S->L,o,LoadString(S));
// break;
// default:
// IF (1, "bad constant");
// break;
// }
// }
// n=LoadInt(S);
// f->p=luaM_newvector(S->L,n,Proto*);
// f->sizep=n;
// for (i=0; i<n; i++) f->p[i]=NULL;
// for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
// }
void loadConstants(Proto f) throws IOException {
int n = loadInt();
LValue[] values = new LValue[n];
@@ -301,32 +170,7 @@ public class LoadState {
protos[i] = loadFunction(f.source);
f.p = protos;
}
//
// static void LoadDebug(LoadState* S, Proto* f)
// {
// int i,n;
// n=LoadInt(S);
// f->lineinfo=luaM_newvector(S->L,n,int);
// f->sizelineinfo=n;
// LoadVector(S,f->lineinfo,n,sizeof(int));
// n=LoadInt(S);
// f->locvars=luaM_newvector(S->L,n,LocVar);
// f->sizelocvars=n;
// for (i=0; i<n; i++) f->locvars[i].varname=NULL;
// for (i=0; i<n; i++)
// {
// f->locvars[i].varname=LoadString(S);
// f->locvars[i].startpc=LoadInt(S);
// f->locvars[i].endpc=LoadInt(S);
// }
// n=LoadInt(S);
// f->upvalues=luaM_newvector(S->L,n,TString*);
// f->sizeupvalues=n;
// for (i=0; i<n; i++) f->upvalues[i]=NULL;
// for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
// }
void loadDebug( Proto f ) throws IOException {
int n = loadInt();
f.lineinfo = new int[n];
@@ -348,27 +192,9 @@ public class LoadState {
f.upvalues[i] = loadString();
}
}
//
// static Proto* LoadFunction(LoadState* S, TString* p)
// {
// Proto* f=luaF_newproto(S->L);
// setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
// f->source=LoadString(S); if (f->source==NULL) f->source=p;
// f->linedefined=LoadInt(S);
// f->lastlinedefined=LoadInt(S);
// f->nups=LoadByte(S);
// f->numparams=LoadByte(S);
// f->is_vararg=LoadByte(S);
// f->maxstacksize=LoadByte(S);
// LoadCode(S,f);
// LoadConstants(S,f);
// LoadDebug(S,f);
// IF (!luaG_checkcode(f), "bad code");
// S->L->top--;
// return f;
// }
public Proto loadFunction(LString p) throws IOException {
Proto f = new Proto(this.L);
Proto f = new Proto();
// this.L.push(f);
f.source = loadString();
f.linedefined = loadInt();
@@ -410,27 +236,8 @@ public class LoadState {
if ( sig != LUAC_HEADER_SIGNATURE )
throw new IllegalArgumentException("bad signature");
}
//
// /*
// ** load precompiled chunk
// */
// Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
// {
// LoadState S;
// if (*name=='@' || *name=='=')
// S.name=name+1;
// else if (*name==LUA_SIGNATURE[0])
// S.name="binary string";
// else
// S.name=name;
// S.L=L;
// S.Z=Z;
// S.b=buff;
// LoadHeader(&S);
// return LoadFunction(&S,luaS_newliteral(L,"=?"));
// }
public static Proto undump( StackState L, InputStream stream, String name ) throws IOException {
public static Proto undump( VM L, InputStream stream, String name ) throws IOException {
String sname = name;
if ( name.startsWith("@") || name.startsWith("=") )
sname = name.substring(1);
@@ -438,12 +245,12 @@ public class LoadState {
sname = "binary string";
LoadState s = new LoadState( L, stream, sname );
s.loadHeader();
LString literal = new LString(L, "=?");
LString literal = new LString("=?");
return s.loadFunction( literal );
}
/** Private constructor for create a load state */
private LoadState( StackState L, InputStream stream, String name ) {
private LoadState( VM L, InputStream stream, String name ) {
this.L = L;
this.name = name;
this.is = new DataInputStream( stream );