[racket] cool things in Whalesong, part 2

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Tue Mar 6 14:01:02 EST 2012

On Tue, Mar 6, 2012 at 1:22 PM, John Clements <clements at brinckerhoff.org> wrote:
> On Mar 5, 2012, at 1:17 PM, Danny Yoo wrote:
>> Here's another example that's starting to work, though it isn't flashy yet.
>>    http://hashcollision.org/tmp/js-get-message/js-get-message-parent.html
> Can you be more explicit about what's cool here? I have a sense that it has to do with JS interacting with compiled racket, and I agree that that's very cool, but I could use a bit more hand-holding :).

Yes; of course.  I'm sorry, I get so caught up sometimes!

The example is demonstrating JS interacting with Racket, particularly
to interact between JS and World programs.  I will be making the world
APIs in Whalesong extensible, so that the set of handlers like on-key,
on-tick, ... etc can be extended by library developers.  The key point
is that I don't (and can't) know all event types out there in the
browser. With the appropriate FFI, I should allow library developers
to add in `on-tilt`, `on-touch`, or any of the other events that the
browser is providing, and without needing to touch the core of the
world implementation.

This means I can (and probably should) factor out behavior that used
to be hardcoded in my world implementation (like on-key and on-mouse)
and push these into libraries.  As a demonstration, I'm doing this in
the context of my web-world library, with an example that listens on
the `postMessage` event.

The FFI I'm coding for Whalesong will let me connect to JavaScript
APIs in two ways: (1) in the world event-driven style, (2) in
synchronous style.  I already had a few of these done by hand.  The
FFI mechanism should make it easier for library developers too.

For example, `sleep` looks like:

    (define raw-sleep
       "function(success, fail, n) { setTimeout(success, n); }"))
    (define (sleep n)
      (unless (real? n)
        (raise-type-error 'sleep "real" n))
      (void (raw-sleep (inexact->exact (floor (* n 1000)))))


    (displayln "hello")
    (sleep 5)
    (displayln "world")

So basically, I'm trying to get Whalesong and JS to interact with each
other more nicely.   This all just started working a day ago, so I got
excited.  :)

Posted on the users mailing list.