Refactor platform handling for web.
This commit is contained in:
@ -26,5 +26,5 @@ limitations under the License.
|
|||||||
{{MENU}}
|
{{MENU}}
|
||||||
<div class="wrapper">
|
<div class="wrapper">
|
||||||
|
|
||||||
<pre id="console">
|
<div id="ueforth"></div>
|
||||||
<script src="ueforth.js"></script>
|
<script src="ueforth.js"></script>
|
||||||
|
|||||||
@ -28,21 +28,22 @@ r|
|
|||||||
: jseval ( a n -- ) 1 call ;
|
: jseval ( a n -- ) 1 call ;
|
||||||
|
|
||||||
r"
|
r"
|
||||||
globalObj.inbuffer = [];
|
context.inbuffer = [];
|
||||||
if (!globalObj['write']) {
|
if (!globalObj.write) {
|
||||||
var con = document.getElementById('console');
|
context.screen = document.getElementById('ueforth');
|
||||||
if (con === null) {
|
if (context.screen === null) {
|
||||||
con = document.createElement('pre');
|
context.screen = document.createElement('div');
|
||||||
con.id = 'console';
|
document.body.appendChild(context.screen);
|
||||||
document.body.appendChild(con);
|
|
||||||
}
|
}
|
||||||
window.outbuffer = '';
|
context.terminal = document.createElement('pre');
|
||||||
|
context.screen.appendChild(context.terminal);
|
||||||
|
context.outbuffer = '';
|
||||||
window.onkeypress = function(e) {
|
window.onkeypress = function(e) {
|
||||||
globalObj.inbuffer.push(e.keyCode);
|
context.inbuffer.push(e.keyCode);
|
||||||
};
|
};
|
||||||
window.onkeydown = function(e) {
|
window.onkeydown = function(e) {
|
||||||
if (e.keyCode == 8) {
|
if (e.keyCode == 8) {
|
||||||
globalObj.inbuffer.push(e.keyCode);
|
context.inbuffer.push(e.keyCode);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -52,23 +53,22 @@ r|
|
|||||||
(function(sp) {
|
(function(sp) {
|
||||||
var n = i32[sp>>2]; sp -= 4;
|
var n = i32[sp>>2]; sp -= 4;
|
||||||
var a = i32[sp>>2]; sp -= 4;
|
var a = i32[sp>>2]; sp -= 4;
|
||||||
if (globalObj['write']) {
|
if (globalObj.write) {
|
||||||
var text = GetString(a, n);
|
var text = GetString(a, n);
|
||||||
write(text);
|
write(text);
|
||||||
} else {
|
} else {
|
||||||
var con = document.getElementById('console');
|
|
||||||
for (var i = 0; i < n; ++i) {
|
for (var i = 0; i < n; ++i) {
|
||||||
var ch = u8[a + i];
|
var ch = u8[a + i];
|
||||||
if (ch == 12) {
|
if (ch == 12) {
|
||||||
window.outbuffer = '';
|
context.outbuffer = '';
|
||||||
} else if (ch == 8) {
|
} else if (ch == 8) {
|
||||||
window.outbuffer = window.outbuffer.slice(0, -1);
|
context.outbuffer = context.outbuffer.slice(0, -1);
|
||||||
} else if (ch == 13) {
|
} else if (ch == 13) {
|
||||||
} else {
|
} else {
|
||||||
window.outbuffer += String.fromCharCode(ch);
|
context.outbuffer += String.fromCharCode(ch);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
con.innerText = window.outbuffer + String.fromCharCode(0x2592);
|
context.terminal.innerText = context.outbuffer + String.fromCharCode(0x2592);
|
||||||
window.scrollTo(0, document.body.scrollHeight);
|
window.scrollTo(0, document.body.scrollHeight);
|
||||||
}
|
}
|
||||||
return sp;
|
return sp;
|
||||||
@ -79,15 +79,15 @@ r|
|
|||||||
|
|
||||||
r|
|
r|
|
||||||
(function(sp) {
|
(function(sp) {
|
||||||
if (globalObj['readline'] && !globalObj.inbuffer.length) {
|
if (globalObj.readline && !context.inbuffer.length) {
|
||||||
var line = readline();
|
var line = readline();
|
||||||
for (var i = 0; i < line.length; ++i) {
|
for (var i = 0; i < line.length; ++i) {
|
||||||
globalObj.inbuffer.push(line.charCodeAt(i));
|
context.inbuffer.push(line.charCodeAt(i));
|
||||||
}
|
}
|
||||||
globalObj.inbuffer.push(13);
|
context.inbuffer.push(13);
|
||||||
}
|
}
|
||||||
if (globalObj.inbuffer.length) {
|
if (context.inbuffer.length) {
|
||||||
sp += 4; i32[sp>>2] = globalObj.inbuffer.shift();
|
sp += 4; i32[sp>>2] = context.inbuffer.shift();
|
||||||
} else {
|
} else {
|
||||||
sp += 4; i32[sp>>2] = 0;
|
sp += 4; i32[sp>>2] = 0;
|
||||||
}
|
}
|
||||||
@ -99,10 +99,11 @@ r|
|
|||||||
|
|
||||||
r|
|
r|
|
||||||
(function(sp) {
|
(function(sp) {
|
||||||
if (globalObj['readline']) {
|
if (globalObj.readline) {
|
||||||
return -1;
|
sp += 4; i32[sp>>2] = -1;
|
||||||
|
return sp;
|
||||||
}
|
}
|
||||||
sp += 4; i32[sp>>2] = globalObj.inbuffer.length ? -1 : 0;
|
sp += 4; i32[sp>>2] = context.inbuffer.length ? -1 : 0;
|
||||||
return sp;
|
return sp;
|
||||||
})
|
})
|
||||||
| 4 jseval!
|
| 4 jseval!
|
||||||
@ -112,7 +113,7 @@ r|
|
|||||||
r|
|
r|
|
||||||
(function(sp) {
|
(function(sp) {
|
||||||
var val = i32[sp>>2]; sp -= 4;
|
var val = i32[sp>>2]; sp -= 4;
|
||||||
if (globalObj['quit']) {
|
if (globalObj.quit) {
|
||||||
quit(val);
|
quit(val);
|
||||||
} else {
|
} else {
|
||||||
Init();
|
Init();
|
||||||
@ -124,7 +125,7 @@ r|
|
|||||||
|
|
||||||
r|
|
r|
|
||||||
(function(sp) {
|
(function(sp) {
|
||||||
if (globalObj['write']) {
|
if (globalObj.write) {
|
||||||
sp += 4; i32[sp>>2] = 0; // Disable echo.
|
sp += 4; i32[sp>>2] = 0; // Disable echo.
|
||||||
} else {
|
} else {
|
||||||
sp += 4; i32[sp>>2] = -1; // Enable echo.
|
sp += 4; i32[sp>>2] = -1; // Enable echo.
|
||||||
|
|||||||
@ -37,6 +37,7 @@ var u8 = new Uint8Array(heap);
|
|||||||
var builtins = [];
|
var builtins = [];
|
||||||
var opcodes = {};
|
var opcodes = {};
|
||||||
var objects = [SetEval];
|
var objects = [SetEval];
|
||||||
|
var context = {}; // For later use by platform.
|
||||||
|
|
||||||
{{sys}}
|
{{sys}}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user