2 ** $Id: dump.c,v 1.20 1999/07/02 19:34:26 lhf Exp $
3 ** save bytecodes to file
4 ** See Copyright Notice in lua.h
13 #define strerror(e) "(no error message provided by operating system)"
16 #define DumpBlock(b,size,D) fwrite(b,size,1,D)
17 #define DumpInt DumpLong
19 static void DumpWord(int i
, FILE* D
)
21 int hi
= 0x0000FF & (i
>>8);
27 static void DumpLong(long i
, FILE* D
)
29 int hi
= 0x00FFFF & (i
>>16);
35 static void DumpNumber(real x
, FILE* D
, int native
, TProtoFunc
* tf
)
38 DumpBlock(&x
,sizeof(x
),D
);
43 sprintf(b
,NUMBER_FMT
"%n",x
,&n
);
44 luaU_str2d(b
,tf
->source
->str
); /* help lundump not to fail */
50 static void DumpCode(TProtoFunc
* tf
, FILE* D
)
52 int size
=luaU_codesize(tf
);
54 DumpBlock(tf
->code
,size
,D
);
57 static void DumpString(char* s
, int size
, FILE* D
)
68 static void DumpTString(TaggedString
* s
, FILE* D
)
73 DumpString(s
->str
,s
->u
.s
.len
+1,D
);
76 static void DumpLocals(TProtoFunc
* tf
, FILE* D
)
78 if (tf
->locvars
==NULL
)
84 for (v
=tf
->locvars
; v
->line
>=0; v
++)
87 for (v
=tf
->locvars
; v
->line
>=0; v
++)
90 DumpTString(v
->varname
,D
);
95 static void DumpFunction(TProtoFunc
* tf
, FILE* D
, int native
);
97 static void DumpConstants(TProtoFunc
* tf
, FILE* D
, int native
)
103 TObject
* o
=tf
->consts
+i
;
104 fputc(-ttype(o
),D
); /* ttype(o) is negative - ORDER LUA_T */
108 DumpNumber(nvalue(o
),D
,native
,tf
);
111 DumpTString(tsvalue(o
),D
);
114 DumpFunction(tfvalue(o
),D
,native
);
118 default: /* cannot happen */
119 luaU_badconstant("dump",i
,o
,tf
);
125 static void DumpFunction(TProtoFunc
* tf
, FILE* D
, int native
)
127 DumpInt(tf
->lineDefined
,D
);
128 DumpTString(tf
->source
,D
);
131 DumpConstants(tf
,D
,native
);
133 luaL_verror("write error" IN
": %s (errno=%d)",INLOC
,strerror(errno
),errno
);
136 static void DumpHeader(TProtoFunc
* Main
, FILE* D
, int native
)
143 fputc(sizeof(real
),D
);
144 DumpNumber(TEST_NUMBER
,D
,native
,Main
);
150 void luaU_dumpchunk(TProtoFunc
* Main
, FILE* D
, int native
)
152 DumpHeader(Main
,D
,native
);
153 DumpFunction(Main
,D
,native
);