diff options
| author | Chris Williamson <chris.williamson@delphix.com> | 2017-06-23 15:56:06 -0700 | 
|---|---|---|
| committer | Matthew Ahrens <mahrens@delphix.com> | 2017-06-26 06:45:06 -0700 | 
| commit | dfc115332c94a2f62058ac7f2bce7631fbd20b3d (patch) | |
| tree | 1bcc8a12ac56d03b394b32e32347d9f28f9af103 /usr/src/uts/common/fs/zfs/lua/ldump.c | |
| parent | 3d9b1a2a543845425f021c3f896a07b1deff87c9 (diff) | |
| download | illumos-joyent-dfc115332c94a2f62058ac7f2bce7631fbd20b3d.tar.gz | |
7431 ZFS Channel Programs
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: John Kennedy <john.kennedy@delphix.com>
Reviewed by: Dan Kimmel <dan.kimmel@delphix.com>
Approved by: Garrett D'Amore <garrett@damore.org>
Diffstat (limited to 'usr/src/uts/common/fs/zfs/lua/ldump.c')
| -rw-r--r-- | usr/src/uts/common/fs/zfs/lua/ldump.c | 173 | 
1 files changed, 173 insertions, 0 deletions
| diff --git a/usr/src/uts/common/fs/zfs/lua/ldump.c b/usr/src/uts/common/fs/zfs/lua/ldump.c new file mode 100644 index 0000000000..64e5649332 --- /dev/null +++ b/usr/src/uts/common/fs/zfs/lua/ldump.c @@ -0,0 +1,173 @@ +/* +** $Id: ldump.c,v 2.17.1.1 2013/04/12 18:48:47 roberto Exp $ +** save precompiled Lua chunks +** See Copyright Notice in lua.h +*/ + +#include <sys/zfs_context.h> + +#define ldump_c +#define LUA_CORE + +#include "lua.h" + +#include "lobject.h" +#include "lstate.h" +#include "lundump.h" + +typedef struct { + lua_State* L; + lua_Writer writer; + void* data; + int strip; + int status; +} DumpState; + +#define DumpMem(b,n,size,D)	DumpBlock(b,(n)*(size),D) +#define DumpVar(x,D)		DumpMem(&x,1,sizeof(x),D) + +static void DumpBlock(const void* b, size_t size, DumpState* D) +{ + if (D->status==0) + { +  lua_unlock(D->L); +  D->status=(*D->writer)(D->L,b,size,D->data); +  lua_lock(D->L); + } +} + +static void DumpChar(int y, DumpState* D) +{ + char x=(char)y; + DumpVar(x,D); +} + +static void DumpInt(int x, DumpState* D) +{ + DumpVar(x,D); +} + +static void DumpNumber(lua_Number x, DumpState* D) +{ + DumpVar(x,D); +} + +static void DumpVector(const void* b, int n, size_t size, DumpState* D) +{ + DumpInt(n,D); + DumpMem(b,n,size,D); +} + +static void DumpString(const TString* s, DumpState* D) +{ + if (s==NULL) + { +  size_t size=0; +  DumpVar(size,D); + } + else + { +  size_t size=s->tsv.len+1;		/* include trailing '\0' */ +  DumpVar(size,D); +  DumpBlock(getstr(s),size*sizeof(char),D); + } +} + +#define DumpCode(f,D)	 DumpVector(f->code,f->sizecode,sizeof(Instruction),D) + +static void DumpFunction(const Proto* f, DumpState* D); + +static void DumpConstants(const Proto* f, DumpState* D) +{ + int i,n=f->sizek; + DumpInt(n,D); + for (i=0; i<n; i++) + { +  const TValue* o=&f->k[i]; +  DumpChar(ttypenv(o),D); +  switch (ttypenv(o)) +  { +   case LUA_TNIL: +	break; +   case LUA_TBOOLEAN: +	DumpChar(bvalue(o),D); +	break; +   case LUA_TNUMBER: +	DumpNumber(nvalue(o),D); +	break; +   case LUA_TSTRING: +	DumpString(rawtsvalue(o),D); +	break; +    default: lua_assert(0); +  } + } + n=f->sizep; + DumpInt(n,D); + for (i=0; i<n; i++) DumpFunction(f->p[i],D); +} + +static void DumpUpvalues(const Proto* f, DumpState* D) +{ + int i,n=f->sizeupvalues; + DumpInt(n,D); + for (i=0; i<n; i++) + { +  DumpChar(f->upvalues[i].instack,D); +  DumpChar(f->upvalues[i].idx,D); + } +} + +static void DumpDebug(const Proto* f, DumpState* D) +{ + int i,n; + DumpString((D->strip) ? NULL : f->source,D); + n= (D->strip) ? 0 : f->sizelineinfo; + DumpVector(f->lineinfo,n,sizeof(int),D); + n= (D->strip) ? 0 : f->sizelocvars; + DumpInt(n,D); + for (i=0; i<n; i++) + { +  DumpString(f->locvars[i].varname,D); +  DumpInt(f->locvars[i].startpc,D); +  DumpInt(f->locvars[i].endpc,D); + } + n= (D->strip) ? 0 : f->sizeupvalues; + DumpInt(n,D); + for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D); +} + +static void DumpFunction(const Proto* f, DumpState* D) +{ + DumpInt(f->linedefined,D); + DumpInt(f->lastlinedefined,D); + DumpChar(f->numparams,D); + DumpChar(f->is_vararg,D); + DumpChar(f->maxstacksize,D); + DumpCode(f,D); + DumpConstants(f,D); + DumpUpvalues(f,D); + DumpDebug(f,D); +} + +static void DumpHeader(DumpState* D) +{ + lu_byte h[LUAC_HEADERSIZE]; + luaU_header(h); + DumpBlock(h,LUAC_HEADERSIZE,D); +} + +/* +** dump Lua function as precompiled chunk +*/ +int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip) +{ + DumpState D; + D.L=L; + D.writer=w; + D.data=data; + D.strip=strip; + D.status=0; + DumpHeader(&D); + DumpFunction(f,&D); + return D.status; +} | 
