Merge V8 5.3.332.45.  DO NOT MERGE

Test: Manual

FPIIM-449

Change-Id: Id3254828b068abdea3cb10442e0172a8c9a98e03
(cherry picked from commit 13e2dadd00298019ed862f2b2fc5068bba730bcf)
diff --git a/test/mjsunit/wasm/adapter-frame.js b/test/mjsunit/wasm/adapter-frame.js
index 39164c7..e595c3f 100644
--- a/test/mjsunit/wasm/adapter-frame.js
+++ b/test/mjsunit/wasm/adapter-frame.js
@@ -26,12 +26,9 @@
   }
 
   var builder = new WasmModuleBuilder();
-  var sig = new Array();
-  sig.push(args);
-  for (var i = 0; i < args; i++) sig.push(type);
-  sig.push(1);
-  sig.push(type);
-  builder.addFunction("select", sig)
+  var params = [];
+  for (var i = 0; i < args; i++) params.push(type);
+  builder.addFunction("select", makeSig(params, [type]))
     .addBody([kExprGetLocal, which])
     .exportFunc();
 
diff --git a/test/mjsunit/wasm/asm-wasm.js b/test/mjsunit/wasm/asm-wasm.js
index 54d7d7a..4c28b61 100644
--- a/test/mjsunit/wasm/asm-wasm.js
+++ b/test/mjsunit/wasm/asm-wasm.js
@@ -682,6 +682,7 @@
 
 assertWasm(28, TestModDoubleNegative);
 
+
 (function () {
 function TestNamedFunctions() {
   "use asm";
@@ -707,6 +708,7 @@
 assertEquals(77.5, module.add());
 })();
 
+
 (function () {
 function TestGlobalsWithInit() {
   "use asm";
@@ -1358,6 +1360,38 @@
 })();
 
 
+(function TestBadAssignDoubleFromIntish() {
+  function Module(stdlib, foreign, heap) {
+    "use asm";
+    function func() {
+      var a = 1;
+      var b = 3.0;
+      b = a;
+    }
+    return {func: func};
+  }
+  assertThrows(function() {
+    Wasm.instantiateModuleFromAsm(Module.toString());
+  });
+})();
+
+
+(function TestBadAssignIntFromDouble() {
+  function Module(stdlib, foreign, heap) {
+    "use asm";
+    function func() {
+      var a = 1;
+      var b = 3.0;
+      a = b;
+    }
+    return {func: func};
+  }
+  assertThrows(function() {
+    Wasm.instantiateModuleFromAsm(Module.toString());
+  });
+})();
+
+
 (function TestBadMultiplyIntish() {
   function Module(stdlib, foreign, heap) {
     "use asm";
diff --git a/test/mjsunit/wasm/default-func-call.js b/test/mjsunit/wasm/default-func-call.js
new file mode 100644
index 0000000..14567d3
--- /dev/null
+++ b/test/mjsunit/wasm/default-func-call.js
@@ -0,0 +1,56 @@
+// Copyright 2015 the V8 project authors. All rights reserved.
+//
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --expose-wasm
+// Flags: --wasm-jit-prototype
+
+load("test/mjsunit/wasm/wasm-constants.js");
+load("test/mjsunit/wasm/wasm-module-builder.js");
+
+var module = (function () {
+  var builder = new WasmModuleBuilder();
+
+  var sig_index = builder.addType(kSig_i_ii);
+  builder.addPadFunctionTable(512);
+  builder.addImport("add", sig_index);
+  builder.addFunction("add", sig_index)
+    .addBody([
+      kExprGetLocal, 0, kExprGetLocal, 1, kExprCallImport, kArity2, 0
+    ]);
+  builder.addFunction("sub", sig_index)
+    .addBody([
+      kExprGetLocal, 0,             // --
+      kExprGetLocal, 1,             // --
+      kExprI32Sub,                  // --
+    ]);
+  builder.addFunction("main", kSig_i_iii)
+    .addBody([
+      kExprGetLocal, 0,
+      kExprGetLocal, 1,
+      kExprGetLocal, 2,
+      kExprCallIndirect, kArity2, sig_index
+    ])
+    .exportFunc()
+  builder.appendToTable([0, 1, 2]);
+
+  return builder.instantiate({add: function(a, b) { return a + b | 0; }});
+})();
+
+// Check the module exists.
+assertFalse(module === undefined);
+assertFalse(module === null);
+assertFalse(module === 0);
+assertEquals("object", typeof module.exports);
+assertEquals("function", typeof module.exports.main);
+
+assertEquals(5, module.exports.main(1, 12, 7));
+assertEquals(19, module.exports.main(0, 12, 7));
+
+assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)");
+assertTraps(kTrapFuncSigMismatch, "module.exports.main(4, 12, 33)");
+assertTraps(kTrapFuncSigMismatch, "module.exports.main(511, 12, 33)");
+assertTraps(kTrapFuncInvalid, "module.exports.main(512, 12, 33)");
+assertTraps(kTrapFuncInvalid, "module.exports.main(1025, 12, 33)");
+assertTraps(kTrapFuncInvalid, "module.exports.main(-1, 12, 33)");
diff --git a/test/mjsunit/wasm/export-table.js b/test/mjsunit/wasm/export-table.js
index a41d85d..2084ddf 100644
--- a/test/mjsunit/wasm/export-table.js
+++ b/test/mjsunit/wasm/export-table.js
@@ -72,3 +72,18 @@
 
   assertEquals(kReturnValue, module.exports["0"]());
 })();
+
+(function testExportNameClash() {
+  var builder = new WasmModuleBuilder();
+
+  builder.addFunction("one",   kSig_v_v).addBody([kExprNop]).exportAs("main");
+  builder.addFunction("two",   kSig_v_v).addBody([kExprNop]).exportAs("other");
+  builder.addFunction("three", kSig_v_v).addBody([kExprNop]).exportAs("main");
+
+  try {
+    builder.instantiate();
+    assertUnreachable("should have thrown an exception");
+  } catch (e) {
+    assertContains("Duplicate export", e.toString());
+  }
+})();
diff --git a/test/mjsunit/wasm/ffi.js b/test/mjsunit/wasm/ffi.js
index 87dfe3b..9db8ea6 100644
--- a/test/mjsunit/wasm/ffi.js
+++ b/test/mjsunit/wasm/ffi.js
@@ -10,7 +10,7 @@
 function testCallFFI(func, check) {
   var builder = new WasmModuleBuilder();
 
-  var sig_index = builder.addSignature(kSig_i_dd);
+  var sig_index = builder.addType(kSig_i_dd);
   builder.addImport("func", sig_index);
   builder.addFunction("main", sig_index)
     .addBody([
diff --git a/test/mjsunit/wasm/function-names.js b/test/mjsunit/wasm/function-names.js
new file mode 100644
index 0000000..15771d8
--- /dev/null
+++ b/test/mjsunit/wasm/function-names.js
@@ -0,0 +1,69 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Flags: --expose-wasm
+
+load("test/mjsunit/wasm/wasm-constants.js");
+load("test/mjsunit/wasm/wasm-module-builder.js");
+
+var builder = new WasmModuleBuilder();
+
+var last_func_index = builder.addFunction("exec_unreachable", kSig_v_v)
+  .addBody([kExprUnreachable])
+
+var illegal_func_name = [0xff];
+var func_names = [ "☠", illegal_func_name, "some math: (½)² = ¼", "" ];
+var expected_names = ["exec_unreachable", "☠", null,
+                      "some math: (½)² = ¼", "", "main"];
+
+for (var func_name of func_names) {
+  last_func_index = builder.addFunction(func_name, kSig_v_v)
+    .addBody([kExprCallFunction, kArity0, last_func_index]).index;
+}
+
+builder.addFunction("main", kSig_v_v)
+  .addBody([kExprCallFunction, kArity0, last_func_index])
+  .exportFunc();
+
+var module = builder.instantiate();
+
+(function testFunctionNamesAsString() {
+  var names = expected_names.concat(["testFunctionNamesAsString", null]);
+  try {
+    module.exports.main();
+    assertFalse("should throw");
+  } catch (e) {
+    var lines = e.stack.split(/\r?\n/);
+    lines.shift();
+    assertEquals(names.length, lines.length);
+    for (var i = 0; i < names.length; ++i) {
+      var line = lines[i].trim();
+      if (names[i] === null) continue;
+      var printed_name = names[i] === undefined ? "<WASM UNNAMED>" : names[i]
+      var expected_start = "at " + printed_name + " (";
+      assertTrue(line.startsWith(expected_start),
+          "should start with '" + expected_start + "': '" + line + "'");
+    }
+  }
+})();
+
+// For the remaining tests, collect the Callsite objects instead of just a
+// string:
+Error.prepareStackTrace = function(error, frames) {
+  return frames;
+};
+
+
+(function testFunctionNamesAsCallSites() {
+  var names = expected_names.concat(["testFunctionNamesAsCallSites", null]);
+  try {
+    module.exports.main();
+    assertFalse("should throw");
+  } catch (e) {
+    assertEquals(names.length, e.stack.length);
+    for (var i = 0; i < names.length; ++i) {
+      assertEquals(names[i], e.stack[i].getFunctionName());
+    }
+  }
+})();
diff --git a/test/mjsunit/wasm/gc-frame.js b/test/mjsunit/wasm/gc-frame.js
index 5fa9b05..9c37fe4 100644
--- a/test/mjsunit/wasm/gc-frame.js
+++ b/test/mjsunit/wasm/gc-frame.js
@@ -10,7 +10,7 @@
 function makeFFI(func, t) {
   var builder = new WasmModuleBuilder();
 
-  var sig_index = builder.addSignature([10,t,t,t,t,t,t,t,t,t,t,1,t]);
+  var sig_index = builder.addType(makeSig([t,t,t,t,t,t,t,t,t,t], [t]));
   builder.addImport("func", sig_index);
   // Try to create a frame with lots of spilled values and parameters
   // on the stack to try to catch GC bugs in the reference maps for
@@ -66,9 +66,32 @@
   }
 })();
 
-(function I32Test() {
+(function F64Test() {
   var main = makeFFI(print10, kAstF64);
   for (var i = 1; i < 2e+80; i *= -1137) {
     main(i - 1, i, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7, i + 8);
   }
 })();
+
+(function GCInJSToWasmTest() {
+  var builder = new WasmModuleBuilder();
+
+  var sig_index = builder.addType(kSig_i_i);
+  builder.addFunction("main", sig_index)
+    .addBody([
+      kExprGetLocal, 0,         // --
+    ])                          // --
+    .exportFunc();
+
+  var main = builder.instantiate({}).exports.main;
+
+  var gc_object = {
+      valueOf: function() {
+        // Call the GC in valueOf, which is called within the JSToWasm wrapper.
+        gc();
+        return {};
+      }
+  };
+
+  main(gc_object);
+})();
diff --git a/test/mjsunit/wasm/import-table.js b/test/mjsunit/wasm/import-table.js
index c3f8cb9..ebba040 100644
--- a/test/mjsunit/wasm/import-table.js
+++ b/test/mjsunit/wasm/import-table.js
@@ -10,7 +10,7 @@
 function testCallImport(func, check) {
   var builder = new WasmModuleBuilder();
 
-  var sig_index = builder.addSignature(kSig_i_dd);
+  var sig_index = builder.addType(kSig_i_dd);
   builder.addImport("func", sig_index);
   builder.addFunction("main", sig_index)
     .addBody([
diff --git a/test/mjsunit/wasm/indirect-calls.js b/test/mjsunit/wasm/indirect-calls.js
index 80bee41..1e87c6f 100644
--- a/test/mjsunit/wasm/indirect-calls.js
+++ b/test/mjsunit/wasm/indirect-calls.js
@@ -10,7 +10,7 @@
 var module = (function () {
   var builder = new WasmModuleBuilder();
 
-  var sig_index = builder.addSignature(kSig_i_ii);
+  var sig_index = builder.addType(kSig_i_ii);
   builder.addImport("add", sig_index);
   builder.addFunction("add", sig_index)
     .addBody([
@@ -30,7 +30,7 @@
       kExprCallIndirect, kArity2, sig_index
     ])
     .exportFunc()
-  builder.appendToFunctionTable([0, 1, 2]);
+  builder.appendToTable([0, 1, 2]);
 
   return builder.instantiate({add: function(a, b) { return a + b | 0; }});
 })();
diff --git a/test/mjsunit/wasm/instantiate-module-basic.js b/test/mjsunit/wasm/instantiate-module-basic.js
index 800dcc1..72a3425 100644
--- a/test/mjsunit/wasm/instantiate-module-basic.js
+++ b/test/mjsunit/wasm/instantiate-module-basic.js
@@ -7,44 +7,54 @@
 load("test/mjsunit/wasm/wasm-constants.js");
 load("test/mjsunit/wasm/wasm-module-builder.js");
 
-var kReturnValue = 117;
+let kReturnValue = 117;
 
-var module = (function Build() {
-  var builder = new WasmModuleBuilder();
-
+let buffer = (() => {
+  let builder = new WasmModuleBuilder();
   builder.addMemory(1, 1, true);
   builder.addFunction("main", kSig_i)
     .addBody([kExprI8Const, kReturnValue])
     .exportFunc();
 
-  return builder.instantiate();
-})();
+  return builder.toBuffer();
+})()
 
-// Check the module exists.
-assertFalse(module === undefined);
-assertFalse(module === null);
-assertFalse(module === 0);
-assertEquals("object", typeof module);
+function CheckInstance(instance) {
+  assertFalse(instance === undefined);
+  assertFalse(instance === null);
+  assertFalse(instance === 0);
+  assertEquals("object", typeof instance);
 
-// Check the memory is an ArrayBuffer.
-var mem = module.exports.memory;
-assertFalse(mem === undefined);
-assertFalse(mem === null);
-assertFalse(mem === 0);
-assertEquals("object", typeof mem);
-assertTrue(mem instanceof ArrayBuffer);
-for (var i = 0; i < 4; i++) {
-  module.exports.memory = 0;  // should be ignored
-  assertEquals(mem, module.exports.memory);
+  // Check the memory is an ArrayBuffer.
+  var mem = instance.exports.memory;
+  assertFalse(mem === undefined);
+  assertFalse(mem === null);
+  assertFalse(mem === 0);
+  assertEquals("object", typeof mem);
+  assertTrue(mem instanceof ArrayBuffer);
+  for (let i = 0; i < 4; i++) {
+    instance.exports.memory = 0;  // should be ignored
+    assertSame(mem, instance.exports.memory);
+  }
+
+  assertEquals(65536, instance.exports.memory.byteLength);
+
+  // Check the properties of the main function.
+  let main = instance.exports.main;
+  assertFalse(main === undefined);
+  assertFalse(main === null);
+  assertFalse(main === 0);
+  assertEquals("function", typeof main);
+
+  assertEquals(kReturnValue, main());
 }
 
-assertEquals(65536, module.exports.memory.byteLength);
+// Deprecated experimental API.
+CheckInstance(Wasm.instantiateModule(buffer));
 
-// Check the properties of the main function.
-var main = module.exports.main;
-assertFalse(main === undefined);
-assertFalse(main === null);
-assertFalse(main === 0);
-assertEquals("function", typeof main);
+// Official API
+let module = new WebAssembly.Module(buffer);
+CheckInstance(new WebAssembly.Instance(module));
 
-assertEquals(kReturnValue, main());
+let promise = WebAssembly.compile(buffer);
+promise.then(module => CheckInstance(new WebAssembly.Instance(module)));
diff --git a/test/mjsunit/wasm/params.js b/test/mjsunit/wasm/params.js
index 180ab1c..fe1b7d4 100644
--- a/test/mjsunit/wasm/params.js
+++ b/test/mjsunit/wasm/params.js
@@ -79,7 +79,7 @@
     print("type = " + t + ", which = " + which);
 
     var builder = new WasmModuleBuilder();
-    builder.addFunction("select", [10,t,t,t,t,t,t,t,t,t,t,1,t])
+    builder.addFunction("select", makeSig([t,t,t,t,t,t,t,t,t,t], [t]))
       .addBody([kExprGetLocal, which])
       .exportFunc();
 
diff --git a/test/mjsunit/wasm/stack.js b/test/mjsunit/wasm/stack.js
index a45db94..4ff0d1d 100644
--- a/test/mjsunit/wasm/stack.js
+++ b/test/mjsunit/wasm/stack.js
@@ -16,10 +16,8 @@
 function verifyStack(frames, expected) {
   assertEquals(expected.length, frames.length, "number of frames mismatch");
   expected.forEach(function(exp, i) {
-    if (exp[1] != "?") {
-      assertEquals(exp[1], frames[i].getFunctionName(),
-          "["+i+"].getFunctionName()");
-    }
+    assertEquals(exp[1], frames[i].getFunctionName(),
+        "["+i+"].getFunctionName()");
     assertEquals(exp[2], frames[i].getLineNumber(), "["+i+"].getLineNumber()");
     if (exp[0])
       assertEquals(exp[3], frames[i].getPosition(),
@@ -27,8 +25,7 @@
     assertContains(exp[4], frames[i].getFileName(), "["+i+"].getFileName()");
     var toString;
     if (exp[0]) {
-      var funName = exp[1] == "?" ? "" : exp[1];
-      toString = funName + " (<WASM>:" + exp[2] + ":" + exp[3] + ")";
+      toString = exp[1] + " (<WASM>[" + exp[2] + "]+" + exp[3] + ")";
     } else {
       toString = exp[4] + ":" + exp[2] + ":";
     }
@@ -71,10 +68,10 @@
 (function testSimpleStack() {
   var expected_string = "Error\n" +
     // The line numbers below will change as this test gains / loses lines..
-    "    at STACK (stack.js:42:11)\n" +           // --
-    "    at main (<WASM>:0:1)\n" +                // --
-    "    at testSimpleStack (stack.js:79:18)\n" + // --
-    "    at stack.js:81:3";                       // --
+    "    at STACK (stack.js:39:11)\n" +           // --
+    "    at main (<WASM>[0]+1)\n" +               // --
+    "    at testSimpleStack (stack.js:76:18)\n" + // --
+    "    at stack.js:78:3";                       // --
 
   module.exports.main();
   assertEquals(expected_string, stripPath(stack));
@@ -91,10 +88,10 @@
 
   verifyStack(stack, [
       // isWasm           function   line  pos        file
-      [   false,           "STACK",    42,   0, "stack.js"],
+      [   false,           "STACK",    39,   0, "stack.js"],
       [    true,            "main",     0,   1,       null],
-      [   false, "testStackFrames",    90,   0, "stack.js"],
-      [   false,              null,    99,   0, "stack.js"]
+      [   false, "testStackFrames",    87,   0, "stack.js"],
+      [   false,              null,    96,   0, "stack.js"]
   ]);
 })();
 
@@ -107,8 +104,8 @@
     verifyStack(e.stack, [
         // isWasm               function   line  pos        file
         [    true,    "exec_unreachable",    1,    1,       null],
-        [   false, "testWasmUnreachable",  103,    0, "stack.js"],
-        [   false,                  null,  114,    0, "stack.js"]
+        [   false, "testWasmUnreachable",  100,    0, "stack.js"],
+        [   false,                  null,  111,    0, "stack.js"]
     ]);
   }
 })();
@@ -121,10 +118,10 @@
     assertContains("out of bounds", e.message);
     verifyStack(e.stack, [
         // isWasm                  function   line  pos        file
-        [    true,                      "?",     2,   3,       null],
+        [    true,                       "",     2,   3,       null],
         [    true, "call_mem_out_of_bounds",     3,   1,       null],
-        [   false, "testWasmMemOutOfBounds",   118,   0, "stack.js"],
-        [   false,                     null,   130,   0, "stack.js"]
+        [   false, "testWasmMemOutOfBounds",   115,   0, "stack.js"],
+        [   false,                     null,   127,   0, "stack.js"]
     ]);
   }
 })();
diff --git a/test/mjsunit/wasm/stackwalk.js b/test/mjsunit/wasm/stackwalk.js
index 5e5a1ef..913269f 100644
--- a/test/mjsunit/wasm/stackwalk.js
+++ b/test/mjsunit/wasm/stackwalk.js
@@ -10,7 +10,7 @@
 function makeFFI(func) {
   var builder = new WasmModuleBuilder();
 
-  var sig_index = builder.addSignature(kSig_i_dd);
+  var sig_index = builder.addType(kSig_i_dd);
   builder.addImport("func", sig_index);
   builder.addFunction("main", sig_index)
     .addBody([
diff --git a/test/mjsunit/wasm/start-function.js b/test/mjsunit/wasm/start-function.js
index 3c5707a..c4d299e 100644
--- a/test/mjsunit/wasm/start-function.js
+++ b/test/mjsunit/wasm/start-function.js
@@ -65,8 +65,8 @@
   var func = builder.addFunction("", kSig_v_v)
     .addBody([kExprNop]);
 
-  builder.addExplicitSection([kDeclStartFunction, 0]);
-  builder.addExplicitSection([kDeclStartFunction, 0]);
+  builder.addExplicitSection([kDeclStart, 0]);
+  builder.addExplicitSection([kDeclStart, 0]);
 
   assertThrows(builder.instantiate);
 })();
@@ -98,7 +98,7 @@
   }};
 
   var builder = new WasmModuleBuilder();
-  var sig_index = builder.addSignature(kSig_v_v);
+  var sig_index = builder.addType(kSig_v_v);
 
   builder.addImport("foo", sig_index);
   var func = builder.addFunction("", sig_index)
diff --git a/test/mjsunit/wasm/test-wasm-module-builder.js b/test/mjsunit/wasm/test-wasm-module-builder.js
index 969b550..72d5a7a 100644
--- a/test/mjsunit/wasm/test-wasm-module-builder.js
+++ b/test/mjsunit/wasm/test-wasm-module-builder.js
@@ -91,7 +91,7 @@
         .addBody([kExprGetLocal,
                   0, kExprGetLocal, 1, kExprGetLocal, 2, kExprCallIndirect, kArity2, 0])
         .exportAs("main");
-    module.appendToFunctionTable([0]);
+    module.appendToTable([0]);
 
     var instance = module.instantiate();
     assertEquals(44, instance.exports.main(0, 11, 33));
diff --git a/test/mjsunit/wasm/trap-location.js b/test/mjsunit/wasm/trap-location.js
index 5e3661d..0440af9 100644
--- a/test/mjsunit/wasm/trap-location.js
+++ b/test/mjsunit/wasm/trap-location.js
@@ -14,7 +14,7 @@
 
 var builder = new WasmModuleBuilder();
 
-var sig_index = builder.addSignature(kSig_i_v)
+var sig_index = builder.addType(kSig_i_v)
 
 // Build a function to resemble this code:
 //   if (idx < 2) {
diff --git a/test/mjsunit/wasm/wasm-constants.js b/test/mjsunit/wasm/wasm-constants.js
index 389383e..319cadc 100644
--- a/test/mjsunit/wasm/wasm-constants.js
+++ b/test/mjsunit/wasm/wasm-constants.js
@@ -52,18 +52,19 @@
 
 // Section declaration constants
 var kDeclMemory = 0x00;
-var kDeclSignatures = 0x01;
+var kDeclTypes = 0x01;
 var kDeclFunctions = 0x02;
 var kDeclGlobals = 0x03;
-var kDeclDataSegments = 0x04;
-var kDeclFunctionTable = 0x05;
+var kDeclData = 0x04;
+var kDeclTable = 0x05;
 var kDeclEnd = 0x06;
-var kDeclStartFunction = 0x07;
-var kDeclImportTable = 0x08;
-var kDeclExportTable = 0x09;
-var kDeclFunctionSignatures = 0x0a;
-var kDeclFunctionBodies = 0x0b;
+var kDeclStart = 0x07;
+var kDeclImports = 0x08;
+var kDeclExports = 0x09;
+var kDeclFunctions = 0x0a;
+var kDeclCode = 0x0b;
 var kDeclNames = 0x0c;
+var kDeclFunctionTablePad = 0x0d;
 
 var kArity0 = 0;
 var kArity1 = 1;
@@ -74,7 +75,7 @@
 var section_names = [
   "memory", "type", "old_function", "global", "data",
   "table", "end", "start", "import", "export",
-  "function", "code", "name"];
+  "function", "code", "name", "table_pad"];
 
 // Function declaration flags
 var kDeclFunctionName   = 0x01;
@@ -90,31 +91,39 @@
 var kAstF64 = 4;
 
 // Useful signatures
-var kSig_i = [0, 1, kAstI32];
-var kSig_d = [0, 1, kAstF64];
-var kSig_i_i = [1, kAstI32, 1, kAstI32];
-var kSig_i_ii = [2, kAstI32, kAstI32, 1, kAstI32];
-var kSig_i_iii = [3, kAstI32, kAstI32, kAstI32, 1, kAstI32];
-var kSig_d_dd = [2, kAstF64, kAstF64, 1, kAstF64];
-var kSig_l_ll = [2, kAstI64, kAstI64, 1, kAstI64];
-var kSig_i_dd = [2, kAstF64, kAstF64, 1, kAstI32];
-var kSig_v_v = [0, 0];
-var kSig_i_v = [0, 1, kAstI32];
+var kSig_i = makeSig([], [kAstI32]);
+var kSig_d = makeSig([], [kAstF64]);
+var kSig_i_i = makeSig([kAstI32], [kAstI32]);
+var kSig_i_ii = makeSig([kAstI32, kAstI32], [kAstI32]);
+var kSig_i_iii = makeSig([kAstI32, kAstI32, kAstI32], [kAstI32]);
+var kSig_d_dd = makeSig([kAstF64, kAstF64], [kAstF64]);
+var kSig_l_ll = makeSig([kAstI64, kAstI64], [kAstI64]);
+var kSig_i_dd = makeSig([kAstF64, kAstF64], [kAstI32]);
+var kSig_v_v = makeSig([], []);
+var kSig_i_v = makeSig([], [kAstI32]);
 
-function makeSig_v_xx(x) {
-  return [2, x, x, 0];
+function makeSig(params, results) {
+  return {params: params, results: results};
 }
 
 function makeSig_v_x(x) {
-  return [1, x, 0];
+  return makeSig([x], []);
 }
 
-function makeSig_r_xx(r, x) {
-  return [2, x, x, 1, r];
+function makeSig_v_xx(x) {
+  return makeSig([x, x], []);
+}
+
+function makeSig_r_v(r) {
+  return makeSig([], [r]);
 }
 
 function makeSig_r_x(r, x) {
-  return [1, x, 1, r];
+  return makeSig([x], [r]);
+}
+
+function makeSig_r_xx(r, x) {
+  return makeSig([x, x], [r]);
 }
 
 // Opcodes
diff --git a/test/mjsunit/wasm/wasm-module-builder.js b/test/mjsunit/wasm/wasm-module-builder.js
index bfc4460..5c5df79 100644
--- a/test/mjsunit/wasm/wasm-module-builder.js
+++ b/test/mjsunit/wasm/wasm-module-builder.js
@@ -2,351 +2,373 @@
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
 
-function WasmFunctionBuilder(name, sig_index) {
-    this.name = name;
-    this.sig_index = sig_index;
-    this.exports = [];
-}
+class Binary extends Array {
+  emit_u8(val) {
+    this.push(val);
+  }
 
-WasmFunctionBuilder.prototype.exportAs = function(name) {
-    this.exports.push(name);
-    return this;
-}
+  emit_u16(val) {
+    this.push(val & 0xff);
+    this.push((val >> 8) & 0xff);
+  }
 
-WasmFunctionBuilder.prototype.exportFunc = function() {
-  this.exports.push(this.name);
-  return this;
-}
+  emit_u32(val) {
+    this.push(val & 0xff);
+    this.push((val >> 8) & 0xff);
+    this.push((val >> 16) & 0xff);
+    this.push((val >> 24) & 0xff);
+  }
 
-WasmFunctionBuilder.prototype.addBody = function(body) {
-    this.body = body;
-    return this;
-}
+  emit_varint(val) {
+    while (true) {
+      let v = val & 0xff;
+      val = val >>> 7;
+      if (val == 0) {
+        this.push(v);
+        break;
+      }
+      this.push(v | 0x80);
+    }
+  }
 
-WasmFunctionBuilder.prototype.addLocals = function(locals) {
-    this.locals = locals;
-    return this;
-}
+  emit_bytes(data) {
+    for (let i = 0; i < data.length; i++) {
+      this.push(data[i] & 0xff);
+    }
+  }
 
-function WasmModuleBuilder() {
-    this.signatures = [];
-    this.imports = [];
-    this.functions = [];
-    this.exports = [];
-    this.function_table = [];
-    this.data_segments = [];
-    this.explicit = [];
-    return this;
-}
-
-WasmModuleBuilder.prototype.addStart = function(start_index) {
-    this.start_index = start_index;
-}
-
-WasmModuleBuilder.prototype.addMemory = function(min, max, exp) {
-    this.memory = {min: min, max: max, exp: exp};
-    return this;
-}
-
-WasmModuleBuilder.prototype.addExplicitSection = function(bytes) {
-  this.explicit.push(bytes);
-  return this;
-}
-
-// Add a signature; format is [param_count, param0, param1, ..., retcount, ret0]
-WasmModuleBuilder.prototype.addSignature = function(sig) {
-    // TODO: canonicalize signatures?
-    this.signatures.push(sig);
-    return this.signatures.length - 1;
-}
-
-WasmModuleBuilder.prototype.addFunction = function(name, sig) {
-    var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig);
-    var func = new WasmFunctionBuilder(name, sig_index);
-    func.index = this.functions.length;
-    this.functions.push(func);
-    return func;
-}
-
-WasmModuleBuilder.prototype.addImportWithModule = function(module, name, sig) {
-  var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig);
-  this.imports.push({module: module, name: name, sig_index: sig_index});
-  return this.imports.length - 1;
-}
-
-WasmModuleBuilder.prototype.addImport = function(name, sig) {
-  this.addImportWithModule(name, undefined, sig);
-}
-
-WasmModuleBuilder.prototype.addDataSegment = function(addr, data, init) {
-    this.data_segments.push({addr: addr, data: data, init: init});
-    return this.data_segments.length - 1;
-}
-
-WasmModuleBuilder.prototype.appendToFunctionTable = function(array) {
-    this.function_table = this.function_table.concat(array);
-    return this;
-}
-
-function emit_u8(bytes, val) {
-    bytes.push(val & 0xff);
-}
-
-function emit_u16(bytes, val) {
-    bytes.push(val & 0xff);
-    bytes.push((val >> 8) & 0xff);
-}
-
-function emit_u32(bytes, val) {
-    bytes.push(val & 0xff);
-    bytes.push((val >> 8) & 0xff);
-    bytes.push((val >> 16) & 0xff);
-    bytes.push((val >> 24) & 0xff);
-}
-
-function emit_string(bytes, string) {
+  emit_string(string) {
     // When testing illegal names, we pass a byte array directly.
     if (string instanceof Array) {
-      emit_varint(bytes, string.length);
-      emit_bytes(bytes, string);
+      this.emit_varint(string.length);
+      this.emit_bytes(string);
       return;
     }
 
-    // This is the hacky way to convert a JavaScript scring to a UTF8 encoded
+    // This is the hacky way to convert a JavaScript string to a UTF8 encoded
     // string only containing single-byte characters.
-    var string_utf8 = unescape(encodeURIComponent(string));
-    emit_varint(bytes, string_utf8.length);
-    for (var i = 0; i < string_utf8.length; i++) {
-      emit_u8(bytes, string_utf8.charCodeAt(i));
+    let string_utf8 = unescape(encodeURIComponent(string));
+    this.emit_varint(string_utf8.length);
+    for (let i = 0; i < string_utf8.length; i++) {
+      this.emit_u8(string_utf8.charCodeAt(i));
     }
-}
+  }
 
-function emit_varint(bytes, val) {
-    while (true) {
-        var v = val & 0xff;
-        val = val >>> 7;
-        if (val == 0) {
-            bytes.push(v);
-            break;
-        }
-        bytes.push(v | 0x80);
-    }
-}
+  emit_header() {
+    this.push(kWasmH0, kWasmH1, kWasmH2, kWasmH3,
+              kWasmV0, kWasmV1, kWasmV2, kWasmV3);
+  }
 
-function emit_bytes(bytes, data) {
-  for (var i = 0; i < data.length; i++) {
-    bytes.push(data[i] & 0xff);
+  emit_section(section_code, content_generator) {
+    // Emit section name.
+    this.emit_string(section_names[section_code]);
+    // Emit the section to a temporary buffer: its full length isn't know yet.
+    let section = new Binary;
+    content_generator(section);
+    // Emit section length.
+    this.emit_varint(section.length);
+    // Copy the temporary buffer.
+    this.push(...section);
   }
 }
 
-function emit_section(bytes, section_code, content_generator) {
-    // Emit section name.
-    emit_string(bytes, section_names[section_code]);
-    // Emit the section to a temporary buffer: its full length isn't know yet.
-    var tmp_bytes = [];
-    content_generator(tmp_bytes);
-    // Emit section length.
-    emit_varint(bytes, tmp_bytes.length);
-    // Copy the temporary buffer.
-    Array.prototype.push.apply(bytes, tmp_bytes);
+class WasmFunctionBuilder {
+  constructor(name, type_index) {
+    this.name = name;
+    this.type_index = type_index;
+    this.exports = [];
+  }
+
+  exportAs(name) {
+    this.exports.push(name);
+    return this;
+  }
+
+  exportFunc() {
+    this.exports.push(this.name);
+    return this;
+  }
+
+  addBody(body) {
+    this.body = body;
+    return this;
+  }
+
+  addLocals(locals) {
+    this.locals = locals;
+    return this;
+  }
 }
 
-WasmModuleBuilder.prototype.toArray = function(debug) {
-    // Add header bytes
-    var bytes = [];
-    bytes = bytes.concat([kWasmH0, kWasmH1, kWasmH2, kWasmH3,
-                          kWasmV0, kWasmV1, kWasmV2, kWasmV3]);
+class WasmModuleBuilder {
+  constructor() {
+    this.types = [];
+    this.imports = [];
+    this.functions = [];
+    this.exports = [];
+    this.table = [];
+    this.segments = [];
+    this.explicit = [];
+    this.pad = null;
+    return this;
+  }
 
-    var wasm = this;
+  addStart(start_index) {
+    this.start_index = start_index;
+  }
 
-    // Add signatures section
-    if (wasm.signatures.length > 0) {
-        if (debug) print("emitting signatures @ " + bytes.length);
-        emit_section(bytes, kDeclSignatures, function(bytes) {
-            emit_varint(bytes, wasm.signatures.length);
-            for (sig of wasm.signatures) {
-                emit_u8(bytes, kWasmFunctionTypeForm);
-                for (var j = 0; j < sig.length; j++) {
-                    emit_u8(bytes, sig[j]);
-                }
-            }
-        });
+  addMemory(min, max, exp) {
+    this.memory = {min: min, max: max, exp: exp};
+    return this;
+  }
+
+  addPadFunctionTable(size) {
+    this.pad = size;
+    return this;
+  }
+
+  addExplicitSection(bytes) {
+    this.explicit.push(bytes);
+    return this;
+  }
+
+  addType(type) {
+    // TODO: canonicalize types?
+    this.types.push(type);
+    return this.types.length - 1;
+  }
+
+  addFunction(name, type) {
+    let type_index = (typeof type) == "number" ? type : this.addType(type);
+    let func = new WasmFunctionBuilder(name, type_index);
+    func.index = this.functions.length;
+    this.functions.push(func);
+    return func;
+  }
+
+  addImportWithModule(module, name, type) {
+    let type_index = (typeof type) == "number" ? type : this.addType(type);
+    this.imports.push({module: module, name: name, type: type_index});
+    return this.imports.length - 1;
+  }
+
+  addImport(name, type) {
+    return this.addImportWithModule(name, undefined, type);
+  }
+
+  addDataSegment(addr, data, init) {
+    this.segments.push({addr: addr, data: data, init: init});
+    return this.segments.length - 1;
+  }
+
+  appendToTable(array) {
+    this.table.push(...array);
+    return this;
+  }
+
+  toArray(debug) {
+    let binary = new Binary;
+    let wasm = this;
+
+    // Add header
+    binary.emit_header();
+
+    // Add type section
+    if (wasm.types.length > 0) {
+      if (debug) print("emitting types @ " + binary.length);
+      binary.emit_section(kDeclTypes, section => {
+        section.emit_varint(wasm.types.length);
+        for (let type of wasm.types) {
+          section.emit_u8(kWasmFunctionTypeForm);
+          section.emit_varint(type.params.length);
+          for (let param of type.params) {
+            section.emit_u8(param);
+          }
+          section.emit_varint(type.results.length);
+          for (let result of type.results) {
+            section.emit_u8(result);
+          }
+        }
+      });
     }
 
     // Add imports section
     if (wasm.imports.length > 0) {
-        if (debug) print("emitting imports @ " + bytes.length);
-        emit_section(bytes, kDeclImportTable, function(bytes) {
-            emit_varint(bytes, wasm.imports.length);
-            for (imp of wasm.imports) {
-                emit_varint(bytes, imp.sig_index);
-                emit_string(bytes, imp.module);
-                emit_string(bytes, imp.name || '');
-            }
-        });
+      if (debug) print("emitting imports @ " + binary.length);
+      binary.emit_section(kDeclImports, section => {
+        section.emit_varint(wasm.imports.length);
+        for (let imp of wasm.imports) {
+          section.emit_varint(imp.type);
+          section.emit_string(imp.module);
+          section.emit_string(imp.name || '');
+        }
+      });
     }
 
     // Add functions declarations
-    var names = false;
-    var exports = 0;
+    let has_names = false;
+    let names = false;
+    let exports = 0;
     if (wasm.functions.length > 0) {
-        var has_names = false;
-
-        // emit function signatures
-        if (debug) print("emitting function sigs @ " + bytes.length);
-        emit_section(bytes, kDeclFunctionSignatures, function(bytes) {
-            emit_varint(bytes, wasm.functions.length);
-            for (func of wasm.functions) {
-              has_names = has_names || (func.name != undefined &&
-                                        func.name.length > 0);
-              exports += func.exports.length;
-
-              emit_varint(bytes, func.sig_index);
-            }
-        });
-
+      if (debug) print("emitting function decls @ " + binary.length);
+      binary.emit_section(kDeclFunctions, section => {
+        section.emit_varint(wasm.functions.length);
+        for (let func of wasm.functions) {
+          has_names = has_names || (func.name != undefined &&
+                                   func.name.length > 0);
+          exports += func.exports.length;
+          section.emit_varint(func.type_index);
+        }
+      });
     }
 
-    // Add function table.
-    if (wasm.function_table.length > 0) {
-        if (debug) print("emitting function table @ " + bytes.length);
-        emit_section(bytes, kDeclFunctionTable, function(bytes) {
-            emit_varint(bytes, wasm.function_table.length);
-            for (index of wasm.function_table) {
-                emit_varint(bytes, index);
-            }
-        });
+    // Add table.
+    if (wasm.table.length > 0) {
+      if (debug) print("emitting table @ " + binary.length);
+      binary.emit_section(kDeclTable, section => {
+        section.emit_varint(wasm.table.length);
+        for (let index of wasm.table) {
+          section.emit_varint(index);
+        }
+      });
     }
 
     // Add memory section
     if (wasm.memory != undefined) {
-        if (debug) print("emitting memory @ " + bytes.length);
-        emit_section(bytes, kDeclMemory, function(bytes) {
-            emit_varint(bytes, wasm.memory.min);
-            emit_varint(bytes, wasm.memory.max);
-            emit_u8(bytes, wasm.memory.exp ? 1 : 0);
-        });
+      if (debug) print("emitting memory @ " + binary.length);
+      binary.emit_section(kDeclMemory, section => {
+        section.emit_varint(wasm.memory.min);
+        section.emit_varint(wasm.memory.max);
+        section.emit_u8(wasm.memory.exp ? 1 : 0);
+      });
     }
 
 
     // Add export table.
     if (exports > 0) {
-        if (debug) print("emitting exports @ " + bytes.length);
-        emit_section(bytes, kDeclExportTable, function(bytes) {
-            emit_varint(bytes, exports);
-            for (func of wasm.functions) {
-                for (exp of func.exports) {
-                    emit_varint(bytes, func.index);
-                    emit_string(bytes, exp);
-                }
-            }
-        });
+      if (debug) print("emitting exports @ " + binary.length);
+      binary.emit_section(kDeclExports, section => {
+        section.emit_varint(exports);
+        for (let func of wasm.functions) {
+          for (let exp of func.exports) {
+            section.emit_varint(func.index);
+            section.emit_string(exp);
+          }
+        }
+      });
     }
 
     // Add start function section.
     if (wasm.start_index != undefined) {
-        if (debug) print("emitting start function @ " + bytes.length);
-        emit_section(bytes, kDeclStartFunction, function(bytes) {
-            emit_varint(bytes, wasm.start_index);
-        });
+      if (debug) print("emitting start function @ " + binary.length);
+      binary.emit_section(kDeclStart, section => {
+        section.emit_varint(wasm.start_index);
+      });
     }
 
     // Add function bodies.
     if (wasm.functions.length > 0) {
-        // emit function bodies
-        if (debug) print("emitting function bodies @ " + bytes.length);
-        emit_section(bytes, kDeclFunctionBodies, function(bytes) {
-            emit_varint(bytes, wasm.functions.length);
-            for (func of wasm.functions) {
-                // Function body length will be patched later.
-                var local_decls = [];
-                var l = func.locals;
-                if (l != undefined) {
-                  var local_decls_count = 0;
-                  if (l.i32_count > 0) {
-                    local_decls.push({count: l.i32_count, type: kAstI32});
-                  }
-                  if (l.i64_count > 0) {
-                    local_decls.push({count: l.i64_count, type: kAstI64});
-                  }
-                  if (l.f32_count > 0) {
-                    local_decls.push({count: l.f32_count, type: kAstF32});
-                  }
-                  if (l.f64_count > 0) {
-                    local_decls.push({count: l.f64_count, type: kAstF64});
-                  }
-                }
-                var header = new Array();
-
-                emit_varint(header, local_decls.length);
-                for (decl of local_decls) {
-                  emit_varint(header, decl.count);
-                  emit_u8(header, decl.type);
-                }
-
-                emit_varint(bytes, header.length + func.body.length);
-                emit_bytes(bytes, header);
-                emit_bytes(bytes, func.body);
+      // emit function bodies
+      if (debug) print("emitting code @ " + binary.length);
+      binary.emit_section(kDeclCode, section => {
+        section.emit_varint(wasm.functions.length);
+        for (let func of wasm.functions) {
+          // Function body length will be patched later.
+          let local_decls = [];
+          let l = func.locals;
+          if (l != undefined) {
+            let local_decls_count = 0;
+            if (l.i32_count > 0) {
+              local_decls.push({count: l.i32_count, type: kAstI32});
             }
-        });
+            if (l.i64_count > 0) {
+              local_decls.push({count: l.i64_count, type: kAstI64});
+            }
+            if (l.f32_count > 0) {
+              local_decls.push({count: l.f32_count, type: kAstF32});
+            }
+            if (l.f64_count > 0) {
+              local_decls.push({count: l.f64_count, type: kAstF64});
+            }
+          }
+
+          let header = new Binary;
+          header.emit_varint(local_decls.length);
+          for (let decl of local_decls) {
+            header.emit_varint(decl.count);
+            header.emit_u8(decl.type);
+          }
+
+          section.emit_varint(header.length + func.body.length);
+          section.emit_bytes(header);
+          section.emit_bytes(func.body);
+        }
+      });
     }
 
     // Add data segments.
-    if (wasm.data_segments.length > 0) {
-        if (debug) print("emitting data segments @ " + bytes.length);
-        emit_section(bytes, kDeclDataSegments, function(bytes) {
-            emit_varint(bytes, wasm.data_segments.length);
-            for (seg of wasm.data_segments) {
-                emit_varint(bytes, seg.addr);
-                emit_varint(bytes, seg.data.length);
-                emit_bytes(bytes, seg.data);
-            }
-        });
+    if (wasm.segments.length > 0) {
+      if (debug) print("emitting data segments @ " + binary.length);
+      binary.emit_section(kDeclData, section => {
+        section.emit_varint(wasm.segments.length);
+        for (let seg of wasm.segments) {
+          section.emit_varint(seg.addr);
+          section.emit_varint(seg.data.length);
+          section.emit_bytes(seg.data);
+        }
+      });
     }
 
     // Add any explicitly added sections
-    for (exp of wasm.explicit) {
-        if (debug) print("emitting explicit @ " + bytes.length);
-        emit_bytes(bytes, exp);
+    for (let exp of wasm.explicit) {
+      if (debug) print("emitting explicit @ " + binary.length);
+      binary.emit_bytes(exp);
     }
 
     // Add function names.
     if (has_names) {
-        if (debug) print("emitting names @ " + bytes.length);
-        emit_section(bytes, kDeclNames, function(bytes) {
-            emit_varint(bytes, wasm.functions.length);
-            for (func of wasm.functions) {
-                var name = func.name == undefined ? "" : func.name;
-                emit_string(bytes, name);
-                emit_u8(bytes, 0);  // local names count == 0
-            }
-        });
+      if (debug) print("emitting names @ " + binary.length);
+      binary.emit_section(kDeclNames, section => {
+        section.emit_varint(wasm.functions.length);
+        for (let func of wasm.functions) {
+          var name = func.name == undefined ? "" : func.name;
+          section.emit_string(name);
+          section.emit_u8(0);  // local names count == 0
+        }
+      });
+    }
+
+    // Add an indirect function table pad section.
+    if (wasm.pad !== null) {
+      if (debug)
+        print("emitting indirect function table pad @ " + binary.length);
+      binary.emit_section(kDeclFunctionTablePad, section => {
+        section.emit_varint(wasm.pad);
+      });
     }
 
     // End the module.
-    if (debug) print("emitting end @ " + bytes.length);
-    emit_section(bytes, kDeclEnd, function(bytes) {});
+    if (debug) print("emitting end @ " + binary.length);
+    binary.emit_section(kDeclEnd, section => {});
 
-    return bytes;
-}
+    return binary;
+  }
 
-WasmModuleBuilder.prototype.toBuffer = function(debug) {
-    var bytes = this.toArray(debug);
-    var buffer = new ArrayBuffer(bytes.length);
-    var view = new Uint8Array(buffer);
-    for (var i = 0; i < bytes.length; i++) {
-        var val = bytes[i];
-        if ((typeof val) == "string") val = val.charCodeAt(0);
-        view[i] = val | 0;
+  toBuffer(debug) {
+    let bytes = this.toArray(debug);
+    let buffer = new ArrayBuffer(bytes.length);
+    let view = new Uint8Array(buffer);
+    for (let i = 0; i < bytes.length; i++) {
+      let val = bytes[i];
+      if ((typeof val) == "string") val = val.charCodeAt(0);
+      view[i] = val | 0;
     }
     return buffer;
-}
+  }
 
-WasmModuleBuilder.prototype.instantiate = function(ffi, memory) {
-    var buffer = this.toBuffer();
-    if (memory != undefined) {
-      return Wasm.instantiateModule(buffer, ffi, memory);
-    } else {
-      return Wasm.instantiateModule(buffer, ffi);
-    }
+  instantiate(...args) {
+    let module = new WebAssembly.Module(this.toBuffer());
+    let instance = new WebAssembly.Instance(module, ...args);
+    return instance;
+  }
 }
diff --git a/test/mjsunit/wasm/wasm-object-api.js b/test/mjsunit/wasm/wasm-object-api.js
index 96088b8..510b571 100644
--- a/test/mjsunit/wasm/wasm-object-api.js
+++ b/test/mjsunit/wasm/wasm-object-api.js
@@ -11,3 +11,8 @@
 assertEquals("function", typeof Wasm.instantiateModule);
 assertEquals("function", typeof Wasm.instantiateModuleFromAsm);
 assertFalse(undefined == Wasm.experimentalVersion);
+
+assertEquals('object', typeof WebAssembly);
+assertEquals('function', typeof WebAssembly.Module);
+assertEquals('function', typeof WebAssembly.Instance);
+assertEquals('function', typeof WebAssembly.compile);