Wednesday, 24 January 2018

Javascript - cscript.exe and script control limited to Ecmascript 3

So the script control (MSScriptControl|Microsoft Script Control 1.0|C:\Windows\SysWOW64\msscript.ocx) is very useful for parsing JSON because Douglas Crockford's scripts run happily but it is limited to Ecmascript version 3. Sadly the command line script executable, cscript.exe (c:\Windows\SysWOW64\cscript.exe) is also restricted to Ecmascript version 3 because they sahare the same JScript engine.

We can see this we some Javascript version detection code. Save this file as test.js

JavaScriptVersion();


function JavaScriptVersion() {
    if (typeof this.WScript != 'undefined') {
        WScript.echo("supports ES3:" + checkES3());
        WScript.echo("supports ES5:" + checkES5());
        WScript.echo("supports foreach:" + forEach());
    }
}


function checkES3() {
    try {
    return "function" === typeof [].hasOwnProperty;
    }
    catch (ex) { }
}

function checkES5() {
    try {
        var methods = 'function' === typeof [].filter &&
            'function' === typeof Function.prototype.bind &&
            'function' === typeof Object.defineProperty &&
            'function' === typeof ''.trim &&
            'object' === typeof JSON;

        var syntax = supports("reservedWords");
    }
    catch (ex) { }

    return methods && syntax;
}

function forEach() {

    try {
        var supportsForEach = false;
        var arr = ['a', 'b', 'c'];

        arr.forEach(function callback(element) {
            var b = element + " ";
        });
        supportsForEach = true;
    }
    catch (ex) { }
    return supportsForEach;
}

So now run this console command

c:\Windows\SysWOW64\cscript.exe test.js

The console command generates the output below showing cscript.exe is limited to ES3.

Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. All rights reserved.

supports ES3:true
supports ES5:false
supports foreach:false

No comments:

Post a Comment