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 {
|
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,
|
2021-12-15 09:41:49 -05:00
|
|
|
TypeError,
|
2022-07-28 08:38:22 -04:00
|
|
|
Int32Array,
|
|
|
|
Uint32Array,
|
|
|
|
BigInt64Array,
|
2022-10-20 00:05:56 -04:00
|
|
|
BigUint64Array,
|
2022-07-28 08:38:22 -04:00
|
|
|
Function,
|
2021-10-05 18:27:05 -04:00
|
|
|
} = window.__bootstrap.primordials;
|
2021-12-15 09:41:49 -05:00
|
|
|
|
2022-10-20 00:05:56 -04:00
|
|
|
const U32_BUFFER = new Uint32Array(2);
|
|
|
|
const U64_BUFFER = new BigUint64Array(U32_BUFFER.buffer);
|
|
|
|
const I64_BUFFER = new BigInt64Array(U32_BUFFER.buffer);
|
2021-12-15 09:41:49 -05:00
|
|
|
class UnsafePointerView {
|
|
|
|
pointer;
|
|
|
|
|
|
|
|
constructor(pointer) {
|
|
|
|
this.pointer = pointer;
|
|
|
|
}
|
|
|
|
|
2022-09-04 23:26:52 -04:00
|
|
|
getBool(offset = 0) {
|
|
|
|
return ops.op_ffi_read_bool(
|
2022-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
2022-09-04 23:26:52 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-15 09:41:49 -05:00
|
|
|
getUint8(offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_read_u8(
|
2022-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
2021-12-15 09:41:49 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getBigUint64(offset = 0) {
|
2022-10-20 00:05:56 -04:00
|
|
|
ops.op_ffi_read_u64(
|
|
|
|
this.pointer,
|
|
|
|
offset,
|
|
|
|
U32_BUFFER,
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2022-10-20 00:05:56 -04:00
|
|
|
return U64_BUFFER[0];
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getBigInt64(offset = 0) {
|
2022-10-20 00:05:56 -04:00
|
|
|
ops.op_ffi_read_i64(
|
|
|
|
this.pointer,
|
|
|
|
offset,
|
|
|
|
U32_BUFFER,
|
2022-06-20 07:06:04 -04:00
|
|
|
);
|
2022-10-20 00:05:56 -04:00
|
|
|
return I64_BUFFER[0];
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
pointer,
|
|
|
|
offset,
|
2022-08-05 12:27:12 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
offset,
|
2022-08-05 12:27:12 -04:00
|
|
|
byteLength,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getArrayBuffer(pointer, byteLength, offset = 0) {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_ffi_get_buf(
|
2022-10-20 00:05:56 -04:00
|
|
|
pointer,
|
|
|
|
offset,
|
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-10-20 00:05:56 -04:00
|
|
|
this.pointer,
|
|
|
|
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
|
|
|
}
|
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-10-20 00:05:56 -04:00
|
|
|
pointer,
|
|
|
|
offset,
|
2022-08-05 12:27:12 -04:00
|
|
|
destination,
|
|
|
|
destination.byteLength,
|
|
|
|
);
|
|
|
|
}
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
|
2022-10-20 00:07:37 -04:00
|
|
|
const OUT_BUFFER = new Uint32Array(2);
|
|
|
|
const OUT_BUFFER_64 = new BigInt64Array(OUT_BUFFER.buffer);
|
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-10-20 00:07:37 -04:00
|
|
|
ops.op_ffi_ptr_of(value, OUT_BUFFER);
|
|
|
|
const result = OUT_BUFFER[0] + 2 ** 32 * OUT_BUFFER[1];
|
|
|
|
if (NumberIsSafeInteger(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return OUT_BUFFER_64[0];
|
2021-12-15 09:41:49 -05:00
|
|
|
}
|
|
|
|
}
|
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-01-12 06:38:26 -05:00
|
|
|
if (this.definition.nonblocking) {
|
2022-10-13 08:06:52 -04:00
|
|
|
return core.opAsync(
|
2022-06-20 07:06:04 -04:00
|
|
|
"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
|
|
|
} 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 isReturnedAsBigInt(type) {
|
2022-10-13 08:06:52 -04:00
|
|
|
return type === "buffer" || type === "pointer" || type === "function" ||
|
|
|
|
type === "u64" || type === "i64" ||
|
2022-06-20 07:06:04 -04:00
|
|
|
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-10-15 09:49:46 -04:00
|
|
|
// Internal promise only meant to keep Deno from exiting
|
|
|
|
#refpromise;
|
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-10-15 09:49:46 -04:00
|
|
|
this.#refpromise = core.opAsync(
|
|
|
|
"op_ffi_unsafe_callback_ref",
|
|
|
|
this.#rid,
|
|
|
|
);
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
2022-10-15 09:49:46 -04:00
|
|
|
return this.#refcount;
|
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-10-15 09:49:46 -04:00
|
|
|
ops.op_ffi_unsafe_callback_unref(this.#rid);
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
2022-10-15 09:49:46 -04:00
|
|
|
return this.#refcount;
|
2022-06-28 05:23:36 -04:00
|
|
|
}
|
|
|
|
|
2022-06-20 07:06:04 -04:00
|
|
|
close() {
|
2022-10-15 09:49:46 -04:00
|
|
|
this.#refcount = 0;
|
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) => {
|
2022-10-13 08:06:52 -04:00
|
|
|
return core.opAsync(
|
2022-06-29 04:13:33 -04:00
|
|
|
"op_ffi_call_nonblocking",
|
|
|
|
this.#rid,
|
|
|
|
symbol,
|
|
|
|
parameters,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
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);
|