Skip to content

Commit dc00d1d

Browse files
committed
Make sure that is_atom considers true, false, and null as atoms
1 parent 482b875 commit dc00d1d

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/javascript/lib/core/erlang_compat/erlang.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,39 @@ import lists from './lists';
44

55
const selfPID = new ErlangTypes.PID();
66

7-
function atom_to_list(atom) {
8-
return Symbol.keyFor(atom);
9-
}
10-
117
function atom_to_binary(atom, encoding = Symbol.for('utf8')) {
128
if (encoding !== Symbol.for('utf8')) {
139
throw new Error(`unsupported encoding ${encoding}`);
1410
}
1511

16-
if (atom.__MODULE__) {
12+
if (atom === null) {
13+
return 'nil';
14+
} else if (is_boolean(atom)) {
15+
return atom.toString();
16+
} else if (atom.__MODULE__) {
1717
return Symbol.keyFor(atom.__MODULE__);
1818
}
1919

2020
return Symbol.keyFor(atom);
2121
}
2222

23+
function atom_to_list(atom) {
24+
return atom_to_binary(atom);
25+
}
26+
2327
function binary_to_atom(binary, encoding = Symbol.for('utf8')) {
2428
if (encoding !== Symbol.for('utf8')) {
2529
throw new Error(`unsupported encoding ${encoding}`);
2630
}
2731

32+
if (binary === 'nil') {
33+
return null;
34+
} else if (binary === 'true') {
35+
return true;
36+
} else if (binary === 'false') {
37+
return false;
38+
}
39+
2840
return Symbol.for(binary);
2941
}
3042

@@ -87,8 +99,16 @@ function bxor(left, right) {
8799
}
88100

89101
function is_atom(value) {
102+
if (value === null) {
103+
return true;
104+
} else if (is_boolean(value)) {
105+
return true;
106+
}
107+
90108
return (
91-
typeof value === 'symbol' || value instanceof Symbol || value.__MODULE__
109+
typeof value === 'symbol' ||
110+
value instanceof Symbol ||
111+
value.__MODULE__ != null
92112
);
93113
}
94114

src/javascript/tests/core/erlang_compat/erlang_spec.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import test from 'ava';
22
import Core from '../../../lib/core';
33

4+
test('is_atom', t => {
5+
t.is(Core.erlang.is_atom(null), true);
6+
t.is(Core.erlang.is_atom(true), true);
7+
t.is(Core.erlang.is_atom(false), true);
8+
t.is(Core.erlang.is_atom(Symbol.for('error')), true);
9+
t.is(Core.erlang.is_atom('Hello'), false);
10+
});
11+
412
test('atom_to_binary', t => {
513
t.is(Core.erlang.atom_to_binary(Symbol.for('error')), 'error');
614
t.is(
715
Core.erlang.atom_to_binary(Symbol.for('error'), Symbol.for('utf8')),
816
'error'
917
);
1018

19+
t.is(Core.erlang.atom_to_binary(null, Symbol.for('utf8')), 'nil');
20+
21+
t.is(Core.erlang.atom_to_binary(true, Symbol.for('utf8')), 'true');
22+
t.is(Core.erlang.atom_to_binary(false, Symbol.for('utf8')), 'false');
23+
1124
t.throws(
1225
() => Core.erlang.atom_to_binary(Symbol.for('error'), Symbol.for('utf16')),
1326
Error
@@ -23,7 +36,7 @@ test('list_concatenation', t => {
2336
3,
2437
4,
2538
5,
26-
6,
39+
6
2740
]);
2841
});
2942

0 commit comments

Comments
 (0)