From 4869b674278e4926ddaf8c376a21d4af6b2bbd61 Mon Sep 17 00:00:00 2001 From: Anthony Beaumont Date: Thu, 21 Sep 2023 19:42:34 +0700 Subject: [PATCH] sync --- README.md | 6 +++--- lib/ffi-napi/helper.js | 5 +++-- lib/koffi/helper.js | 5 +++-- lib/koffi/open.js | 3 ++- types/koffi/open.d.ts | 2 +- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bc48fdf..e4cf7e3 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ ABI convention to use. Use this when you need to ex: winapi x86 requires "stdcal **Return** ```ts -function(symbol: string | number, result: unknown, parameters: unknown[]): any; +function(symbol: string | number, result: unknown, parameters: unknown[]): unknown ``` 💡 `Koffi` can call by ordinal (symbol:number) @@ -231,8 +231,8 @@ const { DWORD, LPCSTR } = types.win32; ```js import { load } from "@xan105/ffi/koffi"; -const call = load("user32.dll", { abi: "stdcall" }); -const MessageBoxA = call("MessageBoxA", "int", ["void *", "LPCSTR", "LPCSTR", "uint"]); +const lib = load("user32.dll", { abi: "stdcall" }); +const MessageBoxA = lib("MessageBoxA", "int", ["void *", "LPCSTR", "LPCSTR", "uint"]); ``` ⚠️ Types are not exposed under their own namespace because some words are illegal or already in use in JavaScript. diff --git a/lib/ffi-napi/helper.js b/lib/ffi-napi/helper.js index 40b2b43..ce21b98 100644 --- a/lib/ffi-napi/helper.js +++ b/lib/ffi-napi/helper.js @@ -64,12 +64,13 @@ function pointer(value){ } function alloc(type){ - return { + const buff = Object.assign(Object.create(null), { pointer: ref.alloc(type), get: function(){ return this.pointer.deref(); } - } + }); + return buff; } export { Callback, pointer, alloc }; \ No newline at end of file diff --git a/lib/koffi/helper.js b/lib/koffi/helper.js index 6194c94..d2a44b7 100644 --- a/lib/koffi/helper.js +++ b/lib/koffi/helper.js @@ -72,12 +72,13 @@ function pointer(value, direction = "in"){ } function alloc(type){ - return { + const buff = Object.assign(Object.create(null), { pointer: Buffer.alloc(koffi.sizeof(type)), get: function(){ return koffi.decode(this.pointer, type); } - } + }); + return buff; } export { Callback, pointer, alloc }; \ No newline at end of file diff --git a/lib/koffi/open.js b/lib/koffi/open.js index 9faf1ed..5b0ab4e 100644 --- a/lib/koffi/open.js +++ b/lib/koffi/open.js @@ -40,7 +40,7 @@ function load(path, option = {}){ }[platform] ?? ".so"; if (path.indexOf(ext) === -1) path += ext; - const [dylib, err] = attemptify(koffi.load)(path); + const [dylib, err] = attemptify(koffi.load)(path, { lazy: false }); if(err && !options.ignoreLoadingFail){ throw new Failure(err.message, { @@ -90,6 +90,7 @@ function dlopen(path, symbols, option){ const fn = handle(symbol, result, parameters); if(typeof fn === "function") lib[name] = nonblocking ? promisify(fn.async) : fn; + } return lib; } diff --git a/types/koffi/open.d.ts b/types/koffi/open.d.ts index 4012c41..e44e6b7 100644 --- a/types/koffi/open.d.ts +++ b/types/koffi/open.d.ts @@ -1,2 +1,2 @@ -export function load(path: string, option?: object): (symbol: string | number, result: unknown, parameters: unknown[]) => any; +export function load(path: string, option?: object): (symbol: string | number, result: unknown, parameters: unknown[]) => unknown; export function dlopen(path: string, symbols: object, option?: object): object;