Skip to content

Inheriting classes won't call constructor of their super class #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kyasbal opened this issue Jul 25, 2018 · 4 comments
Closed

Inheriting classes won't call constructor of their super class #187

kyasbal opened this issue Jul 25, 2018 · 4 comments
Labels

Comments

@kyasbal
Copy link

kyasbal commented Jul 25, 2018

I have written this bunch of code to operate texture passed from WebGL.

export class U8_4 {
  constructor(public x: u8, public y: u8, public z: u8, public w: u8) {}
}

export class Texture {
  public baseAddress: u32 = 0

  public width: u16 = 0

  public height: u16 = 0

  public ch: u8 = 0

  constructor(width: u16, height: u16, ch: u8) {
    this.baseAddress = memory.allocate(<u32>(width * height * <u16>ch))
    this.width = width
    this.height = height
    this.ch = ch
    //*************************************************
    //There are problems here
    //*************************************************
    console.logi(123456) // This logging is not called
  }

  public getAddressAt(i: u16, j: u16): u32 {
    return (<u32>i * <u32>this.width + <u32>j) * <u32>this.ch
  }
}

export class Texture4CH extends Texture {
  constructor(width: u16, height: u16) {
    super(width, height, 4)
    console.logi(width)
    console.logi(height)
  }

  public getAt(i: u16, j: u16): U8_4 {
    let ba: u32 = this.getAddressAt(i, j)
    let x: u8 = load<u8>(ba, 0)
    let y: u8 = load<u8>(ba, 1)
    let z: u8 = load<u8>(ba, 2)
    let w: u8 = load<u8>(ba, 3)
    return new U8_4(x, y, z, w)
  }

  public setAt(i: u16, j: u16, col: U8_4): void {
    let ba: u32 = this.getAddressAt(i, j)
    store<u8>(ba, col.x, 0)
    store<u8>(ba, col.y, 1)
    store<u8>(ba, col.z, 2)
    store<u8>(ba, col.w, 3)
  }
}

As you can see, I have inherited Texture class from Texture4CH. When I instanciate Texture4CH class, the super notation super(width, height, 4) seems not working as intended.
Is this intended limitation of assemblyscript?

@dcodeIO
Copy link
Member

dcodeIO commented Jul 25, 2018

Class support is still relatively basic and when looking at the generated text format it appears that the super call is compiled incorrectly. Seems that its arguments are compiled, but just dropped instead of calling anything with them.

 (func $tests/compiler/empty/Texture4CH#constructor (; 6 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
  (local $3 i32)
  (drop
   (get_local $0) (; this ;)
  )
  (drop
   (get_local $1) (; width ;)
  )
  (drop
   (get_local $2) (; height ;)
  )
  (drop
   (i32.const 4) (; 4 ;)
  )
  ...

@dcodeIO dcodeIO added the bug label Jul 25, 2018
@trusktr
Copy link
Member

trusktr commented Dec 11, 2018

I haven't tried yet, but can we work around this by using a method instead of relying on constructors? F.e. an init method that you just have to remember to call, const foo = new Foo(); foo.init();.

@MaxGraey
Copy link
Member

MaxGraey commented Feb 7, 2019

@dcodeIO could we close this as well?

@dcodeIO dcodeIO closed this as completed Feb 7, 2019
@dcodeIO
Copy link
Member

dcodeIO commented Feb 7, 2019

Yeah, should be fixed meanwhile. Feel free to reopen if there are still any issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants