From df64428f77c581518e17a1a6a914c8b55ad65946 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Sun, 27 May 2018 21:49:22 +0200 Subject: [PATCH] Import memory and TOS in compiled word modules In preparation of #1 --- src/shell/WAForth.js | 4 +++- src/tools/quadruple.wat | 2 ++ src/waforth.wat | 9 +++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/shell/WAForth.js b/src/shell/WAForth.js index 39bf66c..d32e7bf 100644 --- a/src/shell/WAForth.js +++ b/src/shell/WAForth.js @@ -16,6 +16,7 @@ class WAForth { start(options = {}) { const { skipPrelude } = options; let table; + let memory; const buffer = (this.buffer = []); // TODO: Try to bundle this. See https://github.com/parcel-bundler/parcel/issues/647 @@ -79,7 +80,7 @@ class WAForth { // ); var module = new WebAssembly.Module(data); new WebAssembly.Instance(module, { - env: { table, tableBase: index } + env: { table, tableBase: index, memory, tos: -1 } }); } } @@ -88,6 +89,7 @@ class WAForth { .then(instance => { this.core = instance.instance; table = this.core.exports.table; + memory = this.core.exports.memory; if (!skipPrelude) { this.core.exports.loadPrelude(); } diff --git a/src/tools/quadruple.wat b/src/tools/quadruple.wat index c737298..ed4ed31 100644 --- a/src/tools/quadruple.wat +++ b/src/tools/quadruple.wat @@ -3,6 +3,8 @@ (module (import "env" "table" (table 4 anyfunc)) (import "env" "tableBase" (global $tableBase i32)) + (import "env" "memory" (memory 1)) + (import "env" "tos" (global $tos i32)) (type $void (func)) (type $push (func (param i32))) diff --git a/src/waforth.wat b/src/waforth.wat index 08931df..f4108c0 100644 --- a/src/waforth.wat +++ b/src/waforth.wat @@ -46,12 +46,17 @@ "\u0060\u0000\u0001\u007F" ;; (func (result i32)) "\u0060\u0001\u007f\u0001\u007F" ;; (func (param i32) (result i32)) - "\u0002" "\u0020" ;; Import section - "\u0002" ;; #Entries + "\u0002" "\u0039" ;; Import section + "\u0004" ;; #Entries "\u0003\u0065\u006E\u0076" "\u0005\u0074\u0061\u0062\u006C\u0065" ;; 'env' . 'table' "\u0001" "\u0070" "\u0000" "\u0004" ;; table, anyfunc, flags, initial size "\u0003\u0065\u006E\u0076" "\u0009\u0074\u0061\u0062\u006C\u0065\u0042\u0061\u0073\u0065" ;; 'env' . 'tableBase "\u0003" "\u007F" "\u0000" ;; global, i32, immutable + "\u0003\u0065\u006E\u0076" "\u0006\u006d\u0065\u006d\u006f\u0072\u0079" ;; 'env' . 'memory' + "\u0002" "\u0000" "\u0001" ;; memory + "\u0003\u0065\u006E\u0076" "\u0003\u0074\u006f\u0073" ;; 'env' . 'tos' + "\u0003" "\u007F" "\u0000" ;; global, i32, immutable + "\u0003" "\u0002" ;; Function section "\u0001" ;; #Entries