Skip to content

Commit cc874f3

Browse files
authored
Merge pull request #375 from elixirscript/add_erlang_functions
Add math.log2/1, erlang.nodes/0 and erlang.nodes/1
2 parents e0ace72 + 17805e1 commit cc874f3

5 files changed

Lines changed: 58 additions & 2 deletions

File tree

src/javascript/lib/core.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import io from './core/erlang_compat/io';
1111
import binary from './core/erlang_compat/binary';
1212
import unicode from './core/erlang_compat/unicode';
1313
import Store from './core/store';
14+
import math from './core/erlang_compat/math';
1415

1516
class Integer {}
1617
class Float {}
@@ -51,5 +52,6 @@ export default {
5152
io,
5253
binary,
5354
unicode,
54-
elixir_config
55+
elixir_config,
56+
math
5557
};

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,19 @@ function node() {
360360
return Symbol.for('nonode@nohost');
361361
}
362362

363+
function nodes(arg = []) {
364+
const nodeTypes = Array.isArray(arg) ? arg : [arg];
365+
const nodesFound = [];
366+
367+
for (const nodeType of nodeTypes) {
368+
if (nodeType === Symbol.for('this')) {
369+
nodesFound.push(Symbol.for('nonode@nohost'));
370+
console.log(nodesFound);
371+
}
372+
}
373+
return nodesFound;
374+
}
375+
363376
function self() {
364377
return selfPID;
365378
}
@@ -458,5 +471,6 @@ export default {
458471
error,
459472
exit,
460473
raise,
461-
list_to_binary
474+
list_to_binary,
475+
nodes
462476
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function log2(x) {
2+
return Math.log2(x);
3+
}
4+
5+
export default {
6+
log2
7+
};

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,23 @@ test('list_subtraction', t => {
9595
t.deepEqual(Core.erlang.list_subtraction([1, 2, 3], [1, 2, 3]), []);
9696
t.deepEqual(Core.erlang.list_subtraction([1, 2, 3], [1, 2]), [3]);
9797
});
98+
99+
test('node', t => {
100+
t.deepEqual(Core.erlang.node(), Symbol.for('nonode@nohost'));
101+
});
102+
103+
test('nodes/0', t => {
104+
t.deepEqual(Core.erlang.nodes(), []);
105+
});
106+
107+
test('nodes/1', t => {
108+
t.deepEqual(Core.erlang.nodes(Symbol.for('this')), [
109+
Symbol.for('nonode@nohost')
110+
]);
111+
112+
t.deepEqual(Core.erlang.nodes([Symbol.for('this')]), [
113+
Symbol.for('nonode@nohost')
114+
]);
115+
116+
t.deepEqual(Core.erlang.nodes([Symbol.for('connected')]), []);
117+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import test from 'ava';
2+
import Core from '../../../lib/core';
3+
4+
test('log2/1', t => {
5+
let result = Core.math.log2(1);
6+
t.is(result, 0);
7+
8+
result = Core.math.log2(2);
9+
t.is(result, 1);
10+
11+
result = Core.math.log2(4);
12+
t.is(result, 2);
13+
});

0 commit comments

Comments
 (0)