Skip to content

Commit f5063be

Browse files
committed
Initial Commit
0 parents  commit f5063be

File tree

10 files changed

+833
-0
lines changed

10 files changed

+833
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"plugins": ["transform-es2015-modules-commonjs"]
3+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

lib/erlang-types.js

Lines changed: 387 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "erlang-types",
3+
"version": "1.0.0",
4+
"description": "A collection of primitive types from erlang",
5+
"main": "lib/erlang-types.js",
6+
"jsnext:main": "src/index.js",
7+
"scripts": {
8+
"build": "rollup -c rollup.config.js",
9+
"test": "mocha test --recursive --compilers js:babel-core/register"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/bryanjos/erlang-types.git"
14+
},
15+
"author": "Bryan Joseph <bryanjos@gmail.com>",
16+
"license": "MIT",
17+
"devDependencies": {
18+
"babel-core": "^6.9.1",
19+
"babel-preset-es2015": "^6.9.0",
20+
"babel-preset-es2015-rollup": "^1.1.1",
21+
"chai": "^3.5.0",
22+
"mocha": "^2.5.3",
23+
"rollup": "^0.32.0",
24+
"rollup-plugin-babel": "^2.5.1"
25+
}
26+
}

rollup.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { rollup } from 'rollup';
2+
import babel from 'rollup-plugin-babel';
3+
4+
export default {
5+
entry: 'src/index.js',
6+
dest: 'lib/erlang-types.js',
7+
sourceMap: 'inline',
8+
format: 'cjs',
9+
plugins: [
10+
babel({
11+
babelrc: false
12+
})
13+
]
14+
};

src/erlang-types/bit_string.js

Lines changed: 309 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
class BitString {
2+
constructor(...args){
3+
this.value = Object.freeze(this.process(args));
4+
this.length = this.value.length;
5+
this.bit_size = this.length * 8;
6+
this.byte_size = this.length;
7+
}
8+
9+
get(index){
10+
return this.value[index];
11+
}
12+
13+
count(){
14+
return this.value.length;
15+
}
16+
17+
slice(start, end = null){
18+
let s = this.value.slice(start, end);
19+
let ms = s.map((elem) => BitString.integer(elem));
20+
return new BitString(...ms);
21+
}
22+
23+
[Symbol.iterator]() {
24+
return this.value[Symbol.iterator]();
25+
}
26+
27+
toString(){
28+
var i, s = "";
29+
for (i = 0; i < this.count(); i++) {
30+
if (s !== "") {
31+
s += ", ";
32+
}
33+
s += this.get(i).toString();
34+
}
35+
36+
return "<<" + s + ">>";
37+
}
38+
39+
process(bitStringParts){
40+
let processed_values = [];
41+
42+
var i;
43+
for (i = 0; i < bitStringParts.length; i++) {
44+
let processed_value = this['process_' + bitStringParts[i].type](bitStringParts[i]);
45+
46+
for(let attr of bitStringParts[i].attributes){
47+
processed_value = this['process_' + attr](processed_value);
48+
}
49+
50+
processed_values = processed_values.concat(processed_value);
51+
}
52+
53+
return processed_values;
54+
}
55+
56+
process_integer(value){
57+
return value.value;
58+
}
59+
60+
process_float(value){
61+
if(value.size === 64){
62+
return BitString.float64ToBytes(value.value);
63+
}else if(value.size === 32){
64+
return BitString.float32ToBytes(value.value);
65+
}
66+
67+
throw new Error('Invalid size for float');
68+
}
69+
70+
process_bitstring(value){
71+
return value.value.value;
72+
}
73+
74+
process_binary(value){
75+
return BitString.toUTF8Array(value.value);
76+
}
77+
78+
process_utf8(value){
79+
return BitString.toUTF8Array(value.value);
80+
}
81+
82+
process_utf16(value){
83+
return BitString.toUTF16Array(value.value);
84+
}
85+
86+
process_utf32(value){
87+
return BitString.toUTF32Array(value.value);
88+
}
89+
90+
process_signed(value){
91+
return (new Uint8Array([value]))[0];
92+
}
93+
94+
process_unsigned(value){
95+
return value;
96+
}
97+
98+
process_native(value){
99+
return value;
100+
}
101+
102+
process_big(value){
103+
return value;
104+
}
105+
106+
process_little(value){
107+
return value.reverse();
108+
}
109+
110+
process_size(value){
111+
return value;
112+
}
113+
114+
process_unit(value){
115+
return value;
116+
}
117+
118+
static integer(value){
119+
return BitString.wrap(value, { 'type': 'integer', 'unit': 1, 'size': 8 });
120+
}
121+
122+
static float(value){
123+
return BitString.wrap(value, { 'type': 'float', 'unit': 1, 'size': 64 });
124+
}
125+
126+
static bitstring(value){
127+
return BitString.wrap(value, { 'type': 'bitstring', 'unit': 1, 'size': value.bit_size });
128+
}
129+
130+
static bits(value){
131+
return BitString.bitstring(value);
132+
}
133+
134+
static binary(value){
135+
return BitString.wrap(value, { 'type': 'binary', 'unit': 8, 'size': value.length });
136+
}
137+
138+
static bytes(value){
139+
return BitString.binary(value);
140+
}
141+
142+
static utf8(value){
143+
return BitString.wrap(value, { 'type': 'utf8', 'unit': 1, 'size': value.length });
144+
}
145+
146+
static utf16(value){
147+
return BitString.wrap(value, { 'type': 'utf16', 'unit': 1, 'size': value.length * 2 });
148+
}
149+
150+
static utf32(value){
151+
return BitString.wrap(value, { 'type': 'utf32', 'unit': 1, 'size': value.length * 4 });
152+
}
153+
154+
static signed(value){
155+
return BitString.wrap(value, {}, 'signed');
156+
}
157+
158+
static unsigned(value){
159+
return BitString.wrap(value, {}, 'unsigned');
160+
}
161+
162+
static native(value){
163+
return BitString.wrap(value, {}, 'native');
164+
}
165+
166+
static big(value){
167+
return BitString.wrap(value, {}, 'big');
168+
}
169+
170+
static little(value){
171+
return BitString.wrap(value, {}, 'little');
172+
}
173+
174+
static size(value, count){
175+
return BitString.wrap(value, {'size': count});
176+
}
177+
178+
static unit(value, count){
179+
return BitString.wrap(value, {'unit': count});
180+
}
181+
182+
static wrap(value, opt, new_attribute = null){
183+
let the_value = value;
184+
185+
if(!(value instanceof Object)){
186+
the_value = {'value': value, 'attributes': []};
187+
}
188+
189+
the_value = Object.assign(the_value, opt);
190+
191+
if(new_attribute){
192+
the_value.attributes.push(new_attribute);
193+
}
194+
195+
196+
return the_value;
197+
}
198+
199+
static toUTF8Array(str) {
200+
var utf8 = [];
201+
for (var i = 0; i < str.length; i++) {
202+
var charcode = str.charCodeAt(i);
203+
if (charcode < 0x80){
204+
utf8.push(charcode);
205+
}
206+
else if (charcode < 0x800) {
207+
utf8.push(0xc0 | (charcode >> 6),
208+
0x80 | (charcode & 0x3f));
209+
}
210+
else if (charcode < 0xd800 || charcode >= 0xe000) {
211+
utf8.push(0xe0 | (charcode >> 12),
212+
0x80 | ((charcode >> 6) & 0x3f),
213+
0x80 | (charcode & 0x3f));
214+
}
215+
// surrogate pair
216+
else {
217+
i++;
218+
// UTF-16 encodes 0x10000-0x10FFFF by
219+
// subtracting 0x10000 and splitting the
220+
// 20 bits of 0x0-0xFFFFF into two halves
221+
charcode = 0x10000 + (((charcode & 0x3ff) << 10)
222+
| (str.charCodeAt(i) & 0x3ff));
223+
utf8.push(0xf0 | (charcode >> 18),
224+
0x80 | ((charcode >> 12) & 0x3f),
225+
0x80 | ((charcode >> 6) & 0x3f),
226+
0x80 | (charcode & 0x3f));
227+
}
228+
}
229+
return utf8;
230+
}
231+
232+
static toUTF16Array(str) {
233+
var utf16 = [];
234+
for (var i = 0; i < str.length; i++) {
235+
var codePoint = str.codePointAt(i);
236+
237+
if(codePoint <= 255){
238+
utf16.push(0);
239+
utf16.push(codePoint);
240+
}else{
241+
utf16.push(((codePoint >> 8) & 0xFF));
242+
utf16.push((codePoint & 0xFF));
243+
}
244+
}
245+
return utf16;
246+
}
247+
248+
249+
static toUTF32Array(str) {
250+
var utf32 = [];
251+
for (var i = 0; i < str.length; i++) {
252+
var codePoint = str.codePointAt(i);
253+
254+
if(codePoint <= 255){
255+
utf32.push(0);
256+
utf32.push(0);
257+
utf32.push(0);
258+
utf32.push(codePoint);
259+
}else{
260+
utf32.push(0);
261+
utf32.push(0);
262+
utf32.push(((codePoint >> 8) & 0xFF));
263+
utf32.push((codePoint & 0xFF));
264+
}
265+
}
266+
return utf32;
267+
}
268+
269+
//http://stackoverflow.com/questions/2003493/javascript-float-from-to-bits
270+
static float32ToBytes(f) {
271+
var bytes = [];
272+
273+
var buf = new ArrayBuffer(4);
274+
(new Float32Array(buf))[0] = f;
275+
276+
let intVersion = (new Uint32Array(buf))[0];
277+
278+
bytes.push(((intVersion >> 24) & 0xFF));
279+
bytes.push(((intVersion >> 16) & 0xFF));
280+
bytes.push(((intVersion >> 8) & 0xFF));
281+
bytes.push((intVersion & 0xFF));
282+
283+
return bytes;
284+
}
285+
286+
static float64ToBytes(f) {
287+
var bytes = [];
288+
289+
var buf = new ArrayBuffer(8);
290+
(new Float64Array(buf))[0] = f;
291+
292+
var intVersion1 = (new Uint32Array(buf))[0];
293+
var intVersion2 = (new Uint32Array(buf))[1];
294+
295+
bytes.push(((intVersion2 >> 24) & 0xFF));
296+
bytes.push(((intVersion2 >> 16) & 0xFF));
297+
bytes.push(((intVersion2 >> 8) & 0xFF));
298+
bytes.push((intVersion2 & 0xFF));
299+
300+
bytes.push(((intVersion1 >> 24) & 0xFF));
301+
bytes.push(((intVersion1 >> 16) & 0xFF));
302+
bytes.push(((intVersion1 >> 8) & 0xFF));
303+
bytes.push((intVersion1 & 0xFF));
304+
305+
return bytes;
306+
}
307+
}
308+
309+
export default BitString;

src/erlang-types/pid.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let process_counter = -1;
2+
3+
class PID {
4+
constructor(){
5+
process_counter = process_counter + 1;
6+
this.id = process_counter;
7+
}
8+
9+
toString(){
10+
return "PID#<0." + this.id + ".0>";
11+
}
12+
}
13+
14+
export default PID;

src/erlang-types/reference.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
let ref_counter = -1;
2+
3+
class Reference {
4+
constructor(){
5+
ref_counter = ref_counter + 1;
6+
this.id = ref_counter;
7+
this.ref = Symbol();
8+
}
9+
10+
toString(){
11+
return "Ref#<0.0.0." + this.id + ">";
12+
}
13+
}
14+
15+
16+
export default Reference;

0 commit comments

Comments
 (0)