plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


a596fb1 — Peter John 2026-11-10T11:24:20+05:30
wasm codegen: withoutNames, and a game's web build ships without the name section
compiler/cli/src/main.rs CHANGED
@@ -752,7 +752,7 @@ fn cmdBuild(
752
752
  ) -> Result<()> {
753
753
  let program = program::compileProgram(&file, &libPathOrDefault(lib_path), fusion)?;
754
754
  if program.isGame {
755
- return game::buildGame(&file, program.wasm, gameOptions);
755
+ return game::buildGame(&file, plum_wasm_codegen::withoutNames(&program.wasm), gameOptions);
756
756
  }
757
757
  if let Some(flag) = gameOptions.assets.as_ref().map(|_| "--assets") {
758
758
  anyhow::bail!("{flag} is only for games, and {} is not one", file.display());
compiler/wasm-codegen/src/lib.rs CHANGED
@@ -125,6 +125,31 @@ pub fn compile(source: &ast::Source, opts: &CompileOptions) -> Result<Output, Ve
125
125
  }
126
126
  }
127
127
 
128
+ /// `wasm` without its `name` custom section: the function names wasmtime and browsers show
129
+ /// in a backtrace, about a fifth of a game's bytes. A game's web build (`plum build`) ships
130
+ /// without them; `plum run` and native builds keep them. Bytes that do not parse are
131
+ /// returned as they are.
132
+ pub fn withoutNames(wasm: &[u8]) -> Vec<u8> {
133
+ if wasm.len() < 8 {
134
+ return wasm.to_vec();
135
+ }
136
+ let mut out = wasm[..8].to_vec();
137
+ for payload in wasmparser::Parser::new(0).parse_all(wasm) {
138
+ let Ok(payload) = payload else { return wasm.to_vec() };
139
+ if let wasmparser::Payload::CustomSection(c) = &payload {
140
+ if c.name() == "name" {
141
+ continue;
142
+ }
143
+ }
144
+ if let Some((id, range)) = payload.as_section() {
145
+ out.push(id);
146
+ (range.len() as u32).encode(&mut out);
147
+ out.extend_from_slice(&wasm[range]);
148
+ }
149
+ }
150
+ out
151
+ }
152
+
128
153
  /// `layout <Name>: flat|boxed` for every struct, as decided by `plum_core::layout`.
129
154
  pub fn explainLayouts(source: &ast::Source) -> Vec<String> {
130
155
  source
compiler/wasm-codegen/tests/wasm_codegen/main.rs CHANGED
@@ -9,5 +9,6 @@ mod e2e_types;
9
9
  mod examples_test;
10
10
  mod fusion_alloc_test;
11
11
  mod literals_test;
12
+ mod names_test;
12
13
  mod native_test;
13
14
  mod optimize_test;
compiler/wasm-codegen/tests/wasm_codegen/names_test.rs ADDED
@@ -0,0 +1,45 @@
1
+ //! `withoutNames`: a module without its name section.
2
+
3
+ use plum_test_support::compile;
4
+
5
+ const POINT: &str = "\
6
+ import std/Int
7
+ import std/Str
8
+
9
+ struct Point(x: Int, y: Int, label: Str)
10
+
11
+ fun total(p: Point, n: Int) -> Int =
12
+ if n == 0 then p.x + p.y else total(p, n - 1)
13
+
14
+ fun main() -> Int =
15
+ total(Point(x: 3, y: 4, label: \"p\"), 3)
16
+ ";
17
+
18
+ /// `withoutNames` drops the name section and nothing else: the module still validates,
19
+ /// runs, and is smaller by about the section's size.
20
+ #[test]
21
+ fn withoutNamesDropsOnlyTheNameSection() {
22
+ let bytes = compile("nonames", POINT);
23
+ let stripped = plum_wasm_codegen::withoutNames(&bytes);
24
+ wasmparser::validate(&stripped).expect("valid wasm");
25
+ let customs = |b: &[u8]| -> Vec<String> {
26
+ wasmparser::Parser::new(0)
27
+ .parse_all(b)
28
+ .filter_map(|p| match p.unwrap() {
29
+ wasmparser::Payload::CustomSection(c) => Some(c.name().to_string()),
30
+ _ => None,
31
+ })
32
+ .collect()
33
+ };
34
+ assert!(customs(&bytes).contains(&"name".to_string()));
35
+ assert!(customs(&stripped).is_empty(), "{:?}", customs(&stripped));
36
+ assert!(stripped.len() < bytes.len());
37
+ let engine = plum_test_support::engine();
38
+ let module = wasmtime::Module::new(&engine, &stripped).unwrap();
39
+ let mut store = wasmtime::Store::new(&engine, ());
40
+ let mut linker = wasmtime::Linker::new(&engine);
41
+ linker.define_unknown_imports_as_default_values(&mut store, &module).unwrap();
42
+ let instance = linker.instantiate(&mut store, &module).unwrap();
43
+ let main = instance.get_typed_func::<(), i64>(&mut store, "main").unwrap();
44
+ assert_eq!(main.call(&mut store, ()).unwrap(), 7);
45
+ }