From 95c4a4ccf877b09ad41517956134703faf95bb85 Mon Sep 17 00:00:00 2001 From: thomscoder Date: Sun, 13 Nov 2022 16:31:12 +0100 Subject: [PATCH] feat: select mode --- example/css/style.css | 1 - example/dist/bundle.js | 7 ++--- example/index.html | 11 ++++++-- example/js/index.js | 26 +++++++++++-------- example/js/inputDefaultText.js | 47 ++++++++++++++++++++++++++++++++++ example/js/texts/texts.js | 5 ++++ runtime/runtime/start.js | 1 - 7 files changed, 79 insertions(+), 19 deletions(-) create mode 100644 example/js/inputDefaultText.js create mode 100644 example/js/texts/texts.js diff --git a/example/css/style.css b/example/css/style.css index 566f029..599217a 100644 --- a/example/css/style.css +++ b/example/css/style.css @@ -5,7 +5,6 @@ #code { border: solid black; - height: 100%; } .inner-container:not(.inner-container:first-child) { diff --git a/example/dist/bundle.js b/example/dist/bundle.js index 24f86b2..c213b0c 100644 --- a/example/dist/bundle.js +++ b/example/dist/bundle.js @@ -234,12 +234,10 @@ var Processor = class { } executeFunc() { for (const instruction of this.func.instructions) { - if (instruction == Opcodes.get_local) { + if (instruction == Opcodes.get_local) this.stack.push(this.params[this.func.locals.shift()]); - } - if (instruction == Opcodes.i32_const) { + if (instruction == Opcodes.i32_const) this.stack.push(this.func.internals.shift()); - } this.#parseInstruction(instruction); } } @@ -282,7 +280,6 @@ function invokeFunction(ast, funcName, params) { // runtime/runtime/start.js var startAeonRuntime = (wasm, ...args) => { const ast = createAST(wasm); - console.log("FUCK", wasm); const [funcName, ...rest] = args; const params = rest; const result = invokeFunction(ast, funcName, params); diff --git a/example/index.html b/example/index.html index af859a2..3b524cb 100644 --- a/example/index.html +++ b/example/index.html @@ -19,6 +19,13 @@

Luna

+
@@ -29,11 +36,11 @@

Luna

Calculate (add or subtract):
- +
- +
diff --git a/example/js/index.js b/example/js/index.js index 1125e52..8e251d3 100644 --- a/example/js/index.js +++ b/example/js/index.js @@ -1,4 +1,6 @@ import startAeonRuntime from '../dist/bundle.js'; +import { defaultText } from './inputDefaultText.js'; +import { CONST_MODE } from './texts/texts.js'; const go = new Go(); // Defined in wasm_exec.js. Don't forget to add this in your index.html. @@ -43,8 +45,10 @@ const runLunaAddition = async () => { // Set the result onto the doc const input1 = document.getElementById('input-1'); const input2 = document.getElementById('input-2'); + const label2 = document.getElementById('label-2') const codeContainer = document.getElementById('code'); const moduleContainer = document.getElementById('module'); + const selectInstruction = document.getElementById('instruction'); const compile = document.getElementById('compile'); const btn = document.getElementById('btn') @@ -54,21 +58,23 @@ const runLunaAddition = async () => { btn.setAttribute('disabled', true) - const input = `(module - (func (export "addNumbers") (param i32 i32) (result i32) - local.get 0 - local.get 1 - i32.add) -) -` - - const editor = CodeMirror(codeContainer, { - value: input, + let editor = CodeMirror(codeContainer, { + value: defaultText(selectInstruction), mode: "wast", lineNumbers: true, }); + // Change editor content based on selected mode + selectInstruction.onchange = (e) => { + editor.setValue(defaultText(selectInstruction)); + // Hide second input on const + if (e.target.value === CONST_MODE) { + input2.style.display = 'none'; + label2.style.display = 'none'; + } + } + compile.addEventListener('click', async () => { moduleContainer.innerHTML = "" const textContent = editor.getValue() diff --git a/example/js/inputDefaultText.js b/example/js/inputDefaultText.js new file mode 100644 index 0000000..d41fd56 --- /dev/null +++ b/example/js/inputDefaultText.js @@ -0,0 +1,47 @@ +import { ADDITION_MODE, CONST_MODE, DIVISION_MODE, MULTIPLICATION_MODE, SUBTRACTION_MODE } from "./texts/texts.js" + +export const defaultText = (select) => { + const instruction = select.value + switch (instruction) { + case ADDITION_MODE: + return `(module + (func (export "addNumbers") (param i32 i32) (result i32) + local.get 0 + local.get 1 + i32.add) +) +` + case SUBTRACTION_MODE: + return `(module + (func (export "subtractNumbers") (param i32 i32) (result i32) + local.get 0 + local.get 1 + i32.sub) +) +` + case MULTIPLICATION_MODE: + return `(module + (func (export "multiplyNumbers") (param i32 i32) (result i32) + local.get 0 + local.get 1 + i32.mul) +) +` + case DIVISION_MODE: + return `(module + (func (export "divideNumbers") (param i32 i32) (result i32) + local.get 0 + local.get 1 + i32.div) +) +` + case CONST_MODE: + return `(module + (func (export "operationWithInternalVariable") (param i32 i32) (result i32) + local.get 0 + i32.const 10 + i32.add) +) +` + } +} \ No newline at end of file diff --git a/example/js/texts/texts.js b/example/js/texts/texts.js new file mode 100644 index 0000000..3ea8f0a --- /dev/null +++ b/example/js/texts/texts.js @@ -0,0 +1,5 @@ +export const CONST_MODE = 'const'; +export const ADDITION_MODE = 'addition'; +export const SUBTRACTION_MODE = 'subtraction'; +export const MULTIPLICATION_MODE = 'multiplication'; +export const DIVISION_MODE = 'division'; \ No newline at end of file diff --git a/runtime/runtime/start.js b/runtime/runtime/start.js index 0dfbeda..d830926 100644 --- a/runtime/runtime/start.js +++ b/runtime/runtime/start.js @@ -3,7 +3,6 @@ import { invokeFunction } from "./invoker.js"; const startAeonRuntime = (wasm, ...args) => { const ast = createAST(wasm); - console.log("FUCK", wasm) const [funcName, ...rest] = args; const params = rest;