forked from jhs/erlang.js
-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.js
78 lines (68 loc) · 2.42 KB
/
lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
exports.VERSION_MAGIC = 131; // 131 83
exports.MAX_INTEGER = (1 << 27) - 1;
exports.MIN_INTEGER = -(1 << 27);
exports.tags = { 'SMALL_INTEGER' : 'a' // 97 61
, 'INTEGER' : 'b' // 98 62
, 'FLOAT' : 'c' // 99 63
, 'ATOM' : 'd' // 100 64
, 'SMALL_ATOM' : 's' // 115 73
, 'REFERENCE' : 'e' // 101 65
, 'NEW_REFERENCE' : 'r' // 114 72
, 'PORT' : 'f' // 102 66
, 'NEW_FLOAT' : 'F' // 70 46
, 'PID' : 'g' // 103 67
, 'SMALL_TUPLE' : 'h' // 104 68
, 'LARGE_TUPLE' : 'i' // 105 69
, 'NIL' : 'j' // 106 6a
, 'STRING' : 'k' // 107 6b
, 'LIST' : 'l' // 108 6c
, 'BINARY' : 'm' // 109 6d
, 'BIT_BINARY' : 'M' // 77 4d
, 'SMALL_BIG' : 'n' // 110 6e
, 'LARGE_BIG' : 'o' // 111 6f
, 'NEW_FUN' : 'p' // 112 70
, 'EXPORT' : 'q' // 113 71
, 'FUN' : 'u' // 117 75
}
// Actually these need to be integers to be useful.
Object.keys(exports.tags).forEach(function(key) {
exports.tags[key] = exports.tags[key].charCodeAt(0);
})
function to_s(val) {
return Object.prototype.toString.apply(val);
}
typeOf = exports.typeOf = function(value) {
var s = typeof value;
// Note: using Object.prototype.toString instead of instanceof because it's not working.
if (s === 'object') {
if (value) {
if(to_s(value) === '[object Array]')
s = 'array';
else if(to_s(value) === '[object Buffer]')
s = 'buffer';
else if(typeof Buffer === 'function' && value instanceof Buffer)
s = 'buffer';
} else
s = 'null';
}
else if(s === 'function' && to_s(value) === '[object RegExp]')
return 'regexp';
return s;
}
flatten = exports.flatten = function (ar) {
return ar.reduce(function(state, elem) {
return typeOf(elem) === 'array'
? state.concat(flatten(elem))
: state.concat([elem])
}, [])
}
exports.uint32 = function(n) {
return [ n >> 24 & 0xff
, n >> 16 & 0xff
, n >> 8 & 0xff
, n >> 0 & 0xff ]
}
exports.uint16 = function(n) {
return [ n >> 16 & 0xff
, n >> 0 & 0xff ]
}