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
|
|
|
|
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;
|
|
|
|
|
|
|
|
#[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()
|
|
|
|
.arg("run")
|
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\
|
2022-07-24 06:41:11 -04:00
|
|
|
8589934590\n\
|
|
|
|
-8589934590\n\
|
|
|
|
8589934590\n\
|
|
|
|
-8589934590\n\
|
|
|
|
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\
|
2022-07-24 06:41:11 -04:00
|
|
|
8589934590\n\
|
|
|
|
-8589934590\n\
|
|
|
|
8589934590\n\
|
|
|
|
-8589934590\n\
|
|
|
|
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\
|
2022-07-24 06:41:11 -04:00
|
|
|
1 -1 2 -2 3 -3 4 -4 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()
|
2022-07-24 06:41:11 -04:00
|
|
|
.arg("check")
|
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()
|
|
|
|
.arg("run")
|
|
|
|
.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()
|
|
|
|
.arg("run")
|
|
|
|
.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()
|
|
|
|
.arg("run")
|
|
|
|
.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,
|
|
|
|
"Illegal unhandled exception in nonblocking callback.\n".repeat(3)
|
|
|
|
);
|
|
|
|
}
|