diff --git a/js/__tests__/decoder-test.js b/js/__tests__/decoder-test.js index 0d43760..84833d6 100644 --- a/js/__tests__/decoder-test.js +++ b/js/__tests__/decoder-test.js @@ -103,6 +103,10 @@ describe('unpacks', () => { expect(erlpack.unpack(Buffer.from('\x83d\x00\x0Dguild members', 'binary'))).toEqual("guild members"); }); + it('utf8 atoms', () => { + expect(erlpack.unpack(Buffer.from('\x83w\x05jos\xc3\xa9', 'binary'))).toEqual('josé'); + }); + it('tuples', () => { expect(erlpack.unpack(Buffer.from('\x83h\x03m\x00\x00\x00\x06vanisha\x01a\x04', 'binary'))).toEqual(['vanish', 1, 4]); expect(erlpack.unpack(Buffer.from('\x83i\x00\x00\x00\x03m\x00\x00\x00\x06vanisha\x01a\x04', 'binary'))).toEqual(['vanish', 1, 4]); diff --git a/js/decoder.h b/js/decoder.h index 48882f5..69bf48a 100644 --- a/js/decoder.h +++ b/js/decoder.h @@ -157,7 +157,7 @@ class Decoder { return (const char*)str; } - Local processAtom(const char* atom, uint16_t length) { + Local processAtom(const char* atom, uint16_t length, bool utf8) { if (atom == NULL) { return Nan::Undefined(); } @@ -177,19 +177,22 @@ class Decoder { } } + if (utf8) { + return Nan::Encode(atom, length, Nan::Encoding::UTF8); + } return Nan::New(atom, length).ToLocalChecked(); } - Local decodeAtom() { + Local decodeAtom(bool utf8) { auto length = read16(); const char* atom = readString(length); - return processAtom(atom, length); + return processAtom(atom, length, utf8); } - Local decodeSmallAtom() { + Local decodeSmallAtom(bool utf8) { auto length = read8(); const char* atom = readString(length); - return processAtom(atom, length); + return processAtom(atom, length, utf8); } Local decodeFloat() { @@ -410,9 +413,13 @@ class Decoder { case NEW_FLOAT_EXT: return decodeNewFloat(); case ATOM_EXT: - return decodeAtom(); + return decodeAtom(false); + case ATOM_UTF8_EXT: + return decodeAtom(true); case SMALL_ATOM_EXT: - return decodeSmallAtom(); + return decodeSmallAtom(false); + case SMALL_ATOM_UTF8_EXT: + return decodeSmallAtom(true); case SMALL_TUPLE_EXT: return decodeSmallTuple(); case LARGE_TUPLE_EXT: