Skip to content

Commit

Permalink
made Object.prototype.toString and Array.isArray unwrap direct proxie…
Browse files Browse the repository at this point in the history
…s, so as to improve transparency for Arrays. Cf. issue #13
  • Loading branch information
Tom Van Cutsem committed Apr 30, 2013
1 parent aecba8e commit 79da9ef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
17 changes: 16 additions & 1 deletion reflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@
// - Object.getPrototypeOf
// - Object.prototype.valueOf
// - Object.prototype.isPrototypeOf
// - Object.prototype.toString
// - Object.getOwnPropertyDescriptor
// - Function.prototype.toString
// - Date.prototype.toString
// - Array.isArray
// - Proxy
// Adds new globals:
// - Reflect
Expand Down Expand Up @@ -342,7 +345,8 @@ var prim_preventExtensions = Object.preventExtensions,
prim_isFrozen = Object.isFrozen,
prim_getPrototypeOf = Object.getPrototypeOf,
prim_getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor,
prim_defineProperty = Object.defineProperty;
prim_defineProperty = Object.defineProperty,
prim_isArray = Array.isArray;

// these will point to the patched versions of the respective methods on
// Object. They are used within this module as the "intrinsic" bindings
Expand Down Expand Up @@ -1463,10 +1467,21 @@ Object.prototype.valueOf =
makeUnwrapping0ArgMethod(Object.prototype.valueOf);
Object.prototype.isPrototypeOf =
makeUnwrapping1ArgMethod(Object.prototype.isPrototypeOf);
Object.prototype.toString =
makeUnwrapping0ArgMethod(Object.prototype.toString);
Function.prototype.toString =
makeUnwrapping0ArgMethod(Function.prototype.toString);
Date.prototype.toString =
makeUnwrapping0ArgMethod(Date.prototype.toString);

Array.isArray = function(subject) {
var vHandler = directProxies.get(subject);
if (vHandler !== undefined) {
return Array.isArray(vHandler.target);
} else {
return prim_isArray(subject);
}
}

// ============= Reflection module =============
// see http://wiki.ecmascript.org/doku.php?id=harmony:reflect_api
Expand Down
11 changes: 11 additions & 0 deletions test/testReflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ function test() {
[1,2,3]).x === 1, 'construct this');
}());

// test whether proxies for arrays are treated as arrays
(function(){
var p = Proxy([], {}); // a proxy for an array
assert(Object.prototype.toString.call(p) === '[object Array]',
'toString(p) = [object Array]');
assert(Array.isArray(p), 'Array.isArray(p)');
// below test fails because JSON.stringify uses a [[Class]] check
// to test whether p is an array, and we can't intercept that
// assert(Array.isArray(JSON.parse(JSON.stringify(p))), 'JSON stringify array');
}());

}

if (typeof window === "undefined") {
Expand Down

0 comments on commit 79da9ef

Please sign in to comment.