imedo Development Blog

there is no charge for awesomeness

Author Archive

Run Javascript, Run!

with 2 comments

We’re pretty much a Mac shop here at imedo and, as our application is built using Ruby on Rails, it’s safe to say that we are legally obliged to use TextMate for all our editing needs ;) .

One of the features I love about TextMate, catered for in most bundles, is the ability to execute a script and get a nice pop-up window displaying the results. Scrolling through my installed language bundles, I see they all have a “Run Script” command. All, that is, except my second favourite language – Javascript. You fancy rectifying this? You fancy giving Javascript some TextMate/V8 lurv? Then walk this way …

First, lets grab V8. We’ll be using scons to build it. Scons? Aren’t they a strange  muffin-like concoction originating in the fair British Isles? That’ll be ‘scones‘ you’re thinking of. Scons is a software construction tool written in Python. Check it out at here. It must be good – Zed likes it. Install with macports.

And on with the build.

Get v8:

$> svn co http://v8.googlecode.com/svn/trunk v8
- or -
$> git clone git://github.com/v8/v8.git v8

$> cd v8
$> scons

The build should have been successful. V8 comes with a shell which we’re going to use. Build like so:

$> g++ ./samples/shell.cc -o v8-shell -I include libv8.a

Make sure that ‘v8-shell’ is in your PATH. And now for the final step. In TextMate: Bundles > Bundle Editor > Show Bundle Editor. Scroll down to the Javascript bundle and Create a new Command. We’ll use our imagination here and call it “Run Script”. In the Edit Command Window:

Save: Nothing
Command(s):


$(type -p "${TM_RUBY:-ruby}") -e'
require ENV["TM_SUPPORT_PATH"] + "/lib/tm/executor"
require ENV["TM_SUPPORT_PATH"] + "/lib/tm/save_current_document"
TextMate.save_current_document
TextMate::Executor.make_project_master_current_document
TextMate::Executor.run("v8-shell", ENV["TM_FILEPATH"])'

Input: Entire Document
Output: Show as HTML
Activation: Key Equivalent > Cmd+R
Scope Selector: source.js

This is copied and adapted from the “Run Script” Command for the Ruby bundle.

And that – as they say – is that. Time to check it out:

Open a new window in TextMate and type the following:

function addSomeValues (first, second) {
return first + second;
}

print(addSomeValues(2,3))

Select the Javascript scope. Wait for it … Ready? … Go Cmd+R! Hopefully you saw the number five.

Now change the last line to read

print(addSomeValueCATsaTOnKeyboArD(2,3))

Run again and you should get a nice ReferenceError.

Thanks go to gs over at StackOverflow for this post detailing the steps for getting the v8 shell built.

Popularity: 1% [?]

Written by agroves

December 15th, 2009 at 3:14 pm

Posted in Development

Tagged with , ,