Skip to content

Commit

Permalink
refType: force name to writable before updating it.
Browse files Browse the repository at this point in the history
When node is run in strict mode (node --use_strict), the attribute
writable property is enforced and updating "name" will fail if the
writable property is set to false.

This is to fix: #67
  • Loading branch information
kanaka authored and TooTallNate committed Jan 27, 2017
1 parent 218af8d commit 786b739
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/ref.js
Expand Up @@ -306,6 +306,11 @@ exports.refType = function refType (type) {
var rtn = Object.create(_type)
rtn.indirection++
if (_type.name) {
var props = Object.getOwnPropertyDescriptor(_type, "name")
if (props && props.writable === false) {
props.writable = true
Object.defineProperty(rtn, "name", props)
}
rtn.name = _type.name + '*'
}
return rtn
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -27,7 +27,7 @@
"main": "./lib/ref.js",
"scripts": {
"docs": "node docs/compile",
"test": "mocha -gc --reporter spec"
"test": "mocha -gc --reporter spec --use_strict"
},
"dependencies": {
"bindings": "1",
Expand Down
17 changes: 17 additions & 0 deletions test/types.js
Expand Up @@ -19,6 +19,23 @@ describe('types', function () {
assert.equal(intPtr.size, ref.types.int.size)
})

it('should override and update a read-only name property', function () {
// a type similar to ref-struct's StructType
// used for types refType name property test
function StructType() {}
StructType.size = 0
StructType.indirection = 0

// read-only name property
var oldProp = Object.getOwnPropertyDescriptor(StructType, "name")
assert.equal(oldProp.writable, false)

// name property should be writable and updated
var newObj = ref.refType(StructType)
var newProp = Object.getOwnPropertyDescriptor(newObj, "name")
assert.equal(newProp.writable, true)
assert.equal(newObj.name, "StructType*")
})
})

describe('derefType()', function () {
Expand Down

0 comments on commit 786b739

Please sign in to comment.