-
Notifications
You must be signed in to change notification settings - Fork 34
/
source.c
221 lines (174 loc) · 4.94 KB
/
source.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Copyright (C) 2021 Jo-Philipp Wich <jo@mein.io>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include <errno.h>
#include "ucode/source.h"
#include "ucode/platform.h"
uc_source_t *
uc_source_new_file(const char *path)
{
FILE *fp = fopen(path, "rb");
uc_source_t *src;
if (!fp)
return NULL;
src = xalloc(ALIGN(sizeof(*src)) + strlen(path) + 1);
src->header.type = UC_SOURCE;
src->header.refcount = 1;
src->fp = fp;
src->buffer = NULL;
src->filename = strcpy((char *)src + ALIGN(sizeof(*src)), path);
src->runpath = src->filename;
src->lineinfo.count = 0;
src->lineinfo.entries = NULL;
return src;
}
uc_source_t *
uc_source_new_buffer(const char *name, char *buf, size_t len)
{
FILE *fp = fmemopen(buf, len, "rb");
uc_source_t *src;
if (!fp)
return NULL;
src = xalloc(ALIGN(sizeof(*src)) + strlen(name) + 1);
src->header.type = UC_SOURCE;
src->header.refcount = 1;
src->fp = fp;
src->buffer = buf;
src->filename = strcpy((char *)src + ALIGN(sizeof(*src)), name);
src->lineinfo.count = 0;
src->lineinfo.entries = NULL;
return src;
}
size_t
uc_source_get_line(uc_source_t *source, size_t *offset)
{
uc_lineinfo_t *lines = &source->lineinfo;
size_t i, pos = 0, line = 1, lastoff = 0;
for (i = 0; i <= lines->count; i++) {
if (pos >= *offset || i == lines->count) {
*offset = (*offset - lastoff) + 1;
return line;
}
/* don't count first line jump as actual byte */
if (i > 0 && (lines->entries[i] & 0x80)) {
line++;
pos++;
lastoff = pos;
}
pos += (lines->entries[i] & 0x7f);
}
return 0;
}
uc_source_type_t
uc_source_type_test(uc_source_t *source)
{
union { char s[sizeof(uint32_t)]; uint32_t n; } buf = { 0 };
uc_source_type_t type = UC_SOURCE_TYPE_PLAIN;
FILE *fp = source->fp;
size_t rlen;
int c = 0;
if (fread(buf.s, 1, 2, fp) == 2 && !strncmp(buf.s, "#!", 2)) {
source->off += 2;
while ((c = fgetc(fp)) != EOF) {
source->off++;
if (c == '\n')
break;
}
}
else {
if (fseek(fp, 0L, SEEK_SET) == -1)
fprintf(stderr, "Failed to rewind source buffer: %s\n", strerror(errno));
}
rlen = fread(buf.s, 1, 4, fp);
if (rlen == 4 && buf.n == htobe32(UC_PRECOMPILED_BYTECODE_MAGIC)) {
type = UC_SOURCE_TYPE_PRECOMPILED;
}
else {
if (c == '\n') {
uc_source_line_update(source, source->off - 1);
uc_source_line_next(source);
}
else {
uc_source_line_update(source, source->off);
}
}
if (fseek(fp, -(long)rlen, SEEK_CUR) == -1)
fprintf(stderr, "Failed to rewind source buffer: %s\n", strerror(errno));
return type;
}
/* lineinfo is encoded in bytes: the most significant bit specifies whether
* to advance the line count by one or not, while the remaining 7 bits encode
* the amounts of bytes on the current line.
*
* If a line has more than 127 characters, the first byte will be set to
* 0xff (1 1111111) and subsequent bytes will encode the remaining characters
* in bits 1..7 while setting bit 8 to 0. A line with 400 characters will thus
* be encoded as 0xff 0x7f 0x7f 0x13 (1:1111111 + 0:1111111 + 0:1111111 + 0:1111111).
*
* The newline character itself is not counted, so an empty line is encoded as
* 0x80 (1:0000000).
*/
void
uc_source_line_next(uc_source_t *source)
{
uc_vector_push(&source->lineinfo, 0x80);
}
void
uc_source_line_update(uc_source_t *source, size_t off)
{
uc_lineinfo_t *lines = &source->lineinfo;
uint8_t *entry, n;
if (!lines->count)
uc_source_line_next(source);
entry = uc_vector_last(lines);
if ((entry[0] & 0x7f) + off <= 0x7f) {
entry[0] += off;
}
else {
off -= (0x7f - (entry[0] & 0x7f));
entry[0] |= 0x7f;
while (off > 0) {
n = (off > 0x7f) ? 0x7f : off;
uc_vector_push(lines, n);
off -= n;
}
}
}
void
uc_source_runpath_set(uc_source_t *source, const char *runpath)
{
if (source->runpath != source->filename)
free(source->runpath);
source->runpath = runpath ? xstrdup(runpath) : NULL;
}
bool
uc_source_export_add(uc_source_t *source, uc_value_t *name)
{
ssize_t idx = uc_source_export_lookup(source, name);
if (idx > -1)
return false;
uc_vector_push(&source->exports, ucv_get(name));
return true;
}
ssize_t
uc_source_export_lookup(uc_source_t *source, uc_value_t *name)
{
size_t i;
for (i = 0; i < source->exports.count; i++)
if (ucv_is_equal(source->exports.entries[i], name))
return i;
return -1;
}