Skip to content

Commit 2a53caa

Browse files
refactor(wasm): Clean input unit test for wasm release.
1 parent f03c59d commit 2a53caa

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

compiler/src/modules/vm/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub struct VM<'a> {
6161
pub output: Vec<String>,
6262
pub input_buffer: Vec<String>,
6363
pub event_queue: Vec<Val>,
64+
pub strict_input: bool,
6465
}
6566

6667
impl<'a> VM<'a> {
@@ -227,6 +228,7 @@ impl<'a> VM<'a> {
227228
pending_kw_delta: 0,
228229
yielded: false,
229230
resume_ip: 0,
231+
strict_input: false,
230232
output: Vec::new(),
231233
input_buffer: Vec::new(),
232234
event_queue: Vec::new(),

compiler/src/wasm.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#![cfg_attr(target_arch = "wasm32", no_std)] // Enables no_std only for WASM builds.
2+
3+
extern crate alloc; // Enables heap allocation without the standard library.
4+
15
#[cfg(target_arch = "wasm32")]
26
mod runtime {
37
use lol_alloc::LeakingPageAllocator;
@@ -51,6 +55,7 @@ mod runtime {
5155
} else {
5256
crate::modules::vm::optimizer::constant_fold(&mut chunk);
5357
let mut vm = VM::with_limits(&chunk, Limits::sandbox());
58+
vm.strict_input = true;
5459
// Feed input buffer from host
5560
let inp_len = unsafe { INP_LEN };
5661
if inp_len > 0 {
@@ -112,22 +117,31 @@ mod tests {
112117
assert!(errs.is_empty(), "parse error on {:?}: {:?}", case.src, errs.iter().map(|e| &e.msg).collect::<Vec<_>>());
113118

114119
let mut vm = VM::new(&chunk);
120+
vm.strict_input = true;
121+
// Cases that supply an input buffer feed the VM normally; the
122+
// strict_input flag only triggers when the VM tries to read past
123+
// the end of the buffer (i.e. the WASM "no host data" path).
115124
vm.input_buffer = case.input.clone();
116-
// WASM: input tests should produce a RuntimeError
117-
let expect_input_error = case.input.len() > 0 && cfg!(target_arch = "wasm32");
125+
let expects_input_error = case.input.is_empty()
126+
&& (case.src.contains("input(") || case.src.contains("input ("));
118127

119128
match vm.run() {
120129
Ok(_) => {
130+
assert!(
131+
!expects_input_error,
132+
"expected input() to error under WASM strict mode for: {:?}", case.src
133+
);
121134
assert_eq!(vm.output, case.output, "output mismatch on: {:?}", case.src);
122135
}
123136
Err(e) => match &case.error {
124137
Some(expected) => assert!(
125138
e.to_string().contains(expected.as_str()),
126139
"wrong error on {:?}: got '{}', expected '{}'", case.src, e, expected
127140
),
128-
None if expect_input_error => assert!(
141+
None if expects_input_error => assert!(
129142
e.to_string().contains("input"),
130-
"expected input RuntimeError on wasm32 for: {:?}", case.src
143+
"expected input RuntimeError under WASM strict mode for: {:?}, got: {}",
144+
case.src, e
131145
),
132146
None => panic!("VM error on {:?}: {}", case.src, e),
133147
}

demo/style.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
/* Layout & Scroll */
12
/* Layout and Scroll */
23

4+
.no-scrollbar::-webkit-scrollbar { display: none }
5+
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none }
6+
#ed { tab-size: 4; white-space: pre !important; outline: 0; overflow: auto }
37
.no-scrollbar::-webkit-scrollbar { display: none; }
48
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
59
#ed { min-width: 0; tab-size: 4; white-space: pre !important; outline: none; font-variant-ligatures: none; overflow-x: auto; overflow-y: auto; }
610

711
/* Type */
812

13+
.code-font { font: 1.625/1 'JetBrains Mono', 'Fira Code', monospace; font-variant-ligatures: none }
914
.code-font {
1015
font-family: 'JetBrains Mono', 'Fira Code', ui-monospace, monospace;
1116
line-height: 1.625;
1217
}
1318

14-
/* Syntax */
19+
/* Visibility */
1520

1621
.tk-kw { color: #c586c0 }
1722
.tk-lit { color: #569cd6 }

0 commit comments

Comments
 (0)