Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

Commit

Permalink
wasi: make preopen directories configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
cjihrig committed Nov 5, 2019
1 parent b1d62b3 commit 1f8fcae
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
19 changes: 10 additions & 9 deletions lib/wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// TODO(cjihrig): Provide a mechanism to bind to WASM modules.
'use strict';
/* global WebAssembly */
const { Array, ArrayPrototype } = primordials;
const { Array, ArrayPrototype, Object } = primordials;
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const { WASI: _WASI } = internalBinding('wasi');
const kSetMemory = Symbol('setMemory');
Expand Down Expand Up @@ -35,17 +35,18 @@ class WASI {
throw new ERR_INVALID_ARG_TYPE('options.env', 'Object', env);
}

if (preopens == null) {
preopens = null;
} else if (typeof preopens !== 'object') {
const preopenArray = [];

if (typeof preopens === 'object' && preopens !== null) {
Object.keys(preopens).forEach((key) => {
preopenArray.push(String(key));
preopenArray.push(String(preopens[key]));
});
} else if (preopens !== undefined) {
throw new ERR_INVALID_ARG_TYPE('options.preopens', 'Object', preopens);
} else {
//
}

// TODO(cjihrig): Validate preopen object schema.

const wrap = new _WASI(args, envPairs, preopens);
const wrap = new _WASI(args, envPairs, preopenArray);

for (const prop in wrap) {
wrap[prop] = wrap[prop].bind(wrap);
Expand Down
25 changes: 18 additions & 7 deletions src/node_wasi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void WASI::New(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 3);
CHECK(args[0]->IsArray());
CHECK(args[1]->IsArray());
// CHECK(args[2]->IsArray());
CHECK(args[2]->IsArray());

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

// TODO(cjihrig): Process the preopens for real.
options.preopenc = 1;
options.preopens =
static_cast<uvwasi_preopen_t*>(calloc(1, sizeof(uvwasi_preopen_t)));
options.preopens[0].mapped_path = "/sandbox";
options.preopens[0].real_path = ".";
Local<Array> preopens = args[2].As<Array>();
CHECK_EQ(preopens->Length() % 2, 0);
options.preopenc = preopens->Length() / 2;
options.preopens = static_cast<uvwasi_preopen_t*>(
calloc(options.preopenc, sizeof(uvwasi_preopen_t)));
int index = 0;
for (uint32_t i = 0; i < preopens->Length(); i += 2) {
auto mapped = preopens->Get(context, i).ToLocalChecked();
auto real = preopens->Get(context, i + 1).ToLocalChecked();
CHECK(mapped->IsString());
CHECK(real->IsString());
node::Utf8Value mapped_path(env->isolate(), mapped);
node::Utf8Value real_path(env->isolate(), real);
options.preopens[index].mapped_path = strdup(*mapped_path);
options.preopens[index].real_path = strdup(*real_path);
index++;
}

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

Expand Down
3 changes: 2 additions & 1 deletion test/wasi/c/read_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ int main() {

char c = fgetc(file);
while (c != EOF) {
assert(fputc(c, stdout) != EOF);
int wrote = fputc(c, stdout);
assert(wrote != EOF);
c = fgetc(file);
}
}
9 changes: 8 additions & 1 deletion test/wasi/test-wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
require('../common');

if (process.argv[2] === 'wasi-child') {
const fixtures = require('../common/fixtures');
const fs = require('fs');
const path = require('path');
const { WASI } = require('wasi');
const wasmDir = path.join(__dirname, 'wasm');
const wasi = new WASI({ args: [], env: process.env });
const wasi = new WASI({
args: [],
env: process.env,
preopens: {
'/sandbox': fixtures.path('wasi')
}
});
const importObject = { wasi_unstable: wasi.wasiImport };
const modulePath = path.join(wasmDir, `${process.argv[3]}.wasm`);
const buffer = fs.readFileSync(modulePath);
Expand Down
Binary file modified test/wasi/wasm/read_file.wasm
Binary file not shown.

0 comments on commit 1f8fcae

Please sign in to comment.