forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataView.js
More file actions
142 lines (126 loc) · 4.15 KB
/
DataView.js
File metadata and controls
142 lines (126 loc) · 4.15 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// RUN: %hermes -Xhermes-internal-test-methods -O %s | %FileCheck --match-full-lines %s
// RUN: %hermesc -O -emit-binary -out %t.hbc %s && %hermes -Xhermes-internal-test-methods %t.hbc | %FileCheck --match-full-lines %s
print("Check .length");
// CHECK-LABEL: Check .length
print(DataView.length);
// CHECK-NEXT: 1
var view = new DataView(new ArrayBuffer(8));
print("Check .buffer");
// CHECK-LABEL: Check .buffer
print(view.buffer);
// CHECK-NEXT: [object ArrayBuffer]
print(view.byteLength);
// CHECK-NEXT: 8
try { DataView.prototype.buffer; } catch (e) { print('caught', e.name); }
// CHECK-NEXT: caught TypeError
print("Check constructor with byteOffset and byteLength");
// CHECK-LABEL: Check constructor with byteOffset and byteLength
var viewWithOffsetAndLength = new DataView(new ArrayBuffer(16), 8, 8);
print(viewWithOffsetAndLength.byteLength);
// CHECK-NEXT: 8
print(viewWithOffsetAndLength.byteOffset);
// CHECK-NEXT: 8
print("Check get and set at front (BE)");
// CHECK-LABEL: Check get and set at front (BE)
print(view.getInt8(0));
// CHECK-NEXT: 0
view.setInt8(0, 4);
print(view.getInt8(0));
// CHECK-NEXT: 4
// undo
view.setInt8(0, 0);
print("Check get and set in middle (BE)");
// CHECK-LABEL: Check get and set in middle (BE)
print(view.getInt8(4));
// CHECK-NEXT: 0
view.setInt8(4, 5);
print(view.getInt8(4));
// CHECK-NEXT: 5
// undo
view.setInt8(4, 0);
print("Check get and set with multibyte value (BE)");
// CHECK-LABEL: Check get and set with multibyte value (BE)
view.setInt32(0, 5);
print(view.getInt32(0));
// CHECK-NEXT: 5
// undo
view.setInt32(0, 0);
print("Check get and set in middle with multibyte value (BE)");
// CHECK-LABEL: Check get and set in middle with multibyte value (BE)
print(view.getInt32(4));
// CHECK-NEXT: 0
view.setInt32(4, 5);
print(view.getInt32(4));
// CHECK-NEXT: 5
// undo
view.setInt32(4, 0);
print("Check get and set with big multibyte value (BE)");
// CHECK-LABEL: Check get and set with big multibyte value (BE)
view.setInt32(0, 1 << 30);
print(view.getInt32(0));
// CHECK-NEXT: 1073741824
// undo
view.setInt32(0, 0);
print("Check difference between LE and BE");
// CHECK-LABEL: Check difference between LE and BE
view.setInt32(0, 1 << 30, false);
print(view.getInt32(0, false));
// CHECK-NEXT: 1073741824
print(view.getInt32(0, true));
// CHECK-NEXT: 64
// now in reverse
view.setInt32(0, 1 << 30, true);
print(view.getInt32(0, false));
// CHECK-NEXT: 64
print(view.getInt32(0, true));
// CHECK-NEXT: 1073741824
view.setInt32(0, 0);
print("Check reading same value as different signs");
// CHECK-LABEL: Check reading same value as different signs
view.setInt16(0, -1);
print(view.getInt16(0));
// CHECK-NEXT: -1
print(view.getUint16(0));
// CHECK-NEXT: 65535
view.setInt16(0, 0);
print("Check float sanitization");
// CHECK-LABEL: Check float sanitization
view.setUint32(0, 0xffffffff);
print(view.getFloat32(0));
// CHECK-NEXT: NaN
view.setUint32(0, 0);
print("Check conversion");
// CHECK-LABEL: Check conversion
view.setInt16(0, 4160782224, true);
print(view.getInt16(0));
// CHECK-NEXT: -28545
print("Check descriptors");
// CHECK-LABEL: Check descriptors
var desc = Object.getOwnPropertyDescriptor(DataView.prototype, 'buffer');
print(desc.get.name, desc.enumerable, desc.configurable);
// CHECK-NEXT: get buffer false true
var desc = Object.getOwnPropertyDescriptor(DataView.prototype, 'byteLength');
print(desc.get.name, desc.enumerable, desc.configurable);
// CHECK-NEXT: get byteLength false true
var desc = Object.getOwnPropertyDescriptor(DataView.prototype, 'byteOffset');
print(desc.get.name, desc.enumerable, desc.configurable);
// CHECK-NEXT: get byteOffset false true
print("Check get and set on detached ArrayBuffer");
// CHECK-LABEL: Check get and set on detached ArrayBuffer
var buffer = new ArrayBuffer(16);
var view = new DataView(buffer);
HermesInternal.detachArrayBuffer(buffer);
try {
view.setUint32(1,32);
print('Should\'t reach here');
// CHECK-NOT: Shouldn't reach here
} catch (e) {
print(e.constructor === TypeError);
// CHECK-NEXT: true
}