-
Notifications
You must be signed in to change notification settings - Fork 588
/
builtin_error.go
126 lines (99 loc) · 3.48 KB
/
builtin_error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package otto
import (
"fmt"
)
func builtinError(call FunctionCall) Value {
return objectValue(call.runtime.newError(classErrorName, call.Argument(0), 1))
}
func builtinNewError(obj *object, argumentList []Value) Value {
return objectValue(obj.runtime.newError(classErrorName, valueOfArrayIndex(argumentList, 0), 0))
}
func builtinErrorToString(call FunctionCall) Value {
thisObject := call.thisObject()
if thisObject == nil {
panic(call.runtime.panicTypeError("Error.toString is nil"))
}
name := classErrorName
nameValue := thisObject.get("name")
if nameValue.IsDefined() {
name = nameValue.string()
}
message := ""
messageValue := thisObject.get("message")
if messageValue.IsDefined() {
message = messageValue.string()
}
if len(name) == 0 {
return stringValue(message)
}
if len(message) == 0 {
return stringValue(name)
}
return stringValue(fmt.Sprintf("%s: %s", name, message))
}
func (rt *runtime) newEvalError(message Value) *object {
o := rt.newErrorObject("EvalError", message, 0)
o.prototype = rt.global.EvalErrorPrototype
return o
}
func builtinEvalError(call FunctionCall) Value {
return objectValue(call.runtime.newEvalError(call.Argument(0)))
}
func builtinNewEvalError(obj *object, argumentList []Value) Value {
return objectValue(obj.runtime.newEvalError(valueOfArrayIndex(argumentList, 0)))
}
func (rt *runtime) newTypeError(message Value) *object {
o := rt.newErrorObject("TypeError", message, 0)
o.prototype = rt.global.TypeErrorPrototype
return o
}
func builtinTypeError(call FunctionCall) Value {
return objectValue(call.runtime.newTypeError(call.Argument(0)))
}
func builtinNewTypeError(obj *object, argumentList []Value) Value {
return objectValue(obj.runtime.newTypeError(valueOfArrayIndex(argumentList, 0)))
}
func (rt *runtime) newRangeError(message Value) *object {
o := rt.newErrorObject("RangeError", message, 0)
o.prototype = rt.global.RangeErrorPrototype
return o
}
func builtinRangeError(call FunctionCall) Value {
return objectValue(call.runtime.newRangeError(call.Argument(0)))
}
func builtinNewRangeError(obj *object, argumentList []Value) Value {
return objectValue(obj.runtime.newRangeError(valueOfArrayIndex(argumentList, 0)))
}
func (rt *runtime) newURIError(message Value) *object {
o := rt.newErrorObject("URIError", message, 0)
o.prototype = rt.global.URIErrorPrototype
return o
}
func (rt *runtime) newReferenceError(message Value) *object {
o := rt.newErrorObject("ReferenceError", message, 0)
o.prototype = rt.global.ReferenceErrorPrototype
return o
}
func builtinReferenceError(call FunctionCall) Value {
return objectValue(call.runtime.newReferenceError(call.Argument(0)))
}
func builtinNewReferenceError(obj *object, argumentList []Value) Value {
return objectValue(obj.runtime.newReferenceError(valueOfArrayIndex(argumentList, 0)))
}
func (rt *runtime) newSyntaxError(message Value) *object {
o := rt.newErrorObject("SyntaxError", message, 0)
o.prototype = rt.global.SyntaxErrorPrototype
return o
}
func builtinSyntaxError(call FunctionCall) Value {
return objectValue(call.runtime.newSyntaxError(call.Argument(0)))
}
func builtinNewSyntaxError(obj *object, argumentList []Value) Value {
return objectValue(obj.runtime.newSyntaxError(valueOfArrayIndex(argumentList, 0)))
}
func builtinURIError(call FunctionCall) Value {
return objectValue(call.runtime.newURIError(call.Argument(0)))
}
func builtinNewURIError(obj *object, argumentList []Value) Value {
return objectValue(obj.runtime.newURIError(valueOfArrayIndex(argumentList, 0)))
}