diff --git a/gltf/bin/gltfpack.js b/gltf/bin/gltfpack.js index 89226181e..3fa4a6955 100644 --- a/gltf/bin/gltfpack.js +++ b/gltf/bin/gltfpack.js @@ -1,4 +1,5 @@ var fs = require('fs'); +var cp = require('child_process'); var WASI_EBADF = 8; var WASI_EINVAL = 28; @@ -19,6 +20,7 @@ var fds = { }; var args = process.argv.slice(1); +var env = Object.keys(process.env).map(function (key) { return key + '=' + process.env[key] }); function nextFd() { for (var i = 0; ; ++i) { @@ -83,12 +85,24 @@ var wasi = { heap.setUint32(opened_fd, fd, true); return 0; } catch (err) { - return WASI_ENOSYS; + return WASI_EIO; } }, path_unlink_file: function(parent_fd, path, path_len) { - return WASI_EINVAL; + if (fds[parent_fd] !== null) { + return WASI_EBADF; + } + + var heap = getHeap(); + var path_name = Buffer.from(heap.buffer, path, path_len).toString('utf-8'); + + try { + fs.unlinkSys(path_name); + return 0; + } catch (err) { + return WASI_EIO; + } }, args_sizes_get: function(argc, argv_buf_size) { @@ -111,13 +125,13 @@ var wasi = { var argp = argv_buf; for (var i = 0; i < args.length; ++i) { - var au = Buffer.from(args[i], 'utf-8'); + var item = Buffer.from(args[i], 'utf-8'); heap.setUint32(argv + i * 4, argp, true); - au.copy(memory, argp); - heap.setUint8(argp + au.length, 0); + item.copy(memory, argp); + heap.setUint8(argp + item.length, 0); - argp += au.length + 1; + argp += item.length + 1; } return 0; @@ -145,12 +159,33 @@ var wasi = { environ_sizes_get: function(environc, environ_buf_size) { var heap = getHeap(); - heap.setUint32(environc, 0, true); - heap.setUint32(environ_buf_size, 0, true); + + var buf_size = 0; + for (var i = 0; i < env.length; ++i) { + buf_size += Buffer.from(env[i], 'utf-8').length + 1; + } + + heap.setUint32(environc, env.length, true); + heap.setUint32(environ_buf_size, buf_size, true); return 0; }, environ_get: function(environ, environ_buf) { + var heap = getHeap(); + var memory = new Uint8Array(heap.buffer); + + var envp = environ_buf; + + for (var i = 0; i < env.length; ++i) { + var item = Buffer.from(env[i], 'utf-8'); + + heap.setUint32(environ + i * 4, envp, true); + item.copy(memory, envp); + heap.setUint8(envp + item.length, 0); + + envp += item.length + 1; + } + return 0; }, @@ -233,6 +268,18 @@ var wasi = { heap.setUint32(nwritten, written, true); return 0; }, + + path_readlink: function(fd, path, path_len, buf, buf_len, bufused) { + if (fd !== -1) { + return WASI_ENOSYS; + } + + var heap = getHeap(); + var command = Buffer.from(heap.buffer, path, path_len).toString('utf-8'); + + var ret = cp.spawnSync(command, [], {shell:true}); + return ret.status == null ? 256 : ret.status; + }, }; var wasm = fs.readFileSync(__dirname + '/gltfpack.wasm'); diff --git a/gltf/fileio.cpp b/gltf/fileio.cpp index 801725869..b820657f1 100644 --- a/gltf/fileio.cpp +++ b/gltf/fileio.cpp @@ -25,7 +25,7 @@ TempFile::TempFile(const char* suffix) char ids[16]; sprintf(ids, "%d", id++); - path = "/tmp/gltfpack-"; + path = "gltfpack-temp-"; path += ids; path += suffix; #else diff --git a/gltf/image.cpp b/gltf/image.cpp index de46a4c77..ed1ad8118 100644 --- a/gltf/image.cpp +++ b/gltf/image.cpp @@ -104,17 +104,6 @@ static const char* mimeExtension(const char* mime_type) return ".raw"; } -#if defined(__wasi__) -static int execute(const char* cmd_, bool ignore_stdout, bool ignore_stderr) -{ - return -1; -} - -static const char* readenv(const char* name) -{ - return getenv(name); -} -#else static int execute(const char* cmd_, bool ignore_stdout, bool ignore_stderr) { #ifdef _WIN32 @@ -125,10 +114,12 @@ static int execute(const char* cmd_, bool ignore_stdout, bool ignore_stderr) std::string cmd = cmd_; +#ifndef __wasi__ if (ignore_stdout) (cmd += " >") += ignore; if (ignore_stderr) (cmd += " 2>") += ignore; +#endif return system(cmd.c_str()); } @@ -137,7 +128,6 @@ static const char* readenv(const char* name) { return getenv(name); } -#endif bool checkBasis(bool verbose) { diff --git a/gltf/wasistubs.cpp b/gltf/wasistubs.cpp index a4b25cf7a..e15c4bf6a 100644 --- a/gltf/wasistubs.cpp +++ b/gltf/wasistubs.cpp @@ -1,5 +1,6 @@ #ifdef __wasi__ #include +#include #include @@ -40,4 +41,12 @@ __wasi_errno_t __wasi_fd_seek(__wasi_fd_t fd, __wasi_filedelta_t offset, __wasi_ *newoffset = newoffset32; return result; } + +extern "C" int system(const char* command) +{ + // WASI doesn't provide a system() equivalent; we highjack readlink here, the reasoning being that if we run against a real WASI implementation, + // the effect is more likely to be benign. + return __wasi_path_readlink(-1, command, strlen(command), 0, 0, 0); +} + #endif