fabmax / physx-js-webidl

Javascript WASM bindings for Nvidia PhysX
MIT License
117 stars 27 forks source link

Flag/Enums are verbose #5

Closed lucas-jones closed 11 months ago

lucas-jones commented 3 years ago

From what it looks like, the only way to access the flag enums is via

PhysX._emscripten_enum_PxBaseFlagEnum_eOWNS_MEMORY()

I think they should be more like PhysX.PxBaseFlagEnum.eOWNS_MEMORY

fabmax commented 3 years ago

I completely agree. Unfortunately, this is caused by the way the emscripten WebIDL generator generates the enum accessors

As far as I know, the only way to achieve the pattern would be to hand-write additional appropriately named C++ classes which provide the enum values as integer constants, which would be very fiddely and error-prone.

I opened an issue regarding this a while ago. But so far nothing seems to have changed.

lucas-jones commented 2 years ago
const fixEnums = () => {
    const enums = Object.keys(PhysX)
        .filter(key => {
            return key.includes('_emscripten_enum_');
        })
        .map(enumString => {
            const split = enumString.split('_emscripten_enum_')[1].split('();')[0].split('_e');
            return { enumName: split[0], entryName: split[1], emscript: enumString };
        });

    for (const enumEntry of enums) {
        if (!PhysX[enumEntry.enumName]) {
            PhysX[enumEntry.enumName] = {};
        }

        PhysX[enumEntry.enumName][enumEntry.entryName] = PhysX[enumEntry.emscript]();
    }
};

A little hack I use to fix the enums in JS