summaryrefslogtreecommitdiff
path: root/2026/games_submissions/liquidcake1/wasm.mjs
blob: 18bf54744dbfefa081dacece32f62be76a72a62f (plain)
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
export function load_wasm(interpreter) {
  /* 
	(memory $memory 1)
	(export "memory" (memory $memory))
	(func (export "load_first_item_in_mem") (param) (result i32)
	  i32.const 0

	  ;; load first item in memory and return the result
	  i32.load
	  ;; store 10000 in the first location in memory
	  i32.const 0
	  i32.const 10000
	  i32.store
	)
	*/
  /*
	(module
(func $pop (import "my_namespace" "pop") (param i32) (result i32))
(func $push (import "my_namespace" "push") (param i32 i32))
(func (export "plus") (param i32)
  local.get 0
  local.get 0
  call $pop
  local.get 0
  call $pop
  i32.add
  call $push
))
*/
  //let wasm_string = "AGFzbQEAAAABBwFgAn9/AX8DAgEABwoBBmFkZFR3bwAACgkBBwAgACABagsACgRuYW1lAgMBAAA=";
  let wasm_string = "AGFzbQEAAAABDwNgAX8Bf2ACf38AYAF/AAIoAgxteV9uYW1lc3BhY2UDcG9wAAAMbXlfbmFtZXNwYWNlBHB1c2gAAQMCAQIHCAEEcGx1cwACChEBDwAgACAAEAAgABAAahABCwAcBG5hbWUBDAIAA3BvcAEEcHVzaAIHAwAAAQACAA==";
  // Fake minus version
  //let wasm_string = "AGFzbQEAAAABDwNgAX8Bf2ACf38AYAF/AAIoAgxteV9uYW1lc3BhY2UDcG9wAAAMbXlfbmFtZXNwYWNlBHB1c2gAAQMCAQIHCAEEcGx1cwACChEBDwAgACAAEAAgABAAaxABCwAcBG5hbWUBDAIAA3BvcAEEcHVzaAIHAwAAAQACAA==";
  let wasm_array = new TextEncoder().encode(atob(wasm_string))
  const importObject = {
    my_namespace: {
      pop: threadNo => interpreter.threads[threadNo].pop(),
      push: (threadNo, i) => interpreter.threads[threadNo].stack.push(i),
    }
  };
  return WebAssembly.compile(wasm_array).then((mod) =>
    WebAssembly.instantiate(mod, importObject).then(instance => {
      patch_interpreter(interpreter, instance);
      return instance;
    })
  );
}
function patch_interpreter(interpreter, instance) {
  interpreter.instructions_raw["+"].impl = function (thread, thread_num) { instance.exports.plus(thread_num); };
  interpreter.instructions["+".charCodeAt(0)] = function (thread, thread_num) { instance.exports.plus(thread_num); };
}