diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-03-05 15:57:04 +0100 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-03-05 15:57:04 +0100 |
commit | 2ac8543dfd87f4c227385d6890bfcb011fc341f1 (patch) | |
tree | e122a3d4c28406aadeba2557f76bb123a34025ef /src/lstrlib.js | |
parent | db136649e45a63edb8bc511e756249a9e6ca9ea7 (diff) | |
download | fengari-2ac8543dfd87f4c227385d6890bfcb011fc341f1.tar.gz fengari-2ac8543dfd87f4c227385d6890bfcb011fc341f1.tar.bz2 fengari-2ac8543dfd87f4c227385d6890bfcb011fc341f1.zip |
lstrlib, string.len
Diffstat (limited to 'src/lstrlib.js')
-rw-r--r-- | src/lstrlib.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/lstrlib.js b/src/lstrlib.js new file mode 100644 index 0000000..194d6ba --- /dev/null +++ b/src/lstrlib.js @@ -0,0 +1,36 @@ +"use strict"; + +const assert = require('assert'); + +const lua = require('./lua.js'); +const lapi = require('./lapi.js'); +const lauxlib = require('./lauxlib.js'); +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); + return 1; +}; + +const strlib = { + "len": str_len +}; + +const createmetatable = function(L) { + lapi.lua_createtable(L, 0, 1); /* table to be metatable for strings */ + lapi.lua_pushliteral(L, ""); /* dummy string */ + lapi.lua_pushvalue(L, -2); /* copy table */ + lapi.lua_setmetatable(L, -2); /* set table as metatable for strings */ + lapi.lua_pop(L, 1); /* pop dummy string */ + lapi.lua_pushvalue(L, -2); /* get string library */ + lapi.lua_setfield(L, -2, "__index"); /* metatable.__index = string */ + lapi.lua_pop(L, 1); /* pop metatable */ +}; + +const luaopen_string = function(L) { + lauxlib.luaL_newlib(L, strlib); + createmetatable(L); + return 1; +};
\ No newline at end of file |