initial commit

This commit is contained in:
vivlim 2023-12-01 23:44:13 -08:00
commit e9f7255e37
12 changed files with 189 additions and 0 deletions

1
.direnv/flake-profile Symbolic link
View File

@ -0,0 +1 @@
flake-profile-1-link

View File

@ -0,0 +1 @@
/nix/store/2kfkxvijh8lzci4lxlhl1hbb6aj48wmy-nix-shell-env

1
.envrc Normal file
View File

@ -0,0 +1 @@
use flake .

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.wasm
.scratch/
answers/*
inputs/*

18
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/run.js",
"preLaunchTask": "make"
}
]
}

10
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,10 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "process",
"command": "make",
"label": "make"
}
]
}

BIN
1a.wasm Normal file

Binary file not shown.

26
Makefile Normal file
View File

@ -0,0 +1,26 @@
BIN_DIR := ./bin
WATMODULES := $(shell find ./wat -name '*.wat')
# for each .wat file, build path of the resulting .wasm
WASMS := $(WATMODULES:.wat=.wasm)
DAY_INPUTS := $(shell find ./inputs -type f)
# for each input define the answer path
DAY_ANSWERS := ${subst ./inputs,./answers,$(DAY_INPUTS)}
all: $(DAY_ANSWERS)
answers:
mkdir -p answers
answers/%: wat/%.wasm inputs/% | answers
node runwat.js $? $@:
%.wasm: %.wat
wat2wasm $< -o $@
clean: FORCE
rm -f $(WASMS)
rm -f answers/*
FORCE: ;

27
flake.lock Normal file
View File

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1701336116,
"narHash": "sha256-kEmpezCR/FpITc6yMbAh4WrOCiT2zg5pSjnKrq51h5Y=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "f5c27c6136db4d76c30e533c20517df6864c46ee",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

20
flake.nix Normal file
View File

@ -0,0 +1,20 @@
{
description = "viv's aoc 2023 environment";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
outputs = { self, nixpkgs }:
let
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
pkgs = import nixpkgs { inherit system; };
});
in
{
devShells = forEachSupportedSystem ({ pkgs }: {
default = pkgs.mkShell {
packages = with pkgs; [ wabt nodejs ];
};
});
};
}

60
runwat.js Normal file
View File

@ -0,0 +1,60 @@
(async function main() {
const fs = require('fs').promises;
const path = require('path');
const wasmfile = process.argv[2];
const datafile = process.argv[3];
const resultfile = process.argv[4];
if (!wasmfile.endsWith(".wasm"))
throw new Error(`${wasmfile} isn't a .wasm file`)
const basename = path.basename(wasmfile, ".wasm");
console.log(`=== ${basename} ===`);
// Make sure it and the input data exist
await fs.stat(wasmfile);
await fs.stat(datafile);
// Allocate linear memory
const memory = new WebAssembly.Memory({ initial: 1 });
// Collect imports
const moduleImports = {
console: {
log(offset, length) {
const bytes = new Uint8Array(memory.buffer, offset, length);
const string = new TextDecoder("utf8").decode(bytes);
console.log(string);
},
},
js: {
mem: memory,
},
};
// Read the data and load it into memory.
const data = await fs.readFile(datafile, "utf8");
const bytes = new TextEncoder("utf8").encode(data);
// Load data into memory.
// The offset where data has been loaded.
const offset = 2;
memory.buffer[0] = offset;
// The size of the loaded data.
memory.buffer[1] = bytes.length;
// Actually load it.
for (let i = 0; i < bytes.length; i++){
memory.buffer[offset+i] = bytes[i];
}
// Load module
const wasmBuffer = await fs.readFile(wasmfile);
const wasmModule = await WebAssembly.instantiate(wasmBuffer, moduleImports);
wasmModule.instance.exports.writeHi();
})();

21
wat/1a.wat Normal file
View File

@ -0,0 +1,21 @@
(module
(import "console" "log" (func $log (param i32 i32)))
(import "js" "mem" (memory 1)) ;; 1 page (64KB)
(global $source_data_offset i32 (i32.const 0))
(global $source_data_size i32 (i32.const 0))
(i32.const 0)
i32.load
(global.set $source_data_offset)
(i32.const 1)
i32.load
(global.set $source_data_size)
;;(data (i32.const 0) "Hi")
(func $main
global.get $source_data_offset
i32.const 5
call $log
)
(start $main)
)