2013-07-19 14:07:23 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
// Helper.
|
|
|
|
|
|
|
|
function TestWithProxies(test, x, y, z) {
|
2015-11-13 14:13:50 +00:00
|
|
|
test(function(h) { return new Proxy({}, h) }, x, y, z)
|
|
|
|
test(function(h) {
|
2015-12-11 14:55:20 +00:00
|
|
|
return new Proxy(function() {}, h)
|
2015-11-13 14:13:50 +00:00
|
|
|
}, x, y, z)
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Getting.
|
|
|
|
|
|
|
|
function TestWithGet(handler) {
|
|
|
|
TestWithProxies(TestWithGet2, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c = "global"
|
|
|
|
var key = ""
|
|
|
|
|
|
|
|
function TestWithGet2(create, handler) {
|
|
|
|
var b = "local"
|
|
|
|
|
2015-12-11 14:55:20 +00:00
|
|
|
var p = create(handler);
|
|
|
|
assertEquals("onproxy", p.a);
|
|
|
|
assertEquals(undefined, p.b);
|
|
|
|
assertEquals(undefined, p.c);
|
|
|
|
|
2013-07-19 14:07:23 +00:00
|
|
|
with (p) {
|
2015-12-11 14:55:20 +00:00
|
|
|
assertEquals("onproxy", a);
|
|
|
|
assertEquals("local", b);
|
|
|
|
assertEquals("global", c);
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var o = Object.create(p, {d: {value: "own"}})
|
|
|
|
with (o) {
|
|
|
|
assertEquals("onproxy", a)
|
2015-12-11 14:55:20 +00:00
|
|
|
assertEquals("local", b);
|
2013-07-19 14:07:23 +00:00
|
|
|
assertEquals("global", c)
|
|
|
|
assertEquals("own", d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TestWithGet({
|
2015-12-11 14:55:20 +00:00
|
|
|
get(target, k) {
|
2015-11-13 14:13:50 +00:00
|
|
|
key = k;
|
|
|
|
return k === "a" ? "onproxy" : undefined
|
|
|
|
},
|
2015-12-11 14:55:20 +00:00
|
|
|
has(target, k) { return k === 'a' }
|
2013-07-19 14:07:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
TestWithGet({
|
|
|
|
get: function(r, k) { return this.get2(r, k) },
|
|
|
|
get2: function(r, k) { key = k; return k === "a" ? "onproxy" : undefined },
|
2015-12-11 14:55:20 +00:00
|
|
|
has(target, k) { return k === 'a' }
|
2013-07-19 14:07:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Invoking.
|
|
|
|
|
|
|
|
function TestWithGetCall(handler) {
|
|
|
|
TestWithProxies(TestWithGetCall2, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
var receiver = null
|
|
|
|
var c = function() { return "global" }
|
|
|
|
|
|
|
|
function TestWithGetCall2(create, handler) {
|
|
|
|
var b = function() { return "local" }
|
|
|
|
|
|
|
|
var p = create(handler)
|
|
|
|
with (p) {
|
|
|
|
receiver = null
|
|
|
|
assertEquals("onproxy", a())
|
|
|
|
assertSame(p, receiver)
|
|
|
|
assertEquals("local", b())
|
|
|
|
assertEquals("global", c())
|
|
|
|
}
|
|
|
|
|
|
|
|
var o = Object.create(p, {d: {value: function() { return "own" }}})
|
|
|
|
with (o) {
|
|
|
|
receiver = null
|
|
|
|
assertEquals("onproxy", a())
|
|
|
|
assertSame(o, receiver)
|
|
|
|
assertEquals("local", b())
|
|
|
|
assertEquals("global", c())
|
|
|
|
assertEquals("own", d())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onproxy() { receiver = this; return "onproxy" }
|
|
|
|
|
|
|
|
TestWithGetCall({
|
|
|
|
get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
2013-07-19 14:07:23 +00:00
|
|
|
key = k;
|
2015-12-11 14:55:20 +00:00
|
|
|
return k === "a";
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithGetCall({
|
|
|
|
get: function(r, k) { return this.get2(r, k) },
|
|
|
|
get2: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
2013-07-19 14:07:23 +00:00
|
|
|
key = k;
|
2015-12-11 14:55:20 +00:00
|
|
|
return k === "a";
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithGetCall({
|
2015-12-11 14:55:20 +00:00
|
|
|
get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
|
|
|
|
has: function(t, k) {
|
|
|
|
return this.has2(k)
|
|
|
|
},
|
|
|
|
has2: function(k) {
|
2013-07-19 14:07:23 +00:00
|
|
|
key = k;
|
2015-12-11 14:55:20 +00:00
|
|
|
return k === "a";
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithGetCall({
|
2015-12-11 14:55:20 +00:00
|
|
|
get: function(r, k) { key = k; return k === "a" ? onproxy : undefined },
|
|
|
|
has: function(t, k) {
|
2013-07-19 14:07:23 +00:00
|
|
|
key = k;
|
2015-12-11 14:55:20 +00:00
|
|
|
return k === "a";
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
function TestWithGetCallThrow(handler) {
|
|
|
|
TestWithProxies(TestWithGetCallThrow2, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestWithGetCallThrow2(create, handler) {
|
|
|
|
var b = function() { return "local" }
|
|
|
|
|
|
|
|
var p = create(handler)
|
|
|
|
with (p) {
|
2016-01-07 14:48:57 +00:00
|
|
|
assertThrowsEquals(function(){ a() }, "myexn")
|
2013-07-19 14:07:23 +00:00
|
|
|
assertEquals("local", b())
|
|
|
|
assertEquals("global", c())
|
|
|
|
}
|
|
|
|
|
|
|
|
var o = Object.create(p, {d: {value: function() { return "own" }}})
|
|
|
|
with (o) {
|
2016-01-07 14:48:57 +00:00
|
|
|
assertThrowsEquals(function(){ a() }, "myexn")
|
2013-07-19 14:07:23 +00:00
|
|
|
assertEquals("local", b())
|
|
|
|
assertEquals("global", c())
|
|
|
|
assertEquals("own", d())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onproxythrow() { throw "myexn" }
|
|
|
|
|
|
|
|
TestWithGetCallThrow({
|
2016-01-07 14:48:57 +00:00
|
|
|
has: function(r, k) { return k === "a"; },
|
2013-07-19 14:07:23 +00:00
|
|
|
get: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithGetCallThrow({
|
2016-01-07 14:48:57 +00:00
|
|
|
has: function(r, k) { return k === "a"; },
|
2013-07-19 14:07:23 +00:00
|
|
|
get: function(r, k) { return this.get2(r, k) },
|
|
|
|
get2: function(r, k) { key = k; return k === "a" ? onproxythrow : undefined },
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Setting.
|
|
|
|
|
|
|
|
var key
|
|
|
|
var val
|
|
|
|
|
|
|
|
function TestWithSet(handler, hasSetter) {
|
|
|
|
TestWithProxies(TestWithSet2, handler, hasSetter)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c = "global"
|
|
|
|
|
|
|
|
function TestWithSet2(create, handler, hasSetter) {
|
|
|
|
var b = "local"
|
|
|
|
|
|
|
|
var p = create(handler)
|
|
|
|
key = val = undefined
|
|
|
|
with (p) {
|
|
|
|
a = "set"
|
|
|
|
assertEquals("a", key)
|
|
|
|
assertEquals("set", val)
|
|
|
|
assertEquals("local", b)
|
|
|
|
assertEquals("global", c)
|
|
|
|
b = "local"
|
|
|
|
c = "global"
|
|
|
|
assertEquals("a", key)
|
|
|
|
assertEquals("set", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasSetter) return
|
|
|
|
|
|
|
|
var o = Object.create(p, {d: {value: "own"}})
|
|
|
|
key = val = undefined
|
|
|
|
with (o) {
|
|
|
|
a = "set"
|
|
|
|
assertEquals("a", key)
|
|
|
|
assertEquals("set", val)
|
|
|
|
assertEquals("local", b)
|
|
|
|
assertEquals("global", c)
|
|
|
|
assertEquals("own", d)
|
|
|
|
b = "local"
|
|
|
|
c = "global"
|
|
|
|
d = "own"
|
|
|
|
assertEquals("a", key)
|
|
|
|
assertEquals("set", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TestWithSet({
|
|
|
|
set: function(r, k, v) { key = k; val = v; return true },
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSet({
|
|
|
|
set: function(r, k, v) { return this.set2(r, k, v) },
|
|
|
|
set2: function(r, k, v) { key = k; val = v; return true },
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSet({
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
},
|
2015-12-11 14:55:20 +00:00
|
|
|
defineProperty: function(t, k, desc) { key = k; val = desc.value }
|
2013-07-19 14:07:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSet({
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
|
|
|
return this.has2(k)
|
2013-07-19 14:07:23 +00:00
|
|
|
},
|
2015-12-11 14:55:20 +00:00
|
|
|
has2: function(k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
},
|
2015-12-11 14:55:20 +00:00
|
|
|
defineProperty: function(t, k, desc) { this.defineProperty2(k, desc) },
|
2013-07-19 14:07:23 +00:00
|
|
|
defineProperty2: function(k, desc) { key = k; val = desc.value }
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSet({
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
},
|
2015-12-11 14:55:20 +00:00
|
|
|
defineProperty: function(t, k, desc) { key = k; val = desc.value }
|
2013-07-19 14:07:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSet({
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
|
|
|
return this.has2(k) },
|
|
|
|
has2: function(k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
},
|
2015-12-11 14:55:20 +00:00
|
|
|
set: function(t, k, v) { key = k; val = v; return true }
|
2013-07-19 14:07:23 +00:00
|
|
|
}, true)
|
|
|
|
|
|
|
|
TestWithSet({
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
},
|
2015-12-11 14:55:20 +00:00
|
|
|
defineProperty: function(t, k, desc) { key = k; val = desc.value }
|
2013-07-19 14:07:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
function TestWithSetThrow(handler, hasSetter) {
|
|
|
|
TestWithProxies(TestWithSetThrow2, handler, hasSetter)
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestWithSetThrow2(create, handler, hasSetter) {
|
|
|
|
var p = create(handler)
|
2016-01-07 14:48:57 +00:00
|
|
|
assertThrowsEquals(function(){
|
2013-07-19 14:07:23 +00:00
|
|
|
with (p) {
|
|
|
|
a = 1
|
|
|
|
}
|
|
|
|
}, "myexn")
|
|
|
|
|
|
|
|
if (!hasSetter) return
|
|
|
|
|
|
|
|
var o = Object.create(p, {})
|
2016-01-07 14:48:57 +00:00
|
|
|
assertThrowsEquals(function(){
|
2013-07-19 14:07:23 +00:00
|
|
|
with (o) {
|
|
|
|
a = 1
|
|
|
|
}
|
|
|
|
}, "myexn")
|
|
|
|
}
|
|
|
|
|
|
|
|
TestWithSetThrow({
|
2015-12-11 14:55:20 +00:00
|
|
|
set: function() { throw "myexn" },
|
|
|
|
has: function(t, k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSetThrow({
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function() { throw "myexn" },
|
2013-07-19 14:07:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSetThrow({
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function() { throw "myexn" },
|
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSetThrow({
|
|
|
|
has: function(t, k) {
|
|
|
|
return k === "a"
|
2013-07-19 14:07:23 +00:00
|
|
|
},
|
2015-12-11 14:55:20 +00:00
|
|
|
defineProperty: function() { throw "myexn" }
|
2013-07-19 14:07:23 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
TestWithSetThrow({
|
2015-12-11 14:55:20 +00:00
|
|
|
has: function(t, k) {
|
|
|
|
return k === "a"
|
|
|
|
},
|
|
|
|
set: function() { throw "myexn" }
|
2013-07-19 14:07:23 +00:00
|
|
|
}, true)
|