Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions js/__tests__/decoder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
21 changes: 14 additions & 7 deletions js/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Decoder {
return (const char*)str;
}

Local<Value> processAtom(const char* atom, uint16_t length) {
Local<Value> processAtom(const char* atom, uint16_t length, bool utf8) {
if (atom == NULL) {
return Nan::Undefined();
}
Expand All @@ -177,19 +177,22 @@ class Decoder {
}
}

if (utf8) {
return Nan::Encode(atom, length, Nan::Encoding::UTF8);
}
return Nan::New(atom, length).ToLocalChecked();
}

Local<Value> decodeAtom() {
Local<Value> decodeAtom(bool utf8) {
auto length = read16();
const char* atom = readString(length);
return processAtom(atom, length);
return processAtom(atom, length, utf8);
}

Local<Value> decodeSmallAtom() {
Local<Value> decodeSmallAtom(bool utf8) {
auto length = read8();
const char* atom = readString(length);
return processAtom(atom, length);
return processAtom(atom, length, utf8);
}

Local<Value> decodeFloat() {
Expand Down Expand Up @@ -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:
Expand Down