forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-token.js
More file actions
161 lines (131 loc) · 3.44 KB
/
read-token.js
File metadata and controls
161 lines (131 loc) · 3.44 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// read-token.js
var ReadFormat = require("./read-format");
exports.getReadToken = getReadToken;
function getReadToken(options) {
var format = ReadFormat.getReadFormat(options);
if (options && options.useraw) {
return init_useraw(format);
} else {
return init_token(format);
}
}
function init_token(format) {
var i;
var token = new Array(256);
// positive fixint -- 0x00 - 0x7f
for (i = 0x00; i <= 0x7f; i++) {
token[i] = constant(i);
}
// fixmap -- 0x80 - 0x8f
for (i = 0x80; i <= 0x8f; i++) {
token[i] = fix(i - 0x80, format.map);
}
// fixarray -- 0x90 - 0x9f
for (i = 0x90; i <= 0x9f; i++) {
token[i] = fix(i - 0x90, format.array);
}
// fixstr -- 0xa0 - 0xbf
for (i = 0xa0; i <= 0xbf; i++) {
token[i] = fix(i - 0xa0, format.str);
}
// nil -- 0xc0
token[0xc0] = constant(null);
// (never used) -- 0xc1
token[0xc1] = null;
// false -- 0xc2
// true -- 0xc3
token[0xc2] = constant(false);
token[0xc3] = constant(true);
// bin 8 -- 0xc4
// bin 16 -- 0xc5
// bin 32 -- 0xc6
token[0xc4] = flex(format.uint8, format.bin);
token[0xc5] = flex(format.uint16, format.bin);
token[0xc6] = flex(format.uint32, format.bin);
// ext 8 -- 0xc7
// ext 16 -- 0xc8
// ext 32 -- 0xc9
token[0xc7] = flex(format.uint8, format.ext);
token[0xc8] = flex(format.uint16, format.ext);
token[0xc9] = flex(format.uint32, format.ext);
// float 32 -- 0xca
// float 64 -- 0xcb
token[0xca] = format.float32;
token[0xcb] = format.float64;
// uint 8 -- 0xcc
// uint 16 -- 0xcd
// uint 32 -- 0xce
// uint 64 -- 0xcf
token[0xcc] = format.uint8;
token[0xcd] = format.uint16;
token[0xce] = format.uint32;
token[0xcf] = format.uint64;
// int 8 -- 0xd0
// int 16 -- 0xd1
// int 32 -- 0xd2
// int 64 -- 0xd3
token[0xd0] = format.int8;
token[0xd1] = format.int16;
token[0xd2] = format.int32;
token[0xd3] = format.int64;
// fixext 1 -- 0xd4
// fixext 2 -- 0xd5
// fixext 4 -- 0xd6
// fixext 8 -- 0xd7
// fixext 16 -- 0xd8
token[0xd4] = fix(1, format.ext);
token[0xd5] = fix(2, format.ext);
token[0xd6] = fix(4, format.ext);
token[0xd7] = fix(8, format.ext);
token[0xd8] = fix(16, format.ext);
// str 8 -- 0xd9
// str 16 -- 0xda
// str 32 -- 0xdb
token[0xd9] = flex(format.uint8, format.str);
token[0xda] = flex(format.uint16, format.str);
token[0xdb] = flex(format.uint32, format.str);
// array 16 -- 0xdc
// array 32 -- 0xdd
token[0xdc] = flex(format.uint16, format.array);
token[0xdd] = flex(format.uint32, format.array);
// map 16 -- 0xde
// map 32 -- 0xdf
token[0xde] = flex(format.uint16, format.map);
token[0xdf] = flex(format.uint32, format.map);
// negative fixint -- 0xe0 - 0xff
for (i = 0xe0; i <= 0xff; i++) {
token[i] = constant(i - 0x100);
}
return token;
}
function init_useraw(format) {
var i;
var token = init_token(format).slice();
// raw 8 -- 0xd9
// raw 16 -- 0xda
// raw 32 -- 0xdb
token[0xd9] = token[0xc4];
token[0xda] = token[0xc5];
token[0xdb] = token[0xc6];
// fixraw -- 0xa0 - 0xbf
for (i = 0xa0; i <= 0xbf; i++) {
token[i] = fix(i - 0xa0, format.bin);
}
return token;
}
function constant(value) {
return function() {
return value;
};
}
function flex(lenFunc, decodeFunc) {
return function(decoder) {
var len = lenFunc(decoder);
return decodeFunc(decoder, len);
};
}
function fix(len, method) {
return function(decoder) {
return method(decoder, len);
};
}