From 0a8bc0b30644c514d822fb32a184318f7353a285 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 12 Nov 2017 21:59:58 +1100 Subject: Add lua.to_uristring --- README.md | 2 +- src/defs.js | 20 ++++++++++++++++++++ src/lauxlib.js | 3 +-- src/loadlib.js | 6 ++---- src/lua.js | 1 + 5 files changed, 25 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0d10e5f..eefbeea 100644 --- a/README.md +++ b/README.md @@ -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, Lua strings are represented by an array of bytes in Fengari. 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` and `lua.to_jsstring`. +To address that issue, Lua strings are represented by an array of bytes in Fengari. 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`. ### Integers diff --git a/src/defs.js b/src/defs.js index 315364a..3ebb747 100644 --- a/src/defs.js +++ b/src/defs.js @@ -188,6 +188,25 @@ const to_jsstring = function(value, from, to) { return str; }; +const uri_allowed = {}; /* bytes allowed unescaped in a uri */ +for (let c of ";,/?:@&=+$abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,-_.!~*'()#") { + uri_allowed[c.charCodeAt(0)] = true; +} + +/* utility function to convert a lua string to a js string with uri escaping */ +const to_uristring = function(a) { + let s = ""; + for (let i=0; i