<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>svrg.net feed</title>
<language>en</language>
<link>http://svrg.net/rss.xml</link>
<description>
activities from svrg.net
</description>
<pubDate>Fri, 30 Apr 2010 21:13:42 CEST</pubDate>
<lastBuildDate>Fri, 30 Apr 2010 21:13:42 CEST</lastBuildDate>
<item>
<title>CL-Javascript 0.10.05</title>
<pubDate>Fri, 30 Apr 2010 21:13:33 CEST</pubDate>
<link>http://svrg.net/CL-Javascript%200.10.05.html</link>
<guid isPermaLink="true">http://svrg.net/CL-Javascript%200.10.05.html</guid>
<description>
<![CDATA[
<p>CL-Javascript 0.10.05
</p>
<p>
With version 0.10.05 we announce the first public release of
CL-Javascript, translator from Javascript to Common Lisp.
</p>

<div id="outline-container-1" class="outline-2">
<h2 id="sec-1">Project overview </h2>
<div class="outline-text-2" id="text-1">


<p>
The main idea behind the project was to provide a way to extend CL
applications (or libraries) with a standard and widely accepted
scripting language. Javascript seemed to be ideal for such a purpose
since the language is popular, quite small and its semantics are not
that far from Lisp's.
</p>
<p>
Rather than writing an interpreter, CL-Javascript converts Javascript
to CL, which is then compiled as any other Lisp program. With such an
approach most of the runtime overhead is eliminated &ndash; although the
function-calling protocol and runtime dispatching still make
Javascript programs slower compared to pure CL.
</p>
<p>
CL-Javascript is licensed under the MIT Public License.
</p>
<p>
The official project page is at <a href="http://github.com/akapav/js">http://github.com/akapav/js</a>
</p>
</div>

</div>

<div id="outline-container-2" class="outline-2">
<h2 id="sec-2">Implementation </h2>
<div class="outline-text-2" id="text-2">


<p>
For parsing, CL-Javascript uses Marijn Haverbeke's excellent <a href="http://marijn.haverbeke.nl/parse-js">parse-js</a>
library, which generates AST in a form of s-expression which perfectly
suits its needs. This AST is preprocessed (lexical scope analysis,
local function reordering, keywords changed to symbols from the <code>JS</code>
package, and some syntactic forms reduced) so that the resulting form
is a valid and executable CL expression in which every node is further
macroexpanded. On top of that there is a reader macro layer which
allows embedding parts of Javascript directly into Lisp code.
</p>
<p>
The rest of the system contains support for the JS runtime --
including object hash tables, functions, operators, as well as an
implementation (currently incomplete) of the JS standard library.
</p>
</div>

</div>

<div id="outline-container-3" class="outline-2">
<h2 id="sec-3">Usage </h2>
<div class="outline-text-2" id="text-3">


<p>
Since there is no documentation (yet), here are a few examples how to
use CL-Javascript. It is important to mention that while all system
symbols are interned in the <code>JS</code> package, symbols generated during
program execution are interned in the <code>JS-USER</code> package.
</p>
<p>
At the moment, there are several ways to execute JS code from within Lisp.
</p>

</div>

<div id="outline-container-3.1" class="outline-3">
<h3 id="sec-3.1">!eval macro </h3>
<div class="outline-text-3" id="text-3.1">


<p>
Probably the easiest way to invoke JS code from CL is using the
<code>js::!eval</code> macro. The following example just prints hello world and
returns its result (<code>UNDEFINED</code> in this example) back to Lisp.
</p>



<pre class="example">(!eval "print('hello world!');")
</pre>



</div>

</div>

<div id="outline-container-3.2" class="outline-3">
<h3 id="sec-3.2">Reader macros </h3>
<div class="outline-text-3" id="text-3.2">


<p>
Reader macros are designed to embed JS code directly into CL. Code
between <code>#{javascript}</code> and <code>.</code> will be compiled and inserted
in-place.
</p>
<p>
In the following example an anonymous JS function is generated and
returned to Lisp, where it can be used directly.
</p>



<pre class="example">(defun sum-to (n)
  (js::js-funcall
#{javascript}
    (function(n) {
      var sum = 0;
      for(var i = 0; i &lt; n; sum += i++);
      return sum;
    })
.
  n))
</pre>



<p>
Note that unless you are in the <code>JS-USER</code> package you should write
<code>#{js::javascript}</code> instead.
</p>
</div>

</div>

<div id="outline-container-3.3" class="outline-3">
<h3 id="sec-3.3">Loading external files </h3>
<div class="outline-text-3" id="text-3.3">


<p>
To load and execute external JS programs one should use the
<code>js::js-load-file</code> function which accepts a filename as its only
argument.
</p>



<pre class="example">(js-load-file "hello.js")
</pre>



</div>

</div>

<div id="outline-container-3.4" class="outline-3">
<h3 id="sec-3.4">Interning to <code>JS-USER</code> </h3>
<div class="outline-text-3" id="text-3.4">


<p>
At the moment, all properties of <code>[object Global]</code> are interned into
the <code>JS-USER</code> package, so they are directly visible form CL. If some
of those properties are function definitions then a <code>defun</code> wrapper is
also generated.  The drawback to such an approach is that JS
properties are case- sensitive, while most CL implementations use an
uppercase symbol case policy.
</p>



<pre class="example">#{javascript}
var one = 1
function adder(n)
{
   return function(m) {return n+m;}
}
.

(js::js-funcall (adder one) 2) ;=&gt; 3
</pre>



</div>

</div>

<div id="outline-container-3.5" class="outline-3">
<h3 id="sec-3.5">Callbacks </h3>
<div class="outline-text-3" id="text-3.5">


<p>
<i>this part will probably be changed soon</i>
</p>
<p>
At the moment the only way to implement callback functions to CL is to
use the <code>js-function</code> macro.
</p>
<p>
Example from <code>stdlib.lisp</code>:
</p>



<pre class="example">(setf (prop *global* "print")                               ;add 'print' property to global object
      (js-function (arg)                                    ;define js callable function 
        (if (undefined? arg) (format t "~%")                ;underlying lisp code
            (format t "~A~%" (js-funcall string.ctor arg)))))
</pre>



<p>
There are also plans to allow direct embedding of Common Lisp code in
Javascript (the opposite of the <code>#{javascript}</code> reader macro), but
such a change would require some additional modifications to
Javascript's syntax.
</p>
</div>
</div>

</div>

<div id="outline-container-4" class="outline-2">
<h2 id="sec-4">Project status </h2>
<div class="outline-text-2" id="text-4">


<p>
Although most of the ECMA-262 standard is implemented, the project is
still under heavy development. Known missing features are: bitwise
operators, RegExp and Date classes, as well as some Number formatting
methods. Error reporting still needs significant improvement;
additionally, optimizations to operator dispatching would increase the
overall performance of generated code.
</p>
<p>
The project is developed in SBCL, but since no SBCL specific
extensions are used, it <i>should</i> be easy portable to any other Common
Lisp implementation.
</p>
</div>

</div>

<div id="outline-container-5" class="outline-2">
<h2 id="sec-5">Versioning </h2>
<div class="outline-text-2" id="text-5">


<p>
CL-Javascript uses a hybrid versioning schema - the first number
indicates development stage (0 means no stable release yet), and
decimal numbers indicate release date. In general, the schema for
minor releases is [0,1,..].yy.mm, while stable releases use
[1,2,..].00.00.
</p></div>
</div>
]]>
</description>

</item>

</channel>

</rss>


