plum

#treesitter#compiler#wasm

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

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


1ef9f2d — Peter John 2026-11-12T22:52:08+05:30
host: PLUM_NULL_GFX answers the game API with zeros in place of trapping
Files changed (1) hide show
  1. compiler/host/src/lib.rs +48 -1
compiler/host/src/lib.rs CHANGED
@@ -13,11 +13,16 @@ pub struct Caps {
13
13
  pub http: bool,
14
14
  pub clock: bool,
15
15
  pub rng: bool,
16
+ /// The game API (`std/game/gfx`, `audio`, input) this host doesn't define answers with
17
+ /// zeros and nulls and draws nothing, in place of trapping: for timing a game's draw walk
18
+ /// on the command line (benchmarks/screen_views). Off unless `PLUM_NULL_GFX` is set in
19
+ /// the environment.
20
+ pub null_gfx: bool,
16
21
  }
17
22
 
18
23
  impl Caps {
19
24
  pub fn all() -> Caps {
20
- Caps { fs: true, http: true, clock: true, rng: true }
25
+ Caps { fs: true, http: true, clock: true, rng: true, null_gfx: std::env::var_os("PLUM_NULL_GFX").is_some() }
21
26
  }
22
27
  }
23
28
 
@@ -566,6 +571,20 @@ pub fn hostImports(
566
571
  anyhow::anyhow!("plum::{n} import must be a function")
567
572
  })?;
568
573
  let name = n.to_string();
574
+ if caps.null_gfx {
575
+ let defaults: Vec<wasmtime::Val> = func_ty
576
+ .results()
577
+ .map(|t| t.default_value().ok_or_else(|| anyhow::anyhow!("plum::{n} returns a type with no default")))
578
+ .collect::<Result<_>>()?;
579
+ return Ok(wasmtime::Extern::Func(wasmtime::Func::new(
580
+ &mut *store,
581
+ func_ty,
582
+ move |_caller, _params, results| {
583
+ results.clone_from_slice(&defaults);
584
+ Ok(())
585
+ },
586
+ )));
587
+ }
569
588
  Ok(wasmtime::Extern::Func(wasmtime::Func::new(
570
589
  &mut *store,
571
590
  func_ty,
@@ -829,4 +848,32 @@ mod tests {
829
848
  assert_eq!(hostImports(&mut store, &module, Caps::default()).unwrap().len(), 2);
830
849
  assert_eq!(hostImports(&mut store, &module, Caps::all()).unwrap().len(), 2);
831
850
  }
851
+
852
+ /// A game import traps on the command line, unless `null_gfx` is on: then it answers
853
+ /// zero and draws nothing.
854
+ #[test]
855
+ fn gameImportsTrapUnlessNullGfx() {
856
+ let wat = r#"(module
857
+ (import "plum" "screenWidth" (func $w (param anyref) (result f64)))
858
+ (import "plum" "drawRect" (func $r (param anyref f64)))
859
+ (func (export "go") (result f64)
860
+ (call $r (ref.null any) (f64.const 1))
861
+ (call $w (ref.null any))))"#;
862
+ let engine = engine();
863
+ let module = wasmtime::Module::new(&engine, wat::parse_str(wat).unwrap()).unwrap();
864
+ for null_gfx in [false, true] {
865
+ let mut store = wasmtime::Store::new(&engine, ());
866
+ let caps = Caps { null_gfx, ..Caps::default() };
867
+ let imports = hostImports(&mut store, &module, caps).unwrap();
868
+ let inst = wasmtime::Instance::new(&mut store, &module, &imports).unwrap();
869
+ let go = inst.get_typed_func::<(), f64>(&mut store, "go").unwrap();
870
+ let got = go.call(&mut store, ());
871
+ if null_gfx {
872
+ assert_eq!(got.unwrap(), 0.0);
873
+ } else {
874
+ assert!(format!("{:#}", got.unwrap_err()).contains("'drawRect' is not available here"));
875
+ }
876
+ }
877
+ }
878
+
832
879
  }