summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/wasm/log-code-after-post-message.js
blob: d2a718dd382dec5de1f290294f8a4f618bab3586 (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
// Copyright 2022 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.

d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");

function workerCode() {
  function WorkerOnProfileEnd(profile) {
    postMessage(profile.indexOf('foo'));
  }

  onmessage = (wasm_module) => {
    WebAssembly.instantiate(wasm_module, {q: {func: d8.profiler.triggerSample}})
        .then(instance => {
          instance.exports.foo();
          console.profileEnd();
        });
  };

  d8.profiler.setOnProfileEndListener(WorkerOnProfileEnd);
  // Code logging happens for all code objects when profiling gets started,
  // and when new code objects appear after profiling has started. We want to
  // test the second scenario here. As new code objects appear as the
  // parameter of {OnMessage}, we have to start profiling already here before
  // {onMessage} is called.
  console.profile();
  postMessage('Starting worker');
}

const worker = new Worker(workerCode, {type: 'function'});

assertEquals("Starting worker", worker.getMessage());

const builder = new WasmModuleBuilder();
const sig_index = builder.addType(kSig_v_v);
const imp_index = builder.addImport("q", "func", sig_index);
builder.addFunction('foo', kSig_v_v)
    .addBody([
      kExprCallFunction, imp_index,
    ])
    .exportFunc();
const wasm_module = builder.toModule();
worker.postMessage(wasm_module);
assertTrue(worker.getMessage() > 0);