2009-06-23

JSMeta 0.2 available for play


module JSMeta {
language HelloWorld {
syntax Main = ((Bar('q') | Bar) Bar | Bar Bar Bar)+;
syntax Bar = 'a';
token Bar(param0) = 'b' param0 'z';
interleave Ignored = Language.Base.Whitespace | Language.Grammar.Comment;
}
}

2009-06-22

Property Inheritance (with a Constructor) in JavaScript

In JSMeta, I needed instances of a particular "class" (JavaScript doesn't have classes, per se, so what I really mean is objects initialized by a particular "constructor" Function) to "magically" (invisibly, automatically) inherit existing properies from another instance of that "class".

I created the following generic function that does just that:



function spawnOf(progenitor) {
// obtain a reference to the function
// that created the object
var Ctor = typeof(progenitor.constructor)!='undefined'
? progenitor.constructor
: getGlobal()[Object.prototype.toString.call(
progenitor).match(/^\[object\s(.*)\]$/)[1]];
// the below shouldn't ever happen
// (except on undefined or null)
if (typeof Ctor != 'function')
throw 'constructor not found';
// save the original prototype of
// the constructor function
var origProto = Ctor.prototype;
// replace the constructor function's
// prototype with the progenitor object
Ctor.prototype = progenitor;
// create a new "child" object, using the same
// constructor function as the progenitor object.
var childObject = new Ctor();
// replace the constructor function's prototype.
Ctor.prototype = origProto;
return childObject;
}

// from http://www.nczonline.net/blog/2008/04/20/get-the-javascript-global/
// by Nicholas C. Zakas
function getGlobal() {
return (function() {
return this;
}).call(null);
}


Here's an example of how to use it. It's useful in situations where you want to create a new object using a constructor function, but you also want the object to invisibly/automatically inherit its properties from its progenitor.



// for v8(.exe) or jsc(.exe) or whatever
// mozilla's js .exe are named
if (typeof(alert)=='undefined') alert=print;

function Gungan() {
this.toString = function() {
return "_|meesa Gungan|_"
};
if (this.constructor
===Gungan.prototype.constructor) {
// odd quirk here; this is never true in
// v8: (Gungan.prototype instanceof Gungan)
this.parent = Gungan.prototype;
}
}

var jarjar = new Gungan();

jarjar.iq = 100;

var jarjar_kid = spawnOf(jarjar);

alert(jarjar_kid.iq); // alerts "100"

jarjar_kid.iq = 85;

alert(jarjar.iq); // alerts "100"

alert(jarjar_kid.iq); // alerts "85"

alert(jarjar_kid.parent.iq); // alerts "100"

2009-06-16

How to toggle off and on the HTTP or SOCKS proxy quickly on Windows

Here's a handy script I use to quickly (one click) enable/disable the proxy for IE, Safari, and Chrome on Windows (works on vista x64 for me); sorry, I don't remember where I found the original that I modified... I just saved it in a .vbs file and made a shortcut to the .vbs file on my Quick Launch bar.


Const HKCU=&H80000001 'HKEY_CURRENT_USER
Const HKLM=&H80000002 'HKEY_LOCAL_MACHINE

Const REG_SZ=1
Const REG_EXPAND_SZ=2
Const REG_BINARY=3
Const REG_DWORD=4
Const REG_MULTI_SZ=7

Const HKCU_IE_PROXY = "Software\Microsoft\Windows\CurrentVersion\Internet Settings"

Set oReg=GetObject("winmgmts:!root/default:StdRegProv")

Main

Sub Main()

' If Proxy is set then turn it off
If GetValue(HKCU,HKCU_IE_PROXY,"ProxyEnable",REG_DWORD) = 1 Then
CreateValue HKCU,HKCU_IE_PROXY,"ProxyEnable",0,REG_DWORD
wscript.echo "Proxy Disabled"
Else
' If Proxy is not set then turn it on
CreateValue HKCU,HKCU_IE_PROXY,"ProxyEnable",1,REG_DWORD
wscript.echo "Proxy Enabled" & vbcrlf
End If

End Sub

Function CreateValue(Key,SubKey,ValueName,Value,KeyType)
Select Case KeyType
Case REG_SZ
CreateValue = oReg.SetStringValue(Key,SubKey,ValueName,Value)
Case REG_EXPAND_SZ
CreateValue = oReg.SetExpandedStringValue(Key,SubKey,ValueName,Value)
Case REG_BINARY
CreateValue = oReg.SetBinaryValue(Key,SubKey,ValueName,Value)
Case REG_DWORD
CreateValue = oReg.SetDWORDValue(Key,SubKey,ValueName,Value)
Case REG_MULTI_SZ
CreateValue = oReg.SetMultiStringValue(Key,SubKey,ValueName,Value)
End Select
End Function

Function DeleteValue(Key, SubKey, ValueName)
DeleteValue = oReg.DeleteValue(Key,SubKey,ValueName)
End Function

Function GetValue(Key, SubKey, ValueName, KeyType)

Dim Ret

Select Case KeyType
Case REG_SZ
oReg.GetStringValue Key, SubKey, ValueName, Value
Ret = Value
Case REG_EXPAND_SZ
oReg.GetExpandedStringValue Key, SubKey, ValueName, Value
Ret = Value
Case REG_BINARY
oReg.GetBinaryValue Key, SubKey, ValueName, Value
Ret = Value
Case REG_DWORD
oReg.GetDWORDValue Key, SubKey, ValueName, Value
Ret = Value
Case REG_MULTI_SZ
oReg.GetMultiStringValue Key, SubKey, ValueName, Value
Ret = Value
End Select

GetValue = Ret
End Function

2009-06-02

JSMeta now on GoogleCode - formerly "mjs" on Codeplex

This weekend I moved my language workbench project written in ECMA-262 3rd Edition (JavaScript) from codeplex to Google Code: http://jsmeta.googlecode.com

I also renamed it to JSMeta. You can run the test suite here: JSMeta Test Suite, hosted directly out of Google Code's subversion repository.

I moved it because Codeplex' Subversion-over-http implementation unfortunately does not implement serving files with the Content-Type specified in a file's property svn:mime-type , so I couldn't host the application directly out of subversion. I filed this omission as a bug here, but perhaps I should've filed it on the "codeplex" codeplex project instead of the svnbridge codeplex project; oh well.