aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/wasm/ffi-error.js
blob: 6d4787e70abba5fd1333a31e0581975e90154f0d (plain)
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
// 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

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

function testCallFFI(ffi) {
  var builder = new WasmModuleBuilder();

  var sig_index = kSig_i_dd;
  builder.addImport("fun", sig_index);
  builder.addFunction("main", sig_index)
    .addBody([
      kExprGetLocal, 0,              // --
      kExprGetLocal, 1,              // --
      kExprCallFunction, 0, // --
    ])    // --
    .exportFunc();

  var module = builder.instantiate(ffi);
}

// everything is good.
(function() {
  var ffi = new Object();
  ffi.fun = function(a, b) { print(a, b); }
  testCallFFI(ffi);
})();


// FFI object should be an object.
assertThrows(function() {
  var ffi = 0;
  testCallFFI(ffi);
});


// FFI object should have a "fun" property.
assertThrows(function() {
  var ffi = new Object();
  testCallFFI(ffi);
});


// "fun" should be a JS function.
assertThrows(function() {
  var ffi = new Object();
  ffi.fun = new Object();
  testCallFFI(ffi);
});


// "fun" should be a JS function.
assertThrows(function() {
  var ffi = new Object();
  ffi.fun = 0;
  testCallFFI(ffi);
});


(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);
})();

(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);
})();

(function I64JSImportThrows() {
  var builder = new WasmModuleBuilder();
  var sig_index = builder.addType(kSig_i_i);
  var sig_i64_index = builder.addType(kSig_i_l);
  var index = builder.addImport("func", sig_i64_index);
  builder.addFunction("main", sig_index)
    .addBody([
      kExprGetLocal, 0,
      kExprI64SConvertI32,
      kExprCallFunction, index  // --
    ])        // --
    .exportFunc();
  var func = function() {return {};};
  var main = builder.instantiate({func: func}).exports.main;
  assertThrows(function() {
    main(13);
  }, TypeError);
})();