diff options
-rw-r--r-- | undump.c | 54 |
1 files changed, 31 insertions, 23 deletions
@@ -3,18 +3,18 @@ | |||
3 | ** load bytecodes from files | 3 | ** load bytecodes from files |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_undump="$Id$"; | 6 | char *rcs_undump="$Id: undump.c,v 1.1 1996/02/23 19:04:38 lhf Exp lhf $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <string.h> | 9 | #include <string.h> |
10 | #include "luac.h" | 10 | #include "luac.h" |
11 | 11 | ||
12 | static void warn(char *s) | 12 | static void warn(char *s) /* TODO: remove */ |
13 | { | 13 | { |
14 | fprintf(stderr,"luac: %s\n",s); | 14 | fprintf(stderr,"luac: %s\n",s); |
15 | } | 15 | } |
16 | 16 | ||
17 | static void panic(char *s) | 17 | static void panic(char *s) /* TODO: remove */ |
18 | { | 18 | { |
19 | warn(s); | 19 | warn(s); |
20 | exit(1); | 20 | exit(1); |
@@ -35,13 +35,6 @@ static void Unthread(Byte *p, int i, int v) | |||
35 | } | 35 | } |
36 | } | 36 | } |
37 | 37 | ||
38 | static char* LoadBlock(int size, FILE *D) | ||
39 | { | ||
40 | char *b=luaI_malloc(size); | ||
41 | fread(b,size,1,D); | ||
42 | return b; | ||
43 | } | ||
44 | |||
45 | static int LoadWord(FILE *D) | 38 | static int LoadWord(FILE *D) |
46 | { | 39 | { |
47 | Word w; | 40 | Word w; |
@@ -49,56 +42,70 @@ static int LoadWord(FILE *D) | |||
49 | return w; | 42 | return w; |
50 | } | 43 | } |
51 | 44 | ||
52 | static char* LoadString(FILE *D) | 45 | static char* LoadBlock(int size, FILE *D) |
53 | { | 46 | { |
54 | return LoadBlock(LoadWord(D),D); | 47 | char *b=luaI_malloc(size); |
48 | fread(b,size,1,D); | ||
49 | return b; | ||
55 | } | 50 | } |
56 | 51 | ||
57 | static char* LoadCode(int size, FILE *D) | 52 | static char* LoadString(FILE *D) |
58 | { | 53 | { |
59 | return LoadBlock(size,D); | 54 | return LoadBlock(LoadWord(D),D); |
60 | } | 55 | } |
61 | 56 | ||
62 | static TFunc* LoadFunction(FILE *D) | 57 | static TFunc *Main=NULL; |
58 | |||
59 | static void LoadFunction(FILE *D) | ||
63 | { | 60 | { |
64 | TFunc *tf=new(TFunc); | 61 | TFunc *tf=new(TFunc); |
65 | tf->size=LoadWord(D); | 62 | tf->size=LoadWord(D); |
66 | tf->marked=LoadWord(D); | 63 | tf->marked=LoadWord(D); |
67 | tf->lineDefined=LoadWord(D); | 64 | tf->lineDefined=LoadWord(D); |
68 | tf->fileName=LoadString(D); | 65 | tf->fileName=LoadString(D); |
69 | tf->code=LoadCode(tf->size,D); | 66 | tf->code=LoadBlock(tf->size,D); |
70 | while (1) | 67 | if (tf->lineDefined==0) /* new main */ |
68 | Main=tf; | ||
69 | else /* fix PUSHFUNCTION */ | ||
70 | { | ||
71 | CodeCode c; | ||
72 | Byte *p=Main->code; | ||
73 | c.tf=tf; | ||
74 | *p++=c.m.c1; *p++=c.m.c2; *p++=c.m.c3; *p++=c.m.c4; | ||
75 | } | ||
76 | while (1) /* unthread */ | ||
71 | { | 77 | { |
72 | int c=getc(D); | 78 | int c=getc(D); |
73 | if (c=='V') | 79 | if (c=='V') |
74 | { | 80 | { |
75 | int i=LoadWord(D); | 81 | int i=LoadWord(D); |
76 | char *s=LoadString(D); | 82 | char *s=LoadString(D); |
77 | int v=luaI_findsymbolbyname(s); | 83 | int v=luaI_findsymbolbyname(s); /* TODO: free s? */ |
78 | Unthread(tf->code,i,v); | 84 | Unthread(tf->code,i,v); |
79 | } | 85 | } |
80 | else if (c=='S') | 86 | else if (c=='S') |
81 | { | 87 | { |
82 | int i=LoadWord(D); | 88 | int i=LoadWord(D); |
83 | char *s=LoadString(D); | 89 | char *s=LoadString(D); |
84 | int v=luaI_findconstantbyname(s); | 90 | int v=luaI_findconstantbyname(s); /* TODO: free s? */ |
85 | Unthread(tf->code,i,v); | 91 | Unthread(tf->code,i,v); |
86 | } | 92 | } |
87 | else | 93 | else |
88 | { | 94 | { |
89 | PrintFunction(tf); | 95 | PrintFunction(tf); /* TODO: remove */ |
90 | ungetc(c,D); | 96 | ungetc(c,D); |
91 | return tf; | 97 | return; |
92 | } | 98 | } |
93 | } | 99 | } |
94 | } | 100 | } |
95 | 101 | ||
96 | static void LoadHeader(FILE *D) | 102 | static void LoadHeader(FILE *D) /* TODO: error handling */ |
97 | { | 103 | { |
98 | char *s=LoadString(D); | 104 | char *s=LoadString(D); |
99 | Word w,tw=TEST_WORD; | 105 | Word w,tw=TEST_WORD; |
100 | float f,tf=TEST_FLOAT; | 106 | float f,tf=TEST_FLOAT; |
101 | if (strcmp(s,SIGNATURE)!=0) panic("bad signature"); | 107 | if (strcmp(s,SIGNATURE)!=0) panic("bad signature"); |
108 | luaI_free(s); | ||
102 | getc(D); /* skip version */ | 109 | getc(D); /* skip version */ |
103 | fread(&w,sizeof(w),1,D); /* a word for testing byte ordering */ | 110 | fread(&w,sizeof(w),1,D); /* a word for testing byte ordering */ |
104 | if (w!=tw) warn("different byte order"); | 111 | if (w!=tw) warn("different byte order"); |
@@ -112,8 +119,9 @@ static void LoadChunk(FILE *D) | |||
112 | while (1) | 119 | while (1) |
113 | { | 120 | { |
114 | int c=getc(D); | 121 | int c=getc(D); |
115 | if (c=='F') LoadFunction(D); else { ungetc(c,D); return; } | 122 | if (c=='F') LoadFunction(D); else { ungetc(c,D); break; } |
116 | } | 123 | } |
124 | /* TODO: run Main */ | ||
117 | } | 125 | } |
118 | 126 | ||
119 | void Undump(FILE *D) | 127 | void Undump(FILE *D) |