nervosnetwork / molecule

Another serialization system: minimalist and canonicalization.
MIT License
36 stars 23 forks source link

Fix: C code generator should provide `const uint8_t *` as 4th argument to `mol_union_builder_initialize` #69

Closed eval-exec closed 1 year ago

eval-exec commented 1 year ago

This PR fix a bug in C code generator, and added some test case.

eval-exec commented 1 year ago

I just added a test case for Union type:

union UnionC {
    Word,
    byte,
}

and the CI action failed on UnionC, because https://github.com/nervosnetwork/molecule/blob/d998dc4ba6d851c1e67d56917d7628d235150c65/bindings/c/include/molecule_builder.h#L87 want const uint8_t* on 4th argument, but the generated code provide a const uint8_t (*)[2] on 4th argument:

// generated C code
MOLECULE_API_DECORATOR const uint8_t MolDefault_Word[2] = {____, ____};

#define MolBuilder_UnionC_init(b) mol_union_builder_initialize(b, 8, 0, &MolDefault_Word, 2)

So, I think we need to force case 4th argument to const uint8_t * :

// generated C code
#define MolBuilder_UnionC_init(b) mol_union_builder_initialize(b, 8, 0, (const uint8_t *)&MolDefault_Word, 2)

@yangby-cryptape @driftluo @code-monad What do you think?

code-monad commented 1 year ago
(const uint8_t *)&MolDefault_Word

LGTM as a C code.