codecombat/esper.js

Array pop does not change state

Closed this issue · 4 comments

Array.pop is no longer updating the state in the latest build.

Current build (28 September 2017, version ad69d01):

var testArray = esper.eval("var testArray = ['one', 'two', 'three']; testArray.pop(); testArray");
console.log('testArray', testArray); //  ['one', 'two', 'three']

The current version doesn't update the testArray variable after pop.

Previous build on Esper.js demo website (23 Aug 2017, unversioned):

var testArray = esper.eval("var testArray = ['one', 'two', 'three']; testArray.pop(); testArray");
console.log('testArray', testArray); //  ['one', 'two']

The previous build has the correct behavior.

Interesting.

The problem here is actually that esper is missing the magic behavior where setting the length value of an array truncates the integer key values. This misbehavior was sometimes masked in the older version when the object was copied out of the esper because it would set the length property on the output object, and the browser would apply the magic behavior.

That's strange.

From what I've gathered, setting a lengthlet out = new Array(this.getLengthSync()) is not like in other programming languages because JavaScript is happy to assign values past the bounds of the array, so something like this works:

var out = new Array(2);
console.log(out.length); //2
out[0] = 'a';
out[1] = 'b';
out[2] = 'c';
console.log(out.length); // 3

That appears what is happening here as I noticed the value (which should be removed by the pop method) was left in the array properties

I added this to patch it in .pop().

delete thiz.properties[l - 1];

That sorta works, but doesn't really fix the underlaying problem.

function p(o) { console.log(JSON.stringify(o)); }
var arr = [1,2,3];
p(arr);
arr.length = 1;
p(arr);
p(arr[2]);
Anya:~ rob$ node o.js 
[1,2,3]
[1]
undefined


Anya:~ rob$ esper o.js 
o.js
[1,2,3]
[1]
3

Should be fixed by 102e749