summaryrefslogtreecommitdiff
path: root/test_ffi/tests
diff options
context:
space:
mode:
authorElias Sjögreen <eliassjogreen1@gmail.com>2021-08-06 23:28:10 +0200
committerGitHub <noreply@github.com>2021-08-06 23:28:10 +0200
commit33c8d790c3d358a475c9ba828043e2c19e8d4b37 (patch)
tree4ab33b3e5b352d49667d55631c76ee76af2ec5e2 /test_ffi/tests
parent0d1a522a03c22749e96dab06ca7b3e8b428df701 (diff)
feat: ffi to replace plugins (#11152)
This commit removes implementation of "native plugins" and replaces it with FFI API. Effectively "Deno.openPlugin" API was replaced with "Deno.dlopen" API.
Diffstat (limited to 'test_ffi/tests')
-rw-r--r--test_ffi/tests/integration_tests.rs45
-rw-r--r--test_ffi/tests/test.js33
2 files changed, 78 insertions, 0 deletions
diff --git a/test_ffi/tests/integration_tests.rs b/test_ffi/tests/integration_tests.rs
new file mode 100644
index 000000000..7aa8a0611
--- /dev/null
+++ b/test_ffi/tests/integration_tests.rs
@@ -0,0 +1,45 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
+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";
+
+#[test]
+fn basic() {
+ let mut build_plugin_base = Command::new("cargo");
+ let mut build_plugin =
+ build_plugin_base.arg("build").arg("-p").arg("test_ffi");
+ if BUILD_VARIANT == "release" {
+ build_plugin = build_plugin.arg("--release");
+ }
+ let build_plugin_output = build_plugin.output().unwrap();
+ assert!(build_plugin_output.status.success());
+ let output = deno_cmd()
+ .arg("run")
+ .arg("--allow-ffi")
+ .arg("--allow-read")
+ .arg("--unstable")
+ .arg("tests/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() {
+ println!("stdout {}", stdout);
+ println!("stderr {}", stderr);
+ }
+ println!("{:?}", output.status);
+ assert!(output.status.success());
+ let expected = "\
+ something\n\
+ 579\n\
+ Correct number of resources\n";
+ assert_eq!(stdout, expected);
+ assert_eq!(stderr, "");
+}
diff --git a/test_ffi/tests/test.js b/test_ffi/tests/test.js
new file mode 100644
index 000000000..a7b0aba6d
--- /dev/null
+++ b/test_ffi/tests/test.js
@@ -0,0 +1,33 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+// deno-lint-ignore-file
+
+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 resourcesPre = Deno.resources();
+const dylib = Deno.dlopen(libPath, {
+ "print_something": { parameters: [], result: "void" },
+ "add": { parameters: ["u32", "u32"], result: "u32" },
+});
+
+dylib.symbols.print_something();
+console.log(dylib.symbols.add(123, 456));
+
+dylib.close();
+const resourcesPost = Deno.resources();
+
+const preStr = JSON.stringify(resourcesPre, null, 2);
+const postStr = JSON.stringify(resourcesPost, null, 2);
+if (preStr !== postStr) {
+ throw new Error(
+ `Difference in open resources before dlopen and after closing:
+Before: ${preStr}
+After: ${postStr}`,
+ );
+}
+console.log("Correct number of resources");