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

Enum classes #27

Open
projectitis opened this issue Aug 23, 2021 · 5 comments
Open

Enum classes #27

projectitis opened this issue Aug 23, 2021 · 5 comments

Comments

@projectitis
Copy link

Hi ftk,

What is your suggested method of handling an enum class?

// c++
enum class DisplayMode {
    Cover,
    Contain,
    LetterBox,
};

// js
myObject.displayMode = DisplayMode.Contain;

Should I convert my native c++ classes to use enums with explicit (integer) values, and then define the enum purely in JS, like this?

// c++
enum DisplayModeT {
    Cover = 1,
    Contain = 2,
    LetterBox = 3,
} DisplayMode;

// js
DisplayMode = {
  Cover: 1,
  Contain : 2,
  LetterBox : 3
};
Object.freeze(DisplayMode);

myObject.displayMode = DisplayMode.Contain;

Or is there a quickjspp way? :)

@projectitis
Copy link
Author

Although I do have it working with the 'freeze' workaround above, ES2020 supports static class properties, so I think the solution/question here is:

Is there a way to add a "static const" property using quickjspp?

// c++
class DisplayMode {
public:
    static const int Cover = 1;
    static const int Contain = 2;
    static const int LetterBox = 3;
}

module.class_<DisplayMode >( "DisplayMode " )
    .fun<&DisplayMode::Cover>( "Cover" )
    .fun<&DisplayMode::Contain>( "Contain" )
    .fun<&DisplayMode::LetterBox>( "LetterBox" );

/* js expected result
class DisplayMode {
    static Cover = 1;
    static Contain = 2;
    static LetterBox = 3;
}
*/

This results in a bunch of unresolved symbols when compiled. Am I doing it incorrectly, or are static properties not supported (yet).

@dodola
Copy link
Contributor

dodola commented Jun 15, 2022

@projectitis my solution

#define ENUM_DEF(type)                                          \
  template <>                                                   \
  struct js_traits<type> {                                      \
    static type unwrap(JSContext* ctx, JSValue v) noexcept {    \
      uint32_t t;                                               \
      JS_ToUint32(ctx, &t, v);                                  \
      return type((uint16_t)t);                                 \
    }                                                           \
                                                                \
    static JSValue wrap(JSContext* ctx, type opcode) noexcept { \
      return JS_NewUint32(ctx, opcode);                         \
    }                                                           \
  }
ENUM_DEF(MethodItemType);

@projectitis
Copy link
Author

How do you use it, @dodola?

@dodola

This comment was marked as outdated.

@dodola
Copy link
Contributor

dodola commented Jun 21, 2022

@projectitis simple enum support 0b72502

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