File: type_go_map.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 (95 lines) | stat: -rw-r--r-- 2,353 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
package otto

import (
	"reflect"
)

func (runtime *_runtime) newGoMapObject(value reflect.Value) *_object {
	self := runtime.newObject()
	self.class = "Object" // TODO Should this be something else?
	self.objectClass = _classGoMap
	self.value = _newGoMapObject(value)
	return self
}

type _goMapObject struct {
	value     reflect.Value
	keyKind   reflect.Kind
	valueKind reflect.Kind
}

func _newGoMapObject(value reflect.Value) *_goMapObject {
	if value.Kind() != reflect.Map {
		dbgf("%/panic//%@: %v != reflect.Map", value.Kind())
	}
	self := &_goMapObject{
		value:     value,
		keyKind:   value.Type().Key().Kind(),
		valueKind: value.Type().Elem().Kind(),
	}
	return self
}

func (self _goMapObject) toKey(name string) reflect.Value {
	reflectValue, err := stringToReflectValue(name, self.keyKind)
	if err != nil {
		panic(err)
	}
	return reflectValue
}

func (self _goMapObject) toValue(value Value) reflect.Value {
	reflectValue, err := value.toReflectValue(self.valueKind)
	if err != nil {
		panic(err)
	}
	return reflectValue
}

func goMapGetOwnProperty(self *_object, name string) *_property {
	object := self.value.(*_goMapObject)
	value := object.value.MapIndex(object.toKey(name))
	if value.IsValid() {
		return &_property{self.runtime.toValue(value.Interface()), 0111}
	}

	// Other methods
	if method := self.value.(*_goMapObject).value.MethodByName(name); (method != reflect.Value{}) {
		return &_property{
			value: self.runtime.toValue(method.Interface()),
			mode:  0110,
		}
	}

	return nil
}

func goMapEnumerate(self *_object, all bool, each func(string) bool) {
	object := self.value.(*_goMapObject)
	keys := object.value.MapKeys()
	for _, key := range keys {
		if !each(toValue(key).String()) {
			return
		}
	}
}

func goMapDefineOwnProperty(self *_object, name string, descriptor _property, throw bool) bool {
	object := self.value.(*_goMapObject)
	// TODO ...or 0222
	if descriptor.mode != 0111 {
		return self.runtime.typeErrorResult(throw)
	}
	if !descriptor.isDataDescriptor() {
		return self.runtime.typeErrorResult(throw)
	}
	object.value.SetMapIndex(object.toKey(name), object.toValue(descriptor.value.(Value)))
	return true
}

func goMapDelete(self *_object, name string, throw bool) bool {
	object := self.value.(*_goMapObject)
	object.value.SetMapIndex(object.toKey(name), reflect.Value{})
	// FIXME
	return true
}