summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-04-28 14:56:04 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-04-28 14:56:04 +0200
commit51084a2959951aefd416bc3f2f6cd9952616fd16 (patch)
tree68d0d9269ce092157873ebb03c1f19021ec93b05
parent354d659f577fc27969784400c8c1e6090756da7b (diff)
downloadfengari-51084a2959951aefd416bc3f2f6cd9952616fd16.tar.gz
fengari-51084a2959951aefd416bc3f2f6cd9952616fd16.tar.bz2
fengari-51084a2959951aefd416bc3f2f6cd9952616fd16.zip
os.tmpname
-rw-r--r--package.json1
-rw-r--r--src/loslib.js30
2 files changed, 31 insertions, 0 deletions
diff --git a/package.json b/package.json
index c505878..cdfd16d 100644
--- a/package.json
+++ b/package.json
@@ -33,6 +33,7 @@
"tmp": "0.0.31"
},
"dependencies": {
+ "tmp": "0.0.31",
"readline-sync": "^1.4.7",
"seedrandom": "^2.4.2",
"sprintf-js": "giann/sprintf.js"
diff --git a/src/loslib.js b/src/loslib.js
index a715aec..8bf38d9 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -83,6 +83,36 @@ if (process && process.exit) {
syslib.exit = os_exit;
}
+
+// Only with Node
+if (typeof require === "function") {
+
+ let fs = false;
+ let tmp = false;
+ try {
+ fs = require('fs');
+ tmp = require('tmp');
+ } catch (e) {}
+
+ if (fs && tmp) {
+ // TODO: on POSIX system, should create the file
+ const lua_tmpname = function() {
+ return tmp.tmpNameSync();
+ };
+
+ const os_tmpname = function(L) {
+ let name = lua_tmpname();
+ if (!name)
+ return lauxlib.luaL_error(L, lua.to_luastring("unable to generate a unique filename"));
+ lua.lua_pushstring(L, lua.to_luastring(name));
+ return 1;
+ };
+
+ syslib.tmpname = os_tmpname;
+ }
+
+}
+
const luaopen_os = function(L) {
lauxlib.luaL_newlib(L, syslib);
return 1;