Rick Strahl's Weblog
Rick Strahl's FoxPro and Web Connection Weblog
White Papers | Products | Message Board | News |

The Microsoft ScriptControl and JavaScript Evaluation


4 comments
January 02, 2007 • about 2 minutes to read

I’m screwing around with JSON parsing in one of my FoxPro apps at the moment. Sometime ago I built a JSON parser for VFP and it works well enough for outbound JSON generation, but I don’t have a good solution for inbound JSON. JSON is merely a text encoding of values, but it can be nested and parsing this string is not trivial.

 

The problem is that VFP lacks decent language support for building a state machine parser because there’s no efficent way to access each character in a string efficiently. The only mechanism to parse a string character by character is with SUBSTR() which is dreadfully slow!

 

So I got a bright idea earlier today: Windows actually ships with a JavaScript engine and a script control that makes it relatively painless to execute and parse JavaSCript code. So why not take advantage of this control to handle the parsing for me. The control includes a handy Eval method:

 

o = CREATEOBJECT("ScriptControl")

o.Language="JScript"

 

? o.Eval("'Hello \r\n WOrld'")

? o.Eval("100.11")

? o.Eval("true")

? o.Eval("false")

 

loResult = o.Eval([new Date("Mon, 1 Jan 2007 23:09:09 -1000")])

? loResult.toString()

 

 

loResult =  o.Eval([{"x":10,"y":15}])

? loResult.x

 

Took a while to find some documentation on this control – it’s not widely used apparently. I was happy to see that the control works and can parse simple values. However, it choked on the complex expressions. The string, numeric and logical values parsed easily enough (and strings are nice to have parsed that way BTW because of the encoding).

 

But I was unable to get the date to parse. I do get a result of type object back, but I’m unable to do anything with this date in FoxPro. I tried using JavaScript date methods like toString(), getTime() etc. but none of that worked. I also tried things like name, id, class and a host of common container properties, but none of that would work either. Even COMCLASSINFO() wouldn’t return me a ClassID. Whatever this object is – I can’t figure out what the heck it is or what to call on it.

 

In fact it didn’t like this either:

 

loResult =  o.Eval([{"x":10,"y":15}.toString()])

? loResult

 

So the problem is more deep seated in that apparently Jscript can’t deal with JavaScript 1.2 syntax which is required for this object syntax. Bummer as that pretty much breaks all hopes of it being useful for parsing.

 

So I’m back to manually doing this – possibly looking at some fancy RegEx parsing of string chunks at a time. Drat…

Posted in:

Feedback for this Weblog Entry


Re: The Microsoft ScriptControl and JavaScript Evaluation



Christof Wollenhaupt
January 02, 2007

That works for me:

o = CREATEOBJECT("ScriptControl") o.Language="JScript" lnYear = o.Eval([ox=new Date("Mon, 1 Jan 2007 23:09:09 -1000");ox.getYear()]) ? m.lnYear

Re: The Microsoft ScriptControl and JavaScript Evaluation



Rick Strahl
January 02, 2007

Christof, yes you can pass back any simple values - no problems with that. It's passing the full Date object back or getting what effectively amounts to a FoxPro date and not some other representation. If I have to parse the date I can do that right of the JSON string .

The date is relatively trivial actually. The real biggie is the object and array parsing. I just spend several hours building an object parser and that works fine. Arrays are more tricky though - skipping that for the moment.

It sure woulda been nice if the control could have handled all the parsing. On the upside I decided to use the control for string parsing which is significantly faster than VFP on moderately long strings and 1 line of code instead of 20 .

Re: The Microsoft ScriptControl and JavaScript Evaluation



Piter Protsyk
October 18, 2007

Hi there,

Rick, I have read an article about run-time compilation in .net, I guess few years ago. Seems this topic is of your interest.

Finally, I diceded to implement real scripting language for .NET Framework. It is available here: http://www.protsyk.com/scriptdotnet

re: The Microsoft ScriptControl and JavaScript Evaluation



Paul Hemans
May 20, 2012

For anyone viewing this entry you can get the date out of the script control by: ox = o.Eval([ox=new Date("Mon, 1 Jan 2007 23:09:09 -1000");ox.toString();]) ? ox

At least that worked for me, comes back as a string which then needs to be parsed.

 
© Rick Strahl, West Wind Technologies, 2003 - 2025