Skip to content

Proposal: add method to call sandboxed functions at current state of interpreter #37

@BrownBear2

Description

@BrownBear2

This function will take a sandbox typed function and execute it in a child interpreter. When it's done it will run a given callback function.

The function will create a scope for the pseudo-function, apply all arguments, remove the return statement at the end and execute the function in a new interpreter. Once the interpreter is done it will call back. Note: this needs the callback and convenience patches I submitted earlier.

# call a sandbox function at the current state of the interpreter
# fn: sandbox type function
# args: any native arguments to the function
# done: callback to be run when the function call is done
Interpreter::call = (fn, args..., done) ->
  scope = @createScope fn.node.body, fn.parentScope
  for p, i in fn.node.params
    @setProperty scope, @createPrimitive(p.name), @convertToPseudo(args[i])

  argsList = @createObject @ARRAY
  for arg, i in args
    @setProperty argsList, @createPrimitive(i), @convertToPseudo(arg)

  @setProperty scope, "arguments", argsList

  # remove returns from callbacks
  [..., last] = fn.node.body.body
  if last.type == "ReturnStatement"
    last.type = "ExpressionStatement"
    last.expression = last.argument
    delete last.argument

  funcState =
    node: fn.node.body
    scope: scope
    thisExpression: @stateStack[0].funcThis_

  ip = new Interpreter ""
  ip.stateStack.unshift funcState
  ip.run done
  return

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions