AssemblyScript - Linear Nested Class Layout

192 views Asked by At

I'm working on a linear data layout where components are alongside each other in memory. Things were going ok until I realized I don't have a way for making offsetof and changetype calls when dealing with nested classes.

For instance, this works as intended:


class Vec2{
    x:u8
    y:u8
}
const size = offsetof<Vec2>() // 2 -- ok
const ptr = heap.alloc(size)
changeType<Vec2>(ptr).x = 7 // memory = [7,0] -- ok

Naturally this approach fails when nesting classes

class Player{
    position:Vec2
    health:u8
}
const size = offsetof<Player>() //5 -- want 3, position is a pointer
const ptr = heap.alloc(size)
changeType<Player>(ptr).position.x = 7 //[0,0,0,0,0] -- want [7,0,0], instead accidentally changed pointer 0

The goal is for the memory layout to look like this:

| Player 1 | Player 2 | ...
| x y z h  | x y z h  |

Ideally I'd love to be able to create 'value-type' fields, or if this isnt a thing, are there alternative approaches?

I'm hoping to avoid extensive boilerplate whenever writing a new component, ie manual size calculation and doing a changetype for each field at its offset etc.

1

There are 1 answers

0
chantey On

In case anybody is interested I'll post my current solution here. The implementation is a little messy but is certainly automatable using custom scripts or compiler transforms.

Goal: Create a linear proxy for the following class so that the main function behaves as expected:

class Foo {
    position: Vec2
    health: u8
}
export function main(): Info {
    
    const ptr = heap.alloc(FooProxy.size)
    const foo = changetype<FooProxy>(ptr)

    foo.health = 3
    foo.position.x = 9
    foo.position.y = 10
}

Solution: calculate offsets and alignments for each field.


class TypeMetadataBase{
    get align():u32{return 0}
    get offset():u32{return 0}
}
class TypeMetadata<T> extends TypeMetadataBase{
    get align():u32{return alignof<T>()}
    get offset():u32{return offsetof<T>()}

    constructor(){
        super()
        if(this.offset == 0)
        throw new Error('offset shouldnt be zero, for primitive types use PrimitiveMetadata')
    }
};
class PrimitiveMetadata<T> extends TypeMetadataBase{
    get align():u32{return sizeof<T>()}
    get offset():u32{return sizeof<T>()}
};

class LinearSchema{

    metadatas:StaticArray<TypeMetadataBase>
    size:u32
    offsets:StaticArray<u32>
    constructor(metadatas:StaticArray<TypeMetadataBase>){
        let align:u32 = 0
        const offsets = new StaticArray<u32>(metadatas.length)
        for (let i = 0; i < metadatas.length; i++){
            if(metadatas[i].align !== 0)
                while(align % metadatas[i].align !== 0)
                    align++
            offsets[i] = align
            align += metadatas[i].offset
        }
        this.offsets = offsets
        this.metadatas = metadatas
        this.size = align
    }
}


class Vec2 {
    x: u8
    y: u8
}

class FooSchema extends LinearSchema{
    constructor(){
        super([
            new PrimitiveMetadata<u8>(),
            new TypeMetadata<Vec2>(),
        ])
    }

}
const schema = new FooSchema()

class FooProxy{
    static get size():u32{return schema.size}
    set health(value:u8){store<u8>(changetype<usize>(this) + schema.offsets[0],value)}
    get health():u8{return load<u8>(changetype<usize>(this) + schema.offsets[0])}
    get position():Vec2{return changetype<Vec2>(changetype<usize>(this) + schema.offsets[1])}
}