Skip to content

Commit 8aa1937

Browse files
committed
fixup! fixup! fixup! fixup! fixup! src,lib: initial ffi implementation
1 parent 33787e7 commit 8aa1937

2 files changed

Lines changed: 17 additions & 23 deletions

File tree

lib/ffi.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const callBufferDV = new DataView(callBuffer);
3737

3838
const libCache = {};
3939

40-
const POINTER_SIZE = sizes['char *'];
40+
const POINTER_SIZE = sizes['char*'];
4141
const NULL = Symbol('null');
4242

4343
function isSigned(type) {
@@ -229,7 +229,7 @@ function getBufferPointer(buf) {
229229

230230
function sizeof(typ) {
231231
if (sizes[typ]) return sizes[typ];
232-
if (typ.includes('*')) return sizes['char *'];
232+
if (typ.includes('*')) return sizes['char*'];
233233
}
234234

235235
module.exports = {

src/node_ffi.cc

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,18 @@ using v8::Number;
2323
namespace node {
2424
namespace ffi {
2525

26-
static bool lossless = false;
27-
28-
#define ReturnBigInt(isolate, ptr) \
29-
args.GetReturnValue().Set(BigInt::NewFromUnsigned(isolate, (uint64_t)ptr))
26+
#define ReturnPointer(ptr) \
27+
args.GetReturnValue().Set( \
28+
BigInt::NewFromUnsigned(args.GetIsolate(), (uint64_t)ptr))
3029

3130
void GetLibrary(const FunctionCallbackInfo<Value>& args) {
3231
Environment* env = Environment::GetCurrent(args);
3332
THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kFfi, "");
3433

35-
bool isNull = true;
3634
std::string fname = "";
37-
if (!args[0]->IsNull()) {
38-
CHECK(args[0]->IsString());
39-
node::Utf8Value filename(env->isolate(), args[0]);
40-
fname = filename.ToString();
41-
isNull = false;
35+
CHECK(args[0]->IsNull() || args[0]->IsString());
36+
if (args[0]->IsString()) {
37+
fname = *node::Utf8Value(env->isolate(), args[0]);
4238
}
4339

4440
std::map<std::string, binding::DLib*> libraries =
@@ -47,28 +43,27 @@ void GetLibrary(const FunctionCallbackInfo<Value>& args) {
4743
if (libraries[fname] != nullptr) {
4844
lib = libraries[fname];
4945
} else {
50-
if (isNull) {
46+
if (fname.empty()) {
5147
lib = new binding::DLib(binding::DLib::kDefaultFlags);
5248
} else {
5349
lib = new binding::DLib(fname.c_str(), binding::DLib::kDefaultFlags);
5450
}
5551
if (!lib->Open()) return;
5652
libraries[fname] = lib;
5753
}
58-
ReturnBigInt(env->isolate(), lib);
54+
ReturnPointer(lib);
5955
}
6056

6157
void GetSymbol(const FunctionCallbackInfo<Value>& args) {
6258
Environment* env = Environment::GetCurrent(args);
6359
THROW_IF_INSUFFICIENT_PERMISSIONS(env, permission::PermissionScope::kFfi, "");
6460

6561
CHECK(args[0]->IsBigInt());
66-
binding::DLib* lib =
67-
(binding::DLib*)args[0].As<BigInt>()->Uint64Value(&lossless);
62+
binding::DLib* lib = (binding::DLib*)args[0].As<BigInt>()->Uint64Value();
6863
CHECK(args[1]->IsString());
6964
node::Utf8Value symName(env->isolate(), args[1]);
7065
void* symbol = lib->GetSymbolAddress(symName.ToString().c_str());
71-
ReturnBigInt(env->isolate(), symbol);
66+
ReturnPointer(symbol);
7267
}
7368

7469
void GetBufferPointer(const FunctionCallbackInfo<Value>& args) {
@@ -78,7 +73,7 @@ void GetBufferPointer(const FunctionCallbackInfo<Value>& args) {
7873
CHECK(args[0]->IsArrayBuffer() || args[0]->IsSharedArrayBuffer());
7974
void* data = args[0].As<ArrayBuffer>()->Data();
8075

81-
ReturnBigInt(args.GetIsolate(), data);
76+
ReturnPointer(data);
8277
}
8378

8479
void SetCallBuffer(const FunctionCallbackInfo<Value>& args) {
@@ -87,7 +82,7 @@ void SetCallBuffer(const FunctionCallbackInfo<Value>& args) {
8782

8883
void* callBuffer = ab->Data();
8984
Realm::GetBindingData<FfiBindingData>(args)->callBuffer = callBuffer;
90-
ReturnBigInt(args.GetIsolate(), callBuffer);
85+
ReturnPointer(callBuffer);
9186
}
9287

9388
FfiSignature::FfiSignature(Environment* env,
@@ -99,16 +94,15 @@ FfiSignature::FfiSignature(Environment* env,
9994
Local<Context> context = env->context();
10095

10196
cif_ = reinterpret_cast<ffi_cif*>(malloc(sizeof(ffi_cif)));
102-
fn_ = (void (*)())fn->Uint64Value(&lossless);
97+
fn_ = (void (*)())fn->Uint64Value();
10398
uint32_t argc = argTypes->Length();
10499
argvTypes_ = reinterpret_cast<ffi_type**>(malloc(sizeof(ffi_type*) * argc));
105100

106-
ffi_type* retTyp =
107-
reinterpret_cast<ffi_type*>(retType->Uint64Value(&lossless));
101+
ffi_type* retTyp = reinterpret_cast<ffi_type*>(retType->Uint64Value());
108102
for (uint32_t i = 0; i < argc; i++) {
109103
Local<Value> val = argTypes->Get(context, i).ToLocalChecked();
110104
CHECK(val->IsBigInt());
111-
uint64_t arg = val.As<BigInt>()->Uint64Value(&lossless);
105+
uint64_t arg = val.As<BigInt>()->Uint64Value();
112106
argvTypes_[i] = reinterpret_cast<ffi_type*>(arg);
113107
}
114108

0 commit comments

Comments
 (0)