Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All@@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All@@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand DownExpand Up@@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand DownExpand Up@@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All@@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All@@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand DownExpand Up@@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand DownExpand Up@@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand DownExpand Up@@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand DownExpand Up@@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All@@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand DownExpand Up@@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All@@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer`and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All@@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All@@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand DownExpand Up@@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand DownExpand Up@@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All@@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All@@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand DownExpand Up@@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand DownExpand Up@@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand DownExpand Up@@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand DownExpand Up@@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All@@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand DownExpand Up@@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All@@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer`and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All@@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All@@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand DownExpand Up@@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand DownExpand Up@@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All@@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All@@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand DownExpand Up@@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand DownExpand Up@@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand DownExpand Up@@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand DownExpand Up@@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All@@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand DownExpand Up@@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All@@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer`and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All@@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All@@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand DownExpand Up@@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand DownExpand Up@@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All@@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All@@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand DownExpand Up@@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand DownExpand Up@@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand DownExpand Up@@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand DownExpand Up@@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All@@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand DownExpand Up@@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All@@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer`and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All@@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All@@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand DownExpand Up@@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand DownExpand Up@@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All@@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All@@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand DownExpand Up@@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand DownExpand Up@@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand DownExpand Up@@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand DownExpand Up@@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All@@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand DownExpand Up@@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All@@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer`and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All@@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All@@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand DownExpand Up@@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand DownExpand Up@@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All@@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All@@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand DownExpand Up@@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand DownExpand Up@@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand DownExpand Up@@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand DownExpand Up@@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All@@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand DownExpand Up@@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All@@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer`and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All@@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All@@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand DownExpand Up@@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand DownExpand Up@@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All@@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All@@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand DownExpand Up@@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand DownExpand Up@@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand DownExpand Up@@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand DownExpand Up@@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All@@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand DownExpand Up@@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All@@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer`and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 25 additions & 31 deletions doc/api/ffi.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -63,18 +63,18 @@ Supported type names:

* `void`
* `char`
* `i8`, `int8`
* `u8`, `uint8`, `bool`
* `i16`, `int16`
* `u16`, `uint16`
* `i32`, `int32`
* `u32`, `uint32`
* `i64`, `int64`
* `u64`, `uint64`
* `f32`, `float`, `float32`
* `f64`, `double`, `float64`
* `pointer`, `ptr`
* `string`, `str`
* `int8`
* `uint8`
* `int16`
* `uint16`
* `int32`
* `uint32`
* `int64`
* `uint64`
* `float32`
* `float64`
* `pointer`
* `string`
* `buffer`
* `arraybuffer`
* `function`
Expand All@@ -86,11 +86,8 @@ These type names are also exposed as constants on `ffi.types`:
* `ffi.types.BUFFER` = `'buffer'`
* `ffi.types.ARRAY_BUFFER` = `'arraybuffer'`
* `ffi.types.FUNCTION` = `'function'`
* `ffi.types.BOOL` = `'bool'`
* `ffi.types.CHAR` = `'char'`
* `ffi.types.STRING` = `'string'`
* `ffi.types.FLOAT` = `'float'`
* `ffi.types.DOUBLE` = `'double'`
* `ffi.types.INT_8` = `'int8'`
* `ffi.types.UINT_8` = `'uint8'`
* `ffi.types.INT_16` = `'int16'`
Expand All@@ -116,12 +113,9 @@ through reentrant JavaScript such as FFI callbacks. Doing so may crash the
process, produce incorrect output, or corrupt memory.

The `char` type follows the platform C ABI. On platforms where plain C `char`
is signed it behaves like `i8`; otherwise it behaves like `u8`.
is signed it behaves like `int8`; otherwise it behaves like `uint8`.

The `bool` type is marshaled as an 8-bit unsigned integer. Pass numeric values
such as `0` and `1`; JavaScript `true` and `false` are not accepted.

On optimized Fast FFI calls, `pointer`, `ptr`, and `function` parameters accept
On optimized Fast FFI calls, `pointer` and `function` parameters accept
raw pointer `bigint` values. For pointer-like parameters, `null`, `undefined`,
strings, `Buffer`, typed array, `DataView`, and `ArrayBuffer` values are
converted on the JavaScript side before calling the optimized native wrapper.
Expand DownExpand Up@@ -161,8 +155,8 @@ optional:

```js
const signature = {
return: 'i32',
arguments: ['i32', 'i32'],
return: 'int32',
arguments: ['int32', 'int32'],
};
```

Expand DownExpand Up@@ -220,7 +214,7 @@ import { dlopen, suffix } from 'node:ffi';

{
using handle = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
});
console.log(handle.functions.add_i32(20, 22));
} // handle.lib.close() is invoked automatically here.
Expand All@@ -230,8 +224,8 @@ import { dlopen, suffix } from 'node:ffi';
import { dlopen, suffix } from 'node:ffi';

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand All@@ -241,8 +235,8 @@ console.log(functions.add_i32(20, 22));
const { dlopen, suffix } = require('node:ffi');

const { lib, functions } = dlopen(`./mylib.${suffix}`, {
add_i32: { arguments: ['i32', 'i32'], return: 'i32' },
string_length: { arguments: ['pointer'], return: 'u64' },
add_i32: { arguments: ['int32', 'int32'], return: 'int32' },
string_length: { arguments: ['pointer'], return: 'uint64' },
});

console.log(functions.add_i32(20, 22));
Expand DownExpand Up@@ -384,8 +378,8 @@ const { DynamicLibrary, suffix } = require('node:ffi');

const lib = new DynamicLibrary(`./mylib.${suffix}`);
const add = lib.getFunction('add_i32', {
arguments: ['i32', 'i32'],
return: 'i32',
arguments: ['int32', 'int32'],
return: 'int32',
});

console.log(add(20, 22));
Expand DownExpand Up@@ -435,7 +429,7 @@ const { DynamicLibrary, suffix } = require('node:ffi');
const lib = new DynamicLibrary(`./mylib.${suffix}`);

const callback = lib.registerCallback(
{ arguments: ['i32'], return: 'i32' },
{ arguments: ['int32'], return: 'int32' },
(value) => value * 2,
);
```
Expand DownExpand Up@@ -498,7 +492,7 @@ Argument conversion depends on the declared FFI type.
For 8-, 16-, and 32-bit integer types and for floating-point types, pass
JavaScript `number` values that match the declared type.

For 64-bit integer types (`i64` and `u64`), pass JavaScript `bigint` values.
For 64-bit integer types (`int64` and `uint64`), pass JavaScript `bigint` values.

For pointer-like arguments:

Expand Down
3 changes: 0 additions & 3 deletions lib/ffi.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -326,11 +326,8 @@ const types = ObjectFreeze({
BUFFER: 'buffer',
ARRAY_BUFFER: 'arraybuffer',
FUNCTION: 'function',
BOOL: 'bool',
CHAR: 'char',
STRING: 'string',
FLOAT: 'float',
DOUBLE: 'double',
INT_8: 'int8',
UINT_8: 'uint8',
INT_16: 'int16',
Expand Down
15 changes: 0 additions & 15 deletions lib/internal/ffi-shared-buffer.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,39 +64,24 @@ const gF64 = DataViewPrototypeGetFloat64;

const sbTypeInfo = {
__proto__: null,
i8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
int8: { set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ set: sI8, get: gI8, kind: 'int', min: -128, max: 127, label: 'an int8' } :
{ set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
u8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
uint8: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
bool: { set: sU8, get: gU8, kind: 'int', min: 0, max: 255, label: 'a uint8' },
i16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
int16: { set: sI16, get: gI16, kind: 'int', min: -32768, max: 32767, label: 'an int16' },
u16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
uint16: { set: sU16, get: gU16, kind: 'int', min: 0, max: 65535, label: 'a uint16' },
i32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { set: sI32, get: gI32, kind: 'int', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { set: sU32, get: gU32, kind: 'int', min: 0, max: 4294967295, label: 'a uint32' },
i64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
int64: { set: sI64, get: gI64, kind: 'i64', label: 'an int64' },
u64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
uint64: { set: sU64, get: gU64, kind: 'u64', label: 'a uint64' },
f32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
float32: { set: sF32, get: gF32, kind: 'float', label: 'a float' },
f64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
double: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
float64: { set: sF64, get: gF64, kind: 'float', label: 'a double' },
pointer: { set: sU64, get: gU64, kind: 'pointer' },
ptr: { set: sU64, get: gU64, kind: 'pointer' },
function: { set: sU64, get: gU64, kind: 'pointer' },
buffer: { set: sU64, get: gU64, kind: 'pointer' },
arraybuffer: { set: sU64, get: gU64, kind: 'pointer' },
string: { set: sU64, get: gU64, kind: 'pointer' },
str: { set: sU64, get: gU64, kind: 'pointer' },
};

const U64_MAX = 0xFFFFFFFFFFFFFFFFn;
Expand Down
15 changes: 3 additions & 12 deletions lib/internal/ffi/fast-api.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -47,25 +47,16 @@ const fastLibraryStates = new SafeWeakMap();
// conversions, so the public FFI ranges must be checked before the raw call.
const fastIntegerTypeInfo = {
__proto__: null,
i8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
int8: { kind: 'number', min: -128, max: 127, label: 'an int8' },
char: charIsSigned ?
{ kind: 'number', min: -128, max: 127, label: 'an int8' } :
{ kind: 'number', min: 0, max: 255, label: 'a uint8' },
u8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
uint8: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
bool: { kind: 'number', min: 0, max: 255, label: 'a uint8' },
i16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
int16: { kind: 'number', min: -32768, max: 32767, label: 'an int16' },
u16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
uint16: { kind: 'number', min: 0, max: 65535, label: 'a uint16' },
i32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
int32: { kind: 'number', min: -2147483648, max: 2147483647, label: 'an int32' },
u32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
uint32: { kind: 'number', min: 0, max: 4294967295, label: 'a uint32' },
i64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
int64: { kind: 'bigint', min: I64_MIN, max: I64_MAX, label: 'an int64' },
u64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
uint64: { kind: 'bigint', min: 0n, max: U64_MAX, label: 'a uint64' },
};

Expand DownExpand Up@@ -97,16 +88,16 @@ function needsRawPointerConversion(type) {
}

function needsPointerLikeConversion(type) {
return type === 'pointer' || type === 'ptr' || type === 'function' ||
return type === 'pointer' || type === 'function' ||
type === 'buffer' || type === 'arraybuffer';
}

function needsStringPointerConversion(type) {
return type === 'string' || type === 'str' || needsPointerLikeConversion(type);
return type === 'string' || needsPointerLikeConversion(type);
}

function needsNullPointerConversion(type) {
return needsPointerLikeConversion(type) || type === 'string' || type === 'str' ||
return needsPointerLikeConversion(type) || type === 'string' ||
needsRawPointerConversion(type);
}

Expand Down
43 changes: 17 additions & 26 deletions src/ffi/fast.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,9 +19,6 @@ using v8::FastApiCallbackOptions;

bool IsTypeName(std::string_view type,
std::initializer_list<std::string_view> names) {
// Signature parsing accepts several public aliases for the same ABI type.
// The fast path normalizes them by checking the original type name against
// each alias set before selecting a FastFFIType.
for (std::string_view name : names) {
if (type == name) {
return true;
Expand All@@ -36,34 +33,31 @@ bool FastScalarTypeFromName(std::string_view type, FastFFIType* out) {
// JavaScript wrappers handle strings and object-to-pointer conversions.
if (type == "void") {
*out = FastFFIType::kVoid;
} else if (type == "bool") {
*out = FastFFIType::kUint8;
} else if (IsTypeName(type, {"i8", "int8"})) {
} else if (type == "int8") {
*out = FastFFIType::kInt8;
} else if (IsTypeName(type, {"u8", "uint8"})) {
} else if (type == "uint8") {
*out = FastFFIType::kUint8;
} else if (type == "char") {
*out = CHAR_MIN < 0 ? FastFFIType::kInt8 : FastFFIType::kUint8;
} else if (IsTypeName(type, {"i16", "int16"})) {
} else if (type == "int16") {
*out = FastFFIType::kInt16;
} else if (IsTypeName(type, {"u16", "uint16"})) {
} else if (type == "uint16") {
*out = FastFFIType::kUint16;
} else if (IsTypeName(type, {"i32", "int32"})) {
} else if (type == "int32") {
*out = FastFFIType::kInt32;
} else if (IsTypeName(type, {"u32", "uint32"})) {
} else if (type == "uint32") {
*out = FastFFIType::kUint32;
} else if (IsTypeName(type, {"i64", "int64"})) {
} else if (type == "int64") {
*out = FastFFIType::kInt64;
} else if (IsTypeName(type, {"u64", "uint64"})) {
} else if (type == "uint64") {
*out = FastFFIType::kUint64;
} else if (IsTypeName(type, {"f32", "float", "float32"})) {
} else if (type == "float32") {
*out = FastFFIType::kFloat32;
} else if (IsTypeName(type, {"f64", "double", "float64"})) {
} else if (type == "float64") {
*out = FastFFIType::kFloat64;
} else if (IsTypeName(type, {"buffer", "arraybuffer"})) {
*out = FastFFIType::kPointer;
} else if (IsTypeName(type,
{"pointer", "ptr", "string", "str", "function"})) {
} else if (IsTypeName(type, {"pointer", "string", "function"})) {
*out = FastFFIType::kPointer;
} else {
return false;
Expand DownExpand Up@@ -150,8 +144,7 @@ bool SignatureNeedsRawPointerConversions(const FFIFunction& fn) {
// a signature contains them, JS wraps the fast function to perform the
// conversion before V8 enters the CFunction trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "buffer" || name == "arraybuffer" || name == "string" ||
name == "str") {
if (name == "buffer" || name == "arraybuffer" || name == "string") {
return true;
}
}
Expand All@@ -162,21 +155,19 @@ bool SignatureNeedsFastIntegerValidation(const FFIFunction& fn) {
// V8 widens narrow integers to 32 bits and truncates BigInts to 64 bits for
// Fast API calls. These types need a JS range check before the trampoline.
for (const std::string& name : fn.arg_type_names) {
if (name == "bool" || name == "char" || name == "i8" || name == "int8" ||
name == "u8" || name == "uint8" || name == "i16" || name == "int16" ||
name == "u16" || name == "uint16" || name == "i32" || name == "int32" ||
name == "u32" || name == "uint32" || name == "i64" || name == "int64" ||
name == "u64" || name == "uint64") {
if (name == "char" || name == "int8" || name == "uint8" ||
name == "int16" || name == "uint16" || name == "int32" ||
name == "uint32" || name == "int64" || name == "uint64") {
return true;
}
}
return false;
}

bool IsPointerTypeName(const std::string& name) {
// `pointer`, `ptr`, and `function` all use the same uintptr ABI slot; only
// `pointer`and `function` use the same uintptr ABI slot; only
// the public type spelling differs.
return name == "pointer" || name == "ptr" || name == "function";
return name == "pointer" || name == "function";
}

bool IsBufferTypeName(const std::string& name) {
Expand Down
25 changes: 11 additions & 14 deletions src/ffi/types.cc
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,33 +552,30 @@ void WriteFFIReturnToBuffer(ffi_type* type,
v8::Maybe<ffi_type*> ToFFIType(Environment* env, std::string_view type_str) {
if (type_str == "void") {
return Just(&ffi_type_void);
} else if (type_str == "i8" || type_str == "int8") {
} else if (type_str == "int8") {
return Just(&ffi_type_sint8);
} else if (type_str == "u8" || type_str == "uint8" || type_str == "bool") {
} else if (type_str == "uint8") {
return Just(&ffi_type_uint8);
} else if (type_str == "char") {
return Just(CHAR_MIN < 0 ? &ffi_type_sint8 : &ffi_type_uint8);
} else if (type_str == "i16" || type_str == "int16") {
} else if (type_str == "int16") {
return Just(&ffi_type_sint16);
} else if (type_str == "u16" || type_str == "uint16") {
} else if (type_str == "uint16") {
return Just(&ffi_type_uint16);
} else if (type_str == "i32" || type_str == "int32") {
} else if (type_str == "int32") {
return Just(&ffi_type_sint32);
} else if (type_str == "u32" || type_str == "uint32") {
} else if (type_str == "uint32") {
return Just(&ffi_type_uint32);
} else if (type_str == "i64" || type_str == "int64") {
} else if (type_str == "int64") {
return Just(&ffi_type_sint64);
} else if (type_str == "u64" || type_str == "uint64") {
} else if (type_str == "uint64") {
return Just(&ffi_type_uint64);
} else if (type_str == "f32" || type_str == "float" ||
type_str == "float32") {
} else if (type_str == "float32") {
return Just(&ffi_type_float);
} else if (type_str == "f64" || type_str == "double" ||
type_str == "float64") {
} else if (type_str == "float64") {
return Just(&ffi_type_double);
} else if (type_str == "buffer" || type_str == "arraybuffer" ||
type_str == "string" || type_str == "str" ||
type_str == "pointer" || type_str == "ptr" ||
type_str == "string" || type_str == "pointer" ||
type_str == "function") {
return Just(&ffi_type_pointer);
} else {
Expand Down
Loading
Loading