Skip to content

Commit c30a64c

Browse files
committed
WebAssembly: read cwd from environment vars
1 parent e0023a2 commit c30a64c

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

src/file.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cctype>
1919
#include <vector>
2020
#include <algorithm>
21+
#include <stdlib.h>
2122
#include <sys/stat.h>
2223
#include "file.hpp"
2324
#include "context.hpp"
@@ -49,6 +50,15 @@ inline static std::string wstring_to_string(const std::wstring &wstr)
4950
# endif
5051
#endif
5152

53+
#ifdef _WASM
54+
inline static std::string get_cwd_from_env()
55+
{
56+
char* value = getenv("PWD");
57+
if (!value) return "(unreachable)";
58+
return value;
59+
}
60+
#endif
61+
5262
namespace Sass {
5363
namespace File {
5464

@@ -57,10 +67,10 @@ namespace Sass {
5767
// always with trailing slash
5868
std::string get_cwd()
5969
{
60-
const size_t wd_len = 4096;
6170
#ifdef _WASM
62-
std::string cwd = "/";
71+
std::string cwd = get_cwd_from_env();
6372
#else
73+
const size_t wd_len = 4096;
6474
#ifndef _WIN32
6575
char wd[wd_len];
6676
char* pwd = getcwd(wd, wd_len);

wasm/cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function init() {
1515
{"wasi_unstable": wrapper.exports}
1616
);
1717
host.args = [wasmPath, ...process.argv.slice(2)];
18+
host.env = [`PWD=${process.cwd()}`];
1819
host.memory = sassc.exports.memory;
1920
return sassc.exports;
2021
}

wasm/package.json.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sassc-wasi",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)