tests/compiler.js
// Tests if instantiation of a module succeeds
Tests if instantiation of a module succeeds
Tests if instantiation of a module succeeds
function testInstantiate(basename, binaryBuffer, name, glue) {
  var failed = false;
  try {
    let memory = new WebAssembly.Memory({ initial: 10 });
    let exports = {};
    function getString(ptr) {
      const RUNTIME_HEADER_SIZE = 16;
      if (!ptr) return "null";
      var U32 = new Uint32Array(exports.memory ? exports.memory.buffer : memory.buffer);
      var U16 = new Uint16Array(exports.memory ? exports.memory.buffer : memory.buffer);
      var len16 = U32[(ptr - RUNTIME_HEADER_SIZE + 12) >>> 2] >>> 1;
      var ptr16 = ptr >>> 1;
      return String.fromCharCode.apply(String, U16.subarray(ptr16, ptr16 + len16));
    }
    function onerror(e) {
      console.log("  ERROR: " + e);
      failed = true;
      failedMessages.set(basename, e.message);
    }
    function oninfo(i) {
      console.log("  " + i);
    }
    let rtr = rtrace(onerror, args.rtraceVerbose ? oninfo : null);
    let runTime = asc.measure(() => {
      var imports = {
        rtrace: rtr,
        env: {
          memory,
          abort: function(msg, file, line, column) {
            console.log(colorsUtil.red("  abort: " + getString(msg) + " at " + getString(file) + ":" + line + ":" + column));
          },
          trace: function(msg, n) {
            console.log("  trace: " + getString(msg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", "));
          }
        },
        Math,
        Date,
        Reflect
      };
      if (glue.preInstantiate) {
        console.log(colorsUtil.white("  [preInstantiate]"));
        glue.preInstantiate(imports, exports);
      }
      var instance = new WebAssembly.Instance(new WebAssembly.Module(binaryBuffer), imports);
      Object.setPrototypeOf(exports, instance.exports);
      if (exports._start) {
        console.log(colorsUtil.white("  [start]"));
        exports._start();
      }
      if (glue.postInstantiate) {
        console.log(colorsUtil.white("  [postInstantiate]"));
        glue.postInstantiate(instance);
      }
    });
    let leakCount = rtr.check();
    if (leakCount) {
      let msg = "memory leak detected: " + leakCount + " leaking";
      console.log("- " + colorsUtil.red("rtrace " + name + " ERROR: ") + msg);
      failed = true;
      failedMessages.set(basename, msg);
    }
    if (!failed) {
      console.log("- " + colorsUtil.green("instantiate " + name + " OK") + " (" + asc.formatTime(runTime) + ")");
      if (rtr.active) {
        console.log("  " +
          rtr.allocCount + " allocs, " +
          rtr.freeCount + " frees, " +
          rtr.incrementCount + " increments, " +
          rtr.decrementCount + " decrements"
        );
      }
      console.log("");
      for (let key in exports) {
        console.log("  [" + (typeof exports[key]).substring(0, 3) + "] " + key + " = " + exports[key]);
      }
      return true;
    }
  } catch (e) {
    console.log("- " + colorsUtil.red("instantiate " + name + " ERROR: ") + e.stack);
    failed = true;
    failedMessages.set(basename, e.message);
  }
  return false;
}
function testInstantiate ( basename , binaryBuffer , name , glue ) { var failed = false ; try { let memory = new WebAssembly . Memory ( { initial : 10 } ) ; let exports = { } ; function getString ( ptr ) { const RUNTIME_HEADER_SIZE = 16 ; if ( ! ptr ) return "null" ; var U32 = new Uint32Array ( exports . memory ? exports . memory . buffer : memory . buffer ) ; var U16 = new Uint16Array ( exports . memory ? exports . memory . buffer : memory . buffer ) ; var len16 = U32 [ ( ptr - RUNTIME_HEADER_SIZE + 12 ) >>> 2 ] >>> 1 ; var ptr16 = ptr >>> 1 ; return String . fromCharCode . apply ( String , U16 . subarray ( ptr16 , ptr16 + len16 ) ) ; } function onerror ( e ) { console . log ( "  ERROR: " + e ) ; failed = true ; failedMessages . set ( basename , e . message ) ; } function oninfo ( i ) { console . log ( "  " + i ) ; } let rtr = rtrace ( onerror , args . rtraceVerbose ? oninfo : null ) ; let runTime = asc . measure ( ( ) => { var imports = { rtrace : rtr , env : { memory , abort : function ( msg , file , line , column ) { console . log ( colorsUtil . red ( "  abort: " + getString ( msg ) + " at " + getString ( file ) + ":" + line + ":" + column ) ) ; } , trace : function ( msg , n ) { console . log ( "  trace: " + getString ( msg ) + ( n ? " " : "" ) + Array . prototype . slice . call ( arguments , 2 , 2 + n ) . join ( ", " ) ) ; } } , Math , Date , Reflect } ; if ( glue . preInstantiate ) { console . log ( colorsUtil . white ( "  [preInstantiate]" ) ) ; glue . preInstantiate ( imports , exports ) ; } var instance = new WebAssembly . Instance ( new WebAssembly . Module ( binaryBuffer ) , imports ) ; Object . setPrototypeOf ( exports , instance . exports ) ; if ( exports . _start ) { console . log ( colorsUtil . white ( "  [start]" ) ) ; exports . _start ( ) ; } if ( glue . postInstantiate ) { console . log ( colorsUtil . white ( "  [postInstantiate]" ) ) ; glue . postInstantiate ( instance ) ; } } ) ; let leakCount = rtr . check ( ) ; if ( leakCount ) { let msg = "memory leak detected: " + leakCount + " leaking" ; console . log ( "- " + colorsUtil . red ( "rtrace " + name + " ERROR: " ) + msg ) ; failed = true ; failedMessages . set ( basename , msg ) ; } if ( ! failed ) { console . log ( "- " + colorsUtil . green ( "instantiate " + name + " OK" ) + " (" + asc . formatTime ( runTime ) + ")" ) ; if ( rtr . active ) { console . log ( "  " + rtr . allocCount + " allocs, " + rtr . freeCount + " frees, " + rtr . incrementCount + " increments, " + rtr . decrementCount + " decrements" ) ; } console . log ( "" ) ; for ( let key in exports ) { console . log ( "  [" + ( typeof exports [ key ] ) . substring ( 0 , 3 ) + "] " + key + " = " + exports [ key ] ) ; } return true ; } } catch ( e ) { console . log ( "- " + colorsUtil . red ( "instantiate " + name + " ERROR: " ) + e . stack ) ; failed = true ; failedMessages . set ( basename , e . message ) ; } return false ; }
Tests if instantiation of a module succeeds
Tests if instantiation of a module succeeds