Skip to content

Commit

Permalink
test: iniquity
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpie123 committed May 13, 2022
1 parent a67aa1f commit a19ab64
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 54 deletions.
110 changes: 56 additions & 54 deletions server/public/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,43 @@ let STDIN = []
let STDOUT = []
const output = document.getElementById("res")
let result = ""
const importObject = { io: {read: readByte, write: writeByte, peek: peekByte},
err: {error: error}};
const importObject = {
io: { read: readByte, write: writeByte, peek: peekByte },
err: { error: error }
};

function error() {
output.innerHTML = output.innerHTML + "\n-----------------\n| RUNTIME ERROR |\n-----------------"
throw "Runtime Error"
}

function run(){
output.innerHTML = " "
input = document.getElementById("inputbox").value;
(async () => {
console.log("running 'run'");
const response = await fetch('main.wasm');
const buffer = await response.arrayBuffer();
const module = new WebAssembly.Module(buffer);
const instance = new WebAssembly.Instance(module, importObject);
console.log("input: ", input);
console.log("input type: ", typeof input);
STDIN = Array.from(ENCODER.encode(input));
console.log("STDIN: ", STDIN);
console.log("STDOUT: ", STDOUT);
const rawResult = instance.exports.main(42);
const memory = instance.exports.memory;
var i32 = new Uint32Array(memory.buffer)
console.log(i32)
console.log(memory.buffer)
flushSTDOUT();
console.log("raw: ", rawResult);
const unwrappedResult = unwrap(rawResult);
console.log("unwrapped: ", unwrappedResult);
result = unwrappedResult;
console.log("result: ", result);
output.innerHTML = output.innerHTML + result.replace("<", "&lt;").replace(">", "&gt;");
})();
function run() {
output.innerHTML = " "
input = document.getElementById("inputbox").value;
(async () => {
console.log("running 'run'");
const response = await fetch('main.wasm');
const buffer = await response.arrayBuffer();
const module = new WebAssembly.Module(buffer);
const instance = new WebAssembly.Instance(module, importObject);
console.log("input: ", input);
console.log("input type: ", typeof input);
STDIN = Array.from(ENCODER.encode(input));
console.log("STDIN: ", STDIN);
console.log("STDOUT: ", STDOUT);
const rawResult = instance.exports.main(42);
const memory = instance.exports.memory;
var i32 = new Uint32Array(memory.buffer)
console.log(i32)
console.log(memory.buffer)
flushSTDOUT();
console.log("raw: ", rawResult);
const unwrappedResult = unwrap(rawResult);
console.log("unwrapped: ", unwrappedResult);
result = unwrappedResult;
console.log("result: ", result);
output.innerHTML = output.innerHTML + result.replace("<", "&lt;").replace(">", "&gt;");
})();
}

const typesEnum = Object.freeze({
Expand Down Expand Up @@ -139,10 +141,10 @@ function unwrap(raw) {
return "#\<box\>"
case typesEnum.T_VECT:
return "#\<vector\>"
// return "'" + result_interior(raw) // TODO
// return "'" + result_interior(raw) // TODO
case typesEnum.T_STR:
return "#\<string\>"
//return '"' + val_unwrap_str(raw) + '"' // TODO
//return '"' + val_unwrap_str(raw) + '"' // TODO
case typesEnum.T_PROC:
return "#\<procedure\>"
case typesEnum.T_INVALID:
Expand Down Expand Up @@ -200,7 +202,7 @@ function val_unwrap_char(raw) {
}

function val_unwrap_int(raw) {
return raw >> int_shift
return (raw >> int_shift) + ""
}

function val_unwrap_bool(raw) {
Expand All @@ -227,33 +229,33 @@ function val_wrap_void() {
return val_void
}

function readByte(){
console.log("reading byte!");
if(STDIN.length < 1) {
return val_eof
}
return val_wrap_int(BigInt(STDIN.shift()))
function readByte() {
console.log("reading byte!");
if (STDIN.length < 1) {
return val_eof
}
return val_wrap_int(BigInt(STDIN.shift()))
}

function writeByte(byte){
console.log("writing byte!");
const unwrappedByte = val_unwrap_int(byte);
STDOUT.push(Number(unwrappedByte))
return val_wrap_void();
function writeByte(byte) {
console.log("writing byte!");
const unwrappedByte = val_unwrap_int(byte);
STDOUT.push(Number(unwrappedByte))
return val_wrap_void();
}

function peekByte(){
console.log("peeking byte!");
if(STDIN.length < 1) {
return val_eof
}
return val_wrap_int(BigInt(STDIN[0]))
function peekByte() {
console.log("peeking byte!");
if (STDIN.length < 1) {
return val_eof
}
return val_wrap_int(BigInt(STDIN[0]))
}

function flushSTDOUT() {
console.log("STDOUT before Uint8 conversion: ", STDOUT);
STDOUT = Uint8Array.from(STDOUT);
console.log("STDOUT: ", STDOUT);
output.innerHTML = output.innerHTML + DECODER.decode(STDOUT);
STDOUT = []
console.log("STDOUT before Uint8 conversion: ", STDOUT);
STDOUT = Uint8Array.from(STDOUT);
console.log("STDOUT: ", STDOUT);
output.innerHTML = output.innerHTML + DECODER.decode(STDOUT);
STDOUT = []
}
3 changes: 3 additions & 0 deletions tests/iniquity/1.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#lang wacket
(define (f x) (add1 x))
(f 1)
3 changes: 3 additions & 0 deletions tests/iniquity/2.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#lang wacket
(define (f x y) (+ x y))
(f 42 84)
4 changes: 4 additions & 0 deletions tests/iniquity/3.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#lang wacket
(define (f1 a b c) (+ (f2 a b) c))
(define (f2 x y) (* x y))
(f1 42 84 124)
3 changes: 3 additions & 0 deletions tests/iniquity/4.wkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#lang wacket
(define (f x) (car x))
(f (cons 42 69))

0 comments on commit a19ab64

Please sign in to comment.