forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
217 lines (190 loc) · 5.33 KB
/
global.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package otto
import (
"strconv"
"time"
)
var (
prototypeValueObject = interface{}(nil)
prototypeValueFunction = nativeFunctionObject{
call: func(_ FunctionCall) Value {
return Value{}
},
}
prototypeValueString = stringASCII("")
// TODO Make this just false?
prototypeValueBoolean = Value{
kind: valueBoolean,
value: false,
}
prototypeValueNumber = Value{
kind: valueNumber,
value: 0,
}
prototypeValueDate = dateObject{
epoch: 0,
isNaN: false,
time: time.Unix(0, 0).UTC(),
value: Value{
kind: valueNumber,
value: 0,
},
}
prototypeValueRegExp = regExpObject{
regularExpression: nil,
global: false,
ignoreCase: false,
multiline: false,
source: "",
flags: "",
}
)
func newContext() *runtime {
rt := &runtime{}
rt.globalStash = rt.newObjectStash(nil, nil)
rt.globalObject = rt.globalStash.object
rt.newContext()
rt.eval = rt.globalObject.property["eval"].value.(Value).value.(*object)
rt.globalObject.prototype = rt.global.ObjectPrototype
return rt
}
func (rt *runtime) newBaseObject() *object {
return newObject(rt, "")
}
func (rt *runtime) newClassObject(class string) *object {
return newObject(rt, class)
}
func (rt *runtime) newPrimitiveObject(class string, value Value) *object {
o := rt.newClassObject(class)
o.value = value
return o
}
func (o *object) primitiveValue() Value {
switch value := o.value.(type) {
case Value:
return value
case stringObjecter:
return stringValue(value.String())
}
return Value{}
}
func (o *object) hasPrimitive() bool { //nolint: unused
switch o.value.(type) {
case Value, stringObjecter:
return true
}
return false
}
func (rt *runtime) newObject() *object {
o := rt.newClassObject(classObjectName)
o.prototype = rt.global.ObjectPrototype
return o
}
func (rt *runtime) newArray(length uint32) *object {
o := rt.newArrayObject(length)
o.prototype = rt.global.ArrayPrototype
return o
}
func (rt *runtime) newArrayOf(valueArray []Value) *object {
o := rt.newArray(uint32(len(valueArray)))
for index, value := range valueArray {
if value.isEmpty() {
continue
}
o.defineProperty(strconv.FormatInt(int64(index), 10), value, 0o111, false)
}
return o
}
func (rt *runtime) newString(value Value) *object {
o := rt.newStringObject(value)
o.prototype = rt.global.StringPrototype
return o
}
func (rt *runtime) newBoolean(value Value) *object {
o := rt.newBooleanObject(value)
o.prototype = rt.global.BooleanPrototype
return o
}
func (rt *runtime) newNumber(value Value) *object {
o := rt.newNumberObject(value)
o.prototype = rt.global.NumberPrototype
return o
}
func (rt *runtime) newRegExp(patternValue Value, flagsValue Value) *object {
pattern := ""
flags := ""
if obj := patternValue.object(); obj != nil && obj.class == classRegExpName {
if flagsValue.IsDefined() {
panic(rt.panicTypeError("Cannot supply flags when constructing one RegExp from another"))
}
regExp := obj.regExpValue()
pattern = regExp.source
flags = regExp.flags
} else {
if patternValue.IsDefined() {
pattern = patternValue.string()
}
if flagsValue.IsDefined() {
flags = flagsValue.string()
}
}
return rt.newRegExpDirect(pattern, flags)
}
func (rt *runtime) newRegExpDirect(pattern string, flags string) *object {
o := rt.newRegExpObject(pattern, flags)
o.prototype = rt.global.RegExpPrototype
return o
}
// TODO Should (probably) be one argument, right? This is redundant.
func (rt *runtime) newDate(epoch float64) *object {
o := rt.newDateObject(epoch)
o.prototype = rt.global.DatePrototype
return o
}
func (rt *runtime) newError(name string, message Value, stackFramesToPop int) *object {
switch name {
case "EvalError":
return rt.newEvalError(message)
case "TypeError":
return rt.newTypeError(message)
case "RangeError":
return rt.newRangeError(message)
case "ReferenceError":
return rt.newReferenceError(message)
case "SyntaxError":
return rt.newSyntaxError(message)
case "URIError":
return rt.newURIError(message)
}
obj := rt.newErrorObject(name, message, stackFramesToPop)
obj.prototype = rt.global.ErrorPrototype
if name != "" {
obj.defineProperty("name", stringValue(name), 0o111, false)
}
return obj
}
func (rt *runtime) newNativeFunction(name, file string, line int, fn nativeFunction) *object {
o := rt.newNativeFunctionObject(name, file, line, fn, 0)
o.prototype = rt.global.FunctionPrototype
prototype := rt.newObject()
o.defineProperty("prototype", objectValue(prototype), 0o100, false)
prototype.defineProperty("constructor", objectValue(o), 0o100, false)
return o
}
func (rt *runtime) newNodeFunction(node *nodeFunctionLiteral, scopeEnvironment stasher) *object {
// TODO Implement 13.2 fully
o := rt.newNodeFunctionObject(node, scopeEnvironment)
o.prototype = rt.global.FunctionPrototype
prototype := rt.newObject()
o.defineProperty("prototype", objectValue(prototype), 0o100, false)
prototype.defineProperty("constructor", objectValue(o), 0o101, false)
return o
}
// FIXME Only in one place...
func (rt *runtime) newBoundFunction(target *object, this Value, argumentList []Value) *object {
o := rt.newBoundFunctionObject(target, this, argumentList)
o.prototype = rt.global.FunctionPrototype
prototype := rt.newObject()
o.defineProperty("prototype", objectValue(prototype), 0o100, false)
prototype.defineProperty("constructor", objectValue(o), 0o100, false)
return o
}