All files / lib wasi.js

98.5% Statements 131/133
97.67% Branches 42/43
100% Functions 3/3
98.5% Lines 131/133

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 13423x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 41x 41x 32x 32x 32x 32x 32x 41x 1x 1x 31x 31x 31x 41x 20x 1380x 1380x 1380x 1380x 41x 1x 1x 30x 30x 30x 41x 21x 37x 37x 41x 1x 1x 29x 41x 1x 1x 1x 28x 28x 28x 41x 1242x 1242x 27x 41x 1x 1x 27x 27x 27x 27x 27x 27x 41x 23x 23x 33x 1x 1x 1x 32x 32x 32x 33x 33x 25x 25x 25x 33x 1x 1x 1x 24x 33x 1x 1x 23x 23x 23x 23x 23x 23x 33x 2x 2x 33x 1x     1x 23x 23x 33x 23x 23x 23x 23x 23x 23x 1x 1x 1x 1x 1x 1x 1x 1x  
'use strict';
/* global WebAssembly */
const {
  ArrayIsArray,
  ArrayPrototypeMap,
  ArrayPrototypePush,
  FunctionPrototypeBind,
  ObjectEntries,
  Symbol,
} = primordials;
 
const {
  ERR_INVALID_ARG_TYPE,
  ERR_WASI_ALREADY_STARTED
} = require('internal/errors').codes;
const { emitExperimentalWarning } = require('internal/util');
const { WASI: _WASI } = internalBinding('wasi');
const kExitCode = Symbol('exitCode');
const kSetMemory = Symbol('setMemory');
const kStarted = Symbol('started');
 
emitExperimentalWarning('WASI');
 
 
class WASI {
  constructor(options = {}) {
    if (options === null || typeof options !== 'object')
      throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
 
    const { env, preopens, returnOnExit = false } = options;
    let { args = [] } = options;
 
    if (ArrayIsArray(args))
      args = ArrayPrototypeMap(args, (arg) => { return String(arg); });
    else
      throw new ERR_INVALID_ARG_TYPE('options.args', 'Array', args);
 
    const envPairs = [];
 
    if (env !== null && typeof env === 'object') {
      for (const key in env) {
        const value = env[key];
        if (value !== undefined)
          ArrayPrototypePush(envPairs, `${key}=${value}`);
      }
    } else if (env !== undefined) {
      throw new ERR_INVALID_ARG_TYPE('options.env', 'Object', env);
    }
 
    const preopenArray = [];
 
    if (typeof preopens === 'object' && preopens !== null) {
      for (const [key, value] of ObjectEntries(preopens)) {
        ArrayPrototypePush(preopenArray, String(key), String(value));
      }
    } else if (preopens !== undefined) {
      throw new ERR_INVALID_ARG_TYPE('options.preopens', 'Object', preopens);
    }
 
    if (typeof returnOnExit !== 'boolean') {
      throw new ERR_INVALID_ARG_TYPE(
        'options.returnOnExit', 'boolean', returnOnExit);
    }
 
    const wrap = new _WASI(args, envPairs, preopenArray);
 
    for (const prop in wrap) {
      wrap[prop] = FunctionPrototypeBind(wrap[prop], wrap);
    }
 
    if (returnOnExit) {
      wrap.proc_exit = FunctionPrototypeBind(wasiReturnOnProcExit, this);
    }
 
    this[kSetMemory] = wrap._setMemory;
    delete wrap._setMemory;
    this.wasiImport = wrap;
    this[kStarted] = false;
    this[kExitCode] = 0;
  }
 
  start(instance) {
    if (!(instance instanceof WebAssembly.Instance)) {
      throw new ERR_INVALID_ARG_TYPE(
        'instance', 'WebAssembly.Instance', instance);
    }
 
    const exports = instance.exports;
 
    if (exports === null || typeof exports !== 'object')
      throw new ERR_INVALID_ARG_TYPE('instance.exports', 'Object', exports);
 
    const { memory } = exports;
 
    if (!(memory instanceof WebAssembly.Memory)) {
      throw new ERR_INVALID_ARG_TYPE(
        'instance.exports.memory', 'WebAssembly.Memory', memory);
    }
 
    if (this[kStarted]) {
      throw new ERR_WASI_ALREADY_STARTED();
    }
 
    this[kStarted] = true;
    this[kSetMemory](memory);
 
    try {
      if (exports._start)
        exports._start();
      else if (exports.__wasi_unstable_reactor_start)
        exports.__wasi_unstable_reactor_start();
    } catch (err) {
      if (err !== kExitCode) {
        throw err;
      }
    }
 
    return this[kExitCode];
  }
}
 
 
module.exports = { WASI };
 
 
function wasiReturnOnProcExit(rval) {
  // If __wasi_proc_exit() does not terminate the process, an assertion is
  // triggered in the wasm runtime. Node can sidestep the assertion and return
  // an exit code by recording the exit code, and throwing a JavaScript
  // exception that WebAssembly cannot catch.
  this[kExitCode] = rval;
  throw kExitCode;
}