2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-07-11 21:12:26 -04:00
|
|
|
|
2024-05-08 22:45:06 -04:00
|
|
|
#![allow(clippy::print_stdout)]
|
|
|
|
#![allow(clippy::print_stderr)]
|
|
|
|
|
2023-10-05 09:35:21 -04:00
|
|
|
use pretty_assertions::assert_eq;
|
2021-07-11 21:12:26 -04:00
|
|
|
use std::process::Command;
|
|
|
|
use test_util::deno_cmd;
|
2024-02-12 15:46:50 -05:00
|
|
|
use test_util::deno_config_path;
|
|
|
|
use test_util::ffi_tests_path;
|
2021-07-11 21:12:26 -04:00
|
|
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
const BUILD_VARIANT: &str = "debug";
|
|
|
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
const BUILD_VARIANT: &str = "release";
|
|
|
|
|
2022-01-10 10:33:25 -05:00
|
|
|
fn build() {
|
2021-07-11 21:12:26 -04:00
|
|
|
let mut build_plugin_base = Command::new("cargo");
|
|
|
|
let mut build_plugin =
|
2021-08-06 17:28:10 -04:00
|
|
|
build_plugin_base.arg("build").arg("-p").arg("test_ffi");
|
2021-07-11 21:12:26 -04:00
|
|
|
if BUILD_VARIANT == "release" {
|
|
|
|
build_plugin = build_plugin.arg("--release");
|
|
|
|
}
|
|
|
|
let build_plugin_output = build_plugin.output().unwrap();
|
|
|
|
assert!(build_plugin_output.status.success());
|
2022-01-10 10:33:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic() {
|
|
|
|
build();
|
|
|
|
|
2021-07-11 21:12:26 -04:00
|
|
|
let output = deno_cmd()
|
2024-02-12 15:46:50 -05:00
|
|
|
.current_dir(ffi_tests_path())
|
2021-07-11 21:12:26 -04:00
|
|
|
.arg("run")
|
2024-02-12 15:46:50 -05:00
|
|
|
.arg("--config")
|
|
|
|
.arg(deno_config_path())
|
|
|
|
.arg("--no-lock")
|
2021-08-06 17:28:10 -04:00
|
|
|
.arg("--allow-ffi")
|
|
|
|
.arg("--allow-read")
|
2023-11-01 18:15:08 -04:00
|
|
|
.arg("--unstable-ffi")
|
2022-01-05 02:25:31 -05:00
|
|
|
.arg("--quiet")
|
2022-07-11 21:03:05 -04:00
|
|
|
.arg(r#"--v8-flags=--allow-natives-syntax"#)
|
2021-07-11 21:12:26 -04:00
|
|
|
.arg("tests/test.js")
|
2021-08-06 17:28:10 -04:00
|
|
|
.env("NO_COLOR", "1")
|
2021-07-11 21:12:26 -04:00
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let stdout = std::str::from_utf8(&output.stdout).unwrap();
|
|
|
|
let stderr = std::str::from_utf8(&output.stderr).unwrap();
|
|
|
|
if !output.status.success() {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
2021-07-11 21:12:26 -04:00
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
let expected = "\
|
2021-08-06 17:28:10 -04:00
|
|
|
something\n\
|
2021-10-05 18:27:05 -04:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8]\n\
|
2022-06-20 23:46:59 -04:00
|
|
|
[4, 5, 6]\n\
|
2021-10-10 09:18:02 -04:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8] [9, 10]\n\
|
2021-12-15 09:41:49 -05:00
|
|
|
[1, 2, 3, 4, 5, 6, 7, 8]\n\
|
|
|
|
[ 1, 2, 3, 4, 5, 6 ]\n\
|
|
|
|
[ 4, 5, 6 ]\n\
|
|
|
|
[ 4, 5, 6 ]\n\
|
|
|
|
Hello from pointer!\n\
|
|
|
|
pointer!\n\
|
2023-10-05 09:35:21 -04:00
|
|
|
false false\n\
|
|
|
|
true true\n\
|
|
|
|
false false\n\
|
|
|
|
true true\n\
|
|
|
|
false false\n\
|
2021-08-06 17:28:10 -04:00
|
|
|
579\n\
|
2022-01-12 06:38:26 -05:00
|
|
|
true\n\
|
|
|
|
579\n\
|
2021-09-20 14:38:28 -04:00
|
|
|
579\n\
|
2022-07-11 22:50:20 -04:00
|
|
|
5\n\
|
|
|
|
5\n\
|
2022-07-11 21:03:05 -04:00
|
|
|
579\n\
|
BREAKING(ffi/unstable): always return u64 as bigint (#23981)
The mixed `number | bigint` representation was useful optimization for
pointers. Now, pointers are represented as V8 externals. As part of the
FFI stabilization effort we want to make `bigint` the only
representation for `u64` and `i64`.
BigInt representation performance is almost on par with mixed
representation with the added benefit that its less confusing and users
don't need manual checks and conversions for doing operations on the
value.
```
cpu: AMD Ryzen 5 7530U with Radeon Graphics
runtime: deno 1.43.6+92a8d09 (x86_64-unknown-linux-gnu)
file:///home/divy/gh/ffi/main.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
-------------------------------------------------------------------------- -----------------------------
nop 4.01 ns/iter 249,533,690.5 (3.97 ns … 10.8 ns) 3.97 ns 4.36 ns 9.03 ns
ret bigint 7.74 ns/iter 129,127,186.8 (7.72 ns … 10.46 ns) 7.72 ns 8.11 ns 8.82 ns
ret i32 7.81 ns/iter 128,087,100.5 (7.77 ns … 12.72 ns) 7.78 ns 8.57 ns 9.75 ns
ret bigint (add op) 15.02 ns/iter 66,588,253.2 (14.64 ns … 24.99 ns) 14.76 ns 19.13 ns 19.44 ns
ret i32 (add op) 12.02 ns/iter 83,209,131.8 (11.95 ns … 18.18 ns) 11.98 ns 13.11 ns 14.5 ns
```
2024-05-28 00:01:09 -04:00
|
|
|
8589934590n\n\
|
|
|
|
-8589934590n\n\
|
|
|
|
8589934590n\n\
|
|
|
|
-8589934590n\n\
|
2022-07-24 06:41:11 -04:00
|
|
|
9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
-9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
-9007199254740992n\n\
|
2021-09-20 14:38:28 -04:00
|
|
|
579.9119873046875\n\
|
|
|
|
579.912\n\
|
2022-09-04 23:26:52 -04:00
|
|
|
true\n\
|
|
|
|
false\n\
|
2022-09-07 02:53:56 -04:00
|
|
|
579.9119873046875\n\
|
|
|
|
579.9119873046875\n\
|
|
|
|
579.912\n\
|
|
|
|
579.912\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
579\n\
|
2024-10-24 00:11:38 -04:00
|
|
|
8589934590n\n\
|
|
|
|
-8589934590n\n\
|
|
|
|
8589934590n\n\
|
|
|
|
-8589934590n\n\
|
2022-07-24 06:41:11 -04:00
|
|
|
9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
-9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
9007199254740992n\n\
|
|
|
|
-9007199254740992n\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
579.9119873046875\n\
|
|
|
|
579.912\n\
|
2021-10-05 08:50:00 -04:00
|
|
|
Before\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
After\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
logCallback\n\
|
BREAKING(ffi/unstable): always return u64 as bigint (#23981)
The mixed `number | bigint` representation was useful optimization for
pointers. Now, pointers are represented as V8 externals. As part of the
FFI stabilization effort we want to make `bigint` the only
representation for `u64` and `i64`.
BigInt representation performance is almost on par with mixed
representation with the added benefit that its less confusing and users
don't need manual checks and conversions for doing operations on the
value.
```
cpu: AMD Ryzen 5 7530U with Radeon Graphics
runtime: deno 1.43.6+92a8d09 (x86_64-unknown-linux-gnu)
file:///home/divy/gh/ffi/main.ts
benchmark time (avg) iter/s (min … max) p75 p99 p995
-------------------------------------------------------------------------- -----------------------------
nop 4.01 ns/iter 249,533,690.5 (3.97 ns … 10.8 ns) 3.97 ns 4.36 ns 9.03 ns
ret bigint 7.74 ns/iter 129,127,186.8 (7.72 ns … 10.46 ns) 7.72 ns 8.11 ns 8.82 ns
ret i32 7.81 ns/iter 128,087,100.5 (7.77 ns … 12.72 ns) 7.78 ns 8.57 ns 9.75 ns
ret bigint (add op) 15.02 ns/iter 66,588,253.2 (14.64 ns … 24.99 ns) 14.76 ns 19.13 ns 19.44 ns
ret i32 (add op) 12.02 ns/iter 83,209,131.8 (11.95 ns … 18.18 ns) 11.98 ns 13.11 ns 14.5 ns
```
2024-05-28 00:01:09 -04:00
|
|
|
1 -1 2 -2 3 -3 4n -4n 0.5 -0.5 1 2 3 4 5 6 7 8\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
u8: 8\n\
|
|
|
|
buf: [1, 2, 3, 4, 5, 6, 7, 8]\n\
|
|
|
|
logCallback\n\
|
|
|
|
30\n\
|
2022-09-07 02:53:56 -04:00
|
|
|
255 65535 4294967295 4294967296 123.456 789.876 -1 -2 -3 -4 -1000 1000 12345.67891 12345.679 12345.67891 12345.679 12345.67891 12345.679 12345.67891\n\
|
|
|
|
255 65535 4294967295 4294967296 123.456 789.876 -1 -2 -3 -4 -1000 1000 12345.67891 12345.679 12345.67891 12345.679 12345.67891 12345.679 12345.67891\n\
|
|
|
|
0\n\
|
|
|
|
0\n\
|
|
|
|
0\n\
|
|
|
|
0\n\
|
|
|
|
78\n\
|
|
|
|
78\n\
|
2022-06-20 07:06:04 -04:00
|
|
|
STORED_FUNCTION cleared\n\
|
|
|
|
STORED_FUNCTION_2 cleared\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
logCallback\n\
|
|
|
|
u8: 8\n\
|
2023-01-07 22:58:10 -05:00
|
|
|
Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }\n\
|
|
|
|
Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }\n\
|
|
|
|
Rect { x: 20.0, y: 20.0, w: 100.0, h: 200.0 }\n\
|
|
|
|
Mixed { u8: 3, f32: 12.515, rect: Rect { x: 10.0, y: 20.0, w: 100.0, h: 200.0 }, usize: 12456789, array: [8, 32] }\n\
|
2022-09-07 02:53:56 -04:00
|
|
|
2264956937\n\
|
|
|
|
2264956937\n\
|
2021-08-06 17:28:10 -04:00
|
|
|
Correct number of resources\n";
|
2021-07-11 21:12:26 -04:00
|
|
|
assert_eq!(stdout, expected);
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|
2022-01-10 10:33:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn symbol_types() {
|
|
|
|
build();
|
|
|
|
|
|
|
|
let output = deno_cmd()
|
2024-02-12 15:46:50 -05:00
|
|
|
.current_dir(ffi_tests_path())
|
2022-07-24 06:41:11 -04:00
|
|
|
.arg("check")
|
2024-02-12 15:46:50 -05:00
|
|
|
.arg("--config")
|
|
|
|
.arg(deno_config_path())
|
|
|
|
.arg("--no-lock")
|
2024-01-23 09:33:07 -05:00
|
|
|
.arg("--unstable-ffi")
|
2022-01-10 10:33:25 -05:00
|
|
|
.arg("--quiet")
|
|
|
|
.arg("tests/ffi_types.ts")
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let stdout = std::str::from_utf8(&output.stdout).unwrap();
|
|
|
|
let stderr = std::str::from_utf8(&output.stderr).unwrap();
|
|
|
|
if !output.status.success() {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
2022-01-10 10:33:25 -05:00
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|
2022-06-28 05:23:36 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn thread_safe_callback() {
|
|
|
|
build();
|
|
|
|
|
|
|
|
let output = deno_cmd()
|
2024-02-12 15:46:50 -05:00
|
|
|
.current_dir(ffi_tests_path())
|
2022-06-28 05:23:36 -04:00
|
|
|
.arg("run")
|
2024-02-12 15:46:50 -05:00
|
|
|
.arg("--config")
|
|
|
|
.arg(deno_config_path())
|
|
|
|
.arg("--no-lock")
|
2022-06-28 05:23:36 -04:00
|
|
|
.arg("--allow-ffi")
|
|
|
|
.arg("--allow-read")
|
2024-01-23 09:33:07 -05:00
|
|
|
.arg("--unstable-ffi")
|
2022-06-28 05:23:36 -04:00
|
|
|
.arg("--quiet")
|
|
|
|
.arg("tests/thread_safe_test.js")
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let stdout = std::str::from_utf8(&output.stdout).unwrap();
|
|
|
|
let stderr = std::str::from_utf8(&output.stderr).unwrap();
|
|
|
|
if !output.status.success() {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
let expected = "\
|
|
|
|
Callback on main thread\n\
|
|
|
|
Callback on worker thread\n\
|
2023-06-28 16:41:59 -04:00
|
|
|
STORED_FUNCTION cleared\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
Calling callback, isolate should stay asleep until callback is called\n\
|
|
|
|
Callback being called\n\
|
2023-06-28 16:41:59 -04:00
|
|
|
STORED_FUNCTION cleared\n\
|
2022-06-28 05:23:36 -04:00
|
|
|
Isolate should now exit\n";
|
|
|
|
assert_eq!(stdout, expected);
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|
2022-07-09 05:49:20 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn event_loop_integration() {
|
|
|
|
build();
|
|
|
|
|
|
|
|
let output = deno_cmd()
|
2024-02-12 15:46:50 -05:00
|
|
|
.current_dir(ffi_tests_path())
|
2022-07-09 05:49:20 -04:00
|
|
|
.arg("run")
|
2024-02-12 15:46:50 -05:00
|
|
|
.arg("--config")
|
|
|
|
.arg(deno_config_path())
|
|
|
|
.arg("--no-lock")
|
2022-07-09 05:49:20 -04:00
|
|
|
.arg("--allow-ffi")
|
|
|
|
.arg("--allow-read")
|
2024-01-23 09:33:07 -05:00
|
|
|
.arg("--unstable-ffi")
|
2022-07-09 05:49:20 -04:00
|
|
|
.arg("--quiet")
|
|
|
|
.arg("tests/event_loop_integration.ts")
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let stdout = std::str::from_utf8(&output.stdout).unwrap();
|
|
|
|
let stderr = std::str::from_utf8(&output.stderr).unwrap();
|
|
|
|
if !output.status.success() {
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
2022-07-09 05:49:20 -04:00
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
// TODO(aapoalas): The order of logging in thread safe callbacks is
|
|
|
|
// unexpected: The callback logs synchronously and creates an asynchronous
|
|
|
|
// logging task, which then gets called synchronously before the callback
|
|
|
|
// actually yields to the calling thread. This is in contrast to what the
|
|
|
|
// logging would look like if the call was coming from within Deno itself,
|
|
|
|
// and may lead users to unknowingly run heavy asynchronous tasks from thread
|
|
|
|
// safe callbacks synchronously.
|
|
|
|
// The fix would be to make sure microtasks are only run after the event loop
|
|
|
|
// middleware that polls them has completed its work. This just does not seem
|
|
|
|
// to work properly with Linux release builds.
|
|
|
|
let expected = "\
|
|
|
|
SYNCHRONOUS\n\
|
|
|
|
Sync\n\
|
|
|
|
STORED_FUNCTION called\n\
|
|
|
|
Async\n\
|
|
|
|
Timeout\n\
|
|
|
|
THREAD SAFE\n\
|
|
|
|
Sync\n\
|
|
|
|
Async\n\
|
|
|
|
STORED_FUNCTION called\n\
|
2023-02-22 14:09:59 -05:00
|
|
|
Timeout\n\
|
|
|
|
RETRY THREAD SAFE\n\
|
|
|
|
Sync\n\
|
|
|
|
Async\n\
|
|
|
|
STORED_FUNCTION called\n\
|
2022-07-09 05:49:20 -04:00
|
|
|
Timeout\n";
|
|
|
|
assert_eq!(stdout, expected);
|
|
|
|
assert_eq!(stderr, "");
|
|
|
|
}
|
2023-12-11 22:10:33 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ffi_callback_errors_test() {
|
|
|
|
build();
|
|
|
|
|
|
|
|
let output = deno_cmd()
|
2024-02-12 15:46:50 -05:00
|
|
|
.current_dir(ffi_tests_path())
|
2023-12-11 22:10:33 -05:00
|
|
|
.arg("run")
|
2024-02-12 15:46:50 -05:00
|
|
|
.arg("--config")
|
|
|
|
.arg(deno_config_path())
|
|
|
|
.arg("--no-lock")
|
2023-12-11 22:10:33 -05:00
|
|
|
.arg("--allow-ffi")
|
|
|
|
.arg("--allow-read")
|
2024-01-23 09:33:07 -05:00
|
|
|
.arg("--unstable-ffi")
|
2023-12-11 22:10:33 -05:00
|
|
|
.arg("--quiet")
|
|
|
|
.arg("tests/ffi_callback_errors.ts")
|
|
|
|
.env("NO_COLOR", "1")
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
let stdout = std::str::from_utf8(&output.stdout).unwrap();
|
|
|
|
let stderr = std::str::from_utf8(&output.stderr).unwrap();
|
|
|
|
if !output.status.success() {
|
|
|
|
println!("stdout {stdout}");
|
|
|
|
println!("stderr {stderr}");
|
|
|
|
}
|
|
|
|
println!("{:?}", output.status);
|
|
|
|
assert!(output.status.success());
|
|
|
|
|
|
|
|
let expected = "\
|
|
|
|
CallCase: SyncSelf\n\
|
|
|
|
Throwing errors from an UnsafeCallback called from a synchronous UnsafeFnPointer works. Terribly excellent.\n\
|
|
|
|
CallCase: SyncFfi\n\
|
|
|
|
0\n\
|
|
|
|
Throwing errors from an UnsafeCallback called from a synchronous FFI symbol works. Terribly excellent.\n\
|
|
|
|
CallCase: AsyncSelf\n\
|
|
|
|
CallCase: AsyncSyncFfi\n\
|
|
|
|
0\n\
|
|
|
|
Calling\n\
|
|
|
|
CallCase: AsyncFfi\n";
|
|
|
|
assert_eq!(stdout, expected);
|
|
|
|
assert_eq!(
|
|
|
|
stderr,
|
2024-09-04 03:23:19 -04:00
|
|
|
"Illegal unhandled exception in nonblocking callback\n".repeat(3)
|
2023-12-11 22:10:33 -05:00
|
|
|
);
|
|
|
|
}
|