Permalink
Browse files

added support for writing JS strings to the "char" and "uchar" types

  • Loading branch information...
TooTallNate committed May 30, 2012
1 parent 313157f commit f9b4aac204db28547f0cfa78d12f8d01f8380f93
Showing with 23 additions and 0 deletions.
  1. +6 −0 lib/ref.js
  2. +17 −0 test/char.js
View
@@ -337,6 +337,9 @@ exports.types.int8 = {
return buf.readInt8(offset || 0)
}
, set: function set (buf, offset, val) {
+ if (typeof val === 'string') {
+ val = val.charCodeAt(0)
+ }
return buf.writeInt8(val, offset || 0)
}
}
@@ -347,6 +350,9 @@ exports.types.uint8 = {
return buf.readUInt8(offset || 0)
}
, set: function set (buf, offset, val) {
+ if (typeof val === 'string') {
+ val = val.charCodeAt(0)
+ }
return buf.writeUInt8(val, offset || 0)
}
}
View
@@ -0,0 +1,17 @@
+
+var assert = require('assert')
+var ref = require('../')
+
+describe('char', function () {
+
+ it('should accept a JS String, and write the first char\'s code', function () {
+ var val = 'a'
+
+ var buf = ref.alloc('char', val)
+ assert.strictEqual(val.charCodeAt(0), buf.deref())
+
+ buf = ref.alloc('uchar', val)
+ assert.strictEqual(val.charCodeAt(0), buf.deref())
+ })
+
+})

0 comments on commit f9b4aac

Please sign in to comment.