sidorares / crconsole

Remote JavaScript console for Chrome/Webkit
MIT License
262 stars 23 forks source link

Support multi line commands (was: Input a scope in crconsole) #5

Open ShawnHuang opened 11 years ago

ShawnHuang commented 11 years ago

I think it would be better if we can type a scope in crconsole. Just like typing in chrome dev tool and chrome-remote-interface's repl.

02

03

sidorares commented 11 years ago

not sure if I get the idea. Are you talking about multi line input?

ShawnHuang commented 11 years ago

Chrome dev tool's rule is multi line input. I think it's difficult to implement. Chrome-remote-interface's repl used repl module, and its rule that I guess is to detect some keywords (function, if, while) and to let users to complete code scope. I can't differ crconsole which also use repl module from that, but I try to write a sample with the repl module that also has the same feature.

sidorares commented 11 years ago

multiline input would be tricky with repl/readline. Node starts 'multiline mode' when there is syntax error - see https://github.com/joyent/node/blob/master/lib/repl.js#L296

Not sure if that is desired behaviour at all. Maybe we should trigger this only by some errors, like "Unexpected end of input"

ShawnHuang commented 11 years ago

OK, I got it. Thanks for your reply

sidorares commented 11 years ago

Feel free to try to implement it yourself :)

ShawnHuang commented 11 years ago

I will try it:)

ShawnHuang commented 11 years ago

I try to replace the eval function with the following code, but I am not sure that there is no bugs at that code. :)

https://github.com/sidorares/crconsole/blob/master/index.js#L272

    var err, result;                                                                                                            
    try{                                                                                                                        
      var script = require("vm").createScript(cmd.slice(1, -2), {                                                                            
        filename: filename,                                                                                                     
        displayErrors: false                                                                                                    
      });                                                                                                                       
    } catch (e){                                                                                                                
      err = e;                                                                                                                  
    }                                                                                                                           
    var mess = !!err;                                                                                                           
    if(mess) cb(err, result);                                                                                                   
    else{                                                                                                                       
      this.client.Runtime.evaluate({expression: cmd.slice(1, -2), generatePreview: true}, function(err, resp) {                              
        return cb(null, resp);                                                                                                  
      });                                                                                                                       
    }
sidorares commented 11 years ago

It will throw exceptions for some valid code, for example if you reference on the right hand side variable which is defined in remote context but not defined locally. I suggest to use esprima and check if it can build complete AST as a flag

ShawnHuang commented 11 years ago

Oh!! Let me think about it. Thank you!