summaryrefslogtreecommitdiff
path: root/ext/ffi/00_ffi.js
diff options
context:
space:
mode:
authorAapo Alasuutari <aapo.alasuutari@gmail.com>2022-07-24 13:41:11 +0300
committerGitHub <noreply@github.com>2022-07-24 16:11:11 +0530
commitf8fee6cd21cce82d6c34e539d39da86df7b036f7 (patch)
tree8099527951e4b532934a00c25797758aeb496ab6 /ext/ffi/00_ffi.js
parente1cbd2364f536a1cef817961967e1738b89be734 (diff)
feat(ext/ffi): Safe number pointers (#15173)
Diffstat (limited to 'ext/ffi/00_ffi.js')
-rw-r--r--ext/ffi/00_ffi.js38
1 files changed, 23 insertions, 15 deletions
diff --git a/ext/ffi/00_ffi.js b/ext/ffi/00_ffi.js
index ac8dda317..bc03dfb14 100644
--- a/ext/ffi/00_ffi.js
+++ b/ext/ffi/00_ffi.js
@@ -12,11 +12,19 @@
TypeError,
} = window.__bootstrap.primordials;
- function unpackU64([hi, lo]) {
+ function unpackU64(returnValue) {
+ if (typeof returnValue === "number") {
+ return returnValue;
+ }
+ const [hi, lo] = returnValue;
return BigInt(hi) << 32n | BigInt(lo);
}
- function unpackI64([hi, lo]) {
+ function unpackI64(returnValue) {
+ if (typeof returnValue === "number") {
+ return returnValue;
+ }
+ const [hi, lo] = returnValue;
const u64 = unpackU64([hi, lo]);
return u64 >> 63n ? u64 - 0x10000000000000000n : u64;
}
@@ -31,77 +39,77 @@
getUint8(offset = 0) {
return core.opSync(
"op_ffi_read_u8",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getInt8(offset = 0) {
return core.opSync(
"op_ffi_read_i8",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getUint16(offset = 0) {
return core.opSync(
"op_ffi_read_u16",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getInt16(offset = 0) {
return core.opSync(
"op_ffi_read_i16",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getUint32(offset = 0) {
return core.opSync(
"op_ffi_read_u32",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getInt32(offset = 0) {
return core.opSync(
"op_ffi_read_i32",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getBigUint64(offset = 0) {
return core.opSync(
"op_ffi_read_u64",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getBigInt64(offset = 0) {
return core.opSync(
- "op_ffi_read_u64",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ "op_ffi_read_i64",
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getFloat32(offset = 0) {
return core.opSync(
"op_ffi_read_f32",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getFloat64(offset = 0) {
return core.opSync(
"op_ffi_read_f64",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
getCString(offset = 0) {
return core.opSync(
"op_ffi_cstr_read",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
);
}
@@ -116,7 +124,7 @@
copyInto(destination, offset = 0) {
core.opSync(
"op_ffi_buf_copy_into",
- offset ? this.pointer + BigInt(offset) : this.pointer,
+ offset ? BigInt(this.pointer) + BigInt(offset) : this.pointer,
destination,
destination.byteLength,
);