How to use :meta with :flash.property

I’m working on a Haxe library that is being compiled to .swc so that it may be used with ActionScript 3.0 and Adobe AIR. I want to add AS3 metadata to a getter in my .swc file.

I understand that if I add @flash.property to the property, the getter and setter will become native in AS3. In other words, this Haxe code:

private var value:Float = 0.0;

@:flash.property
public var value(get, set):Float;

private function get_value():Float {
	return this._value;
}

private function set_value(value:Float):Float {
	if(this._value == value) {
		return this._value;
	}
	this._value = value;
	this.dispatchEvent(new Event(Event.CHANGE));
	return this._value;
}

Generates something resembling this in the AS3 .swc (I know that it’s probably a bit more complicated than that, but that’s the gist):

private var _value:Number = 0.0;

public function get value():Number {
	return this._value;
}

public function set value(value:Number):void {
	if(this._value == value) {
		return this._value;
	}
	this._value = value;
	this.dispatchEvent(new Event(Event.CHANGE));
}

I would like to add some AS3 metadata to the getter.

Previously, I’ve successfully added @:meta to a class and verified that it was included in the .swc file. However, I can’t seem to get it to work with getters and setters. I’ve tried two things.

  1. On the property definition:
@:meta(Bindable("change"))
@:flash.property
public var value(get, set):Float;
  1. On the getter definition:
@:meta(Bindable("change"))
private function get_value():Float {
	return this._value;
}

Neither seems to work, though. Is there another location to add @:meta to a property that I’m missing?

I’ve also tried using the older @:getter and @:setter instead of @:flash.property, but that seems to result in couple of different issues, depending on how I code it up.

  1. I either get strange runtime exceptions like this, when I run it in Adobe AIR:

VerifyError: Error #1053: Illegal override of MyClass in com.example.MyClass.

  1. Or trying to use my class in AS3 won’t compile because I get this error:

Ambiguous reference to value

I also seem to recall that I couldn’t properly implement getters/setters defined on Haxe interfaces with AS3 classes without using @:flash.property because other Haxe code would assume that get_ and set_ functions would always be available unless @:flash.property were specified in the interface. Asking AS3 developers to add extra methods to their classes that implement an interface in order to make Haxe happy is not ideal here, and I’d like to avoid it.

So I don’t think that I can make this work without @:flash.property.

Any ideas?

Just adding some notes, in case I ever get brave enough to try to modify and build the Haxe compiler. Ideally, I’d like to submit a pull request to fix this limitation. I don’t know OCaml at all, but I tried my best to understand the code that generates SWF.

I may be wrong, but I think that the native getter/setter for @:flash.property is generated here in generate_prop:

I think that the line(s) with hlf_metas = None; is where the AS3 metadata is defined. Obviously, there is no AS3 metadata now, but I think it can be copied from the field by calling extract_meta, which is defined here:

Here’s the behavior I want to implement: If it’s read-only or read/write, the AS3 metadata should be added to the getter. If it’s write-only, it should be added to the setter. Never both.

Maybe I’ll come back to this at some point.

1 Like