Adding JavaScript Source to the Scope of a CommonObject

23 views
Skip to first unread message

Kevin Swiber

unread,
Aug 9, 2011, 3:03:44 PM8/9/11
to IronJS
Howdy.

I'm building support for running JavaScript-based applications and
middleware on top of the NRack[1] framework. I recently moved from
Jurassic to IronJS, specifically for the Mono support and the latest
benchmarks claiming IronJS is faster.

Currently, I'm using CommonJS to handle JavaScript module referencing.

The starting point for an app running on this framework is a config.js
file. Inside this config file, I reference functions to chain
middleware and applications (map, use, run). As a temporary hack, I'm
putting these functions on the global object.

Here's what I'd like to do...

I'd like to define my map, use, and run functions inside a
CommonObject in C#. I'd like to yank the source from the config.js
file and add that to the scope of the CommonObject for execution.

This is really due to an API decision. I'd like my config.js file to
look something like...

// config.js

var helloWorld = require("./helloWorld"),
methodOverride = require("./methodOverride");

use(methodOverride);
run(helloWorld);

//-------------------------

Is there a way to accomplish this in IronJS with the current release?

Thanks!

[1] A port of Ruby's Rack framework to C#, https://github.com/kevinswiber/NRack/

Kevin Swiber

unread,
Aug 9, 2011, 4:02:16 PM8/9/11
to IronJS
Thinking about this a little deeper, I think I need to expose my CommonObject to JavaScript, extend it in JavaScript to include a function that wraps the config source, and then execute that function.

I will update after I try this out.

If there's a better way, let me know!.

Thanks!
--
Kevin Swiber
Projects: https://github.com/kevinswiber
Twitter: @kevinswiber

Kevin Swiber

unread,
Aug 9, 2011, 8:43:31 PM8/9/11
to IronJS

On Tue, Aug 9, 2011 at 4:02 PM, Kevin Swiber <ksw...@gmail.com> wrote:
Thinking about this a little deeper, I think I need to expose my CommonObject to JavaScript, extend it in JavaScript to include a function that wraps the config source, and then execute that function.

I will update after I try this out.

Okay, so I apologize for the wackiness of me responding to my own question twice in a row, but here's the solution I implemented (for now).

A pretty-formatted gist: https://gist.github.com/1135641

In plain English:
1. Grab the IronJS context.
2. Expose a Config class to JavaScript that contains map, use, and run functions.
3. Read the contents of the config.js source file.
4. Execute a self-invoking function in JavaScript that wraps Config's public methods and appends the contents of config.js.

Again, let me know if you think there's a better way.  I'm still working my way through the IronJS API.


Thanks!

John Gietzen

unread,
Aug 9, 2011, 11:15:37 PM8/9/11
to Kevin Swiber, IronJS
Seems fine to me.

Sent from my Windows Phone

From: Kevin Swiber
Sent: Tuesday, August 09, 2011 7:43 PM
To: IronJS
Subject: Re: [IronJS:33] Adding JavaScript Source to the Scope of a CommonObject

Tom Robinson

unread,
Aug 11, 2011, 9:49:42 PM8/11/11
to iro...@googlegroups.com
I can't help with your question, but you should consider using the JSGI API which was inspired by Rack and WSGI:

http://wiki.commonjs.org/wiki/JSGI

Kevin Swiber

unread,
Aug 12, 2011, 5:43:12 PM8/12/11
to iro...@googlegroups.com
On Thu, Aug 11, 2011 at 9:49 PM, Tom Robinson <tlrob...@gmail.com> wrote:
I can't help with your question, but you should consider using the JSGI API which was inspired by Rack and WSGI:

http://wiki.commonjs.org/wiki/JSGI



Hey, Tom!  Thanks for the input.  I'm totally looking at this right now.  The conversion to JSGI and back will be simple since NRack is already supporting a small twist on the Rack spec.

I'm close to the OWIN[1] project right now, which defines a WSGI/Rack-like spec for .NET.  From a host offering, there are handlers for ASP.NET and Kayak[2].

NRack will very soon have OWIN support (implementing this is pretty trivial).

What's really cool about this (to me) is that it will be possible to write a Web app using server-side JavaScript that will run on Windows or Linux and can be hosted in a variety of Web servers (IIS, nginx, etc.).

Options.

Going a little deeper into the future... not JUST JavaScript, but any CLR/DLR language could run on top of NRack.

More options.

I'm pretty stoked about this.

Reply all
Reply to author
Forward
0 new messages