OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 "use strict"; | 28 "use strict"; |
29 | 29 |
30 // This file relies on the fact that the following declarations have been made | 30 var $ArrayBuffer = global.ArrayBuffer; |
31 // in runtime.js: | |
32 // var $Function = global.Function; | |
33 | 31 |
34 // ---------------------------------------------------------------------------- | 32 // ------------------------------------------------------------------- |
| 33 |
| 34 function ArrayBufferConstructor(byteLength) { // length = 1 |
| 35 if (%_IsConstructCall()) { |
| 36 var l = TO_POSITIVE_INTEGER(byteLength); |
| 37 %ArrayBufferInitialize(this, l); |
| 38 } else { |
| 39 return new $ArrayBuffer(byteLength); |
| 40 } |
| 41 } |
| 42 |
| 43 function ArrayBufferGetByteLength() { |
| 44 if (!IS_ARRAYBUFFER(this)) { |
| 45 throw MakeTypeError('incompatible_method_receiver', |
| 46 ['ArrayBuffer.prototype.byteLength', this]); |
| 47 } |
| 48 return %ArrayBufferGetByteLength(this); |
| 49 } |
| 50 |
| 51 // ES6 Draft 15.13.5.5.3 |
| 52 function ArrayBufferSlice(start, end) { |
| 53 if (!IS_ARRAYBUFFER(this)) { |
| 54 throw MakeTypeError('incompatible_method_receiver', |
| 55 ['ArrayBuffer.prototype.slice', this]); |
| 56 } |
| 57 |
| 58 var relativeStart = TO_INTEGER(start); |
| 59 var first; |
| 60 if (relativeStart < 0) { |
| 61 first = MathMax(this.byteLength + relativeStart, 0); |
| 62 } else { |
| 63 first = MathMin(relativeStart, this.byteLength); |
| 64 } |
| 65 var relativeEnd = IS_UNDEFINED(end) ? this.byteLength : TO_INTEGER(end); |
| 66 var fin; |
| 67 if (relativeEnd < 0) { |
| 68 fin = MathMax(this.byteLength + relativeEnd, 0); |
| 69 } else { |
| 70 fin = MathMin(relativeEnd, this.byteLength); |
| 71 } |
| 72 |
| 73 var newLen = fin - first; |
| 74 // TODO(dslomov): implement inheritance |
| 75 var result = new $ArrayBuffer(newLen); |
| 76 |
| 77 %ArrayBufferSliceImpl(this, result, first); |
| 78 return result; |
| 79 } |
| 80 |
| 81 function SetUpArrayBuffer() { |
| 82 %CheckIsBootstrapping(); |
| 83 |
| 84 // Set up the ArrayBuffer constructor function. |
| 85 %SetCode($ArrayBuffer, ArrayBufferConstructor); |
| 86 %FunctionSetPrototype($ArrayBuffer, new $Object()); |
| 87 |
| 88 // Set up the constructor property on the ArrayBuffer prototype object. |
| 89 %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); |
| 90 |
| 91 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); |
| 92 |
| 93 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( |
| 94 "slice", ArrayBufferSlice |
| 95 )); |
| 96 } |
| 97 |
| 98 SetUpArrayBuffer(); |
35 | 99 |
36 | 100 |
37 // TODO(wingo): Give link to specification. For now, the following diagram is | |
38 // the spec: | |
39 // http://wiki.ecmascript.org/lib/exe/fetch.php?cache=cache&media=harmony:es6_ge
nerator_object_model_3-29-13.png | |
40 | |
41 function GeneratorObjectNext() { | |
42 if (!IS_GENERATOR(this)) { | |
43 throw MakeTypeError('incompatible_method_receiver', | |
44 ['[Generator].prototype.next', this]); | |
45 } | |
46 | |
47 return %_GeneratorSend(this, void 0); | |
48 } | |
49 | |
50 function GeneratorObjectSend(value) { | |
51 if (!IS_GENERATOR(this)) { | |
52 throw MakeTypeError('incompatible_method_receiver', | |
53 ['[Generator].prototype.send', this]); | |
54 } | |
55 | |
56 return %_GeneratorSend(this, value); | |
57 } | |
58 | |
59 function GeneratorObjectThrow(exn) { | |
60 if (!IS_GENERATOR(this)) { | |
61 throw MakeTypeError('incompatible_method_receiver', | |
62 ['[Generator].prototype.throw', this]); | |
63 } | |
64 | |
65 return %_GeneratorThrow(this, exn); | |
66 } | |
67 | |
68 function SetUpGenerators() { | |
69 %CheckIsBootstrapping(); | |
70 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; | |
71 InstallFunctions(GeneratorObjectPrototype, | |
72 DONT_ENUM | DONT_DELETE | READ_ONLY, | |
73 ["next", GeneratorObjectNext, | |
74 "send", GeneratorObjectSend, | |
75 "throw", GeneratorObjectThrow]); | |
76 %SetProperty(GeneratorObjectPrototype, "constructor", | |
77 GeneratorFunctionPrototype, DONT_ENUM | DONT_DELETE | READ_ONLY); | |
78 %SetPrototype(GeneratorFunctionPrototype, $Function.prototype); | |
79 %SetProperty(GeneratorFunctionPrototype, "constructor", | |
80 GeneratorFunction, DONT_ENUM | DONT_DELETE | READ_ONLY); | |
81 %SetPrototype(GeneratorFunction, $Function); | |
82 } | |
83 | |
84 SetUpGenerators(); | |
OLD | NEW |