Home | History | Annotate | Download | only in wasm
      1 // Copyright 2015 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Flags: --expose-wasm
      6 
      7 load("test/mjsunit/wasm/wasm-constants.js");
      8 
      9 var module = (function () {
     10   var kFuncWithBody = 9;
     11   var kFuncImported = 7;
     12   var kBodySize1 = 5;
     13   var kBodySize2 = 8;
     14   var kFuncTableSize = 8;
     15   var kSubOffset = 13 + kFuncWithBody + kBodySize1 + kFuncImported + kFuncWithBody + kBodySize2 + kFuncTableSize + 1;
     16   var kAddOffset = kSubOffset + 4;
     17   var kMainOffset = kAddOffset + 4;
     18 
     19   var ffi = new Object();
     20   ffi.add = (function(a, b) { return a + b | 0; });
     21 
     22   return _WASMEXP_.instantiateModule(bytes(
     23     // -- signatures
     24     kDeclSignatures, 2,
     25     2, kAstI32, kAstI32, kAstI32, // int, int -> int
     26     3, kAstI32, kAstI32, kAstI32, kAstI32, // int, int, int -> int
     27     // -- function #0 (sub)
     28     kDeclFunctions, 3,
     29     kDeclFunctionName,
     30     0, 0,                         // signature offset
     31     kSubOffset, 0, 0, 0,          // name offset
     32     kBodySize1, 0,                // body size
     33     kExprI32Sub,                  // --
     34     kExprGetLocal, 0,             // --
     35     kExprGetLocal, 1,             // --
     36     // -- function #1 (add)
     37     kDeclFunctionName | kDeclFunctionImport,
     38     0, 0,                         // signature offset
     39     kAddOffset, 0, 0, 0,          // name offset
     40     // -- function #2 (main)
     41     kDeclFunctionName | kDeclFunctionExport,
     42     1, 0,                         // signature offset
     43     kMainOffset, 0, 0, 0,         // name offset
     44     kBodySize2, 0,                // body size
     45     kExprCallIndirect, 0,
     46     kExprGetLocal, 0,
     47     kExprGetLocal, 1,
     48     kExprGetLocal, 2,
     49     // -- function table
     50     kDeclFunctionTable,
     51     3,
     52     0, 0,
     53     1, 0,
     54     2, 0,
     55     kDeclEnd,
     56     's', 'u', 'b', 0,              // name
     57     'a', 'd', 'd', 0,              // name
     58     'm', 'a', 'i', 'n', 0          // name
     59   ), ffi);
     60 })();
     61 
     62 // Check the module exists.
     63 assertFalse(module === undefined);
     64 assertFalse(module === null);
     65 assertFalse(module === 0);
     66 assertEquals("object", typeof module);
     67 assertEquals("function", typeof module.main);
     68 
     69 assertEquals(5, module.main(0, 12, 7));
     70 assertEquals(19, module.main(1, 12, 7));
     71 
     72 assertTraps(kTrapFuncSigMismatch, "module.main(2, 12, 33)");
     73 assertTraps(kTrapFuncInvalid, "module.main(3, 12, 33)");
     74