plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
e2e893e
— Peter John
2026-11-11T01:21:19+05:30
codegen: scan for crash sites while the AST is formatted, not after
compiler/wasm-codegen/src/driver.rs
CHANGED
|
@@ -442,7 +442,7 @@ impl<'a> Setup<'a> {
|
|
|
442
442
|
let Setup { source, module, func_ids, .. } = self;
|
|
443
443
|
// `plum::crash(Str)`
|
|
444
444
|
{
|
|
445
|
-
let uses_crash =
|
|
445
|
+
let uses_crash = itemsUseCrash(&source.items);
|
|
446
446
|
if uses_crash {
|
|
447
447
|
let str_vt = plumTypeToValtype(&strType());
|
|
448
448
|
let type_idx = module.addType(&[str_vt], &[]);
|
compiler/wasm-codegen/src/keys.rs
CHANGED
|
@@ -44,26 +44,129 @@ pub(crate) const CRASH_FN_KEY: &str = "__crash";
|
|
|
44
44
|
/// Whether the `Debug` dump of the AST contains a `Crash(` constructor token
|
|
45
45
|
/// outside any quoted string (string literals and identifiers are quoted in
|
|
46
46
|
/// `Debug` output, so they can't produce a false match).
|
|
47
|
+
#[cfg(test)]
|
|
47
48
|
pub(crate) fn sourceUsesCrash(dbg: &str) -> bool {
|
|
49
|
+
let mut scan = CrashScan::default();
|
|
50
|
+
let _ = std::fmt::Write::write_str(&mut scan, dbg);
|
|
51
|
+
scan.found
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/// `sourceUsesCrash` of `items`' `Debug` dump, read as it is formatted: the dump (megabytes for
|
|
55
|
+
/// a game) is never held, and formatting stops at the first match.
|
|
56
|
+
pub(crate) fn itemsUseCrash(items: &[plum_core::ast::Item]) -> bool {
|
|
57
|
+
let mut scan = CrashScan::default();
|
|
58
|
+
let _ = std::fmt::Write::write_fmt(&mut scan, format_args!("{items:?}"));
|
|
59
|
+
scan.found
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/// The scan of `sourceUsesCrash` over text that arrives in pieces: outside a quoted string
|
|
63
|
+
/// (a `\` in one skips the next byte), a `Crash(` or `Bang(` right after `[`, `(`, a space
|
|
64
|
+
/// or the start.
|
|
65
|
+
#[derive(Default)]
|
|
66
|
+
struct CrashScan {
|
|
67
|
+
inStr: bool,
|
|
68
|
+
esc: bool,
|
|
69
|
+
/// The last bytes seen, newest last (enough for `Crash(` and the byte before it).
|
|
70
|
+
tail: [u8; 7],
|
|
71
|
+
/// How many bytes have been seen.
|
|
72
|
+
seen: usize,
|
|
73
|
+
found: bool,
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
impl CrashScan {
|
|
77
|
+
fn ends(&self, word: &[u8]) -> bool {
|
|
48
|
-
|
|
78
|
+
let n = word.len();
|
|
79
|
+
if self.seen < n || self.tail[7 - n..] != *word {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
self.seen == n || matches!(self.tail[6 - n], b'[' | b'(' | b' ')
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
impl std::fmt::Write for CrashScan {
|
|
87
|
+
fn write_str(&mut self, s: &str) -> std::fmt::Result {
|
|
88
|
+
for &c in s.as_bytes() {
|
|
89
|
+
self.tail.copy_within(1.., 0);
|
|
90
|
+
self.tail[6] = c;
|
|
49
|
-
|
|
91
|
+
self.seen += 1;
|
|
92
|
+
if self.inStr {
|
|
50
|
-
|
|
93
|
+
if self.esc {
|
|
51
|
-
|
|
94
|
+
self.esc = false;
|
|
52
|
-
b'"' => {
|
|
53
|
-
i += 1;
|
|
54
|
-
while i < b.len() && b[i] != b'"' {
|
|
55
|
-
|
|
95
|
+
} else if c == b'\\' {
|
|
96
|
+
self.esc = true;
|
|
97
|
+
} else if c == b'"' {
|
|
98
|
+
self.inStr = false;
|
|
56
99
|
}
|
|
100
|
+
} else if c == b'"' {
|
|
57
|
-
|
|
101
|
+
self.inStr = true;
|
|
102
|
+
} else if c == b'(' && (self.ends(b"Crash(") || self.ends(b"Bang(")) {
|
|
103
|
+
self.found = true;
|
|
104
|
+
// stops the formatting: the answer is known
|
|
105
|
+
return Err(std::fmt::Error);
|
|
58
106
|
}
|
|
107
|
+
}
|
|
108
|
+
Ok(())
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#[cfg(test)]
|
|
113
|
+
mod crashScanTests {
|
|
114
|
+
/// The scan `CrashScan` replaced, on the whole text at once.
|
|
115
|
+
fn whole(dbg: &str) -> bool {
|
|
116
|
+
let b = dbg.as_bytes();
|
|
117
|
+
let mut i = 0;
|
|
118
|
+
while i < b.len() {
|
|
119
|
+
match b[i] {
|
|
120
|
+
b'"' => {
|
|
121
|
+
i += 1;
|
|
122
|
+
while i < b.len() && b[i] != b'"' {
|
|
123
|
+
i += if b[i] == b'\\' { 2 } else { 1 };
|
|
124
|
+
}
|
|
125
|
+
i += 1;
|
|
126
|
+
}
|
|
59
|
-
|
|
127
|
+
b'C' | b'B'
|
|
60
|
-
|
|
128
|
+
if (dbg[i..].starts_with("Crash(") || dbg[i..].starts_with("Bang("))
|
|
61
|
-
|
|
129
|
+
&& (i == 0 || matches!(b[i - 1], b'[' | b'(' | b' ')) =>
|
|
62
|
-
|
|
130
|
+
{
|
|
63
|
-
|
|
131
|
+
return true
|
|
132
|
+
}
|
|
133
|
+
_ => i += 1,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
false
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
#[test]
|
|
140
|
+
fn piecesAnswerAsTheWholeText() {
|
|
141
|
+
let cases = [
|
|
142
|
+
"Crash(x)",
|
|
143
|
+
"Bang(x)",
|
|
144
|
+
"[Crash(x)]",
|
|
145
|
+
" Bang(",
|
|
146
|
+
"xCrash(",
|
|
147
|
+
"\"Crash(\"",
|
|
148
|
+
"\"a\\\"Crash(\" Crash(",
|
|
149
|
+
"\"a\\\\\" Crash(",
|
|
150
|
+
"\"unterminated Crash(",
|
|
151
|
+
"Crash",
|
|
152
|
+
"(Bang(Crash(",
|
|
153
|
+
"Stmt { kind: Crash(Expr { id: 1 }) }",
|
|
154
|
+
"Name(\"Crash(\")",
|
|
155
|
+
"aBang(",
|
|
156
|
+
"",
|
|
157
|
+
];
|
|
158
|
+
for text in cases {
|
|
159
|
+
let want = whole(text);
|
|
160
|
+
assert_eq!(super::sourceUsesCrash(text), want, "{text:?}");
|
|
161
|
+
// every split into two pieces
|
|
162
|
+
for k in 0..=text.len() {
|
|
163
|
+
let mut scan = super::CrashScan::default();
|
|
164
|
+
let a = std::fmt::Write::write_str(&mut scan, &text[..k]);
|
|
165
|
+
if a.is_ok() {
|
|
166
|
+
let _ = std::fmt::Write::write_str(&mut scan, &text[k..]);
|
|
167
|
+
}
|
|
168
|
+
assert_eq!(scan.found, want, "{text:?} split at {k}");
|
|
64
169
|
}
|
|
65
|
-
_ => i += 1,
|
|
66
170
|
}
|
|
67
171
|
}
|
|
68
|
-
false
|
|
69
172
|
}
|