Overriding native getters and setters on flash?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Overriding native getters and setters on flash?

Drakim
So, I was googling around for this and what I saw mostly was posts saying "maybe in the future" two years back.

I'm in the need of preventing the memory allocation that happens when you increase flash.ByteArray.length to something. I tried using the Reflect API to set it because I know that it ignores setters and getters that we make ourselves, but no luck.

From what I understand this is possible in AS3 so it should be physically possible to do.

Any suggestions? I'd be happy with any way that helps me avoid triggering the setter. I don't actually need to replace it with anything else.
Reply | Threaded
Open this post in threaded view
|

Re: Overriding native getters and setters on flash?

Drakim
Doh, right after I posted I somehow got it to work with this:

  @:setter(length)
  function setLength(_value:UInt):Void {
    length=_value;
  }

However, it's looping around into a stack overflow because the length=_value triggers the setter again (which doesn't happen to me in my own setters). Any idea on how to avoid this?
Reply | Threaded
Open this post in threaded view
|

Re: Overriding native getters and setters on flash?

Nicolas Cannasse
Le 30/10/2011 19:24, Drakim a écrit :

> Doh, right after I posted I somehow got it to work with this:
>
>    @:setter(length)
>    function setLength(_value:UInt):Void {
>      length=_value;
>    }
>
> However, it's looping around into a stack overflow because the length=_value
> triggers the setter again (which doesn't happen to me in my own setters).
> Any idea on how to avoid this?

You would have to call the super.length setter, which is not possible in
haXe. But in that case you would still trigger the Bytearray resize
anyway. In AS3 there is no actual length "field", only getter and setter
methods so you cannot only modify the field itself.

Best,
Nicolas

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Overriding native getters and setters on flash?

Drakim
In reply to this post by Drakim
Double Doh, I can't seem to get anything to work now

I am attempting to do this trick with ByteArray.writeBytes. I want to clear out the whole ByteArray to a certain value, and it's certainly achievable with writeBytes. However, writeBytes only takes another ByteArray as a source parameter, it doesn't allow you to specify a single value to write. This is cumbersome because if you are gonna change a huge ByteArray you need another just as huge ByteArray as the source, filled with nothing but that one value you want. This is quite the waste of memory.

So my idea was to extend the source ByteArray, and replace it's read methods with something that statically returned a single value. I thought I had this working at first (only bothered by the .length issue mentioned above) but now I can't get my override methods to trigger anymore and I'm starting to wonder if I ever got them to work in the first place.

So, to sum up, is it possible to extend a class, override a method, and pass an instance of that that class as a parameter to another native class that uses the overriden method, causing your custom method to be used instead?