2021-08-06 17:28:10 -04:00
|
|
|
// 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();
|
2021-10-07 11:03:00 -04:00
|
|
|
|
|
|
|
// dlopen shouldn't panic
|
|
|
|
try {
|
|
|
|
Deno.dlopen("cli/src/main.rs", {});
|
|
|
|
} catch (_) {
|
|
|
|
console.log("dlopen doesn't panic");
|
|
|
|
}
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
const dylib = Deno.dlopen(libPath, {
|
|
|
|
"print_something": { parameters: [], result: "void" },
|
2021-10-05 18:27:05 -04:00
|
|
|
"print_buffer": { parameters: ["buffer", "usize"], result: "void" },
|
2021-10-10 09:18:02 -04:00
|
|
|
"print_buffer2": {
|
|
|
|
parameters: ["buffer", "usize", "buffer", "usize"],
|
|
|
|
result: "void",
|
|
|
|
},
|
2021-09-20 14:38:28 -04:00
|
|
|
"add_u32": { parameters: ["u32", "u32"], result: "u32" },
|
|
|
|
"add_i32": { parameters: ["i32", "i32"], result: "i32" },
|
|
|
|
"add_u64": { parameters: ["u64", "u64"], result: "u64" },
|
|
|
|
"add_i64": { parameters: ["i64", "i64"], result: "i64" },
|
|
|
|
"add_usize": { parameters: ["usize", "usize"], result: "usize" },
|
|
|
|
"add_isize": { parameters: ["isize", "isize"], result: "isize" },
|
|
|
|
"add_f32": { parameters: ["f32", "f32"], result: "f32" },
|
|
|
|
"add_f64": { parameters: ["f64", "f64"], result: "f64" },
|
2021-10-05 08:50:00 -04:00
|
|
|
"sleep_blocking": { parameters: ["u64"], result: "void", nonblocking: true },
|
2021-10-05 18:27:05 -04:00
|
|
|
"nonblocking_buffer": {
|
|
|
|
parameters: ["buffer", "usize"],
|
|
|
|
result: "void",
|
|
|
|
nonblocking: true,
|
|
|
|
},
|
2021-08-06 17:28:10 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
dylib.symbols.print_something();
|
2021-10-05 18:27:05 -04:00
|
|
|
const buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
2021-10-10 09:18:02 -04:00
|
|
|
const buffer2 = new Uint8Array([9, 10]);
|
2021-10-05 18:27:05 -04:00
|
|
|
dylib.symbols.print_buffer(buffer, buffer.length);
|
2021-10-10 09:18:02 -04:00
|
|
|
dylib.symbols.print_buffer2(buffer, buffer.length, buffer2, buffer2.length);
|
2021-09-20 14:38:28 -04:00
|
|
|
console.log(dylib.symbols.add_u32(123, 456));
|
|
|
|
console.log(dylib.symbols.add_i32(123, 456));
|
|
|
|
console.log(dylib.symbols.add_u64(123, 456));
|
|
|
|
console.log(dylib.symbols.add_i64(123, 456));
|
|
|
|
console.log(dylib.symbols.add_usize(123, 456));
|
|
|
|
console.log(dylib.symbols.add_isize(123, 456));
|
|
|
|
console.log(dylib.symbols.add_f32(123.123, 456.789));
|
|
|
|
console.log(dylib.symbols.add_f64(123.123, 456.789));
|
2021-08-06 17:28:10 -04:00
|
|
|
|
2021-10-05 08:50:00 -04:00
|
|
|
// Test non blocking calls
|
2021-10-05 18:27:05 -04:00
|
|
|
|
|
|
|
function deferred() {
|
|
|
|
let methods;
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
methods = {
|
|
|
|
async resolve(value) {
|
|
|
|
await value;
|
|
|
|
resolve(value);
|
|
|
|
},
|
|
|
|
reject(reason) {
|
|
|
|
reject(reason);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return Object.assign(promise, methods);
|
|
|
|
}
|
|
|
|
|
|
|
|
const promise = deferred();
|
2021-10-10 09:18:02 -04:00
|
|
|
const buffer3 = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
|
|
|
dylib.symbols.nonblocking_buffer(buffer3, buffer3.length).then(() => {
|
2021-10-05 18:27:05 -04:00
|
|
|
promise.resolve();
|
|
|
|
});
|
|
|
|
await promise;
|
|
|
|
|
2021-10-05 08:50:00 -04:00
|
|
|
const start = performance.now();
|
|
|
|
dylib.symbols.sleep_blocking(100).then(() => {
|
|
|
|
console.log("After");
|
|
|
|
console.log(performance.now() - start >= 100);
|
|
|
|
// Close after task is complete.
|
|
|
|
cleanup();
|
|
|
|
});
|
|
|
|
console.log("Before");
|
|
|
|
console.log(performance.now() - start < 100);
|
|
|
|
|
|
|
|
function cleanup() {
|
|
|
|
dylib.close();
|
2021-08-06 17:28:10 -04:00
|
|
|
|
2021-10-05 08:50:00 -04:00
|
|
|
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:
|
2021-08-06 17:28:10 -04:00
|
|
|
Before: ${preStr}
|
|
|
|
After: ${postStr}`,
|
2021-10-05 08:50:00 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("Correct number of resources");
|
2021-08-06 17:28:10 -04:00
|
|
|
}
|