question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use `enum` with `ffi`?

See original GitHub issue

Hi! I’m working on a project that integrates a shared C library with Node. I’m using the ffi module for this which, in turn, uses the ref library for types. I’ve seen that @TooTallNate did a PR awhile back to implement the type interface so this could be used with ref. I can’t, for the life of me, figure out how to get this to work. Full disclosure: I’m not a C programmer at all.

Here’s the enum definition in the C code:

typedef enum {
    DEVICE_UNKNOWN,
    DEVICE_IOS_IPHONE,
    DEVICE_IOS_IPOD,
    DEVICE_IOS_IPAD,
    DEVICE_WINDOWS_PC,
    DEVICE_WINDOWS_PHONE,
    DEVICE_WINDOWS_TABLET,
    DEVICE_MAC
} E_DEVICE_TYPE;

There is a method called SetDeviceType that accepts DEVICE_MAC as it’s input parameter. In my Javascript I’ve done the following:

const deviceTypeEnum = new Enum([
  'DEVICE_UNKNOWN',
  'DEVICE_IOS_IPHONE',
  'DEVICE_IOS_IPOD',
  'DEVICE_IOS_IPAD',
  'DEVICE_WINDOWS_PC',
  'DEVICE_WINDOWS_PHONE',
  'DEVICE_WINDOWS_TABLET',
  'DEVICE_MAC',
], { name: 'E_DEVICE_TYPE' });

I’ve also tried:

const deviceTypeEnum = new Enum({
  DEVICE_UNKNOWN: 0,
  DEVICE_IOS_IPHONE: 1,
  DEVICE_IOS_IPOD: 2,
  DEVICE_IOS_IPAD: 3,
  DEVICE_WINDOWS_PC: 4,
  DEVICE_WINDOWS_PHONE: 5,
  DEVICE_WINDOWS_TABLET: 6,
  DEVICE_MAC: 7,
}, { name: 'E_DEVICE_TYPE' });

I then call my function (defined on ffi) like so:

this.client.setDeviceType(deviceTypeEnum.DEVICE_MAC);

and also tried:

this.client.setDeviceType(deviceTypeEnum.DEVICE_MAC.value);

On the ffi definition I’ve tried setting the parameter type as int and uint32. Unfortunately, this doesn’t seem to be setting the correct value in the C library. Can anyone see if I’m missing something? It’s probably something obvious but I’ve been bashing my head against this for a couple of days and thought I’d reach out for assistance. Thanks for any help you might be able to provide!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
goel-sunnycommented, Jun 14, 2017

no need of defining enum in your javascript code , but at the place of enum define an int variable , so at the time of passing of argument pass only an integer value to the dll , dll at backend will accept this .

0reactions
adraicommented, Apr 10, 2020

This thread has been closed since there has not been any recent activity. Please reopen if still needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

On an enum - `safer_ffi` User Guide
A C enum is a field-less enum, i.e., an enum that only has unit variants. ... enum Ordering { Less /* = 0...
Read more >
Accessing cffi enums - python - Stack Overflow
Use ffi.dlopen , and access the enum value by qualifying using the return value of the ffi.dlopen : >>> from cffi import FFI...
Read more >
C-style enums in FFI (and a proposal to lump in with unions)
C-style unsafe enums would be used to unsafely match C enum values, while the struct-membered variety would stand in for unions. What do...
Read more >
FFI::Platypus::Type::Enum - MetaCPAN
This type plugin is a helper for making enumerated types. It makes the most sense to use this when you have an enumerated...
Read more >
Ffi pack: enum values - Pack - SWI-Prolog
I must say that the more I use the ffi pack the more impressed I am :slight_smile: It is a great piece of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found