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;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
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-07-28 08:38:22 -04:00
|
|
|
ArrayPrototypeMap,
|
|
|
|
Number,
|
|
|
|
NumberIsSafeInteger,
|
|
|
|
ArrayPrototypeJoin,
|
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-07-28 08:38:22 -04:00
|
|
|
Int32Array,
|
|
|
|
Uint32Array,
|
|
|
|
BigInt64Array,
|
|
|
|
Function,
|
2021-10-05 18:27:05 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2021-12-15 09:41:49 -05:00
|
|
|
|
2022-07-24 06:41:11 -04:00
|
|
|
function unpackU64(returnValue) {
|
|
|
|
if (typeof returnValue === "number") {
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
const [hi, lo] = returnValue;
|
2022-06-08 07:13:10 -04:00
|
|
|
return BigInt(hi) << 32n | BigInt(lo);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
2022-07-24 06:41:11 -04:00
|
|
|
function unpackI64(returnValue) {
|
|
|
|
if (typeof returnValue === "number") {
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
const [hi, lo] = returnValue;
|
2021-12-15 09:41:49 -05:00
|
|
|
const u64 = unpackU64([hi, lo]);
|
|
|
|
return u64 >> 63n ? u64 - 0x10000000000000000n : u64;
|
|
|
|
}
|
|
|
|
|
|
|
|
class UnsafePointerView {
|
|
|
|
pointer;
|
|
|
|
|
|
|
|
constructor(pointer) {
|
|
|
|
this.pointer = pointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
getUint8(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_u8(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getInt8(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_i8(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUint16(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_u16(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getInt16(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_i16(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getUint32(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_u32(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getInt32(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_i32(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getBigUint64(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_u64(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getBigInt64(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_i64(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getFloat32(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_f32(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFloat64(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_f64(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getCString(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_cstr_read(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:27:12 -04:00
|
|
|
static getCString(pointer, offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_cstr_read(
|
2022-08-05 12:27:12 -04:00
|
|
|
offset ? BigInt(pointer) + BigInt(offset) : pointer,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-15 09:41:49 -05:00
|
|
|
getArrayBuffer(byteLength, offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_get_buf(
|
2022-08-05 12:27:12 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
|
|
|
byteLength,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getArrayBuffer(pointer, byteLength, offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_get_buf(
|
2022-08-05 12:27:12 -04:00
|
|
|
offset ? BigInt(pointer) + BigInt(offset) : pointer,
|
2022-07-23 13:11:06 -04:00
|
|
|
byteLength,
|
|
|
|
);
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
copyInto(destination, offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_ffi_buf_copy_into(
|
2022-07-24 06:41:11 -04:00
|
|
|
offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
|
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
|
|
|
}
|
2022-08-05 12:27:12 -04:00
|
|
|
|
|
|
|
static copyInto(pointer, destination, offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_ffi_buf_copy_into(
|
2022-08-05 12:27:12 -04:00
|
|
|
offset ? BigInt(pointer) + BigInt(offset) : pointer,
|
|
|
|
destination,
|
|
|
|
destination.byteLength,
|
|
|
|
);
|
|
|
|
}
|
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-08-11 09:56:56 -04:00
|
|
|
return ops.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-08-11 09:56:56 -04:00
|
|
|
return ops.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";
|
|
|
|
}
|
|
|
|
|
2022-07-28 08:38:22 -04:00
|
|
|
function isI64(type) {
|
|
|
|
return type === "i64" || type === "isize";
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:06:04 -04:00
|
|
|
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",
|
|
|
|
);
|
|
|
|
}
|
2022-08-11 09:56:56 -04:00
|
|
|
const [rid, pointer] = ops.op_ffi_unsafe_callback_create(
|
2022-06-20 07:06:04 -04:00
|
|
|
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) {
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_ffi_unsafe_callback_ref(true);
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-08-11 09:56:56 -04:00
|
|
|
ops.op_ffi_unsafe_callback_ref(false);
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 07:06:04 -04:00
|
|
|
close() {
|
2022-06-28 05:23:36 -04:00
|
|
|
if (this.#refcount) {
|
|
|
|
this.#refcount = 0;
|
2022-08-11 09:56:56 -04:00
|
|
|
ops.op_ffi_unsafe_callback_ref(false);
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
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-08-11 09:56:56 -04:00
|
|
|
[this.#rid, this.symbols] = ops.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-08-11 09:56:56 -04:00
|
|
|
const value = ops.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-07-28 08:38:22 -04:00
|
|
|
const resultType = symbols[symbol].result;
|
|
|
|
const needsUnpacking = isReturnedAsBigInt(resultType);
|
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
|
|
|
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
|
|
|
}
|
2022-07-28 08:38:22 -04:00
|
|
|
|
|
|
|
if (needsUnpacking && !isNonBlocking) {
|
|
|
|
const call = this.symbols[symbol];
|
|
|
|
const parameters = symbols[symbol].parameters;
|
|
|
|
const vi = new Int32Array(2);
|
|
|
|
const vui = new Uint32Array(vi.buffer);
|
|
|
|
const b = new BigInt64Array(vi.buffer);
|
|
|
|
|
|
|
|
const params = ArrayPrototypeJoin(
|
|
|
|
ArrayPrototypeMap(parameters, (_, index) => `p${index}`),
|
|
|
|
", ",
|
|
|
|
);
|
|
|
|
// Make sure V8 has no excuse to not optimize this function.
|
|
|
|
this.symbols[symbol] = new Function(
|
|
|
|
"vi",
|
|
|
|
"vui",
|
|
|
|
"b",
|
|
|
|
"call",
|
|
|
|
"NumberIsSafeInteger",
|
|
|
|
"Number",
|
|
|
|
`return function (${params}) {
|
|
|
|
call(${params}${parameters.length > 0 ? ", " : ""}vi);
|
|
|
|
${
|
|
|
|
isI64(resultType)
|
|
|
|
? `const n1 = Number(b[0])`
|
|
|
|
: `const n1 = vui[0] + 2 ** 32 * vui[1]` // Faster path for u64
|
|
|
|
};
|
|
|
|
if (NumberIsSafeInteger(n1)) return n1;
|
|
|
|
return b[0];
|
|
|
|
}`,
|
|
|
|
)(vi, vui, b, call, NumberIsSafeInteger, Number);
|
|
|
|
}
|
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);
|