Skip to content
Open
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
22 changes: 22 additions & 0 deletions src/node_ffi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,15 @@ void DynamicLibrary::InvokeCallback(ffi_cif* cif,
}

void DynamicLibrary::GetPath(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!GetConstructorTemplate(env)->HasInstance(args.This())) {
THROW_ERR_INVALID_THIS(env,
"Value of \"this\" must be of type DynamicLibrary");
return;
}

DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This());
CHECK_NOT_NULL(lib);

Local<Value> path;
if (!ToV8Value(lib->env()->context(), lib->path_, args.GetIsolate())
Expand Down Expand Up @@ -806,9 +814,16 @@ void DynamicLibrary::GetFunction(const FunctionCallbackInfo<Value>& args) {

void DynamicLibrary::GetFunctions(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!GetConstructorTemplate(env)->HasInstance(args.This())) {
THROW_ERR_INVALID_THIS(env,
"Value of \"this\" must be of type DynamicLibrary");
return;
}

Isolate* isolate = env->isolate();
Local<Context> context = env->context();
DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This());
CHECK_NOT_NULL(lib);

if (lib->is_closed()) {
THROW_ERR_FFI_LIBRARY_CLOSED(env);
Expand Down Expand Up @@ -955,9 +970,16 @@ void DynamicLibrary::GetSymbol(const FunctionCallbackInfo<Value>& args) {

void DynamicLibrary::GetSymbols(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
if (!GetConstructorTemplate(env)->HasInstance(args.This())) {
THROW_ERR_INVALID_THIS(env,
"Value of \"this\" must be of type DynamicLibrary");
return;
}

Isolate* isolate = env->isolate();
Local<Context> context = env->context();
DynamicLibrary* lib = Unwrap<DynamicLibrary>(args.This());
CHECK_NOT_NULL(lib);

if (lib->is_closed()) {
THROW_ERR_FFI_LIBRARY_CLOSED(env);
Expand Down
19 changes: 19 additions & 0 deletions test/ffi/test-ffi-dynamic-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ test('DynamicLibrary exposes functions and symbols', () => {
}
});

test('DynamicLibrary getters reject incompatible receivers', () => {
const lib = new ffi.DynamicLibrary(libraryPath);

try {
const getters = [
Object.getOwnPropertyDescriptor(lib, 'path').get,
Object.getOwnPropertyDescriptor(lib, 'symbols').get,
Object.getOwnPropertyDescriptor(
ffi.DynamicLibrary.prototype, 'functions').get,
];

for (const getter of getters) {
assert.throws(() => getter.call({}), { code: 'ERR_INVALID_THIS' });
}
} finally {
lib.close();
}
});

test('DynamicLibrary evaluates function signatures once', () => {
function makeChangingSignature() {
const reads = { arguments: 0, return: 0 };
Expand Down
Loading