File: global.go

package info (click to toggle)
golang-github-robertkrimen-otto 0.0~git20200922.ef014fd-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,796 kB
  • sloc: perl: 1,227; makefile: 79
file content (221 lines) | stat: -rw-r--r-- 5,978 bytes parent folder | download | duplicates (2)
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
218
219
220
221
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 {

	self := &_runtime{}

	self.globalStash = self.newObjectStash(nil, nil)
	self.globalObject = self.globalStash.object

	_newContext(self)

	self.eval = self.globalObject.property["eval"].value.(Value).value.(*_object)
	self.globalObject.prototype = self.global.ObjectPrototype

	return self
}

func (runtime *_runtime) newBaseObject() *_object {
	self := newObject(runtime, "")
	return self
}

func (runtime *_runtime) newClassObject(class string) *_object {
	return newObject(runtime, class)
}

func (runtime *_runtime) newPrimitiveObject(class string, value Value) *_object {
	self := runtime.newClassObject(class)
	self.value = value
	return self
}

func (self *_object) primitiveValue() Value {
	switch value := self.value.(type) {
	case Value:
		return value
	case _stringObject:
		return toValue_string(value.String())
	}
	return Value{}
}

func (self *_object) hasPrimitive() bool {
	switch self.value.(type) {
	case Value, _stringObject:
		return true
	}
	return false
}

func (runtime *_runtime) newObject() *_object {
	self := runtime.newClassObject("Object")
	self.prototype = runtime.global.ObjectPrototype
	return self
}

func (runtime *_runtime) newArray(length uint32) *_object {
	self := runtime.newArrayObject(length)
	self.prototype = runtime.global.ArrayPrototype
	return self
}

func (runtime *_runtime) newArrayOf(valueArray []Value) *_object {
	self := runtime.newArray(uint32(len(valueArray)))
	for index, value := range valueArray {
		if value.isEmpty() {
			continue
		}
		self.defineProperty(strconv.FormatInt(int64(index), 10), value, 0111, false)
	}
	return self
}

func (runtime *_runtime) newString(value Value) *_object {
	self := runtime.newStringObject(value)
	self.prototype = runtime.global.StringPrototype
	return self
}

func (runtime *_runtime) newBoolean(value Value) *_object {
	self := runtime.newBooleanObject(value)
	self.prototype = runtime.global.BooleanPrototype
	return self
}

func (runtime *_runtime) newNumber(value Value) *_object {
	self := runtime.newNumberObject(value)
	self.prototype = runtime.global.NumberPrototype
	return self
}

func (runtime *_runtime) newRegExp(patternValue Value, flagsValue Value) *_object {

	pattern := ""
	flags := ""
	if object := patternValue._object(); object != nil && object.class == "RegExp" {
		if flagsValue.IsDefined() {
			panic(runtime.panicTypeError("Cannot supply flags when constructing one RegExp from another"))
		}
		regExp := object.regExpValue()
		pattern = regExp.source
		flags = regExp.flags
	} else {
		if patternValue.IsDefined() {
			pattern = patternValue.string()
		}
		if flagsValue.IsDefined() {
			flags = flagsValue.string()
		}
	}

	return runtime._newRegExp(pattern, flags)
}

func (runtime *_runtime) _newRegExp(pattern string, flags string) *_object {
	self := runtime.newRegExpObject(pattern, flags)
	self.prototype = runtime.global.RegExpPrototype
	return self
}

// TODO Should (probably) be one argument, right? This is redundant
func (runtime *_runtime) newDate(epoch float64) *_object {
	self := runtime.newDateObject(epoch)
	self.prototype = runtime.global.DatePrototype
	return self
}

func (runtime *_runtime) newError(name string, message Value, stackFramesToPop int) *_object {
	var self *_object
	switch name {
	case "EvalError":
		return runtime.newEvalError(message)
	case "TypeError":
		return runtime.newTypeError(message)
	case "RangeError":
		return runtime.newRangeError(message)
	case "ReferenceError":
		return runtime.newReferenceError(message)
	case "SyntaxError":
		return runtime.newSyntaxError(message)
	case "URIError":
		return runtime.newURIError(message)
	}

	self = runtime.newErrorObject(name, message, stackFramesToPop)
	self.prototype = runtime.global.ErrorPrototype
	if name != "" {
		self.defineProperty("name", toValue_string(name), 0111, false)
	}
	return self
}

func (runtime *_runtime) newNativeFunction(name, file string, line int, _nativeFunction _nativeFunction) *_object {
	self := runtime.newNativeFunctionObject(name, file, line, _nativeFunction, 0)
	self.prototype = runtime.global.FunctionPrototype
	prototype := runtime.newObject()
	self.defineProperty("prototype", toValue_object(prototype), 0100, false)
	prototype.defineProperty("constructor", toValue_object(self), 0100, false)
	return self
}

func (runtime *_runtime) newNodeFunction(node *_nodeFunctionLiteral, scopeEnvironment _stash) *_object {
	// TODO Implement 13.2 fully
	self := runtime.newNodeFunctionObject(node, scopeEnvironment)
	self.prototype = runtime.global.FunctionPrototype
	prototype := runtime.newObject()
	self.defineProperty("prototype", toValue_object(prototype), 0100, false)
	prototype.defineProperty("constructor", toValue_object(self), 0101, false)
	return self
}

// FIXME Only in one place...
func (runtime *_runtime) newBoundFunction(target *_object, this Value, argumentList []Value) *_object {
	self := runtime.newBoundFunctionObject(target, this, argumentList)
	self.prototype = runtime.global.FunctionPrototype
	prototype := runtime.newObject()
	self.defineProperty("prototype", toValue_object(prototype), 0100, false)
	prototype.defineProperty("constructor", toValue_object(self), 0100, false)
	return self
}