[haXe] best strategy to port an existing js library

Ian bcposn802 at sneakemail.com
Fri Dec 7 17:30:41 CET 2007


wow. that wasn't at all what I was thinking, but I prefer this.  you 
lose type checking, but you eliminate the wrapper.  my thought was:

// js library (same)
function niceAlert(txt) {
  alert("### " + txt + " ###");
}

// js wrapper
function JsWrapper() {
  this.callNiceAlert = niceAlert;
}

// haxe code
extern class JsWrapper {
    public function new():Void;
    public function callNiceAlert():Void;
}
class Main
{
    public static function main() {
      var wrapper = new JsWrapper();
      wrapper.callNiceAlert("hello to you");
    }
}

-ianxm

Franco Ponticelli franco.ponticelli-at-gmail.com |haxe| wrote:
> Yes, I think you are correct. The only two options are depicted in the 
> example below... any other idea?
>
> // js library
> function niceAlert(txt) {
>   alert("### " + txt + " ###");
> }
>
> // haxe code
> class Main {
>     public static function main() {
>     Main.msg("hello to you");
>     Main.msg2("goodbye");
>     }
>
>   public static var msg : String -> Void = untyped niceAlert;
>
>   public static function msg2(s : String) {
>     untyped niceAlert(s);
>   }
> }
>
> Franco.
>
> On Dec 7, 2007 3:42 PM, Ian <bcposn802 at sneakemail.com 
> <mailto:bcposn802 at sneakemail.com>> wrote:
>
>     how would you call a javascript function from haXe?  It seems like
>     extern only works with classes.
>     I guess I could wrap the javascript functions in a javascript class.
>
>     -ianxm
>
>     Franco Ponticelli franco.ponticelli-at-gmail.com
>     <http://franco.ponticelli-at-gmail.com> |haxe| wrote:
>     > There is no loader and I doubt you need one since you can
>     include your
>     > JS library just before the haXe generated file.
>     > About wrapping you can use both extern or typedef depending on what
>     > you are implementing.
>     > In your case the extern could be the right solution.
>     >
>     > extern HelloClass {
>     >   public function new() : Void;
>     >   public function showText() : String;
>     > }
>     >
>     > Pay attention to the case of fields that in haXe must start with a
>     > lower case letter. This can be origin to incompabilities.
>     >
>     > Regards,
>     > Franco.
>     >
>     > On Dec 7, 2007 11:00 AM, picnic < bitvolt at gmail.com
>     <mailto:bitvolt at gmail.com>
>     > <mailto:bitvolt at gmail.com <mailto:bitvolt at gmail.com>>> wrote:
>     >
>     >     hi
>     >     what is best strategy to port an existing js library? How is
>     >     encapsulation done, e.g.  for an js class in a separate file:
>     >
>     >     -------------HelloClass.js------------------
>     >     function HelloClass(){
>     >        var m_text = "Hello World";
>     >        this.ShowText = DisplayText;
>     >       function DisplayText(){
>     >            alert( m_text );
>     >            return;}}
>     >     --------------------------------------------------
>     >
>     >     i guess there should be some kind of a loader function in
>     haxe.js
>     >
>     >     thanks
>     >     picnic
>     >
>     >     --
>     >     haXe - an open source web programming language
>     >     http://haxe.org
>     >
>     >
>
>
>
>     --
>     haXe - an open source web programming language
>     http://haxe.org
>
>





More information about the Haxe mailing list