public
Last active

  • Download Gist
AO-parameters-arguments.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/**
* Abstract representation of ES3
* or ES5-non-strict sharing of
* passed formal parameters and
* corresponding indexes of the
* arguments object
*
* Implemented in ES5, non-strict
* tested in BESEN rev. 109
*
* @author Dmitry A. Soshnikov (C) 2010
*/
 
print("==Exact===\n");
 
function foo(a, b) {
print(arguments.length); // 1
print(arguments.callee.length); // 2
print(a); // 10
 
arguments[0] = 20;
print(a); // 20
 
a = 30;
print(arguments[0]); // 30
 
b = 100;
print(arguments[1]); // undefined
delete a; // false
 
delete arguments[0]; // true
print(arguments[0]); // undefined
print(a); // 30
 
arguments[0] = 50;
print(arguments[0]); // 50
print(a); // 30
 
a = 40;
print(a); // 40
print(arguments[0]); // 50
 
}
 
// call function, passing
// only first parameter
foo(10);
 
// Abstract description of handling
// variable/activation object in ES3, for details see:
// http://dmitrysoshnikov.com/ecmascript/chapter-2-variable-object/
 
print("\n==Abstract===\n");
 
// 1. Entering the context stage
 
var AO = Object.defineProperties({}, {
arguments: {
value: Object.defineProperties({}, {
// create accessor only for the
// passed argument - "a", index 0
0: {
get: function getA() {
return AO.a;
},
set: function setA(value) {
AO.a = value;
},
configurable: true
},
// length is a number of
// the actual arguments passed
length: {value: 1, writable: true, configurable: true},
// current function
callee: {value: foo, writable: true, configurable: true}
})
},
// create formal parameters
a: {value: 10, writable: true},
b: {value: undefined, writable: true},
});
 
// 2. Code execution stage
 
// use with to emulate identifier
// resolution in function's AO
 
with (AO) {
print(arguments.length); // 1
print(arguments.callee.length); // 2
 
print(a); // 10
 
arguments[0] = 20;
print(a); // 20
 
a = 30;
print(arguments[0]); // 30
 
// "b" is not passed to
// function, so it's not shared
b = 100;
print(arguments[1]); // undefined, as should be
// can't delete formal parameter,
// because it's not configurable
// {DontDelete} == true ES3,
// [[Configurable]] == false ES5
delete a; // false
 
// but can delete corresponding
// arguments' property; however,
// deleting it, we lose accessor
delete arguments[0]; // true
print(arguments[0]); // undefined
print(a); // 30
 
// arguments[0] now is a
// simple data property
arguments[0] = 50;
print(arguments[0]); // 50
print(a); // 30
 
a = 40;
print(a); // 40
print(arguments[0]); // 50
 
}
 
// This correct behavior currently
// is broken in Chrome, for details
// see http://code.google.com/p/chromium/issues/detail?id=52484

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.