lib/runner.js
function exec(script, numkeys) {
  var KEYS = slice.call(arguments, 2, 2 + numkeys);
  var ARGS = slice.call(arguments, numkeys + 2);

  function cb(err, reply) {
    if(err) reply = err.message;
    process.send(
      {
        err: (err instanceof Error),
        data: reply,
        str: (reply instanceof String)
    });
  }

  var redis = {
    sha1hex: sha1hex,
    error_reply: function(err) {
      return new Error(err)
    },
    status_reply: function(status) {
      return new String(status)
    },
    call: function(cmd) {
      //console.dir('calling out with: ' + cmd);
      var args = slice.call(arguments, 1)
        , callback = typeof args[args.length - 1] === 'function'
          ? args.pop() : null;

      function onMessage(msg) {
        if(cb) {
          if(msg.err) return cb(new Error(msg.data));
          cb(null, msg.data);
        }
      }
      process.once('message', onMessage);
      process.send({event: 'call', cmd: cmd, args: args});
      return callback || cb;
    },
    LOG_DEBUG: logger.DEBUG,
    LOG_VERBOSE: logger.VERBOSE,
    LOG_NOTICE: logger.NOTICE,
    LOG_WARNING: logger.WARNING,
    log: function rlog() {
      return log.log.apply(log, arguments);
    }
  }

  //console.dir(redis);

  var sandbox = {
    Error: Error,
    Buffer: Buffer,
    JSON: JSON,
    crypto: crypto,
    util: util,
    redis: redis,
    KEYS: KEYS,
    ARGS: ARGS,
    cb: cb
  }

  script.runInNewContext(sandbox);

  process.send({event: 'eval', data: sandbox.method.name});

  var result;
  try {
    result = sandbox.method(sandbox.KEYS, sandbox.ARGS, sandbox.cb);
  // coerce thrown errors
  }catch(e) {
    result = e;
  }

  //console.dir(result);
  if(Buffer.isBuffer(result)) {
    result = result.toString();
  }

  if(result
    && !(result instanceof Error)
    && typeof result === 'object') {
    if(result.err) {
      result = new Error(result.err);
    }else if(result.ok) {
      result = new String(result.ok);
    }
  }

  //console.dir(result);
  return {
    result: result,
    sandbox: sandbox,
    script: script,
    name: sandbox.method.name
  };
}
function exec(script, numkeys) {
  var KEYS = slice.call(arguments, 2, 2 + numkeys);
  var ARGS = slice.call(arguments, numkeys + 2);

  function cb(err, reply) {
    if(err) reply = err.message;
    process.send(
      {
        err: (err instanceof Error),
        data: reply,
        str: (reply instanceof String)
    });
  }

  var redis = {
    sha1hex: sha1hex,
    error_reply: function(err) {
      return new Error(err)
    },
    status_reply: function(status) {
      return new String(status)
    },
    call: function(cmd) {
      //console.dir('calling out with: ' + cmd);
      var args = slice.call(arguments, 1)
        , callback = typeof args[args.length - 1] === 'function'
          ? args.pop() : null;

      function onMessage(msg) {
        if(cb) {
          if(msg.err) return cb(new Error(msg.data));
          cb(null, msg.data);
        }
      }
      process.once('message', onMessage);
      process.send({event: 'call', cmd: cmd, args: args});
      return callback || cb;
    },
    LOG_DEBUG: logger.DEBUG,
    LOG_VERBOSE: logger.VERBOSE,
    LOG_NOTICE: logger.NOTICE,
    LOG_WARNING: logger.WARNING,
    log: function rlog() {
      return log.log.apply(log, arguments);
    }
  }

  //console.dir(redis);

  var sandbox = {
    Error: Error,
    Buffer: Buffer,
    JSON: JSON,
    crypto: crypto,
    util: util,
    redis: redis,
    KEYS: KEYS,
    ARGS: ARGS,
    cb: cb
  }

  script.runInNewContext(sandbox);

  process.send({event: 'eval', data: sandbox.method.name});

  var result;
  try {
    result = sandbox.method(sandbox.KEYS, sandbox.ARGS, sandbox.cb);
  // coerce thrown errors
  }catch(e) {
    result = e;
  }

  //console.dir(result);
  if(Buffer.isBuffer(result)) {
    result = result.toString();
  }

  if(result
    && !(result instanceof Error)
    && typeof result === 'object') {
    if(result.err) {
      result = new Error(result.err);
    }else if(result.ok) {
      result = new String(result.ok);
    }
  }

  //console.dir(result);
  return {
    result: result,
    sandbox: sandbox,
    script: script,
    name: sandbox.method.name
  };
}
function exec ( script , numkeys ) { var KEYS = slice . call ( arguments , 2 , 2 + numkeys ) ; var ARGS = slice . call ( arguments , numkeys + 2 ) ; function cb ( err , reply ) { if ( err ) reply = err . message ; process . send ( { err : ( err instanceof Error ) , data : reply , str : ( reply instanceof String ) } ) ; } var redis = { sha1hex : sha1hex , error_reply : function ( err ) { return new Error ( err ) } , status_reply : function ( status ) { return new String ( status ) } , call : function ( cmd ) { //console.dir('calling out with: ' + cmd); var args = slice . call ( arguments , 1 ) , callback = typeof args [ args . length - 1 ] === 'function' ? args . pop ( ) : null ; function onMessage ( msg ) { if ( cb ) { if ( msg . err ) return cb ( new Error ( msg . data ) ) ; cb ( null , msg . data ) ; } } process . once ( 'message' , onMessage ) ; process . send ( { event : 'call' , cmd : cmd , args : args } ) ; return callback || cb ; } , LOG_DEBUG : logger . DEBUG , LOG_VERBOSE : logger . VERBOSE , LOG_NOTICE : logger . NOTICE , LOG_WARNING : logger . WARNING , log : function rlog ( ) { return log . log . apply ( log , arguments ) ; } } //console.dir(redis); var sandbox = { Error : Error , Buffer : Buffer , JSON : JSON , crypto : crypto , util : util , redis : redis , KEYS : KEYS , ARGS : ARGS , cb : cb } script . runInNewContext ( sandbox ) ; process . send ( { event : 'eval' , data : sandbox . method . name } ) ; var result ; try { result = sandbox . method ( sandbox . KEYS , sandbox . ARGS , sandbox . cb ) ; // coerce thrown errors } catch ( e ) { result = e ; } //console.dir(result); if ( Buffer . isBuffer ( result ) ) { result = result . toString ( ) ; } if ( result && ! ( result instanceof Error ) && typeof result === 'object' ) { if ( result . err ) { result = new Error ( result . err ) ; } else if ( result . ok ) { result = new String ( result . ok ) ; } } //console.dir(result); return { result : result , sandbox : sandbox , script : script , name : sandbox . method . name } ; }
Execute a pre-compiled script.
Execute a pre - compiled script .