[Haskell-cafe] [ANNAUNCE] ghcjs-0.1.0 Haskell to Javascript compiler

Victor Nazarov asviraspossible at gmail.com
Wed Oct 27 10:20:07 EDT 2010


On Wed, Oct 27, 2010 at 4:30 PM, Martijn Schrage <martijn at oblomov.com> wrote:
> On 21-10-10 01:01, Victor Nazarov wrote:
>>
>> I've been working on this for some month and I think now I'm ready to
>> share the results.
>
> Great stuff! I've been looking for something like this for a long time.
>
> If you add "|| transport.status == 0" to line 90 of examples/rts-common.js,
> it also works on a local file system.
>

Thank you, I'll fix it.

> I played around with it a bit to see how easy it was to call JavaScript from
> Haskell, and it turned out to be straightforward. With a little guessing, I
> constructed a JavaScript representation of a thunk, which evaluates its
> string argument as a JavaScript expression and returns the resulting string
> to Haskell. This thunk can be passed to the JavaScript-compiled Haskell
> function. To pass it around in Haskell and force its evaluation, I
> constructed a simple monad.
>

Very cool. I'll incorporate your changes, If you don't mind.
However, I have some minor remarks.
You shouldn't override hscall function, or you may break partial
application implementation. And you shouldn't overide properties of
evalFn, I wonder that this doesn't break your example...

var evalFn = new $hs.Func(1);
evalFn.evaluate(arg) = function(arg) {
  var argStr = $hs.fromHaskellString(arg);
  var res = eval(argStr);
  return $hs.toHaskellString(arg); // This function should be added to
$hs object/namespace
}

> Now, on your web page, you can do something like:
>
> <input type="text" onkeyup="execHaskell('validate')" id="inputField"/>
>
> and in a Haskell module:
>
> validate :: JS ()
> validate =
>  do { inputValue <- eval "document.getElementById('inputField').value"
>    ; exec $ "document.getElementById('inputField').style.backgroundColor="++
>             color inputValue
>    }
>  where color str = if and (map isDigit str) then "'white'" else "'red'"
>

I think we should do something like this:

data JsObject = ... -- Should be made abstract

and

eval :: String -> [JsObject] -> JS JsObject

So we can pass around javascript-objects in haskell program and bind
them back into javascript calls.
And use some conversion functions in case when we need data:

jsObjectToString :: JsObject -> JS String
jsObjectToInt :: JsObject -> JS Int

-- 
Victor Nazarov


More information about the Haskell-Cafe mailing list