Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c46df15

Browse files
committedNov 4, 2019
wasi: make preopen directories configurable
1 parent 8c2ceb1 commit c46df15

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed
 

‎lib/wasi.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// TODO(cjihrig): Provide a mechanism to bind to WASM modules.
33
'use strict';
44
/* global WebAssembly */
5-
const { Array, ArrayPrototype } = primordials;
5+
const { Array, ArrayPrototype, Object } = primordials;
66
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
77
const { WASI: _WASI } = internalBinding('wasi');
88
const kSetMemory = Symbol('setMemory');
@@ -35,17 +35,18 @@ class WASI {
3535
throw new ERR_INVALID_ARG_TYPE('options.env', 'Object', env);
3636
}
3737

38-
if (preopens == null) {
39-
preopens = null;
40-
} else if (typeof preopens !== 'object') {
38+
const preopenArray = [];
39+
40+
if (typeof preopens === 'object' && preopens !== null) {
41+
Object.keys(preopens).forEach((key) => {
42+
preopenArray.push(String(key));
43+
preopenArray.push(String(preopens[key]));
44+
});
45+
} else if (preopens !== undefined) {
4146
throw new ERR_INVALID_ARG_TYPE('options.preopens', 'Object', preopens);
42-
} else {
43-
//
4447
}
4548

46-
// TODO(cjihrig): Validate preopen object schema.
47-
48-
const wrap = new _WASI(args, envPairs, preopens);
49+
const wrap = new _WASI(args, envPairs, preopenArray);
4950

5051
for (const prop in wrap) {
5152
wrap[prop] = wrap[prop].bind(wrap);

‎src/node_wasi.cc

+18-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void WASI::New(const FunctionCallbackInfo<Value>& args) {
8181
CHECK_EQ(args.Length(), 3);
8282
CHECK(args[0]->IsArray());
8383
CHECK(args[1]->IsArray());
84-
// CHECK(args[2]->IsArray());
84+
CHECK(args[2]->IsArray());
8585

8686
Environment* env = Environment::GetCurrent(args);
8787
Local<Context> context = env->context();
@@ -113,12 +113,23 @@ void WASI::New(const FunctionCallbackInfo<Value>& args) {
113113
}
114114
options.envp[envc] = nullptr;
115115

116-
// TODO(cjihrig): Process the preopens for real.
117-
options.preopenc = 1;
118-
options.preopens =
119-
static_cast<uvwasi_preopen_t*>(calloc(1, sizeof(uvwasi_preopen_t)));
120-
options.preopens[0].mapped_path = "/sandbox";
121-
options.preopens[0].real_path = ".";
116+
Local<Array> preopens = args[2].As<Array>();
117+
CHECK_EQ(preopens->Length() % 2, 0);
118+
options.preopenc = preopens->Length() / 2;
119+
options.preopens = static_cast<uvwasi_preopen_t*>(
120+
calloc(options.preopenc, sizeof(uvwasi_preopen_t)));
121+
int index = 0;
122+
for (uint32_t i = 0; i < preopens->Length(); i += 2) {
123+
auto mapped = preopens->Get(context, i).ToLocalChecked();
124+
auto real = preopens->Get(context, i + 1).ToLocalChecked();
125+
CHECK(mapped->IsString());
126+
CHECK(real->IsString());
127+
node::Utf8Value mapped_path(env->isolate(), mapped);
128+
node::Utf8Value real_path(env->isolate(), real);
129+
options.preopens[index].mapped_path = strdup(*mapped_path);
130+
options.preopens[index].real_path = strdup(*real_path);
131+
index++;
132+
}
122133

123134
new WASI(env, args.This(), &options);
124135

‎test/wasi/c/read_file.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ int main() {
77

88
char c = fgetc(file);
99
while (c != EOF) {
10-
assert(fputc(c, stdout) != EOF);
10+
int wrote = fputc(c, stdout);
11+
assert(wrote != EOF);
1112
c = fgetc(file);
1213
}
1314
}

‎test/wasi/test-wasi.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
require('../common');
33

44
if (process.argv[2] === 'wasi-child') {
5+
const fixtures = require('../common/fixtures');
56
const fs = require('fs');
67
const path = require('path');
78
const { WASI } = require('wasi');
89
const wasmDir = path.join(__dirname, 'wasm');
9-
const wasi = new WASI({ args: [], env: process.env });
10+
const wasi = new WASI({
11+
args: [],
12+
env: process.env,
13+
preopens: {
14+
'/sandbox': fixtures.path('wasi')
15+
}
16+
});
1017
const importObject = { wasi_unstable: wasi.wasiImport };
1118
const modulePath = path.join(wasmDir, `${process.argv[3]}.wasm`);
1219
const buffer = fs.readFileSync(modulePath);

‎test/wasi/wasm/read_file.wasm

14 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
This repository has been archived.