physx-js-webidl icon indicating copy to clipboard operation
physx-js-webidl copied to clipboard

Flag/Enums are verbose

Open lucas-jones opened this issue 4 years ago • 2 comments

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

lucas-jones avatar Jun 09 '21 17:06 lucas-jones

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.

fabmax avatar Jun 09 '21 17:06 fabmax

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

lucas-jones avatar Jan 25 '22 18:01 lucas-jones