summaryrefslogtreecommitdiff
path: root/test_ffi/tests/event_loop_integration.ts
blob: d9ada60273f5a2d8c0e8a2defb3fd187d7ce2bc1 (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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

const targetDir = Deno.execPath().replace(/[^\/\\]+$/, "");
const [libPrefix, libSuffix] = {
  darwin: ["lib", "dylib"],
  linux: ["lib", "so"],
  windows: ["", "dll"],
}[Deno.build.os];
const libPath = `${targetDir}/${libPrefix}test_ffi.${libSuffix}`;

const dylib = Deno.dlopen(
  libPath,
  {
    store_function: {
      parameters: ["function"],
      result: "void",
    },
    call_stored_function: {
      parameters: [],
      result: "void",
    },
    call_stored_function_thread_safe_and_log: {
      parameters: [],
      result: "void",
    },
  } as const,
);

let retry = false;
const tripleLogCallback = () => {
  console.log("Sync");
  queueMicrotask(() => {
    console.log("Async");
    callback.unref();
  });
  setTimeout(() => {
    console.log("Timeout");
    callback.unref();

    if (retry) {
      // Re-ref and retry the call to make sure re-refing works.
      console.log("RETRY THREAD SAFE");
      retry = false;
      callback.ref();
      dylib.symbols.call_stored_function_thread_safe_and_log();
    }
  }, 100);
};

const callback = Deno.UnsafeCallback.threadSafe(
  {
    parameters: [],
    result: "void",
  } as const,
  tripleLogCallback,
);

// Store function
dylib.symbols.store_function(callback.pointer);

// Synchronous callback logging
console.log("SYNCHRONOUS");
// This function only calls the callback, and does not log.
dylib.symbols.call_stored_function();
console.log("STORED_FUNCTION called");

// Wait to make sure synch logging and async logging
await new Promise((res) => setTimeout(res, 200));

// Ref once to make sure both `queueMicrotask()` and `setTimeout()`
// must resolve and unref before isolate exists.
// One ref'ing has been done by `threadSafe` constructor.
callback.ref();

console.log("THREAD SAFE");
retry = true;
// This function calls the callback and logs 'STORED_FUNCTION called'
dylib.symbols.call_stored_function_thread_safe_and_log();