cmpl_evaluate.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package otto
  2. import (
  3. "strconv"
  4. )
  5. func (rt *runtime) cmplEvaluateNodeProgram(node *nodeProgram, eval bool) Value {
  6. if !eval {
  7. rt.enterGlobalScope()
  8. defer rt.leaveScope()
  9. }
  10. rt.cmplFunctionDeclaration(node.functionList)
  11. rt.cmplVariableDeclaration(node.varList)
  12. rt.scope.frame.file = node.file
  13. return rt.cmplEvaluateNodeStatementList(node.body)
  14. }
  15. func (rt *runtime) cmplCallNodeFunction(function *object, stash *fnStash, node *nodeFunctionLiteral, argumentList []Value) Value {
  16. indexOfParameterName := make([]string, len(argumentList))
  17. // function(abc, def, ghi)
  18. // indexOfParameterName[0] = "abc"
  19. // indexOfParameterName[1] = "def"
  20. // indexOfParameterName[2] = "ghi"
  21. // ...
  22. argumentsFound := false
  23. for index, name := range node.parameterList {
  24. if name == "arguments" {
  25. argumentsFound = true
  26. }
  27. value := Value{}
  28. if index < len(argumentList) {
  29. value = argumentList[index]
  30. indexOfParameterName[index] = name
  31. }
  32. // strict = false
  33. rt.scope.lexical.setValue(name, value, false)
  34. }
  35. if !argumentsFound {
  36. arguments := rt.newArgumentsObject(indexOfParameterName, stash, len(argumentList))
  37. arguments.defineProperty("callee", objectValue(function), 0o101, false)
  38. stash.arguments = arguments
  39. // strict = false
  40. rt.scope.lexical.setValue("arguments", objectValue(arguments), false)
  41. for index := range argumentList {
  42. if index < len(node.parameterList) {
  43. continue
  44. }
  45. indexAsString := strconv.FormatInt(int64(index), 10)
  46. arguments.defineProperty(indexAsString, argumentList[index], 0o111, false)
  47. }
  48. }
  49. rt.cmplFunctionDeclaration(node.functionList)
  50. rt.cmplVariableDeclaration(node.varList)
  51. result := rt.cmplEvaluateNodeStatement(node.body)
  52. if result.kind == valueResult {
  53. return result
  54. }
  55. return Value{}
  56. }
  57. func (rt *runtime) cmplFunctionDeclaration(list []*nodeFunctionLiteral) {
  58. executionContext := rt.scope
  59. eval := executionContext.eval
  60. stash := executionContext.variable
  61. for _, function := range list {
  62. name := function.name
  63. value := rt.cmplEvaluateNodeExpression(function)
  64. if !stash.hasBinding(name) {
  65. stash.createBinding(name, eval, value)
  66. } else {
  67. // TODO 10.5.5.e
  68. stash.setBinding(name, value, false) // TODO strict
  69. }
  70. }
  71. }
  72. func (rt *runtime) cmplVariableDeclaration(list []string) {
  73. executionContext := rt.scope
  74. eval := executionContext.eval
  75. stash := executionContext.variable
  76. for _, name := range list {
  77. if !stash.hasBinding(name) {
  78. stash.createBinding(name, eval, Value{}) // TODO strict?
  79. }
  80. }
  81. }