bytecodealliance/jco

Implement flags as bigint integers

Opened this issue · 2 comments

This one comes out of discussion with @dbaeumer on the base-level type implementations.

Currently we implement flags as an object of bools, the suggestion would be switch to implementing all flags as BigInt values.

For users to be able to interpret and create flags, they would need to explicitly import from the flag type itself in JavaScript, something like:

import { MyFlags, fnReturningFlags, fnTakingFlags } from 'lib';

const flagValue = fnReturningFlags();
typeof flagValue === 'bigint'; // true
if (flagValue & MyFlags.ValueA) {
  // we have ValueA true
}

fnTakingFlags(MyFlags.ValueA | MyFlags.ValueB);

BigInts have full support for binary operations in JS, so the above works out okay. In addition BigInts have unlimited length so will be fully compatible with arbitrary flags.

There would be a slight savings over allocating full JS objects for all values, but the major benefit would be simplicity from a user interface perspective, where creating bags of bools can feel unintuitive at the moment (especially when it is currently ambiguous that falsy values are optional in inputs).

I'm bringing this up now, because if made it would be the last major breaking change in jco before we stabilize preview2, so seems a good time to think about making this change now.

I like this idea. I found bags of bools a pretty odd implementation choice.

Note that #235 is a prerequisite for this work.