Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
71 lines (54 sloc) 1.9 KB
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.