OLD | NEW |
| (Empty) |
1 // Copyright 2013 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 (function(global, utils) { | |
6 | |
7 "use strict"; | |
8 | |
9 %CheckIsBootstrapping(); | |
10 | |
11 // ------------------------------------------------------------------- | |
12 // Imports | |
13 | |
14 var GlobalArrayBuffer = global.ArrayBuffer; | |
15 var GlobalObject = global.Object; | |
16 var MathMax; | |
17 var MathMin; | |
18 var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); | |
19 | |
20 utils.Import(function(from) { | |
21 MathMax = from.MathMax; | |
22 MathMin = from.MathMin; | |
23 }); | |
24 | |
25 // ------------------------------------------------------------------- | |
26 | |
27 function ArrayBufferConstructor(length) { // length = 1 | |
28 if (%_IsConstructCall()) { | |
29 var byteLength = $toPositiveInteger(length, kInvalidArrayBufferLength); | |
30 %ArrayBufferInitialize(this, byteLength, kNotShared); | |
31 } else { | |
32 throw MakeTypeError(kConstructorNotFunction, "ArrayBuffer"); | |
33 } | |
34 } | |
35 | |
36 function ArrayBufferGetByteLen() { | |
37 if (!IS_ARRAYBUFFER(this)) { | |
38 throw MakeTypeError(kIncompatibleMethodReceiver, | |
39 'ArrayBuffer.prototype.byteLength', this); | |
40 } | |
41 return %_ArrayBufferGetByteLength(this); | |
42 } | |
43 | |
44 // ES6 Draft 15.13.5.5.3 | |
45 function ArrayBufferSlice(start, end) { | |
46 if (!IS_ARRAYBUFFER(this)) { | |
47 throw MakeTypeError(kIncompatibleMethodReceiver, | |
48 'ArrayBuffer.prototype.slice', this); | |
49 } | |
50 | |
51 var relativeStart = TO_INTEGER(start); | |
52 if (!IS_UNDEFINED(end)) { | |
53 end = TO_INTEGER(end); | |
54 } | |
55 var first; | |
56 var byte_length = %_ArrayBufferGetByteLength(this); | |
57 if (relativeStart < 0) { | |
58 first = MathMax(byte_length + relativeStart, 0); | |
59 } else { | |
60 first = MathMin(relativeStart, byte_length); | |
61 } | |
62 var relativeEnd = IS_UNDEFINED(end) ? byte_length : end; | |
63 var fin; | |
64 if (relativeEnd < 0) { | |
65 fin = MathMax(byte_length + relativeEnd, 0); | |
66 } else { | |
67 fin = MathMin(relativeEnd, byte_length); | |
68 } | |
69 | |
70 if (fin < first) { | |
71 fin = first; | |
72 } | |
73 var newLen = fin - first; | |
74 // TODO(dslomov): implement inheritance | |
75 var result = new GlobalArrayBuffer(newLen); | |
76 | |
77 %ArrayBufferSliceImpl(this, result, first); | |
78 return result; | |
79 } | |
80 | |
81 function ArrayBufferIsViewJS(obj) { | |
82 return %ArrayBufferIsView(obj); | |
83 } | |
84 | |
85 | |
86 // Set up the ArrayBuffer constructor function. | |
87 %SetCode(GlobalArrayBuffer, ArrayBufferConstructor); | |
88 %FunctionSetPrototype(GlobalArrayBuffer, new GlobalObject()); | |
89 | |
90 // Set up the constructor property on the ArrayBuffer prototype object. | |
91 %AddNamedProperty( | |
92 GlobalArrayBuffer.prototype, "constructor", GlobalArrayBuffer, DONT_ENUM); | |
93 | |
94 %AddNamedProperty(GlobalArrayBuffer.prototype, | |
95 toStringTagSymbol, "ArrayBuffer", DONT_ENUM | READ_ONLY); | |
96 | |
97 utils.InstallGetter(GlobalArrayBuffer.prototype, "byteLength", | |
98 ArrayBufferGetByteLen); | |
99 | |
100 utils.InstallFunctions(GlobalArrayBuffer, DONT_ENUM, [ | |
101 "isView", ArrayBufferIsViewJS | |
102 ]); | |
103 | |
104 utils.InstallFunctions(GlobalArrayBuffer.prototype, DONT_ENUM, [ | |
105 "slice", ArrayBufferSlice | |
106 ]); | |
107 | |
108 }) | |
OLD | NEW |