2009-07-06

JavaScripty C#, Part 1 of Some

I've spent the last week porting JSMeta and JSMetaPad to C# 3.0 on Silverlight 2.0. My approach to this has been the same as that of all my implementation designs lately: direct. To port JSMeta to C#, I literally copied and pasted the JavaScript code into Visual Studio 2010 beta 1 and set to work creating abstractions in C# to support the JavaScriptisms in JSMeta. Luckily, there weren't actually very many.

Lambda Expressions, Deeply



It took me a few days to realize I was actually creating a runtime that would support JavaScript's language features. I implemented JavaScript's scope [chain] as a Dictionary, accessed through a couple of layers of wrapper objects through indexer getters. It ends up being an invisibly-inherited "expando object", with members accessed by name. Those indexer getters treat their results differently depending on their type (whether they match one of several particular delegate type signatures).

I overloaded the indexer by arity: public Activation this [ string memberName, object [] ] { get {...} } , which allows the runtime "code" (actually lambda expressions; no strings/eval/CodeDom involved; pun intended) generator to "call" or "apply" the scope's member by passing in an array of arguments, if necessary.

Here's some code from the Context class demonstrating the mechanics... Sorry I'm not posting all of the code yet; I will at some point soon (this week).


//


More explanation later...

No comments:

Post a Comment