2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-08-06 17:28:10 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
2021-08-24 09:09:00 -04:00
|
|
|
const __bootstrap = window.__bootstrap;
|
2021-10-05 18:27:05 -04:00
|
|
|
const {
|
2021-12-15 09:41:49 -05:00
|
|
|
BigInt,
|
2022-02-18 07:21:19 -05:00
|
|
|
ObjectDefineProperty,
|
2022-02-01 12:06:11 -05:00
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2022-06-20 07:06:04 -04:00
|
|
|
PromisePrototypeThen,
|
2021-12-15 09:41:49 -05:00
|
|
|
TypeError,
|
2022-06-20 07:06:04 -04:00
|
|
|
Uint8Array,
|
2021-10-05 18:27:05 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2021-12-15 09:41:49 -05:00
|
|
|
|
2022-06-08 07:13:10 -04:00
|
|
|
function unpackU64([hi, lo]) {
|
|
|
|
return BigInt(hi) << 32n | BigInt(lo);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function unpackI64([hi, lo]) {
|
|
|
|
const u64 = unpackU64([hi, lo]);
|
|
|
|
return u64 >> 63n ? u64 - 0x10000000000000000n : u64;
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnsafePointerView {
|
|
|
|
pointer;
|
|
|
|
|
|
|
|
constructor(pointer) {
|
|
|
|
this.pointer = pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
getUint8(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_read_u8",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getInt8(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_read_i8",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUint16(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_read_u16",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getInt16(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_read_i16",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUint32(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_read_u32",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getInt32(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_read_i32",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getBigUint64(offset = 0) {
|
2022-06-20 07:06:04 -04:00
|
|
|
return core.opSync(
|
2021-12-15 09:41:49 -05:00
|
|
|
"op_ffi_read_u64",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getBigInt64(offset = 0) {
|
2022-06-20 07:06:04 -04:00
|
|
|
return core.opSync(
|
2021-12-15 09:41:49 -05:00
|
|
|
"op_ffi_read_u64",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getFloat32(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_read_f32",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFloat64(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_read_f64",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getCString(offset = 0) {
|
|
|
|
return core.opSync(
|
|
|
|
"op_ffi_cstr_read",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getArrayBuffer(byteLength, offset = 0) {
|
|
|
|
const uint8array = new Uint8Array(byteLength);
|
|
|
|
this.copyInto(uint8array, offset);
|
|
|
|
return uint8array.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
copyInto(destination, offset = 0) {
|
2022-06-20 07:06:04 -04:00
|
|
|
core.opSync(
|
|
|
|
"op_ffi_buf_copy_into",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer + BigInt(offset),
|
2021-12-15 09:41:49 -05:00
|
|
|
destination,
|
|
|
|
destination.byteLength,
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnsafePointer {
|
2022-06-20 09:38:10 -04:00
|
|
|
static of(value) {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(UnsafeCallbackPrototype, value)) {
|
|
|
|
return value.pointer;
|
2022-06-20 07:06:04 -04:00
|
|
|
}
|
2022-06-20 09:38:10 -04:00
|
|
|
return core.opSync("op_ffi_ptr_of", value);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-12 06:38:26 -05:00
|
|
|
|
2022-06-20 07:06:04 -04:00
|
|
|
function unpackNonblockingReturnValue(type, result) {
|
|
|
|
if (
|
|
|
|
typeof type === "object" && type !== null && "function" in type ||
|
|
|
|
type === "pointer"
|
|
|
|
) {
|
2022-06-20 09:38:10 -04:00
|
|
|
return unpackU64(result);
|
2022-06-20 07:06:04 -04:00
|
|
|
}
|
2022-06-08 07:13:10 -04:00
|
|
|
switch (type) {
|
2022-06-20 07:06:04 -04:00
|
|
|
case "isize":
|
2022-06-08 07:13:10 -04:00
|
|
|
case "i64":
|
|
|
|
return unpackI64(result);
|
|
|
|
case "usize":
|
2022-06-20 07:06:04 -04:00
|
|
|
case "u64":
|
2022-06-08 07:13:10 -04:00
|
|
|
return unpackU64(result);
|
|
|
|
default:
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 06:38:26 -05:00
|
|
|
class UnsafeFnPointer {
|
|
|
|
pointer;
|
|
|
|
definition;
|
|
|
|
|
|
|
|
constructor(pointer, definition) {
|
|
|
|
this.pointer = pointer;
|
|
|
|
this.definition = definition;
|
|
|
|
}
|
|
|
|
|
2022-06-20 09:38:10 -04:00
|
|
|
call(...parameters) {
|
2022-06-20 07:06:04 -04:00
|
|
|
const resultType = this.definition.result;
|
2022-01-12 06:38:26 -05:00
|
|
|
if (this.definition.nonblocking) {
|
2022-06-20 07:06:04 -04:00
|
|
|
const promise = core.opAsync(
|
|
|
|
"op_ffi_call_ptr_nonblocking",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer,
|
2022-06-20 07:06:04 -04:00
|
|
|
this.definition,
|
2022-01-12 06:38:26 -05:00
|
|
|
parameters,
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2022-01-12 06:38:26 -05:00
|
|
|
|
2022-06-20 07:06:04 -04:00
|
|
|
if (
|
|
|
|
isReturnedAsBigInt(resultType)
|
|
|
|
) {
|
|
|
|
return PromisePrototypeThen(
|
|
|
|
promise,
|
|
|
|
(result) => unpackNonblockingReturnValue(resultType, result),
|
|
|
|
);
|
2022-01-12 06:38:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
} else {
|
2022-06-20 09:38:10 -04:00
|
|
|
return core.opSync(
|
2022-06-20 07:06:04 -04:00
|
|
|
"op_ffi_call_ptr",
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer,
|
2022-06-20 07:06:04 -04:00
|
|
|
this.definition,
|
2022-01-12 06:38:26 -05:00
|
|
|
parameters,
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2022-01-12 06:38:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:06:04 -04:00
|
|
|
function isPointerType(type) {
|
|
|
|
return type === "pointer" ||
|
|
|
|
typeof type === "object" && type !== null && "function" in type;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isReturnedAsBigInt(type) {
|
|
|
|
return isPointerType(type) || type === "u64" || type === "i64" ||
|
|
|
|
type === "usize" || type === "isize";
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnsafeCallback {
|
2022-06-28 05:23:36 -04:00
|
|
|
#refcount;
|
2022-06-20 07:06:04 -04:00
|
|
|
#rid;
|
|
|
|
definition;
|
|
|
|
callback;
|
|
|
|
pointer;
|
|
|
|
|
|
|
|
constructor(definition, callback) {
|
|
|
|
if (definition.nonblocking) {
|
|
|
|
throw new TypeError(
|
|
|
|
"Invalid UnsafeCallback, cannot be nonblocking",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const [rid, pointer] = core.opSync(
|
|
|
|
"op_ffi_unsafe_callback_create",
|
|
|
|
definition,
|
2022-06-20 09:38:10 -04:00
|
|
|
callback,
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2022-06-28 05:23:36 -04:00
|
|
|
this.#refcount = 0;
|
2022-06-20 07:06:04 -04:00
|
|
|
this.#rid = rid;
|
2022-06-20 09:38:10 -04:00
|
|
|
this.pointer = pointer;
|
2022-06-20 07:06:04 -04:00
|
|
|
this.definition = definition;
|
|
|
|
this.callback = callback;
|
|
|
|
}
|
|
|
|
|
2022-06-28 05:23:36 -04:00
|
|
|
ref() {
|
|
|
|
if (this.#refcount++ === 0) {
|
|
|
|
core.opSync("op_ffi_unsafe_callback_ref", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unref() {
|
2022-07-09 05:49:20 -04:00
|
|
|
// Only decrement refcount if it is positive, and only
|
|
|
|
// unref the callback if refcount reaches zero.
|
|
|
|
if (this.#refcount > 0 && --this.#refcount === 0) {
|
2022-06-28 05:23:36 -04:00
|
|
|
core.opSync("op_ffi_unsafe_callback_ref", false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:06:04 -04:00
|
|
|
close() {
|
2022-06-28 05:23:36 -04:00
|
|
|
if (this.#refcount) {
|
|
|
|
this.#refcount = 0;
|
|
|
|
core.opSync("op_ffi_unsafe_callback_ref", false);
|
|
|
|
}
|
2022-06-20 07:06:04 -04:00
|
|
|
core.close(this.#rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const UnsafeCallbackPrototype = UnsafeCallback.prototype;
|
|
|
|
|
2021-08-06 17:28:10 -04:00
|
|
|
class DynamicLibrary {
|
|
|
|
#rid;
|
|
|
|
symbols = {};
|
|
|
|
|
|
|
|
constructor(path, symbols) {
|
2022-06-29 04:13:33 -04:00
|
|
|
[this.#rid, this.symbols] = core.opSync("op_ffi_load", { path, symbols });
|
2021-08-06 17:28:10 -04:00
|
|
|
for (const symbol in symbols) {
|
2022-02-18 07:21:19 -05:00
|
|
|
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;
|
2022-06-20 09:38:10 -04:00
|
|
|
const value = core.opSync(
|
2022-02-18 07:21:19 -05:00
|
|
|
"op_ffi_get_static",
|
2022-06-20 07:06:04 -04:00
|
|
|
this.#rid,
|
|
|
|
name,
|
|
|
|
type,
|
2022-02-18 07:21:19 -05:00
|
|
|
);
|
|
|
|
ObjectDefineProperty(
|
|
|
|
this.symbols,
|
|
|
|
symbol,
|
|
|
|
{
|
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
value,
|
|
|
|
writable: false,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-08 07:13:10 -04:00
|
|
|
|
2021-10-05 18:27:05 -04:00
|
|
|
const isNonBlocking = symbols[symbol].nonblocking;
|
2022-06-20 07:06:04 -04:00
|
|
|
if (isNonBlocking) {
|
2022-06-29 04:13:33 -04:00
|
|
|
const resultType = symbols[symbol].result;
|
2022-06-20 07:06:04 -04:00
|
|
|
const needsUnpacking = isReturnedAsBigInt(resultType);
|
2022-06-29 04:13:33 -04:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
);
|
2022-06-20 07:06:04 -04:00
|
|
|
}
|
2021-08-06 17:28:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
core.close(this.#rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function dlopen(path, symbols) {
|
2021-08-24 09:09:00 -04:00
|
|
|
// URL support is progressively enhanced by util in `runtime/js`.
|
|
|
|
const pathFromURL = __bootstrap.util.pathFromURL ?? ((p) => p);
|
|
|
|
return new DynamicLibrary(pathFromURL(path), symbols);
|
2021-08-06 17:28:10 -04:00
|
|
|
}
|
|
|
|
|
2022-01-12 06:38:26 -05:00
|
|
|
window.__bootstrap.ffi = {
|
|
|
|
dlopen,
|
2022-06-20 07:06:04 -04:00
|
|
|
UnsafeCallback,
|
2022-01-12 06:38:26 -05:00
|
|
|
UnsafePointer,
|
|
|
|
UnsafePointerView,
|
|
|
|
UnsafeFnPointer,
|
|
|
|
};
|
2021-08-06 17:28:10 -04:00
|
|
|
})(this);
|