ecmascript-types icon indicating copy to clipboard operation
ecmascript-types copied to clipboard

Cover the use of typed objects with composition and inheritance

Open sirisian opened this issue 8 years ago • 2 comments

The spec needs concrete examples of classes with composition and inheritance.

There's numerous details that might need to be explained about the size of objects with composition and inheritance with types. Examples will help people to think about them.

sirisian avatar Jun 29 '17 18:06 sirisian

I think one of the biggest things to define is member ordering for data. Each derived class would simply be placed after the parent in the inheritance tree. The alignment and memory ordering is only defined if align or offset are used. If they aren't then the interpreter/JIT is free to do whatever.

class A
{
    a:uint8;
}
class B extends A
{
    b:uint8;
}
// So the memory layout would be the same as:
class AB
{
    a:uint8;
    b:uint8;
}

This has to work with #11 and the align and offset syntax. It makes sense to me that offset would be from the a relative position in the structure.

class A
{
    a1:uint8;
    @offset(3)
    a2:uint8;
}
class B extends A
{
    @offset(2)
    b:uint8;
}
// So the memory layout would be the same as:
class AB
{
    a1:uint8; // byte 0
    @offset(3)
    a2:uint8; // byte 3
    @offset(6) // 2 + 4 bytes
    b:uint8;
}

This design keeps the base and derived memory layouts relatively distinct and either can use or not use alignment and offset. Kind of wondering though if maybe just making alignment and offset undefined when inheritance is used might be better for the interpreter/JIT. That is it's such a niche use case that no one would care. Most file and network type usage would ultimately not use inheritance I'm guessing.

sirisian avatar Jan 11 '18 05:01 sirisian

https://github.com/sirisian/ecmascript-types#member-memory-alignment-and-offset

Added more details. There's still some details to refine which are listed at the bottom of that section. Might need more examples.

sirisian avatar Jan 25 '19 05:01 sirisian