diff options
author | Aapo Alasuutari <aapo.alasuutari@gmail.com> | 2022-02-18 14:21:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-18 17:51:19 +0530 |
commit | b1a6555c0502196174bed1454e446717daf52f12 (patch) | |
tree | 3e0c74d87a63044090564638dee0af22f4e2ca10 /ext/ffi/00_ffi.js | |
parent | 4a144c7d6e55a5e047080cb1e2377b70657f1809 (diff) |
feat(ext/ffi): Support read only global statics (#13662)
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r-- | ext/ffi/00_ffi.js | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js index b979c73d4..3debeef14 100644 --- a/ext/ffi/00_ffi.js +++ b/ext/ffi/00_ffi.js @@ -9,6 +9,7 @@ Uint8Array, BigInt, Number, + ObjectDefineProperty, ObjectPrototypeIsPrototypeOf, TypeError, } = window.__bootstrap.primordials; @@ -230,10 +231,47 @@ this.#rid = core.opSync("op_ffi_load", { path, symbols }); for (const symbol in symbols) { + if ("type" in symbols[symbol]) { + const type = symbols[symbol].type; + if (type === "void") { + throw new TypeError( + "Foreign symbol of type 'void' is not supported.", + ); + } + + const name = symbols[symbol].name || symbol; + let value = core.opSync( + "op_ffi_get_static", + { + rid: this.#rid, + name, + type, + }, + ); + if (type === "pointer" || type === "u64") { + value = unpackU64(value); + if (type === "pointer") { + value = new UnsafePointer(value); + } + } else if (type === "i64") { + value = unpackI64(value); + } + ObjectDefineProperty( + this.symbols, + symbol, + { + configurable: false, + enumerable: true, + value, + writable: false, + }, + ); + continue; + } const isNonBlocking = symbols[symbol].nonblocking; const types = symbols[symbol].parameters; - this.symbols[symbol] = (...args) => { + const fn = (...args) => { const { parameters, buffers } = prepareArgs(types, args); if (isNonBlocking) { @@ -266,6 +304,17 @@ return result; } }; + + ObjectDefineProperty( + this.symbols, + symbol, + { + configurable: false, + enumerable: true, + value: fn, + writable: false, + }, + ); } } |