Having trouble with JS externs

54 views
Skip to first unread message

Drakim

unread,
May 12, 2014, 9:17:55 AM5/12/14
to haxe...@googlegroups.com
I'm trying to create JS externs to use Gibberish in my HTML5 game.

Gibberish has a rather unorthodox structure, and I'm having trouble properly reflecting that with my extern definitions.

Here is the basic hello world example in JS:
Gibberish.init();  
a = new Gibberish.Sine().connect();

The Gibberish class here is purely static, and it contains a static reference to the class Sine, which is instanced, and then the connect method of Sine is called.

Here is my attempt at building an extern for this:
extern class Gibberish {
    static public function init(?bufferSize:Int):Void;
    static public var Sine:Sine;
}

extern class Sine {
    public function new();
    public function connect():Sine;
}

Which does compile my haxe code
Gibberish.init(); var a = new Gibberish.Sine().connect();

but the JS output is wrong:
Gibberish.init();
var a = new Sine().connect();

This gives me an error that Sine is not defined, since it doesn't exist in the global scope, only under Gibberish.Sine.

So, what can I do to avoid this problem? I really don't want to port the entire library to Haxe, or heavily restructure the JS code to play well with the externs.

Andy Li

unread,
May 12, 2014, 9:30:25 AM5/12/14
to haxe...@googlegroups.com
Here is one solution that use @:native metadata to specify the runtime reference of the class:
extern class Gibberish {
    static public function init(?bufferSize:Int):Void;
}

@:native("Gibberish.Sine")
extern class Sine {
    public function new();
    public function connect():Sine;
}

class Test {
static function main()
{
Gibberish.init();
var a = new Sine().connect();
}
}

It will produce:
(function () { "use strict";
var Test = function() { };
Test.main = function() {
Gibberish.init();
var a = new Gibberish.Sine().connect();
};
Test.main();
})();

Best,
Andy


--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Drakim

unread,
May 12, 2014, 9:37:54 AM5/12/14
to haxe...@googlegroups.com
Supreme! That was exactly what I needed.

Many thanks.

Cambiata

unread,
May 12, 2014, 2:50:12 PM5/12/14
to haxe...@googlegroups.com
Cool library!
+1 for your externilization work, Drakim!

J

Reply all
Reply to author
Forward
0 new messages