Coding: Futures

In order to integrate smoothly with callback-based JavaScript APIs, Strands provides futures. A future can be used as an event handler that resumes execution when an event is received. Futures may be used for callback event handlers for any JavaScript event API.

For example, you can use a future to wait for a button click:
    function waitForButton() {
        // do some work

        // create our future
        var future = new Future();

        // attach our future to the button
        document.getElementById("myButton").onclick = future.fulfill;

        // wait for the button to be clicked
        future.result();

        // do more work
    }

JavaScript 1.7 Support

Strands can run natively in JavaScript environment at version 1.7 or higher such as Firefox 2.0+. However, to use the native support you must place a yield operator in front of function calls that may suspend, and you must wrap your suspendable functions with a strand call. These functions can still be compiled and executed in non-1.7 enviroments (like IE). So a function that is designed to be able to run natively in JS 1.7 with coroutine support would look like:

    waitForButton=strand(function () {
        // do some work
        // wait for the button to be clicked
        yield future.result();
    })

Futures are discussed in greater detail in the example and in the API reference.

Debugging

Strands maintains line numbering and optionally original source code to maximize debuggability, however Strands utilizes with blocks to emulate frame scope. Some debuggers properly handle with blocks by showing all the properties of the with object as variables and some do not. In particular, Firebug does not do this properly. To debug your strands code, simply add a watch variable _scope. The _scope variable represents the current scope, and the properties of it represent the current variables in the function. Besides this quirk, debugging Strands compiled code can be done just like normal JavaScript code.

Direct Calls

Strands supports a special syntax to make direct calls that do not check for suspension, that is when a callee suspends, the calling function will not suspend. While it is not functionally necessary to ever use direct calls (since you can create threads i.e. prevent suspension using the Future constructor), and using direct calls is not recommended for normal use, you can use direct calls to improve performance in situations where either the function is known to never suspend or you don't want the calling function to suspend when the called function suspends. Using a direct call will simply indicate to the compiler to not add the suspension check, which will result in smaller, faster calls. This can be useful for code that might execute with massive repetition and utilize large amounts of time (and be very performance sensitive). The syntax for direct calls is to add "->" between the function name and the argument parentheses:
	func->(args)
			

Strands Global Variables

Strands utilizes the following global variable names:

  • strands
  • strand
  • Future
  • _frm
  • _S