summaryrefslogtreecommitdiff
path: root/src/lbaselib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-17 16:04:42 +0100
committerBenoit Giannangeli <giann008@gmail.com>2017-02-17 21:28:40 +0100
commit06ec7904c37b897b2e87f4321198926ff22da1d9 (patch)
tree216d4a0e2fd2b0c984401caf91067b224a25cc28 /src/lbaselib.js
parent5131a5ab1f471655d6398748b1eaa9abd47c14da (diff)
downloadfengari-06ec7904c37b897b2e87f4321198926ff22da1d9.tar.gz
fengari-06ec7904c37b897b2e87f4321198926ff22da1d9.tar.bz2
fengari-06ec7904c37b897b2e87f4321198926ff22da1d9.zip
Basis to load std lib
Diffstat (limited to 'src/lbaselib.js')
-rw-r--r--src/lbaselib.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/lbaselib.js b/src/lbaselib.js
new file mode 100644
index 0000000..d28a3d5
--- /dev/null
+++ b/src/lbaselib.js
@@ -0,0 +1,56 @@
+/* jshint esversion: 6 */
+"use strict";
+
+const assert = require('assert');
+
+const lapi = require('./lapi.js');
+const lauxlib = require('./lauxlib.js');
+
+const luaB_print = function(L) {
+ let n = lapi.lua_gettop(L); /* number of arguments */
+ let str = "";
+
+ lapi.lua_getglobal(L, "tostring");
+ for (let i = 1; i <= n; i++) {
+ lapi.lua_pushvalue(L, -1); /* function to be called */
+ lapi.lua_pushvalue(L, i); /* value to print */
+ lapi.lua_call(L, 1, 1);
+ s = lapi.lua_tolstring(L, -1, null);
+ if (s === null)
+ throw new Error("'tostring' must return a string to 'print");
+ if (i > 1) s = `\t${s}`;
+ str = `${str}${s}`;
+ lapi.lua_pop(L, 1);
+ }
+
+ console.log(str);
+ return 0;
+};
+
+const luaB_tostring = function(L) {
+ lauxlib.luaL_checkany(L, 1);
+ lauxlib.luaL_tolstring(L, 1, null);
+
+ return true;
+};
+
+const base_funcs = {
+ "print": luaB_print,
+ "tostring": luaB_tostring,
+};
+
+const luaopen_base = function(L) {
+ /* open lib into global table */
+ lapi.lua_pushglobaltable(L);
+ lauxlib.luaL_setfuncs(L, base_funcs, 0);
+ /* set global _G */
+ lapi.lua_pushvalue(L, -1);
+ lapi.lua_setfield(L, -2, "_G");
+ /* set global _VERSION */
+ lapi.lua_pushliteral(L, lua.LUA_VERSION);
+ lapi.lua_setfield(L, -2, "_VERSION");
+ return true;
+};
+
+module.exports.luaB_tostring = luaB_tostring;
+module.exports.luaB_print = luaB_print;