summaryrefslogtreecommitdiff
path: root/src/lstrlib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-06 07:34:00 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-06 08:07:34 +0100
commitd792a7dad54039074890d9d379eec8676cb9fa5a (patch)
tree3bb2391b1a48f4fa460ab5f20f9f439b533c9e65 /src/lstrlib.js
parent2ac8543dfd87f4c227385d6890bfcb011fc341f1 (diff)
downloadfengari-d792a7dad54039074890d9d379eec8676cb9fa5a.tar.gz
fengari-d792a7dad54039074890d9d379eec8676cb9fa5a.tar.bz2
fengari-d792a7dad54039074890d9d379eec8676cb9fa5a.zip
string.char
Diffstat (limited to 'src/lstrlib.js')
-rw-r--r--src/lstrlib.js22
1 files changed, 18 insertions, 4 deletions
diff --git a/src/lstrlib.js b/src/lstrlib.js
index 194d6ba..69f634d 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -9,13 +9,25 @@ const CT = lua.constant_types;
const TS = lua.thread_status;
const str_len = function(L) {
- lauxlib.luaL_checkstring(L, 1);
- lapi.lua_pushinteger(L, lapi.lua_tostring(L, 1).length);
+ lapi.lua_pushinteger(L, lauxlib.luaL_checkstring(L, 1).length);
+ return 1;
+};
+
+const str_char = function(L) {
+ let n = lapi.lua_gettop(L); /* number of arguments */
+ let p = "";
+ for (let i = 1; i <= n; i++) {
+ let c = lauxlib.luaL_checkinteger(L, i);
+ lauxlib.luaL_argcheck(L, c >= 0 && c <= 255, "value out of range"); // Strings are 8-bit clean
+ p += String.fromCharCode(c);
+ }
+ lapi.lua_pushstring(L, p);
return 1;
};
const strlib = {
- "len": str_len
+ "len": str_len,
+ "char": str_char
};
const createmetatable = function(L) {
@@ -33,4 +45,6 @@ const luaopen_string = function(L) {
lauxlib.luaL_newlib(L, strlib);
createmetatable(L);
return 1;
-}; \ No newline at end of file
+};
+
+module.exports.luaopen_string = luaopen_string; \ No newline at end of file