summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--index.html17
-rw-r--r--main.mjs58
-rwxr-xr-xsrc/lib.rs10
3 files changed, 72 insertions, 13 deletions
diff --git a/index.html b/index.html
index 16b132f..7e2d4c6 100644
--- a/index.html
+++ b/index.html
@@ -18,10 +18,19 @@
</textarea>
<button id='compile'>compile</button>
<button id='tick'>tick</button>
- <button id='run'>run</button>
- <div id='datastack'></div>
- <div id='callstack'></div>
- <div id='wordlist'></div>
+ <button id='bench'>bench</button>
+ <fieldset>
+ <legend>stack</legend>
+ <ol id='stack'></ol>
+ </fieldset>
+ <fieldset>
+ <legend>call stack</legend>
+ <ol id='callstack'></ol>
+ </fieldset>
+ <fieldset>
+ <legend>word list</legend>
+ <div id='wordlist'></div>
+ </fieldset>
<canvas id='arena' width='512' height='512'>no canvas!</canvas>
<script src='./main.mjs' type='module'></script>
</body>
diff --git a/main.mjs b/main.mjs
index 7bc2ba4..f8c9267 100644
--- a/main.mjs
+++ b/main.mjs
@@ -20,6 +20,47 @@ function selectorForIP(word, offset) {
return sel;
}
+function initWordlist() {
+ const sel = selectorForIP(0, 0);
+ document.querySelectorAll(sel).forEach(e => {
+ e.classList.add('ip')
+ });
+}
+
+function renderStack(interp) {
+ document.querySelectorAll('#stack').forEach(e => {
+ while (e.lastChild) {
+ e.removeChild(e.lastChild);
+ }
+ interp.stack().reverse()
+ .forEach(datum => {
+ console.debug('creating elt for', datum);
+ const elt = document.createElement('li');
+ elt.textContent = datum;
+ console.debug('elt', elt);
+ e.appendChild(elt);
+ return elt;
+ });
+ });
+}
+
+function renderCallStack(interp) {
+ document.querySelectorAll('#callstack').forEach(e => {
+ while (e.lastChild) {
+ e.removeChild(e.lastChild);
+ }
+ interp.callstack().reverse()
+ .forEach(datum => {
+ console.debug('creating elt for', datum);
+ const elt = document.createElement('li');
+ elt.textContent = `${datum.word}@${datum.offset}`;
+ console.debug('elt', elt);
+ e.appendChild(elt);
+ return elt;
+ });
+ });
+}
+
async function loaded() {
console.debug('running init');
const mod = await init();
@@ -45,15 +86,14 @@ async function loaded() {
if (res) {
const wordlist = wordlistElts(interp.wordlist());
wordlist.forEach(elt => wordlistContainer.appendChild(elt));
- const sel = selectorForIP(0, 0);
- document.querySelectorAll(sel).forEach(e => {
- e.classList.add('ip')
- });
+ initWordlist();
}
};
document.querySelector('#tick').onclick = e => {
console.debug('tick clicked', e);
- interp.tick();
+ if (!interp.tick()) {
+ interp.reset_ip();
+ }
console.info('callstack', interp.callstack());
console.info('stack', interp.stack());
@@ -62,11 +102,13 @@ async function loaded() {
document.querySelectorAll('#wordlist .ip').forEach(e => e.classList.remove('ip'));
const sel = selectorForIP(word, offset);
document.querySelectorAll(sel).forEach(e => {
- e.classList.add('ip')
+ e.classList.add('ip');
});
+ renderStack(interp);
+ renderCallStack(interp);
};
- document.querySelector('#run').onclick = e => {
- console.debug('run clicked', e);
+ document.querySelector('#bench').onclick = e => {
+ console.debug('bench clicked', e);
const start = performance.now();
let tickCount = 0;
for (let i = 0; i < 1_000_000; i++) {
diff --git a/src/lib.rs b/src/lib.rs
index 840a86c..f3c9a89 100755
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -119,11 +119,19 @@ impl ExportedInterp {
}
pub fn ip(&self) -> ExportedInstructionPointer {
- let Some(interp) = self.i.as_ref() else {
+ let Some(interp) = &self.i else {
return ExportedInstructionPointer { word: 0, offset: 0 }
};
(&interp.ip).into()
}
+
+ pub fn reset_ip(&mut self) {
+ let Some(interp) = &mut self.i else {
+ return;
+ };
+ interp.ip.word = 0;
+ interp.ip.offset = 0;
+ }
}
#[wasm_bindgen]