summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-04-28 15:29:41 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-04-28 15:29:41 +0200
commit5d4b124a2fd10e3c156a5cf440a901cff13bfadf (patch)
treeebf2de3bf60a37d58de755746ed213f5cd643add
parent247cd4cc52f48d3d16ad6230eec3b4e94aa03c69 (diff)
downloadfengari-5d4b124a2fd10e3c156a5cf440a901cff13bfadf.tar.gz
fengari-5d4b124a2fd10e3c156a5cf440a901cff13bfadf.tar.bz2
fengari-5d4b124a2fd10e3c156a5cf440a901cff13bfadf.zip
os.getenv
-rw-r--r--README.md2
-rw-r--r--src/loslib.js8
-rw-r--r--tests/loslib.js31
3 files changed, 39 insertions, 2 deletions
diff --git a/README.md b/README.md
index 98874ec..a92dd46 100644
--- a/README.md
+++ b/README.md
@@ -25,6 +25,7 @@
- [ ] Package
- [ ] os
- [x] `os.exit()`
+ - [x] `os.getenv()`
- [x] `os.remove()`
- [x] `os.rename()`
- [x] `os.time()`
@@ -33,7 +34,6 @@
- [ ] `os.date()`
- [ ] `os.difftime()`
- [ ] `os.execute()`
- - [ ] `os.getenv()`
- [ ] `os.setlocale()`
- [ ] io
- [ ] `io.stdin`
diff --git a/src/loslib.js b/src/loslib.js
index aa868f5..1cc7d84 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -67,7 +67,7 @@ const syslib = {
};
// Only with Node
-if (process && process.exit) {
+if (process && process.exit && process.env) {
const os_exit = function(L) {
let status;
if (lua.lua_isboolean(L, 1))
@@ -80,7 +80,13 @@ if (process && process.exit) {
return 0;
};
+ const os_getenv = function(L) {
+ lua.lua_pushliteral(L, process.env[lua.to_jsstring(lauxlib.luaL_checkstring(L, 1))]); /* if NULL push nil */
+ return 1;
+ };
+
syslib.exit = os_exit;
+ syslib.getenv = os_getenv;
}
diff --git a/tests/loslib.js b/tests/loslib.js
index 968a338..c460919 100644
--- a/tests/loslib.js
+++ b/tests/loslib.js
@@ -34,3 +34,34 @@ test('os.time', function (t) {
);
});
+
+
+test('os.getenv', function (t) {
+ let luaCode = `
+ return os.getenv('HOME')
+ `, L;
+
+ t.plan(3);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ lauxlib.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lua.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+ t.ok(
+ lua.lua_isstring(L, -1),
+ "Correct element(s) on the stack"
+ );
+
+});