Bluish Coder

Programming Languages, Martials Arts and Computers. The Weblog of Chris Double.


Factor to Javascript compiler updates

I've done some minor updates to my compiler from a subset of Factor to Javascript. The updates are now running on the fjsc test page and available from my repository:

darcs get

Javascript FFI

An FFI interface to Javascript modelled after Factor's 'alien-invoke'. This allows calling any method of a Javascript object. 'alien-invoke' takes three arguments on the stack. The first is an array of strings defining the type of the object returned by the Javascript call. The second is the name of the method. The third is an array of strings defining the type of the objects expected as parameters to the method. The resulting code generated by 'alien-invoke' expects the object to call the method on to be on the top of the stack. Here are some examples that work on the test page:

: my-alert ( string -- )
  #! Display an alert box with the given string.
  #! The 'window' word returns the 'window' javascript object
  window { } "alert" { "string" } alien-invoke

"hello world!" my-alert

: my-substr ( end start string -- string )
  #! Return the substring of the given string
  { "string" } "String.prototype.substr" { "number" "number" } alien-invoke ;

6 1 "Hello World" my-substr my-alert

The FFI is still a work in progress. As I write wrappers for DOM routines I expect it to change.

Parser Changes

The parser now supports escaping words with '\ word', stack effects and comments:

1 2 \ + execute
: square ( a -- b )
  #! Square the argument
  dup * ;

Some improvements to parser combinators have been made to speed up the parser as well. The number of backtracking possibilities has been dramatically reduced.

Ajax Support

Simple Ajax support has been added in the form of 'http-get' and 'run-file'. 'http-get' takes an URL on the stack and returns the data at that URL as a string. 'run-file' will compile and evaluate the Factor code at the given URL. Note that due to Ajax limitations these can only be URL's on the local server. I may add proxy capability to work around this in the future:

"/responder/fjsc-resources/bootstrap.factor" http-get
"/responder/fjsc-resources/bootstrap.factor" run-file

The limitations with the Ajax support also include the fact that they are run asyncronously. So they aren't much good in running programs, more for interactive use at the REPL. I'll either make this synchronous in the future or add continuations to the compiler.

Bootstrap Factor file

A file located at /responder/fjsc-resources/bootstrap.factor can contain Factor code that is compiled and run when the 'bootstrap' word is run. From the reply run 'bootstrap' and it will execute the code.


I plan to add (in no particular order):

  • Continuations
  • The ability to compile Factor quotations from the server into Javascript without having to have the Factor code in strings first. This way any code on the server can be compiled to Javascript and removes the need to manually craft Javascript to send to the client in web applications.
  • Improve the code generation.
  • Wrap various DOM routines, etc
  • Write a better REPL in Factor itself


This site is accessable over tor as hidden service mh7mkfvezts5j6yu.onion, or Freenet using key: