2023-12-18 13:12:25 +08:00
|
|
|
'use strict';
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var mockProperty = require('mock-property');
|
|
|
|
|
2023-12-18 13:12:25 +08:00
|
|
|
module.exports = function (getDescriptors, t) {
|
|
|
|
var enumDescriptor = {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: false,
|
|
|
|
value: true,
|
|
|
|
writable: false
|
|
|
|
};
|
|
|
|
var writableDescriptor = {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value: 42,
|
|
|
|
writable: true
|
|
|
|
};
|
|
|
|
|
|
|
|
t.test('works with Object.prototype poisoned setter', { skip: !Object.defineProperty }, function (st) {
|
|
|
|
var key = 'foo';
|
|
|
|
|
|
|
|
var obj = {};
|
|
|
|
obj[key] = 42;
|
|
|
|
|
|
|
|
var expected = {};
|
|
|
|
expected[key] = {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value: 42,
|
|
|
|
writable: true
|
|
|
|
};
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
st.teardown(mockProperty(Object.prototype, key, { set: function (v) { throw new Error(v); } }));
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
var hasOwnNamesBug = false;
|
|
|
|
try {
|
|
|
|
Object.getOwnPropertyNames(obj);
|
|
|
|
} catch (e) {
|
|
|
|
// v8 in node 0.6 - 0.12 has a bug :-(
|
|
|
|
hasOwnNamesBug = true;
|
|
|
|
st.comment('SKIP: this engine has a bug with Object.getOwnPropertyNames: it can not handle a throwing setter on Object.prototype.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasOwnNamesBug) {
|
|
|
|
st.doesNotThrow(function () {
|
|
|
|
var result = getDescriptors(obj);
|
|
|
|
st.deepEqual(result, expected, 'got expected descriptors');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
delete Object.prototype[key];
|
|
|
|
st.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
t.test('gets all expected non-Symbol descriptors', function (st) {
|
|
|
|
var obj = { normal: Infinity };
|
|
|
|
Object.defineProperty(obj, 'enumerable', enumDescriptor);
|
|
|
|
Object.defineProperty(obj, 'writable', writableDescriptor);
|
|
|
|
|
|
|
|
var descriptors = getDescriptors(obj);
|
|
|
|
|
|
|
|
st.deepEqual(descriptors, {
|
|
|
|
enumerable: enumDescriptor,
|
|
|
|
normal: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value: Infinity,
|
|
|
|
writable: true
|
|
|
|
},
|
|
|
|
writable: writableDescriptor
|
|
|
|
});
|
|
|
|
st.end();
|
|
|
|
});
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
var supportsSymbols = typeof Symbol === 'function' && typeof Symbol('foo') === 'symbol';
|
2023-12-18 13:12:25 +08:00
|
|
|
t.test('gets Symbol descriptors too', { skip: !supportsSymbols }, function (st) {
|
2024-01-16 21:26:16 +08:00
|
|
|
var symbol = Symbol('sym');
|
2023-12-18 13:12:25 +08:00
|
|
|
var symDescriptor = {
|
|
|
|
configurable: false,
|
|
|
|
enumerable: true,
|
|
|
|
value: [symbol],
|
|
|
|
writable: true
|
|
|
|
};
|
|
|
|
var obj = { normal: Infinity };
|
|
|
|
Object.defineProperty(obj, 'enumerable', enumDescriptor);
|
|
|
|
Object.defineProperty(obj, 'writable', writableDescriptor);
|
|
|
|
Object.defineProperty(obj, 'symbol', symDescriptor);
|
|
|
|
|
|
|
|
var descriptors = getDescriptors(obj);
|
|
|
|
|
|
|
|
st.deepEqual(descriptors, {
|
|
|
|
enumerable: enumDescriptor,
|
|
|
|
normal: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
value: Infinity,
|
|
|
|
writable: true
|
|
|
|
},
|
|
|
|
symbol: symDescriptor,
|
|
|
|
writable: writableDescriptor
|
|
|
|
});
|
|
|
|
st.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
var supportsProxy = typeof Proxy === 'function';
|
|
|
|
t.test('Proxies that return an undefined descriptor', { skip: !supportsProxy }, function (st) {
|
|
|
|
var obj = { foo: true };
|
|
|
|
var fooDescriptor = Object.getOwnPropertyDescriptor(obj, 'foo');
|
|
|
|
|
|
|
|
var proxy = new Proxy(obj, {
|
|
|
|
getOwnPropertyDescriptor: function (target, key) {
|
|
|
|
return Object.getOwnPropertyDescriptor(target, key);
|
|
|
|
},
|
|
|
|
ownKeys: function () {
|
2024-01-16 21:26:16 +08:00
|
|
|
return ['foo', 'bar'];
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
st.deepEqual(getDescriptors(proxy), { foo: fooDescriptor }, 'object has no descriptors');
|
|
|
|
st.end();
|
|
|
|
});
|
|
|
|
};
|