summaryrefslogtreecommitdiff
path: root/ext/ffi/00_ffi.js
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-06-29 13:43:33 +0530
committerGitHub <noreply@github.com>2022-06-29 13:43:33 +0530
commit76d387fb93c741a5def8d120892be65e35164a01 (patch)
treeceb675eb56ab4b71a29eaf882ec8292c74a819c5 /ext/ffi/00_ffi.js
parent4e4d9874867a20d4259f1f22162e8e9a204df98a (diff)
perf(ext/ffi): optimize synchronous calls (#14945)
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r--ext/ffi/00_ffi.js70
1 files changed, 29 insertions, 41 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js
index f308ecad9..2730cae72 100644
--- a/ext/ffi/00_ffi.js
+++ b/ext/ffi/00_ffi.js
@@ -253,8 +253,7 @@
symbols = {};
constructor(path, symbols) {
- this.#rid = core.opSync("op_ffi_load", { path, symbols });
-
+ [this.#rid, this.symbols] = core.opSync("op_ffi_load", { path, symbols });
for (const symbol in symbols) {
if ("type" in symbols[symbol]) {
const type = symbols[symbol].type;
@@ -285,48 +284,37 @@
}
const isNonBlocking = symbols[symbol].nonblocking;
- const resultType = symbols[symbol].result;
-
- let fn;
if (isNonBlocking) {
+ const resultType = symbols[symbol].result;
const needsUnpacking = isReturnedAsBigInt(resultType);
- fn = (...parameters) => {
- const promise = core.opAsync(
- "op_ffi_call_nonblocking",
- this.#rid,
- symbol,
- parameters,
- );
-
- if (needsUnpacking) {
- return PromisePrototypeThen(
- promise,
- (result) => unpackNonblockingReturnValue(resultType, result),
- );
- }
-
- return promise;
- };
- } else {
- fn = (...parameters) =>
- core.opSync(
- "op_ffi_call",
- this.#rid,
- symbol,
- parameters,
- );
+ ObjectDefineProperty(
+ this.symbols,
+ symbol,
+ {
+ configurable: false,
+ enumerable: true,
+ value: (...parameters) => {
+ const promise = core.opAsync(
+ "op_ffi_call_nonblocking",
+ this.#rid,
+ symbol,
+ parameters,
+ );
+
+ if (needsUnpacking) {
+ return PromisePrototypeThen(
+ promise,
+ (result) =>
+ unpackNonblockingReturnValue(resultType, result),
+ );
+ }
+
+ return promise;
+ },
+ writable: false,
+ },
+ );
}
-
- ObjectDefineProperty(
- this.symbols,
- symbol,
- {
- configurable: false,
- enumerable: true,
- value: fn,
- writable: false,
- },
- );
}
}