문제

Is it possible to require fay-compiled modules from other simple javascript files on the server side in nodejs? That would be just great. Maybe there is some option in compiler to produce commonjs-compatible modules?

도움이 되었습니까?

해결책

This is probably a bad idea, since it depends quite a bit on peculiarities of fay's generated code.

Note the following points:

  • Regardless of the name of the compiled module, fay instantiates into the variable main.
  • Under node.js, the return value from require is the module's modules.export (which initially is the same object as export -- but it doesn't necessarily stay that way).
  • A variable can be used before its scoping is declared with var. The variable referenced is the same. Its cares nothing for its source ordering, and everything for what has happened at runtime.
  • Fay by default (i.e. without --library) can instantiate an object and execute main.

Notably, this means that we can, within main, modify module.exports or exports to export the fay code. Of course, we must use the ffi, but it's a rather simple affair; the following, compiled without --library (which, yes, is mildly counterintuitive, and really does lend credence to the hypothesis that this is a nasty hack, doesn't it) work to some extent:

import FFI

main :: Fay ()
main = ffi "module.exports = main"

when require'd from node, the returned object is something to the effect of

{ 'Main$main': 
   { forced: true,
     value: { value: [Circular] } },
  _: [Function: Fay$$_],
  '$': [Function: Fay$$$],
  '$fayToJs': [Function: Fay$$fayToJs],
  '$jsToFay': [Function: Fay$$jsToFay] }

With a working knowledge of Fay's internal representations, it is then possible (though perhaps too much effort) to write a javascript wrapper for all the thunk-forcing and such.

(We could do more -- in fact, with a bit more ffi work, we could write all the bindings as ffi code. It would be mildly silly, though.)

다른 팁

It's possible to use fay code from javascript, but at the moment it's a bit verbose, you need to use fully qualified names and manually force function calls.

var m = new Main();
document.body.innerHTML = "The 10th fibonacci number is : " + m._(m.Main$fibN(9));

Everything is flat inside Main at the moment, we'd want to separate the compilation of each module so each can be output separately. (then we can also migrate to haskell-packages)

Then we need the possibility to output a wrapper that does forcing and type conversions for each module so you don't need to this manually when called from JavaScript.

Here are some related tickets: #279, 260

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top