forked from guybedford/es-module-lexer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
440 lines (396 loc) · 10.9 KB
/
lib.rs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use bumpalo::Bump;
use core::alloc::Layout;
use std::{borrow::Cow, ffi::c_void, marker::PhantomData, mem::MaybeUninit, ptr, sync::atomic::AtomicPtr};
type Allocate = unsafe extern "C" fn(bytes: u32, user_data: *mut c_void) -> *mut c_void;
extern "C" {
fn parse(ptr: *const u8, len: u32, alloc: Allocate, user_data: *mut c_void, result: *mut ParseResult) -> bool;
}
#[repr(C)]
pub struct Import<'a> {
start: *const u8,
end: *const u8,
statement_start: *const u8,
statement_end: *const u8,
assert_index: *const u8,
dynamic: *const u8,
safe: bool,
next: *const Import<'a>,
phantom: PhantomData<&'a ()>,
}
unsafe impl<'a> Send for Import<'a> {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImportKind {
Standard,
DynamicString,
DynamicExpression,
Meta,
}
impl<'a> Import<'a> {
pub fn specifier(&self) -> Cow<'a, str> {
let (start, end) = if self.kind() == ImportKind::DynamicString {
unsafe { (self.start.offset(1), self.end.offset(-1)) }
} else {
(self.start, self.end)
};
let s =
unsafe { std::str::from_utf8_unchecked(std::slice::from_raw_parts(start, end as usize - start as usize)) };
if matches!(self.kind(), ImportKind::Standard | ImportKind::DynamicString) {
unescape(s).unwrap_or(Cow::Borrowed(s))
} else {
Cow::Borrowed(s)
}
}
pub fn statement(&self) -> &'a str {
unsafe {
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
self.statement_start,
self.statement_end as usize - self.statement_start as usize,
))
}
}
pub fn kind(&self) -> ImportKind {
if self.dynamic as usize == 1 {
ImportKind::Standard
} else if self.dynamic as usize == 2 {
ImportKind::Meta
} else if self.safe {
ImportKind::DynamicString
} else {
ImportKind::DynamicExpression
}
}
}
fn unescape<'a>(s: &'a str) -> Result<Cow<'a, str>, ()> {
let mut cow = Cow::Borrowed(s);
let bytes = s.as_bytes();
let mut i = 0;
let mut si = 0;
while i < bytes.len() {
let start = i;
let b = bytes[i];
if b == b'\\' && i + 1 < bytes.len() {
i += 1;
let c = match bytes[i] {
b'n' => '\n',
b'r' => '\r',
b't' => '\t',
b'b' => 8 as char,
b'v' => 11 as char,
b'f' => 12 as char,
b'\r' | b'\n' => {
// Unicode new line characters after \ get removed from output in both
// template literals and strings
cow.to_mut().replace_range(si..si + 2, "");
i += 1;
continue;
}
b'x' => {
i += 1;
read_hex(bytes, &mut i, 2)?
}
b'u' if i + 1 < bytes.len() => {
i += 1;
if bytes[i] == b'{' {
i += 1;
if let Some(end) = bytes[i..].iter().position(|c| *c == b'}') {
let res = read_hex(bytes, &mut i, end)?;
i += 1;
res
} else {
return Err(());
}
} else {
read_hex(bytes, &mut i, 4)?
}
}
c if c >= b'0' && c <= b'7' => {
let mut total = c - b'0';
i += 1;
for _ in 0..2 {
if i < bytes.len() && bytes[i] >= b'0' && bytes[i] <= b'7' {
if let Some(t) = total.checked_mul(8).and_then(|t| t.checked_add(bytes[i] - b'0')) {
total = t;
i += 1;
} else {
break;
}
} else {
break;
}
}
if i < bytes.len() && (bytes[i] == b'8' || bytes[i] == b'9') {
return Err(());
}
total as char
}
c => c as char,
};
let mut buf = [0; 4];
let r = c.encode_utf8(&mut buf);
cow.to_mut().replace_range(si..si + i - start, r);
si += r.len();
} else {
si += 1;
i += 1;
}
}
Ok(cow)
}
fn read_hex(bytes: &[u8], pos: &mut usize, len: usize) -> Result<char, ()> {
let start = *pos;
let mut total: u32 = 0;
let mut last_code = 0;
for _ in 0..len {
let code = bytes[*pos];
if code == b'_' {
if last_code == b'_' {
return Err(());
}
last_code = code;
*pos += 1;
continue;
}
let val = if code >= b'a' {
code - b'a' + 10
} else if code >= b'A' {
code - b'A' + 10
} else if code >= b'0' && code <= b'9' {
code - b'0'
} else {
break;
};
if val >= 16 {
break;
}
last_code = code;
total = total * 16 + (val as u32);
*pos += 1;
}
if last_code == b'_' || *pos - start != len {
return Err(());
}
Ok(unsafe { char::from_u32_unchecked(total) })
}
impl<'a> NextPtr for Import<'a> {
fn next(&self) -> *const Self {
self.next
}
}
#[repr(C)]
pub struct Export {
start: *const u8,
end: *const u8,
local_start: *const u8,
local_end: *const u8,
next: *const Export,
}
impl NextPtr for Export {
fn next(&self) -> *const Self {
self.next
}
}
impl Export {
pub fn exported(&self) -> &str {
unsafe {
std::str::from_utf8_unchecked(std::slice::from_raw_parts(
self.start,
self.end as usize - self.start as usize,
))
}
}
pub fn local(&self) -> Option<&str> {
if self.local_start.is_null() {
return None;
}
unsafe {
Some(std::str::from_utf8_unchecked(std::slice::from_raw_parts(
self.local_start,
self.local_end as usize - self.local_start as usize,
)))
}
}
}
#[repr(C)]
struct ParseResult<'a> {
first_import: *const Import<'a>,
first_export: *const Export,
parse_error: u32,
}
pub struct LexResult<'a> {
bump: Bump,
first_import: *const Import<'a>,
first_export: *const Export,
}
impl<'a> LexResult<'a> {
pub fn imports(&'a self) -> ResultIter<'a, Import> {
ResultIter {
ptr: AtomicPtr::new(self.first_import as *mut Import),
lifetime: PhantomData,
}
}
pub fn exports(&self) -> ResultIter<Export> {
ResultIter {
ptr: AtomicPtr::new(self.first_export as *mut Export),
lifetime: PhantomData,
}
}
}
trait NextPtr {
fn next(&self) -> *const Self;
}
pub struct ResultIter<'a, T> {
ptr: AtomicPtr<T>,
lifetime: PhantomData<&'a T>,
}
unsafe impl<'a, T: Send> Send for ResultIter<'a, T> {}
impl<'a, T: NextPtr> Iterator for ResultIter<'a, T> {
type Item = &'a mut T;
fn next(&mut self) -> Option<Self::Item> {
let ptr = self.ptr.load(std::sync::atomic::Ordering::Relaxed);
if ptr.is_null() {
return None;
}
let item = unsafe { &mut *ptr };
self.ptr.store(item.next() as *mut T, std::sync::atomic::Ordering::Relaxed);
Some(item)
}
}
unsafe extern "C" fn alloc(bytes: u32, user_data: *mut c_void) -> *mut c_void {
let bump: &mut Bump = &mut *(user_data as *mut Bump);
let align = std::mem::align_of::<usize>();
let layout = Layout::from_size_align_unchecked(bytes as usize, align);
bump.alloc_layout(layout).as_ptr() as *mut c_void
}
pub fn lex<'a>(code: &'a str) -> Result<LexResult<'a>, usize> {
let code_ptr = code.as_ptr();
let mut res = LexResult {
bump: Bump::new(),
first_import: ptr::null(),
first_export: ptr::null(),
};
let mut result: ParseResult = unsafe { MaybeUninit::zeroed().assume_init() };
let success = unsafe {
parse(
code_ptr,
code.len() as u32,
alloc,
&mut res.bump as *mut Bump as *mut c_void,
&mut result as *mut ParseResult,
)
};
if success {
res.first_import = result.first_import;
res.first_export = result.first_export;
return Ok(res);
}
return Err(result.parse_error as usize);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn esm() {
let res = lex(
r#"
import bar from "foo";
export * as foo from "yoooo";
export {test as hi};
import("./bar");
import('./baz');
import(`./a`);
import(hi);
import('./test/' + foo);
import(`./test/${foo}`);
"#,
)
.unwrap();
let imports: Vec<(Cow<'_, str>, ImportKind)> = res.imports().map(|i| (i.specifier(), i.kind())).collect();
let imports: Vec<(&str, ImportKind)> = imports.iter().map(|(i, k)| (i.as_ref(), *k)).collect();
assert_eq!(
imports,
vec![
("foo", ImportKind::Standard),
("yoooo", ImportKind::Standard),
("./bar", ImportKind::DynamicString),
("./baz", ImportKind::DynamicString),
("./a", ImportKind::DynamicString),
("hi", ImportKind::DynamicExpression),
("'./test/' + foo", ImportKind::DynamicExpression),
("`./test/${foo}`", ImportKind::DynamicExpression),
]
);
}
#[test]
fn requires() {
let res = lex(
r#"
require("./foo");
require('./bar');
require(`./baz`);
require(/* something*/ 'yo');
require(
'test'
);
require(hi);
require('foo/' + bar);
require(`foo/${bar}`);
require('foo', bar);
foo(require);
foo.require(); // ignored
let req = require;
req('foo');
var require = 2;
require.cache; // ignored
require.resolve('foo'); // ignored
var required = 'hi'; // ignored
var test_require = 'hi'; // ignored
var x = {require: true}; // ignored
var x = {require : true}; // ignored
var x = {require(test) {}}; // ignored
var x = {require};
// require (ignored)
/* require (ignored) */
var req = true ? require : false; // TODO
if (true) {
require('bar');
}
if (typeof require !== 'undefined') {
crypto = require('crypto');
}
require('./\u{20204}.js');
require('./\x61\x62\x63.js');
require('./\251.js');
require('./foo\
.js');
"#,
)
.unwrap();
let imports: Vec<(Cow<'_, str>, ImportKind)> = res.imports().map(|i| (i.specifier(), i.kind())).collect();
let imports: Vec<(&str, ImportKind)> = imports.iter().map(|(i, k)| (i.as_ref(), *k)).collect();
assert_eq!(
imports,
vec![
("./foo", ImportKind::DynamicString),
("./bar", ImportKind::DynamicString),
("./baz", ImportKind::DynamicString),
("yo", ImportKind::DynamicString),
("test", ImportKind::DynamicString),
("hi", ImportKind::DynamicExpression),
("'foo/' + bar", ImportKind::DynamicExpression),
("`foo/${bar}`", ImportKind::DynamicExpression),
("'foo', bar", ImportKind::DynamicExpression),
("", ImportKind::DynamicExpression),
("", ImportKind::DynamicExpression),
("", ImportKind::DynamicExpression),
("", ImportKind::DynamicExpression),
("bar", ImportKind::DynamicString),
("", ImportKind::DynamicExpression),
("crypto", ImportKind::DynamicString),
("./𠈄.js", ImportKind::DynamicString),
("./abc.js", ImportKind::DynamicString),
("./©.js", ImportKind::DynamicString),
("./foo.js", ImportKind::DynamicString),
]
);
}
}