Skip to content
Browse files

add support for -F, which does filtering faster, but less safely

  • Loading branch information...
1 parent d3f1dde commit e64e8f1a3c9ad7f832ca0dbdcad0e0f724c91ab4 @davepacheco davepacheco committed Nov 4, 2013
Showing with 32 additions and 3 deletions.
  1. +7 −1 bin/daggr.js
  2. +25 −2 lib/daggr.js
View
8 bin/daggr.js
@@ -56,11 +56,12 @@ function main()
var value = '1';
var action = 'print'
var mode = 'text';
+ var filtermode = 'isolate';
var outputs = null;
var filters = [];
var source, stream, args, consumer;
- parser = new mod_getopt.BasicParser('f:jk:o:v:', process.argv);
+ parser = new mod_getopt.BasicParser('Ff:jk:o:v:', process.argv);
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'k':
@@ -71,6 +72,10 @@ function main()
filters.push(option.optarg);
break;
+ case 'F':
+ filtermode = 'fast';
+ break;
+
case 'j':
mode = 'json'
break;
@@ -109,6 +114,7 @@ function main()
'value': value,
'stream': stream,
'outputs': outputs,
+ 'filtermode': filtermode,
'outstream': process.stdout
};
View
27 lib/daggr.js
@@ -95,7 +95,12 @@ function FilterStream(args, filter)
this.fs_json = args['mode'] == 'json';
this.fs_source = args['stream'];
- this.fs_script = mod_vm.createScript(filter);
+ this.fs_gofast = args['filtermode'] == 'fast';
+
+ if (this.fs_gofast)
+ this.fs_func = makeFunction(filter);
+ else
+ this.fs_script = mod_vm.createScript(filter);
this.fs_source.on('row', function (row) {
var sandbox = {};
@@ -111,7 +116,10 @@ function FilterStream(args, filter)
}
try {
- rv = fs.fs_script.runInNewContext(sandbox);
+ if (fs.fs_script)
+ rv = fs.fs_script.runInNewContext(sandbox);
+ else
+ rv = fs.fs_func.call(sandbox);
} catch (err) {
/* XXX */
console.error('warn: ' + err.name + ': ' + err.message);
@@ -127,6 +135,21 @@ function FilterStream(args, filter)
mod_util.inherits(FilterStream, mod_events.EventEmitter);
+/*
+ * This is truly awful, but it's much faster (and less safe) than
+ * vm.runInNewContext.
+ */
+function makeFunction(code)
+{
+ if (code.indexOf('return') == -1) {
+ if (code.substring(code.length - 1) == ';')
+ code = code.substring(0, code.length - 1);
+ code = 'return (' + code + ')';
+ }
+
+ return (new Function('line', code));
+}
+
function Printer(args)
{

0 comments on commit e64e8f1

Please sign in to comment.
Something went wrong with that request. Please try again.