ffi-error.js 4.81 KB
Newer Older
1 2 3 4
// 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.

5 6
// Flags: --expose-wasm

7
load("test/mjsunit/wasm/wasm-constants.js");
8
load("test/mjsunit/wasm/wasm-module-builder.js");
9

10
function instantiateWithFFI(ffi) {
11 12
  var builder = new WasmModuleBuilder();

13
  var sig_index = kSig_i_dd;
14
  builder.addImport("mod", "fun", sig_index);
15 16
  builder.addFunction("main", sig_index)
    .addBody([
17 18
      kExprGetLocal, 0,              // --
      kExprGetLocal, 1,              // --
19
      kExprCallFunction, 0, // --
20
    ])    // --
21 22
    .exportFunc();

23
  return builder.instantiate(ffi);
24 25 26 27
}

// everything is good.
(function() {
28 29
  var ffi = {"mod": {fun: function(a, b) { print(a, b); }}}
  instantiateWithFFI(ffi);
30 31 32 33 34 35
})();


// FFI object should be an object.
assertThrows(function() {
  var ffi = 0;
36 37 38 39 40 41 42
  instantiateWithFFI(ffi);
});


// FFI object should have a "mod" property.
assertThrows(function() {
  instantiateWithFFI({});
43 44 45 46 47
});


// FFI object should have a "fun" property.
assertThrows(function() {
48
  instantiateWithFFI({mod: {}});
49 50 51 52 53
});


// "fun" should be a JS function.
assertThrows(function() {
54
  instantiateWithFFI({mod: {fun: new Object()}});
55 56 57 58 59
});


// "fun" should be a JS function.
assertThrows(function() {
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  instantiateWithFFI({mod: {fun: 0}});
});

// "fun" should have signature "i_dd"
assertThrows(function () {
  var builder = new WasmModuleBuilder();

  var sig_index = kSig_i_dd;
  builder.addFunction("exp", kSig_i_i)
    .addBody([
      kExprGetLocal, 0,
    ])    // --
    .exportFunc();

  var exported = builder.instantiate().exports.exp;
  instantiateWithFFI({mod: {fun: exported}});
76
});
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91
// "fun" matches signature "i_dd"
(function () {
  var builder = new WasmModuleBuilder();

  builder.addFunction("exp", kSig_i_dd)
    .addBody([
      kExprI32Const, 33,
    ])    // --
    .exportFunc();

  var exported = builder.instantiate().exports.exp;
  var instance = instantiateWithFFI({mod: {fun: exported}});
  assertEquals(33, instance.exports.main());
})();
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

(function I64InSignatureThrows() {
  var builder = new WasmModuleBuilder();

  builder.addMemory(1, 1, true);
  builder.addFunction("function_with_invalid_signature", kSig_l_ll)
    .addBody([           // --
      kExprGetLocal, 0,  // --
      kExprGetLocal, 1,  // --
      kExprI64Sub])      // --
    .exportFunc()

  var module = builder.instantiate();

  assertThrows(function() {
      module.exports.function_with_invalid_signature(33, 88);
    }, TypeError);
})();
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
(function I64ParamsInSignatureThrows() {
  var builder = new WasmModuleBuilder();

  builder.addMemory(1, 1, true);
  builder.addFunction("function_with_invalid_signature", kSig_i_l)
    .addBody([
       kExprGetLocal, 0,
       kExprI32ConvertI64
     ])
    .exportFunc()

  var module = builder.instantiate();

  assertThrows(function() {
      module.exports.function_with_invalid_signature(33);
    }, TypeError);
})();
128 129 130 131 132

(function I64JSImportThrows() {
  var builder = new WasmModuleBuilder();
  var sig_index = builder.addType(kSig_i_i);
  var sig_i64_index = builder.addType(kSig_i_l);
133
  var index = builder.addImport("", "func", sig_i64_index);
134 135 136 137
  builder.addFunction("main", sig_index)
    .addBody([
      kExprGetLocal, 0,
      kExprI64SConvertI32,
138
      kExprCallFunction, index  // --
139 140 141
    ])        // --
    .exportFunc();
  var func = function() {return {};};
142
  var main = builder.instantiate({"": {func: func}}).exports.main;
143 144 145 146
  assertThrows(function() {
    main(13);
  }, TypeError);
})();
147

148 149 150 151 152 153 154 155 156 157 158 159 160
(function ImportI64ParamWithF64ReturnThrows() {
  // This tests that we generate correct code by using the correct return
  // register. See bug 6096.
  var builder = new WasmModuleBuilder();
  builder.addImport('', 'f', makeSig([kWasmI64], [kWasmF64]));
  builder.addFunction('main', kSig_v_v)
      .addBody([kExprI64Const, 0, kExprCallFunction, 0, kExprDrop])
      .exportFunc();
  var instance = builder.instantiate({'': {f: i => i}});

  assertThrows(() => instance.exports.main(), TypeError);
})();

161 162 163 164 165 166 167 168 169 170 171 172 173
(function ImportI64Return() {
  // This tests that we generate correct code by using the correct return
  // register(s). See bug 6104.
  var builder = new WasmModuleBuilder();
  builder.addImport('', 'f', makeSig([], [kWasmI64]));
  builder.addFunction('main', kSig_v_v)
      .addBody([kExprCallFunction, 0, kExprDrop])
      .exportFunc();
  var instance = builder.instantiate({'': {f: () => 1}});

  assertThrows(() => instance.exports.main(), TypeError);
})();

174 175
(function ImportSymbolToNumberThrows() {
  var builder = new WasmModuleBuilder();
176
  var index = builder.addImport("", "func", kSig_i_v);
177 178 179 180
  builder.addFunction("main", kSig_i_v)
      .addBody([kExprCallFunction, 0])
      .exportFunc();
  var func = () => Symbol();
181
  var main = builder.instantiate({"": {func: func}}).exports.main;
182 183
  assertThrows(() => main(), TypeError);
})();