diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-26 23:42:18 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-04-26 23:56:52 +1000 |
commit | 2c6ad8ae871ec8f511f1b983867fccf031bf38b3 (patch) | |
tree | eacefbe085e0e551e4460046bd474395fcc74a32 /src/defs.js | |
parent | a70eeebe4aa5014e08b67da7ec07be903c3cf6a5 (diff) | |
download | fengari-2c6ad8ae871ec8f511f1b983867fccf031bf38b3.tar.gz fengari-2c6ad8ae871ec8f511f1b983867fccf031bf38b3.tar.bz2 fengari-2c6ad8ae871ec8f511f1b983867fccf031bf38b3.zip |
Move jsstring() from lobject.js to defs.js; export from lua.js
Diffstat (limited to 'src/defs.js')
-rw-r--r-- | src/defs.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/defs.js b/src/defs.js index fd8038a..34316e9 100644 --- a/src/defs.js +++ b/src/defs.js @@ -134,6 +134,50 @@ class lua_Debug { } +const to_jsstring = function(value, from, to) { + assert(Array.isArray(value), "jsstring expect a array of bytes"); + + let u0, u1, u2, u3, u4, u5; + let idx = 0; + value = value.slice(from ? from : 0, to); + + var str = ''; + while (1) { + // For UTF8 byte structure, see http://en.wikipedia.org/wiki/UTF-8#Description and https://www.ietf.org/rfc/rfc2279.txt and https://tools.ietf.org/html/rfc3629 + u0 = value[idx++]; + if (u0 === 0) { str += "\0"; continue; } // Lua string embed '\0' + if (!u0) return str; + if (!(u0 & 0x80)) { str += String.fromCharCode(u0); continue; } + u1 = value[idx++] & 63; + if ((u0 & 0xE0) == 0xC0) { str += String.fromCharCode(((u0 & 31) << 6) | u1); continue; } + u2 = value[idx++] & 63; + if ((u0 & 0xF0) == 0xE0) { + u0 = ((u0 & 15) << 12) | (u1 << 6) | u2; + } else { + u3 = value[idx++] & 63; + if ((u0 & 0xF8) == 0xF0) { + u0 = ((u0 & 7) << 18) | (u1 << 12) | (u2 << 6) | u3; + } else { + u4 = value[idx++] & 63; + if ((u0 & 0xFC) == 0xF8) { + u0 = ((u0 & 3) << 24) | (u1 << 18) | (u2 << 12) | (u3 << 6) | u4; + } else { + u5 = value[idx++] & 63; + u0 = ((u0 & 1) << 30) | (u1 << 24) | (u2 << 18) | (u3 << 12) | (u4 << 6) | u5; + } + } + } + if (u0 < 0x10000) { + str += String.fromCharCode(u0); + } else { + var ch = u0 - 0x10000; + str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF)); + } + } + + return str; +}; + const to_luastring_cache = {}; const to_luastring = function(str, cache, maxBytesToWrite) { @@ -277,4 +321,5 @@ module.exports.constant_types = constant_types; module.exports.lua_Debug = lua_Debug; module.exports.lua_upvalueindex = lua_upvalueindex; module.exports.thread_status = thread_status; +module.exports.to_jsstring = to_jsstring; module.exports.to_luastring = to_luastring; |