diff options
author | daurnimator <quae@daurnimator.com> | 2018-01-06 19:32:31 +1100 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2018-01-06 19:32:43 +1100 |
commit | a0fa83dc9c170542630dbc83d1732d3e7a6313e9 (patch) | |
tree | f49965560f11a43e0fcb8dbc6bae1ce9ee22cb93 | |
parent | 110636150e4b3470e21622b022bec2844d8e8b2f (diff) | |
download | fengari-a0fa83dc9c170542630dbc83d1732d3e7a6313e9.tar.gz fengari-a0fa83dc9c170542630dbc83d1732d3e7a6313e9.tar.bz2 fengari-a0fa83dc9c170542630dbc83d1732d3e7a6313e9.zip |
Expose lua.luastring_of to construct a lua 'string' from raw byte values
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/defs.js | 4 | ||||
-rw-r--r-- | src/ldump.js | 4 | ||||
-rw-r--r-- | src/lobject.js | 2 | ||||
-rw-r--r-- | src/lua.js | 1 | ||||
-rw-r--r-- | src/lutf8lib.js | 2 | ||||
-rw-r--r-- | tests/defs.js | 14 |
7 files changed, 15 insertions, 14 deletions
@@ -45,7 +45,7 @@ Fengari implements Lua 5.3 semantics and will hopefully follow future Lua releas Lua strings are 8-bits clean and can embed `\0`. Which means that invalid UTF-8/16 strings are valid Lua strings. Lua functions like `string.dump` even use strings as a way of storing binary data. -To address that issue, Fengari uses [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) objects containing the raw bytes to implement lua strings. To push a JS string on the stack you can use `lua_pushliteral` which will convert it to an array of bytes before pushing it. To get a Lua string on the stack as a JS string you can use `lua_tojsstring` which will attempt to convert it to a UTF-16 JS string. The latter won't give you what you expect if the Lua string is not a valid UTF-16 sequence. You can also convert strings with `lua.to_luastring`, `lua.to_jsstring` and `lua.to_uristring`. +To address that issue, Fengari uses [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) objects containing the raw bytes to implement lua strings. To push a JS string on the stack you can use `lua_pushliteral` which will convert it to an array of bytes before pushing it. To get a Lua string on the stack as a JS string you can use `lua_tojsstring` which will attempt to convert it to a UTF-16 JS string. The latter won't give you what you expect if the Lua string is not a valid UTF-16 sequence. You can also convert strings with `lua.luastring_of`, `lua.to_luastring`, `lua.to_jsstring` and `lua.to_uristring`. ### Integers diff --git a/src/defs.js b/src/defs.js index bb98485..0d53c66 100644 --- a/src/defs.js +++ b/src/defs.js @@ -131,7 +131,7 @@ class lua_Debug { } -const string_of = Uint8Array.of.bind(Uint8Array); +const luastring_of = Uint8Array.of.bind(Uint8Array); const is_luastring = function(s) { return s instanceof Uint8Array; @@ -431,7 +431,7 @@ module.exports.lua_upvalueindex = lua_upvalueindex; module.exports.thread_status = thread_status; module.exports.is_luastring = is_luastring; module.exports.luastring_cmp = luastring_cmp; -module.exports.string_of = string_of; +module.exports.luastring_of = luastring_of; module.exports.to_jsstring = to_jsstring; module.exports.to_luastring = to_luastring; module.exports.to_uristring = to_uristring; diff --git a/src/ldump.js b/src/ldump.js index 7e62da3..f736e22 100644 --- a/src/ldump.js +++ b/src/ldump.js @@ -3,7 +3,7 @@ const defs = require('./defs.js'); const CT = defs.constant_types; -const LUAC_DATA = defs.string_of(25, 147, 13, 10, 26, 10); +const LUAC_DATA = defs.luastring_of(25, 147, 13, 10, 26, 10); const LUAC_INT = 0x5678; const LUAC_NUM = 370.5; const LUAC_VERSION = Number.parseInt(defs.LUA_VERSION_MAJOR) * 16 + Number.parseInt(defs.LUA_VERSION_MINOR); @@ -30,7 +30,7 @@ const DumpLiteral = function(s, D) { }; const DumpByte = function(y, D) { - DumpBlock(defs.string_of(y), 1, D); + DumpBlock(defs.luastring_of(y), 1, D); }; const DumpInt = function(x, D) { diff --git a/src/lobject.js b/src/lobject.js index 1b490f3..bd10be8 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -559,7 +559,7 @@ const luaO_pushvfstring = function(L, fmt, argp) { case char['c']: { let buff = argp[a++]; if (ljstype.lisprint(buff)) - pushstr(L, defs.string_of(buff)); + pushstr(L, defs.luastring_of(buff)); else luaO_pushfstring(L, defs.to_luastring("<\\%d>", true), buff); break; @@ -78,6 +78,7 @@ module.exports.LUA_VERSUFFIX = defs.LUA_VERSUFFIX; module.exports.LUA_YIELD = defs.thread_status.LUA_YIELD; module.exports.lua_Debug = defs.lua_Debug; module.exports.lua_upvalueindex = defs.lua_upvalueindex; +module.exports.luastring_of = defs.luastring_of; module.exports.to_jsstring = defs.to_jsstring; module.exports.to_luastring = defs.to_luastring; module.exports.to_uristring = defs.to_uristring; diff --git a/src/lutf8lib.js b/src/lutf8lib.js index f581a9e..722d9f1 100644 --- a/src/lutf8lib.js +++ b/src/lutf8lib.js @@ -216,7 +216,7 @@ const funcs = { }; /* pattern to match a single UTF-8 character */ -const UTF8PATT = Uint8Array.of(91, 0, 45, 127, 194, 45, 244, 93, 91, 128, 45, 191, 93, 42); +const UTF8PATT = lua.luastring_of(91, 0, 45, 127, 194, 45, 244, 93, 91, 128, 45, 191, 93, 42); const luaopen_utf8 = function(L) { lauxlib.luaL_newlib(L, funcs); diff --git a/tests/defs.js b/tests/defs.js index 1a935d6..f24dee0 100644 --- a/tests/defs.js +++ b/tests/defs.js @@ -6,37 +6,37 @@ const unicode_tests = [ { description: "Convert normal ascii string", literal: "foo", - byte_array: defs.string_of("f".charCodeAt(0), "o".charCodeAt(0), "o".charCodeAt(0)) + byte_array: defs.luastring_of("f".charCodeAt(0), "o".charCodeAt(0), "o".charCodeAt(0)) }, { description: "Convert ascii string containing null byte", literal: "fo\0o", - byte_array: defs.string_of("f".charCodeAt(0), "o".charCodeAt(0), 0, "o".charCodeAt(0)) + byte_array: defs.luastring_of("f".charCodeAt(0), "o".charCodeAt(0), 0, "o".charCodeAt(0)) }, { description: "Convert string with BMP unicode chars", literal: "Café", - byte_array: defs.string_of(67, 97, 102, 195, 169) + byte_array: defs.luastring_of(67, 97, 102, 195, 169) }, { description: "Convert string with codepoint in PUA (U+E000 to U+F8FF)", literal: "", - byte_array: defs.string_of(239, 163, 191) + byte_array: defs.luastring_of(239, 163, 191) }, { description: "Convert string with surrogate pair", literal: "❤️🍾", - byte_array: defs.string_of(226, 157, 164, 239, 184, 143, 240, 159, 141, 190) + byte_array: defs.luastring_of(226, 157, 164, 239, 184, 143, 240, 159, 141, 190) }, { description: "Convert string with broken surrogate pair", literal: "\uD800a", - byte_array: defs.string_of(237, 160, 128, 97) + byte_array: defs.luastring_of(237, 160, 128, 97) }, { description: "Convert string with broken surrogate pair at end of string", literal: "\uD823", - byte_array: defs.string_of(237, 160, 163) + byte_array: defs.luastring_of(237, 160, 163) } ]; |