JavaScript
V8 and FastCGI, Exploring an Idea
March 9, 2009 - 1:49am | by R. Tyler CroyOver the past couple years I've talked a lot of trash about JavaScript (really, a lot) but I've slowly started to come around to a more neutral stance, I actually hate browsers, I like JavaScript just fine by itself! While the prototype-based object system is a little weird at first coming from a more classical object-oriented background, the concept grows on you the more you use it.
Since I hate browsers so much (I really do), I was pleased as punch to hear that Google's V8 JavaScript Engine was embeddable. While WebKit's JavaScriptCore is quite a nice JavaScript engine, it doesn't lend itself to being embedded the same way that V8 does. The only immediate downside to V8 is that it's written entirely in C++, which does provide some hurdles to embedding (for example, I'm likely never going to be able to embed it into a Mono application), but for the majority of cases embedding the engine into a project shouldn't be all that difficult.
A few weekends ago I started exploring the possibility of running server-side JavaScript courtesy of V8, after reading about mod_v8 I felt more confident to try my project: FastJS.
In a nutshell, FastJS is a FastCGI server to process server-side JavaScript, this means FastJS can hook up to Lighttpd, Nginx, or even Apache via mod_fcgi. Currently FastJS is in a state of "extremely unstable and downright difficult", there's not a lot there as I'm exploring what should be provided by the FastJS server-side software, and what should be provided by JavaScript libraries. As it stands now, FastJS preloads the environment with jQuery 1.3.2 and a "fastjs" object which contains some important callbacks like:
fastjs.write() // write to the output stream fastjs.log() // write to the FastCGI error.lgo fastjs.source() // Include and execute other JavaScript files
On the server side, a typical request looks something like this (for now):
2009-03-09 05:04:06: (response.c.114) Response-Header: HTTP/1.1 200 OK Transfer-Encoding: chunked Content-type: text/html X-FastJS-Request: 1 X-FastJS-Process: 11515 X-FastJS-Engine: V8 Date: Mon, 09 Mar 2009 09:04:06 GMT Server: lighttpd/1.4.18
Lazily loading attributes.
September 14, 2008 - 11:27pm | by R. Tyler CroyI found myself talking to Jason today about the virtues of getattr(), setattr(), and hasattr() in Python and "abusing" the dynamic nature of the language which reminded me of some lazy-loading code I wrote a while back. In February I found the need to have portions of the logic behind one of our web applications fetch data once per-request. The nature of the web applications we're building on top of the MySpace, Hi5 and Facebook platforms require some level of network data-access (traditionally via REST-like APIs). This breaks our data access model into the following tiers:

Working with network-centric data resources is difficult in any scenario (desktop, mobile, web) but the particularly difficult thing about network data access in the mod_python-driven request model is that it will be synchronous (mod_python doesn't support "asynchronous pages" like ASP.NET does). This means every REST call to Facebook, for example, is going to block execution of the request handler until the REST request to Facebook's API tier completes.
def request_handler(self, *args, **kwargs): fb_uid = kwargs.get('fb_sig_user') print "Fetching the name for %s" % fb_uid print time.time() name = facebook.users.getInfo(uid=fb_uid) ### WAIT-WAIT-WAIT-WAIT-WAIT print time.time() ### Continue generating the page...
Resurgange of the shell.
September 10, 2008 - 11:57pm | by R. Tyler CroyTwo things happened in such short proximity time-wise that I can't help but thing they're somehow related to the larger shift to interpreters. Earlier this week Miguel introduced csharp shell which forced me to dust off my shoddy Mono 1.9 build and rebuild Mono from Subversion just because this is too interesting to pass up on.
One of my favorite aspects of using IronPyhton, or Python for that matter is the interpreter which allows for prototyping that doesn't involve creating little test apps that I have to build to prove a point. For example, I can work through fetching a web page in the csharp shell really easily, instead of creating a silly little application, compiling, fixing errors, and recompiling:
tyler@pineapple:~/source/mono-project/mono> csharp Mono C# Shell, type "help;" for help Enter statements below. csharp> using System; csharp> Console.WriteLine("This changes everything."); This changes everything. csharp> String url = "http://tycho.usno.navy.mil/cgi-bin/timer.pl"; csharp> using System.Web; csharp> using System.Net; csharp> using System.IO; csharp> using System.Text; csharp> HttpWebRequest req = HttpWebRequest.Create(url); <interactive>(1,17): error CS0266: Cannot implicitly convert type `System.Net.WebRequest' to `System.Net.HttpWebRequest'. An explicit conversion exists (are you missing a cast?) csharp> HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest; csharp> HttpWebResponse response = req.GetResponse() as HttpWebResponse; csharp> StreamReader reader = new StreamReader(req.GetResponseStream() as Stream, Encoding.UTF8); <interactive>(1,45): error CS1061: Type `System.Net.HttpWebRequest' does not contain a definition for `GetResponseStream' and no extension method `GetResponseStream' of type `System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?) csharp> StreamReader reader = new StreamReader(response.GetResponseStream() as Stream, Encoding.UTF8); csharp> String result = reader.ReadToEnd(); csharp> Console.WriteLine(result); <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final"//EN> <html> <body> <TITLE>What time is it?</TITLE> <H2> US Naval Observatory Master Clock Time</H2> <H3> <BR>Sep. 11, 07:29:02 UTC <BR>Sep. 11, 03:29:02 AM EDT <BR>Sep. 11, 02:29:02 AM CDT <BR>Sep. 11, 01:29:02 AM MDT <BR>Sep. 11, 12:29:02 AM PDT <BR>Sep. 10, 11:29:02 PM AKDT <BR>Sep. 10, 09:29:02 PM HAST </H3><P><A HREF="http://tycho.usno.navy.mil">Time Service Department, US Naval Observatory</A> </body></html> csharp> reader.Close(); csharp> response.Close(); csharp>
Data Binding with jQuery
April 2, 2008 - 6:52am | by R. Tyler CroySince I've come from the land of desktop application development, there are a few concepts that I don't think quite "made the voyage" from desktop/thick-client development to web/thin-client development. The concept of "data binding" is completely lost in my opinion in the land of Javascript and HTML (not to mention the concept of "controls" to begin with).A few weeks ago while exploring a couple other concepts for how to improve our overall frontend development at Slide I prototyped a means of "databinding" controls, or at the very least DOM elements to data-providing Javascript functions.
I've posted an example here of some of the data binding code I've written for experimentation purposes. In the example page linked, there is a <ul> tag that is "bound" to a Javascript function, the Javascript function creates an array of associative arrays inline (it could very well be powered by some AJAX-oriented Javascript with minor adjustments). Using the results of the "databind" function specified on the bindable element, it creates a set of child nodes to attach to the parent list. In effect, the following code:
<script type="text/javascript"> var bindSimpleList = function() { var simple_list = new Array(); simple_list.push({'contents' : 'List Item #1 OMG', 'onclick' : function() { alert("You clicked the first list item! lulz"); }}); simple_list.push({'contents' : 'List Item #2', 'style' : 'border-bottom: 1px dashed red; width: 100px;'}); simple_list.push({'contents' : '<strong>List Item #3</strong>'}); return simple_list; } </script> <ul class="bindable" databind="bindSimpleList" databindto="li"</ul> </ul>
<ul class="bindable" databind="bindSimpleList" databindto="li"> <li onclick="alert('You clicked the first list item! lulz');">List Item #1 OMG</li> <li style="border-bottom: 1px dashed red; width: 100px;">List Item #2</li> <li><strong>List Item #3</strong></li> </ul>
Since the code is relatively simple (in my opinion) I figured I would throw it out there in all it's minimalistic glory and get some general feedback on the concept before I go "all out" and create a full-on jQuery extension based off of the linked prototype above. I'm trying to think of ways to make it more powerful as well, built-in support for binding to the results of an asynchrounous call to a URL that returns JSON that would then create the elements is at the top of my TODO list at this point. Feedback, flames and actual useful critiques are all welcome; I'll be sure to post again once I have the time to create the jQuery extension for binding, this however is more experimental quality (i.e. don't use it, i'm not).
What do you think?
Did you know! Slide is hiring! Looking for talented engineers to write some good Python and/or JavaScript, feel free to contact me at tyler[at]slide
Srsly ur doin it wrong.
March 24, 2008 - 3:18am | by R. Tyler CroyVia twitter I have been griping a bit about Javascript recently. It's quite possible that I've been complaining about it far more than I complain about other things via twitter, which is a tall order to match.
When addressing something as big and scary as say, a platform built on Javascript, it forces you into looking at Javascript in a way different than how I think most developers (myself included) have looked at Javascript. Most Javascript that I've seen has been hideous. Gobs and gobs of functions and procedural garbage thrown into a series of files that kinda makes sense, but really doesn't. It would seem that most developers charged with writing Javascript don't understand how to write object-oriented Javascript. In fact about two or three months ago when considering topics to discuss in a front-end developers meeting here at Slide, I bit the bullet, raised my hand and said "Can you explain how to do object-oriented Javascript? Because I honestly don't have a fucking clue."
In the past Javascript that I've written has been to compliment existing backend web-application code and front-end code, i.e. I wasn't looking at Javascript as one of the building blocks of my application, I was looking at it as a bit of mortar spread between the cracks to smooth out the surface of the application. The difference in how you start to use Javascript in a web application makes an enormous difference 6 months to a year down the road. How terrible your code (this isn't actually segregated to Javascript) is becomes far more apparent when other developers start to work with your code as well, it's tremendously embarrassing to have to answer questions like "where's the code that generates that one DOM element?" As a general rule, coding all by your lonesome, especially with a tight schedule, will produce less than clean results (unfortunately Javascript is one of the languages I've found where this is more of the norm than the exception).
A lot of what's driven the change from my Javascript being the mortar to being the bricks in my work has been the adoption of jQuery which I highly recommend along with the jQuery.ui library. jQuery makes developing Javascript feel like actual programming, instead of hackish-scripting, which means you'll start to view your Javascript code differently too. Dealing with scoping issues, and prototype-based programming in Javascript isn't all rainbows and butterflies but "doing it right" will help you sleep at night and help reduce the amount of embarrassing questions you'll have to answer to the next poor unfortunate soul that inherits your code.
Some of the resources I've found useful in getting over the barrier to object-oriented Javascript have been:
- This Introduction to Object-Oriented Javascript on the Mozilla Developer Center
- Almost anything John Resig writes, especially gems like his recent post on Simple Javascript Inheritance
- The code included in some mainstream javascript libraries
I'm still not a huge fan of Javascript, but I'm hating it less these days :)
Javascript Wonks: "missing : after property id"
March 21, 2008 - 1:33am | by R. Tyler CroyI've been doing work with OpenSocial recently and have used the opportunity to bring my tolerance talent in Javascript up a notch or two. In doing so, I've been slowly but surely running into a myriad of browser-specific quirks along with a few cross-browser gems that have left me thinking about putting some browser developers on my "To Anonymously Beat Up In Alleyway" list (so far, James Gosling, and this man top the list).
After working on a few "classes" tonight (the notion that Javascript is object-oriented still makes me chuckle) I ran into an interesting problem with some of my global-level "constants" defined in the same file that I was working in, that my "class" just so happened to make use of. As I tend to do when I fall into situations like this to where I can't tell if I'm hallucinating or if something with Javascript has gone awry, I called over Sergio (in-house CSS master and Javascript Lvl. 60 Mage).
Some background to how Javascript works
Javascript engines essentially have two "modes" that it runs over your code that you can spot errors in. The first mode, "parsing", is where you'll find syntax errors spewing into the Javascript console. If you've used any interpreted language before (Python, Java, C#, Ruby), this is really just "compilation". Using Python as an example, when you import a module (i.e. import some_module) the Python interpreter actually compiles your code into Python byte-code to be executed at a later date. The second mode, "execution", is where you'll run into your run-time errors, using an accessing an undefined object property, overrunning an array index, etc. In Python/Java terms, this is where your compiled byte-code is actually being run in the Python/Java virtual machine.
The gripe
The crux of the problem comes down to two different ways to declare an associative array in Javascript, the following two notations are both correct and both "work":
Notation #1
var mapped_values = {}; mapped_values['key'] = 'value';
Notation #2
var mapped_values = {'key' : 'value'};
Everything looks correct yes? (hint: say yes)