builtin_error.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package otto
  2. import (
  3. "fmt"
  4. )
  5. func builtinError(call FunctionCall) Value {
  6. return toValue_object(call.runtime.newError(classError, call.Argument(0), 1))
  7. }
  8. func builtinNewError(self *_object, argumentList []Value) Value {
  9. return toValue_object(self.runtime.newError(classError, valueOfArrayIndex(argumentList, 0), 0))
  10. }
  11. func builtinError_toString(call FunctionCall) Value {
  12. thisObject := call.thisObject()
  13. if thisObject == nil {
  14. panic(call.runtime.panicTypeError())
  15. }
  16. name := classError
  17. nameValue := thisObject.get("name")
  18. if nameValue.IsDefined() {
  19. name = nameValue.string()
  20. }
  21. message := ""
  22. messageValue := thisObject.get("message")
  23. if messageValue.IsDefined() {
  24. message = messageValue.string()
  25. }
  26. if len(name) == 0 {
  27. return toValue_string(message)
  28. }
  29. if len(message) == 0 {
  30. return toValue_string(name)
  31. }
  32. return toValue_string(fmt.Sprintf("%s: %s", name, message))
  33. }
  34. func (runtime *_runtime) newEvalError(message Value) *_object {
  35. self := runtime.newErrorObject("EvalError", message, 0)
  36. self.prototype = runtime.global.EvalErrorPrototype
  37. return self
  38. }
  39. func builtinEvalError(call FunctionCall) Value {
  40. return toValue_object(call.runtime.newEvalError(call.Argument(0)))
  41. }
  42. func builtinNewEvalError(self *_object, argumentList []Value) Value {
  43. return toValue_object(self.runtime.newEvalError(valueOfArrayIndex(argumentList, 0)))
  44. }
  45. func (runtime *_runtime) newTypeError(message Value) *_object {
  46. self := runtime.newErrorObject("TypeError", message, 0)
  47. self.prototype = runtime.global.TypeErrorPrototype
  48. return self
  49. }
  50. func builtinTypeError(call FunctionCall) Value {
  51. return toValue_object(call.runtime.newTypeError(call.Argument(0)))
  52. }
  53. func builtinNewTypeError(self *_object, argumentList []Value) Value {
  54. return toValue_object(self.runtime.newTypeError(valueOfArrayIndex(argumentList, 0)))
  55. }
  56. func (runtime *_runtime) newRangeError(message Value) *_object {
  57. self := runtime.newErrorObject("RangeError", message, 0)
  58. self.prototype = runtime.global.RangeErrorPrototype
  59. return self
  60. }
  61. func builtinRangeError(call FunctionCall) Value {
  62. return toValue_object(call.runtime.newRangeError(call.Argument(0)))
  63. }
  64. func builtinNewRangeError(self *_object, argumentList []Value) Value {
  65. return toValue_object(self.runtime.newRangeError(valueOfArrayIndex(argumentList, 0)))
  66. }
  67. func (runtime *_runtime) newURIError(message Value) *_object {
  68. self := runtime.newErrorObject("URIError", message, 0)
  69. self.prototype = runtime.global.URIErrorPrototype
  70. return self
  71. }
  72. func (runtime *_runtime) newReferenceError(message Value) *_object {
  73. self := runtime.newErrorObject("ReferenceError", message, 0)
  74. self.prototype = runtime.global.ReferenceErrorPrototype
  75. return self
  76. }
  77. func builtinReferenceError(call FunctionCall) Value {
  78. return toValue_object(call.runtime.newReferenceError(call.Argument(0)))
  79. }
  80. func builtinNewReferenceError(self *_object, argumentList []Value) Value {
  81. return toValue_object(self.runtime.newReferenceError(valueOfArrayIndex(argumentList, 0)))
  82. }
  83. func (runtime *_runtime) newSyntaxError(message Value) *_object {
  84. self := runtime.newErrorObject("SyntaxError", message, 0)
  85. self.prototype = runtime.global.SyntaxErrorPrototype
  86. return self
  87. }
  88. func builtinSyntaxError(call FunctionCall) Value {
  89. return toValue_object(call.runtime.newSyntaxError(call.Argument(0)))
  90. }
  91. func builtinNewSyntaxError(self *_object, argumentList []Value) Value {
  92. return toValue_object(self.runtime.newSyntaxError(valueOfArrayIndex(argumentList, 0)))
  93. }
  94. func builtinURIError(call FunctionCall) Value {
  95. return toValue_object(call.runtime.newURIError(call.Argument(0)))
  96. }
  97. func builtinNewURIError(self *_object, argumentList []Value) Value {
  98. return toValue_object(self.runtime.newURIError(valueOfArrayIndex(argumentList, 0)))
  99. }