Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

.net - What is the ProgId or CLSID for IE9's Javascript engine (code-named "Chakra")

Using .NET, I can write an app that hosts a scripting engine that complies with Microsoft's IActiveScript conventions. This includes JScript and VBScript from Microsoft, and also PerlScript, RubyScript and I don't know what else from third-parties.

The way to do it in code is something like this:

    Type engine = Type.GetTypeFromProgID(progId, true);
    _engine = Activator.CreateInstance(engine) as IActiveScript;

where the progId can take the value Javascript, JScript, ECMAScript, VBScript, and others. You can do something similar when running cscript.exe, specifying the progId on the command line with the //E option. For example, this command:

cscript.exe  <file>  //e:JScript

..will run the specified file, regardless of its extension, through the JScript engine.

On my machine, if I look in HKLMSWClasses , the three progIds {Javascript, JScript, ECMAScript} all point to the same CLSID, which I guess is the JScript 5.8 script engine: {f414c260-6ac0-11cf-b6d1-00aa00bbbb58}

Is there a ProgId or CLSID I can specify to run IE9's Javascript engine, aka "Chakra"?

Does IE9's engine still get loaded by IActiveScript?
Microsoft's documentation suggests that it does, but does not specify a ProgId or CLSID.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The CLSID for the Chakra Javascript engine installed with IE9 is
{16d51579-a30b-4c8b-a276-0ff4dc41e755}.

The InProcServer32 is %windir%System32jscript9.dll .

There is no ProgId that I could find. That's a bit odd; normally paired ProgId and CLSID entries refer to each other. For a given COM object, the ProgId key in the registry has a subkey called CLSID, and the CLSID registry key has a subkey called ProgId, and they refer to each other. But the ProgId subkey for the IE9 CLSID is "JScript", which of course refers to the v5.8 Jscript CLSID. Not sure if this was a mistake by Microsoft, or a purposeful bit of obfuscation, because they don't want anyone using the Chakra engine outside of IE9. Looks purposeful to me.


I learned of the CLSID by just searching the registry for jscript9.dll .


If you have .NET code that hosts scripting engines, you can instantiate the IActiveScript object for the IE9 javascript engine ("Chakra") by using the CLSID directly. The code needs to be something like this:

private const string clsIdPattern =
    @"^(?<curly>{)?[a-zA-Z0-9]{8}(?:-[a-zA-Z0-9]{4}){3}-[a-zA-Z0-9]{12}(?(curly)})$";

public ScriptEngine(string language)
{
    if (language == null)
        throw new ArgumentNullException("language");

    Type engineType = null;

    if (Regex.IsMatch(language, clsIdPattern))
    {
        // it's a CLSID
        var guid = new System.Guid(language);
        engineType = Type.GetTypeFromCLSID(guid, true);
    }
    else
    {
        // assume vanilla progId
        engineType = Type.GetTypeFromProgID(language, true);
    }

    var engine = Activator.CreateInstance(engineType) as IActiveScript;

In the above, clsIdPattern is a regular expression that matches the familiar GUID format, either with or without surrounding curlies.

Given the code above, you could pass "jscript", "Javascript", or "ECMAScript" and get the v5.8 JScript engine. Or you could pass "{16d51579-a30b-4c8b-a276-0ff4dc41e755}" and get the IE9 Javascript engine. Obviously you need to have IE9 installed in order for this to work.

I just tried this and it works for simple cases. I'll be playing with that some more, real soon.


If you want to run Chakra from WSH, like from cscript.exe, then you will need a ProgId, I think.
If I create "Chakra" as a Progid in the registry, referring to the correct CLSID, I can run JS files through IE9's engine like this:

cscript.exe  module.js  //E:Chakra 

For example, after inserting the new "Chakra" ProgId, given a script like this:

WScript.Echo( ScriptEngineMajorVersion() + "." +
              ScriptEngineMinorVersion() + "." +
              ScriptEngineBuildVersion());

...the output is like this:

C:devjs>Version.js
5.8.16982

C:devjs>cscript.exe Version.js  //E:Chakra
9.0.16434

And here's the result of a test of AES encryption in Javascript, comparing Chakra with JScript 5.8:

C:devjsSlowAES>cscript.exe test.aes.js
AES encryption in Javascript.
password  : Albatros1
salt      : saltines (73616c74696e6573)
iterations: 1000
key       : 172,52,20,51,98,71,49,195,14,31,141,51,129,8,94,66
iv        : 212,27,28,156,83,245,0,35,176,157,45,141,209,143,158,248
plaintext : Hello, /r/javascript.
ciphertext: fdebac9f1ed1a13bac58f3cc6558e8b0367a94245dbbfe53cacc4979799fc182
decrypted : Hello, /r/javascript.
elapsed   : 5011ms

C:devjsSlowAES>cscript.exe test.aes.js //E:Chakra
AES encryption in Javascript.
password  : Albatros1
salt      : saltines (73616c74696e6573)
iterations: 1000
key       : 172,52,20,51,98,71,49,195,14,31,141,51,129,8,94,66
iv        : 212,27,28,156,83,245,0,35,176,157,45,141,209,143,158,248
plaintext : Hello, /r/javascript.
ciphertext: fdebac9f1ed1a13bac58f3cc6558e8b0367a94245dbbfe53cacc4979799fc182
decrypted : Hello, /r/javascript.
elapsed   : 2593ms

To set the ProgId in my registry, I used this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINESOFTWAREClassesCLSID{16d51579-a30b-4c8b-a276-0ff4dc41e755}ProgID]
@="Chakra"

[HKEY_LOCAL_MACHINESOFTWAREClassesWow6432NodeCLSID{16d51579-a30b-4c8b-a276-0ff4dc41e755}ProgID]
@="Chakra"

[HKEY_LOCAL_MACHINESOFTWAREClassesChakra]
@="JScript Language"

[HKEY_LOCAL_MACHINESOFTWAREClassesChakraCLSID]
@="{16d51579-a30b-4c8b-a276-0ff4dc41e755}"

[HKEY_LOCAL_MACHINESOFTWAREClassesChakraOLEScript]

and to unexpose Chakra, or revert the registry, I did this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINESOFTWAREClassesCLSID{16d51579-a30b-4c8b-a276-0ff4dc41e755}ProgID]
@="JScript"

[HKEY_LOCAL_MACHINESOFTWAREClassesWow6432NodeCLSID{16d51579-a30b-4c8b-a276-0ff4dc41e755}ProgID]
@="JScript"

[-HKEY_LOCAL_MACHINESOFTWAREClassesChakra]

This registry script worked with x64 Windows; if you don't have x64, then you'll need to remove the WOW6432Node lines.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...