Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bool type #56

Closed
simon-p-r opened this issue Oct 3, 2016 · 7 comments
Closed

bool type #56

simon-p-r opened this issue Oct 3, 2016 · 7 comments

Comments

@simon-p-r
Copy link

Hi

I am trying to use a bool type however the value in c code is not true or false, I have tried these 2 different ways

const traceMode = ref.alloc('bool', 1); // true

and

const traceMode = ref.alloc(ref.types.bool, 1); // true

Any pointers would be appreciated!

Thanks

@TooTallNate
Copy link
Owner

Is it possible to show me a more complete sample code to demonstrate your problem? What C lib are you trying to invoke?

@simon-p-r
Copy link
Author

I have just done a stripped down version of api, the C api is like this

void create(struct Obj *obj) {

   // value should be true
   if (obj->traceMode) {
       // do something
   }
}

Struct for c api is defined like this

struct Obj {
    bool traceMode;
}

It is defined and used in javascript like this

const Obj = Struct({
    traceMode: ref.types.bool
});

const newObj = new Obj({
    traceMode: ref.alloc('bool', 1) // have also tried ref.alloc(ref.types.bool, 1) as well
});

const structPtr = ref.refType(Obj);

const lib = ffi.Library(dllPath, { 
    'create': ['void', [structPtr]]
});

lib.create(newObj.ref());

@TooTallNate
Copy link
Owner

Oh I see. You don't need to do the ref.alloc() part. Just pass JS true or false in and ref.types.bool handles the coercion to the C value:

const newObj = new Obj({
    traceMode: true
});

What you were doing was passing in a pointer to a bool, which was an address in memory which would basically always be "true".

@simon-p-r
Copy link
Author

Ok I will try that, thanks for your swift answer!

@simon-p-r
Copy link
Author

I am testing value of bool inside c like this but it still doesn't recognise the ref past from javascript.

if (obj->traceMode) {
    printf("Trace mode is on\n");
}
else {
    prinf("Trace mode is off\n");
}

@simon-p-r simon-p-r reopened this Oct 4, 2016
TooTallNate added a commit to node-ffi/node-ffi that referenced this issue Oct 13, 2016
Seems to be working as expected
@TooTallNate
Copy link
Owner

@simon-p-r I added a test case in node-ffi/node-ffi@7a928f3 which seems to be working as expected. Let me know if I'm missing anything.

@simon-p-r
Copy link
Author

Thanks @TooTallNate forgot about this, I think my problem was a boundary problem with the struct I was working with. I am a total c novice, thanks for this excellent module!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants