names.js 1.49 KB
Newer Older
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
// Copyright 2017 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-module-builder.js');

function toBytes(string) {
  var a = new Array(string.length + 1);
  a[0] = string.length;
  for (i = 0; i < string.length; i++) {
    a[i + 1] = string.charCodeAt(i);
  }
  return a;
}

(function TestEmptyNamesSection() {
  print('TestEmptyNamesSection...');
  var builder = new WasmModuleBuilder();

  builder.addExplicitSection([kUnknownSectionCode, 6, ...toBytes('name'), 0]);

  var buffer = builder.toBuffer();
  assertTrue(WebAssembly.validate(buffer));
  assertTrue((new WebAssembly.Module(buffer)) instanceof WebAssembly.Module);
})();

(function TestTruncatedNamesSection() {
  print('TestTruncatedNamesSection...');
  var builder = new WasmModuleBuilder();

  builder.addExplicitSection([kUnknownSectionCode, 6, ...toBytes('name'), 1]);

  var buffer = builder.toBuffer();
  assertTrue(WebAssembly.validate(buffer));
  assertTrue((new WebAssembly.Module(buffer)) instanceof WebAssembly.Module);
})();

(function TestBrokenNamesSection() {
  print('TestBrokenNamesSection...');
  var builder = new WasmModuleBuilder();

  builder.addExplicitSection(
      [kUnknownSectionCode, 7, ...toBytes('name'), 1, 100]);

  var buffer = builder.toBuffer();
  assertTrue(WebAssembly.validate(buffer));
  assertTrue((new WebAssembly.Module(buffer)) instanceof WebAssembly.Module);
})();