1'use strict'; 2const common = require('../../common'); 3const assert = require('assert'); 4const readonlyErrorRE = 5 /^TypeError: Cannot assign to read only property '.*' of object '#<Object>'$/; 6const getterOnlyErrorRE = 7 /^TypeError: Cannot set property .* of #<Object> which has only a getter$/; 8 9// Testing api calls for defining properties 10const test_object = require(`./build/${common.buildType}/test_properties`); 11 12assert.strictEqual(test_object.echo('hello'), 'hello'); 13 14test_object.readwriteValue = 1; 15assert.strictEqual(test_object.readwriteValue, 1); 16test_object.readwriteValue = 2; 17assert.strictEqual(test_object.readwriteValue, 2); 18 19assert.throws(() => { test_object.readonlyValue = 3; }, readonlyErrorRE); 20 21assert.ok(test_object.hiddenValue); 22 23// Properties with napi_enumerable attribute should be enumerable. 24const propertyNames = []; 25for (const name in test_object) { 26 propertyNames.push(name); 27} 28assert.ok(propertyNames.includes('echo')); 29assert.ok(propertyNames.includes('readwriteValue')); 30assert.ok(propertyNames.includes('readonlyValue')); 31assert.ok(!propertyNames.includes('hiddenValue')); 32assert.ok(propertyNames.includes('NameKeyValue')); 33assert.ok(!propertyNames.includes('readwriteAccessor1')); 34assert.ok(!propertyNames.includes('readwriteAccessor2')); 35assert.ok(!propertyNames.includes('readonlyAccessor1')); 36assert.ok(!propertyNames.includes('readonlyAccessor2')); 37 38// Validate property created with symbol 39const start = 'Symbol('.length; 40const end = start + 'NameKeySymbol'.length; 41const symbolDescription = 42 String(Object.getOwnPropertySymbols(test_object)[0]).slice(start, end); 43assert.strictEqual(symbolDescription, 'NameKeySymbol'); 44 45// The napi_writable attribute should be ignored for accessors. 46const readwriteAccessor1Descriptor = 47 Object.getOwnPropertyDescriptor(test_object, 'readwriteAccessor1'); 48const readonlyAccessor1Descriptor = 49 Object.getOwnPropertyDescriptor(test_object, 'readonlyAccessor1'); 50assert.ok(readwriteAccessor1Descriptor.get != null); 51assert.ok(readwriteAccessor1Descriptor.set != null); 52assert.ok(readwriteAccessor1Descriptor.value === undefined); 53assert.ok(readonlyAccessor1Descriptor.get != null); 54assert.ok(readonlyAccessor1Descriptor.set === undefined); 55assert.ok(readonlyAccessor1Descriptor.value === undefined); 56test_object.readwriteAccessor1 = 1; 57assert.strictEqual(test_object.readwriteAccessor1, 1); 58assert.strictEqual(test_object.readonlyAccessor1, 1); 59assert.throws(() => { test_object.readonlyAccessor1 = 3; }, getterOnlyErrorRE); 60test_object.readwriteAccessor2 = 2; 61assert.strictEqual(test_object.readwriteAccessor2, 2); 62assert.strictEqual(test_object.readonlyAccessor2, 2); 63assert.throws(() => { test_object.readonlyAccessor2 = 3; }, getterOnlyErrorRE); 64 65assert.strictEqual(test_object.hasNamedProperty(test_object, 'echo'), true); 66assert.strictEqual(test_object.hasNamedProperty(test_object, 'hiddenValue'), 67 true); 68assert.strictEqual(test_object.hasNamedProperty(test_object, 'doesnotexist'), 69 false); 70