Skip to content
This repository has been archived by the owner on May 8, 2018. It is now read-only.

Commit

Permalink
Move eval from a legacy action to POST /eval.
Browse files Browse the repository at this point in the history
  • Loading branch information
eventualbuddha committed Mar 18, 2012
1 parent e51713d commit 4cfd4c9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
57 changes: 25 additions & 32 deletions application/chrome/content/wesabe/download/Controller.coffee
Expand Up @@ -38,10 +38,33 @@ class Controller
@[action]? @server.request.json.body, (resdata) => @[action]? @server.request.json.body, (resdata) =>
@server.deliver resdata @server.deliver resdata
else else
throw new Server.NotFoundError "Unrecognized request action #{inspect action}" message = "Unrecognized legacy action #{inspect action}"
logger.error message
throw new Server.NotFoundError message


return true


@server.post '/eval', =>
params = @server.request.json
script = params.script
@scope ||=
logger: Logger.loggerForFile('repl'),
__filename: 'repl',
__dirname: '.'
@scope.job = @job

if params.type is 'text/coffeescript'
script = string.trim(CoffeeScript.compile "(-> #{script})()", bare: on)
else
script = "(function(){#{script}})()" if /[;\n]/.test script

script = "return #{script}"
result = func.callWithScope script, @scope, @scope

@scope._ = result

@server.deliver {result: inspect(result, undefined, undefined, color: params.color)}

return true


job_start: (data, respond) -> job_start: (data, respond) ->
try try
Expand Down Expand Up @@ -202,34 +225,4 @@ class Controller
status: 'error' status: 'error'
error: e.toString() error: e.toString()


eval: (data, respond) ->
try
script = data.script
@scope ||=
logger: Logger.loggerForFile('repl'),
__filename: 'repl',
__dirname: '.'
@scope.job = @job

if data.type is 'text/coffeescript'
script = string.trim(CoffeeScript.compile "(-> #{script})()", bare: on)
else
script = "(function(){#{script}})()" if /[;\n]/.test script

script = "return #{script}"
result = func.callWithScope script, @scope, @scope

@scope._ = result

respond response:
status: 'ok'
eval: inspect(result, undefined, undefined, color: data.color)

catch e
logger.error 'eval: error: ', e
respond response:
status: 'error'
error: e.toString()


module.exports = Controller module.exports = Controller
5 changes: 4 additions & 1 deletion script/api.rb
Expand Up @@ -14,7 +14,10 @@ def port=(port)
end end


def profile def profile
self['eval', {:script => '(require "io/Dir").profile.path', :type => 'text/coffeescript'}] response = post '/eval', :script => '(require "io/Dir").profile.path',
:type => 'text/coffeescript'

JSON.parse(response.body)['result']
end end


def post(path, data=nil) def post(path, data=nil)
Expand Down
2 changes: 1 addition & 1 deletion script/console.rb
Expand Up @@ -70,7 +70,7 @@ def start_script_repl(command, type)


history << script history << script
begin begin
puts Api['eval', {:script => script, :type => type, :color => $stdin.tty?}] puts post('/eval', :script => script, :type => type, :color => $stdin.tty?)['result']
rescue Api::Error => e rescue Api::Error => e
puts e puts e
rescue => e rescue => e
Expand Down

0 comments on commit 4cfd4c9

Please sign in to comment.