Is there a way to keep ESTK environment clean?

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

undavide

Is there a way to keep ESTK environment clean?

Post by undavide »

Hello,
I'm having a bit of troubles with ESTK today: apparently there's some data that ESTK keeps in memory that I don't know how to get rid of. Possibly someone can help!

Now I've a regular script (ESTK CC - File - New Javascript):

Code: Select all$.evalFile("~/Desktop/ES5shim.js")

var d = new Date();
$.writeln(d.toISOString());
// Console: 2013-08-22T13:44:10.448Z


The ES5shim.js file just provides a new .toISOString() method - it's an EcmaScript 5 shim:

Code: Select all// ES5 15.9.5.43
// http://es5.github.com/#x15.9.5.43
// This function returns a String value represent the instance in time
// represented by this Date object. The format of the String is the Date Time
// string format defined in 15.9.1.15. All fields are present in the String.
// The time zone is always UTC, denoted by the suffix Z. If the time value of
// this object is not a finite Number a RangeError exception is thrown.
var negativeDate = -62198755200000,
    negativeYearString = "-000001";
if (
    !Date.prototype.toISOString ||
    (new Date(negativeDate).toISOString().indexOf(negativeYearString) === -1)
) {
    Date.prototype.toISOString = function toISOString() {
        var result, length, value, year, month;
        if (!isFinite(this)) {
            throw new RangeError("Date.prototype.toISOString called on non-finite value.");
        }

        year = this.getUTCFullYear();

        month = this.getUTCMonth();
        // see https://github.com/kriskowal/es5-shim/issues/111
        year += Math.floor(month / 12);
        month = (month % 12 + 12) % 12;

        // the date time string format is specified in 15.9.1.15.
        result = [month + 1, this.getUTCDate(),
            this.getUTCHours(), this.getUTCMinutes(), this.getUTCSeconds()];
        year = (
            (year < 0 ? "-" : (year > 9999 ? "+" : "")) +
            ("00000" + Math.abs(year))
            .slice(0 <= year && year <= 9999 ? -4 : -6)
        );

        length = result.length;
        while (length--) {
            value = result[length];
            // pad months, days, hours, minutes, and seconds to have two
            // digits.
            if (value < 10) {
                result[length] = "0" + value;
            }
        }
        // pad milliseconds to have three digits.
        return (
            year + "-" + result.slice(0, 2).join("-") +
            "T" + result.slice(2).join(":") + "." +
            ("000" + this.getUTCMilliseconds()).slice(-3) + "Z"
        );
    };
}

Now, if I comment out the $.evalFile and run the script again, the toISOString() still works!

Code: Select all//$.evalFile("~/Desktop/ES5shim.js")

var d = new Date();
$.writeln(d.toISOString());
// Console: 2013-08-22T13:44:10.448Z


I wonder why.
Of course if I close ESTK (CC, on Mac) and reopen it, the version without the shim throw the ".toISOString() is not a function" error.

Apparently, the content of an $.evalFile keeps in ESTK memory - in fact even if you create a new file, the evaluated content works even if it's not included at all.
Is this as designed, a bug, or should I perform some task in order to free it?
Thank you!

Davide Barranca
http://www.davidebarranca.com
undavide

Is there a way to keep ESTK environment clean?

Post by undavide »

ESTK, create a new JSX and run the following:

Code: Select allvar b;
var c = new Object();
$.writeln(typeof b + " - "+ typeof c);
// undefined - object

Now I create a second new JSX and run it:

Code: Select allvar b = new Object()
$.writeln(typeof b);
// object

And now the weirdness: back to the first file, if I run it a second time as it is:
Code: Select allvar b;
var c = new Object();
$.writeln(typeof b + " - "+ typeof c);
// object - object

So the b variable, since it is just declared (should be 'undefined'), keeps being an Object.
I've run:

Code: Select all$.writeln($.global.toSource())
// ({})

So I'm really puzzled - possibly I'm missing something really obvious here?

Davide
Mike Hale

Is there a way to keep ESTK environment clean?

Post by Mike Hale »

I don't do much scripting with ESTK as the target so I have never noticed this before. But it does appear that any script run with ESTK as the target stays in memory during that ESTK session. Scripts in the Photoshop startup folder also stay in memory during the Photoshop session.

As I understand it there really isn't a way to delete an object in javascript. I guess one way to avoid variable name collisions is to explicitly declare all variables.
Code: Select allvar b = undefined;// explicitly set b to undefined
//var b;// implicitly set b to undefined
var c = new Object();
$.writeln(typeof b + " - "+ typeof c);
// undefined - object
Mike Hale

Is there a way to keep ESTK environment clean?

Post by Mike Hale »

You can clear the memory in ESTK by choosing the Debug-Reset menu.
undavide

Is there a way to keep ESTK environment clean?

Post by undavide »

Do you think it has been designed this way, Mike?
It's not really handy, imho. Anyway, thanks for the Reset tip, I didn't notice it.
Mike Hale

Is there a way to keep ESTK environment clean?

Post by Mike Hale »

Just a guess on my part but yes I think it was designed that way. Why else have that menu item?