-
Notifications
You must be signed in to change notification settings - Fork 12
/
sketches
70 lines (54 loc) · 1.9 KB
/
sketches
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
bitsyntax(
"Size:32/integer, Frame:Size/binary, _/binary", function(vars) {
//...
},
// ...
true, function() {
//...
});
// Now we'd like to avoid making this a value then applying
// it. Possibly we can compile it and cache it at the call site (but
// then, how do we identify it if there's more than one?).
// NB in general we will want to compile groups of patterns, because
// there is more opportunity for optimisation when compiling a group
// of patterns into one procedure. (Just doing things in sequence may
// result in repeated computations).
// Construct dispatcher:
var matcher = bitsyntax(
"Size:32/integer, Frame:Size/binary, _/binary", function(vars) {
//...
},
// ...
true, function() {
//...
});
matcher.match(binary, freevars);
// In this case the compiled patterns can be memoised, all together, in matcher.
bitsyntax(
"Size:32/integer, Frame:Size/binary, _/binary", function(vars) {
//...
},
// ...
true, function() {
//...
}).match(binary) // -> vars
// Getting free variables from the environment. This or very similar is basically the only way: the Function constructor refers only to the global scope.
function foo() {
return new Function('bar', "return function() { return bar + 'ooo' };")(eval("bar"));
}
// === Composite buffers
// May help with parsing, and avoiding consing new buffers, although
// that is pretty cheap if they are small (they are allocated out of
// 8k shared pools and copy uses memmove. Although, how do those get
// collected?)
var buf = new Composite(b1, b2, b3);
buf.length // = b1.length + b2.length + b3.length
buf.readUIntBlah();
buf.bufferSlice(...);
// and so on.
// === Constructors
// Helpful to use patterns for constructing buffers too
var cons = bs.constructor('a/binary, b/binary');
var newbuf = cons({a: buf1, b: buf2});
May also be useful to be able to write into a buffer, maybe returning
the bytes written.