summaryrefslogtreecommitdiff
path: root/src/lbaselib.js
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-01-06 19:06:27 +1100
committerdaurnimator <quae@daurnimator.com>2018-01-06 19:06:27 +1100
commit110636150e4b3470e21622b022bec2844d8e8b2f (patch)
treeacd66e0868ec195dd476f53a464594ff54d1be37 /src/lbaselib.js
parent12b37db70edf83849dcf88b4e50e987234561695 (diff)
downloadfengari-110636150e4b3470e21622b022bec2844d8e8b2f.tar.gz
fengari-110636150e4b3470e21622b022bec2844d8e8b2f.tar.bz2
fengari-110636150e4b3470e21622b022bec2844d8e8b2f.zip
src/lbaselib.js: Fallback for when TextDecoder doesn't exist
Diffstat (limited to 'src/lbaselib.js')
-rw-r--r--src/lbaselib.js40
1 files changed, 29 insertions, 11 deletions
diff --git a/src/lbaselib.js b/src/lbaselib.js
index 86ba6cc..9a534f2 100644
--- a/src/lbaselib.js
+++ b/src/lbaselib.js
@@ -6,17 +6,35 @@ const lauxlib = require('./lauxlib.js');
let lua_writestring;
let lua_writeline;
if (typeof process === "undefined") {
- let buff = "";
- let decoder = new TextDecoder("utf-8");
- lua_writestring = function(s) {
- buff += decoder.decode(s, {stream: true});
- };
- let empty = new Uint8Array(0);
- lua_writeline = function() {
- buff += decoder.decode(empty);
- console.log(buff);
- buff = "";
- };
+ if (typeof TextDecoder === "function") { /* Older browsers don't have TextDecoder */
+ let buff = "";
+ let decoder = new TextDecoder("utf-8");
+ lua_writestring = function(s) {
+ buff += decoder.decode(s, {stream: true});
+ };
+ let empty = new Uint8Array(0);
+ lua_writeline = function() {
+ buff += decoder.decode(empty);
+ console.log(buff);
+ buff = "";
+ };
+ } else {
+ let buff = [];
+ lua_writestring = function(s) {
+ try {
+ /* If the string is valid utf8, then we can use to_jsstring */
+ s = lua.to_jsstring(s);
+ } catch(e) {
+ /* otherwise push copy of raw array */
+ s = s.slice(0);
+ }
+ buff.push(s);
+ };
+ lua_writeline = function() {
+ console.log.apply(console.log, buff);
+ buff = [];
+ };
+ }
} else {
lua_writestring = function(s) {
process.stdout.write(Buffer.from(s));