File: builtin_typedarrays.go

package info (click to toggle)
golang-github-dop251-goja 0.0~git20170430.0.d382686-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 964 kB
  • sloc: javascript: 454; perl: 184; makefile: 6
file content (105 lines) | stat: -rw-r--r-- 2,466 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
package goja

type objectArrayBuffer struct {
	baseObject
	data []byte
}

func (o *objectArrayBuffer) export() interface{} {
	return o.data
}

func (r *Runtime) _newArrayBuffer(proto *Object, o *Object) *objectArrayBuffer {
	if o == nil {
		o = &Object{runtime: r}
	}
	b := &objectArrayBuffer{
		baseObject: baseObject{
			class:      classObject,
			val:        o,
			prototype:  proto,
			extensible: true,
		},
	}
	o.self = b
	b.init()
	return b
}

func (r *Runtime) builtin_ArrayBuffer(args []Value, proto *Object) *Object {
	b := r._newArrayBuffer(proto, nil)
	if len(args) > 0 {
		b.data = make([]byte, toLength(args[0]))
	}
	return b.val
}

func (r *Runtime) arrayBufferProto_getByteLength(call FunctionCall) Value {
	o := r.toObject(call.This)
	if b, ok := o.self.(*objectArrayBuffer); ok {
		return intToValue(int64(len(b.data)))
	}
	r.typeErrorResult(true, "Object is not ArrayBuffer: %s", o)
	panic("unreachable")
}

func (r *Runtime) arrayBufferProto_slice(call FunctionCall) Value {
	o := r.toObject(call.This)
	if b, ok := o.self.(*objectArrayBuffer); ok {
		l := int64(len(b.data))
		start := toLength(call.Argument(0))
		if start < 0 {
			start = l + start
		}
		if start < 0 {
			start = 0
		} else if start > l {
			start = l
		}
		var stop int64
		if arg := call.Argument(1); arg != _undefined {
			stop = toLength(arg)
			if stop < 0 {
				stop = int64(len(b.data)) + stop
			}
			if stop < 0 {
				stop = 0
			} else if stop > l {
				stop = l
			}

		} else {
			stop = l
		}

		ret := r._newArrayBuffer(r.global.ArrayBufferPrototype, nil)

		if stop > start {
			ret.data = b.data[start:stop]
		}

		return ret.val
	}
	r.typeErrorResult(true, "Object is not ArrayBuffer: %s", o)
	panic("unreachable")
}

func (r *Runtime) createArrayBufferProto(val *Object) objectImpl {
	b := r._newArrayBuffer(r.global.Object, val)
	byteLengthProp := &valueProperty{
		accessor:     true,
		configurable: true,
		getterFunc:   r.newNativeFunc(r.arrayBufferProto_getByteLength, nil, "get byteLength", nil, 0),
	}
	b._put("byteLength", byteLengthProp)
	b._putProp("slice", r.newNativeFunc(r.arrayBufferProto_slice, nil, "slice", nil, 2), true, false, true)
	return b
}

func (r *Runtime) initTypedArrays() {

	r.global.ArrayBufferPrototype = r.newLazyObject(r.createArrayBufferProto)

	r.global.ArrayBuffer = r.newNativeFuncConstruct(r.builtin_ArrayBuffer, "ArrayBuffer", r.global.ArrayBufferPrototype, 1)
	r.addToGlobal("ArrayBuffer", r.global.ArrayBuffer)
}