 compileJavascript = (js, go) ->
   debug js
   try
-    closure = new Function '_h2o_context_', '_h2o_routines_', '_h2o_results_', js
+    closure = new Function 'h2o', '_h2o_context_', '_h2o_results_', js
     go null, closure
   catch error
     go

executeJavascript = (sandbox) ->
  (closure, go) ->
    try
      go null, closure sandbox.context, sandbox.routines, sandbox.results
    catch error
      go
        message: 'Error executing javascript'

      cause: error


createGlobalScope = (rootScope, routines) ->
  globalScope = {}

  for name, identifier of rootScope
    globalScope[name] = identifier

  for name of routines
    globalScope[name] = name: name, object: 'h2o'

  globalScope

rewriteJavascript = (sandbox) ->
  (rootScope, program, go) ->
    globalScope = createGlobalScope rootScope, sandbox.routines 

    try
      traverseJavascriptScoped [ globalScope ], globalScope, null, null, program, (globalScope, parent, key, node) ->
        if node.type is 'Identifier'
          return if parent.type is 'VariableDeclarator' and key is 'id' # ignore var declarations
          return if key is 'property' # ignore members
          return unless identifier = globalScope[node.name]

          # qualify identifier with '_h2o_context_'
          parent[key] =
            type: 'MemberExpression'
            computed: no
            object:
              type: 'Identifier'
              name: identifier.object
            property:
              type: 'Identifier'
              name: identifier.name
      go null, program
    catch error
      go
        message: 'Error rewriting javascript'
        cause: error

generateJavascript = (program, go) ->
  try
