$bind, not always present into third party js libraries, make valid haXe code crash horribly..

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

$bind, not always present into third party js libraries, make valid haXe code crash horribly..

sledorze
Hi!

I was hacking some nodeJs code in haXe and wanted to show a friend how you would get terminaison with haXe type system, bringing confidence into error handling of the code produced with it.
The experiment was just fun until it just failed miserably.. the server simply did not answered but just returned a very strange error about serialization.. (!?)

I've finally nailed down the issue to a problem with extern libraries.
Third party JS libraries (like mongodb-native) does not always provides $bind to their functions.
It makes somewhat difficult errors to track down for users writting perfectly normal haxe code.

Here's an example of the problem (async nodeJs code):

Works:
    (function (cb) context.mongoDb.collection("Avatars", cb))
    .flatMap(function (collection, cb) collection.find(selector, cb))
    .flatMap(function (cursor, cb) cursor.toArray(cb))
    .map(function (avatars : Array<Avatar>) {
      avatars.cleanIds();
      return Right(avatars);
    })(cb);

Fails:
    (callback(context.mongoDb.collection, "Avatars")) // <----- this has changed
    .flatMap(function (collection, cb) collection.find(selector, cb))
    .flatMap(function (cursor, cb) cursor.toArray(cb))
    .map(function (avatars : Array<Avatar>) {
      avatars.cleanIds();
      return Right(avatars);
    })(cb);

Here the reported error:

DEBUG: node.js:134
11:04:11 web.1     |         throw e; // process.nextTick error, or 'error' event on first tick
11:04:11 web.1     |         ^
11:04:11 web.1     | Cannot serialize function

and no more information.. to go back to the real problem..

which is (generated code):
($_=this.context.mongoDb,$_.collection.$bind($_))   // <-- $bind does not exists!


I think that it should not be the responsability of libraries users to take care of that but of library mainteners (coz it's a time killer and just appear as "haxe does not work with extern JS libraries").

A solution would be to indicate in externs that $bind can't be used and issue a different code generation or a compiler error.

Nicolas, do you think of something that can be worked out?

Thanks,
Stephane
Reply | Threaded
Open this post in threaded view
|

Re: $bind, not always present into third party js libraries, make valid haXe code crash horribly..

Nicolas Cannasse
Le 07/11/2011 11:20, sledorze a écrit :
[...]
> I think that it should not be the responsability of libraries users to take
> care of that but of library mainteners (coz it's a time killer and just
> appear as "haxe does not work with extern JS libraries").
>
> A solution would be to indicate in externs that $bind can't be used and
> issue a different code generation or a compiler error.
>
> Nicolas, do you think of something that can be worked out?

I'm not sure I understand the actual issue.
$bind is added to the Function prototype in js/Boot.hx, so it should be
available for all JS code, including third party libraries.

Best,
Nicolas


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

Re: $bind, not always present into third party js libraries, make valid haXe code crash horribly..

sledorze
Hmmmm.. perhaps we're closer to the answer then.. (and a bright future)

putting that in my main:

import js.Boot;

class Main {

    static function main() {

    trace("DOIT!");
    function display(x) trace(Std.string(x));
    untyped __js__("
      for (el in Function.prototype) {
        display(Function.prototype[el]);
      }    
    ");
    trace("DONE!");

displays:

DOIT!
DONE!

And I tried to put some traces and surrounding try catch in the __init but it does not looks like it is executed on NodeJs (executed in the browser however).

I'm not very familiar with __init but looks like there's nothing special to do..

Any ideas to test?

Reply | Threaded
Open this post in threaded view
|

Re: $bind, not always present into third party js libraries, make valid haXe code crash horribly..

sledorze
In reply to this post by Nicolas Cannasse
the related js.Boot code is not included into the final javascript file.
Reply | Threaded
Open this post in threaded view
|

Re: $bind, not always present into third party js libraries, make valid haXe code crash horribly..

sledorze
In reply to this post by Nicolas Cannasse
got it!

nodeJs Library has its own js.Boot class (this makes sense) which lacks the Function prototype extension.
Adding it solves the issue :)
I'll ask Richie to update it!

Thanks for pointing me in the right direction!
Stephane
Reply | Threaded
Open this post in threaded view
|

Re: $bind, not always present into third party js libraries, make valid haXe code crash horribly..

blackdog
On 11/07/2011 12:38 PM, sledorze wrote:

> got it!
>
> nodeJs Library has its own js.Boot class (this makes sense) which lacks the
> Function prototype extension.
> Adding it solves the issue :)
> I'll ask Richie to update it!
>
> Thanks for pointing me in the right direction!
> Stephane
>
>
> --
> View this message in context: http://haxe.1354130.n2.nabble.com/bind-not-always-present-into-third-party-js-libraries-make-valid-haXe-code-crash-horribly-tp6969844p6970812.html
> Sent from the Haxe mailing list archive at Nabble.com.
>
Hi Stephane will look at updating today. I need to work on a few things
for the latest release of Node anyhow.

Thanks

R

--
Simplicity is the ultimate sophistication. ~ Leonardo da Vinci


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

Re: $bind, not always present into third party js libraries, make valid haXe code crash horribly..

sledorze
You just have to accept the pull request I've sent you.. :)