OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 define(function() { | 5 define(function() { |
6 | 6 |
7 // Memory ------------------------------------------------------------------- | 7 // Memory ------------------------------------------------------------------- |
8 | 8 |
9 function store8(memory, pointer, val) { | 9 function store8(memory, pointer, val) { |
10 memory[pointer] = val; | 10 memory[pointer] = val; |
(...skipping 26 matching lines...) Expand all Loading... |
37 (memory[pointer + 1] << 8); | 37 (memory[pointer + 1] << 8); |
38 } | 38 } |
39 | 39 |
40 function load32(memory, pointer) { | 40 function load32(memory, pointer) { |
41 return (memory[pointer + 0] << 0) + | 41 return (memory[pointer + 0] << 0) + |
42 (memory[pointer + 1] << 8) + | 42 (memory[pointer + 1] << 8) + |
43 (memory[pointer + 2] << 16) + | 43 (memory[pointer + 2] << 16) + |
44 (memory[pointer + 3] << 24); | 44 (memory[pointer + 3] << 24); |
45 } | 45 } |
46 | 46 |
47 function load64(memory, pointer) { | |
48 var low = load32(memory, pointer); | |
49 var high = load32(memory, pointer + 4); | |
50 return low + high * 0x10000; | |
51 } | |
52 | |
53 var kAlignment = 8; | 47 var kAlignment = 8; |
54 | 48 |
55 function align(size) { | 49 function align(size) { |
56 return size + (kAlignment - (size % kAlignment)) % kAlignment; | 50 return size + (kAlignment - (size % kAlignment)) % kAlignment; |
57 } | 51 } |
58 | 52 |
59 // Buffer ------------------------------------------------------------------- | 53 // Buffer ------------------------------------------------------------------- |
60 | 54 |
61 function Buffer(size) { | 55 function Buffer(size) { |
62 this.memory = new Uint8Array(size); | 56 this.memory = new Uint8Array(size); |
(...skipping 28 matching lines...) Expand all Loading... |
91 var kStructHeaderSize = 8; | 85 var kStructHeaderSize = 8; |
92 var kMessageHeaderSize = 8; | 86 var kMessageHeaderSize = 8; |
93 | 87 |
94 // Decoder ------------------------------------------------------------------ | 88 // Decoder ------------------------------------------------------------------ |
95 | 89 |
96 function Decoder(memory, handles, base) { | 90 function Decoder(memory, handles, base) { |
97 this.memory = memory; | 91 this.memory = memory; |
98 this.handles = handles; | 92 this.handles = handles; |
99 this.base = base; | 93 this.base = base; |
100 this.next = base; | 94 this.next = base; |
| 95 this.viewU32 = new Uint32Array( |
| 96 this.memory.buffer, 0, |
| 97 Math.floor(this.memory.length / Uint32Array.BYTES_PER_ELEMENT)); |
101 } | 98 } |
102 | 99 |
103 Decoder.prototype.skip = function(offset) { | 100 Decoder.prototype.skip = function(offset) { |
104 this.next += offset; | 101 this.next += offset; |
105 }; | 102 }; |
106 | 103 |
107 Decoder.prototype.read8 = function() { | 104 Decoder.prototype.read8 = function() { |
108 var result = load8(this.memory, this.next); | 105 var result = load8(this.memory, this.next); |
109 this.next += 1; | 106 this.next += 1; |
110 return result; | 107 return result; |
111 }; | 108 }; |
112 | 109 |
113 Decoder.prototype.read32 = function() { | 110 Decoder.prototype.read32 = function() { |
114 var result = load32(this.memory, this.next); | 111 var result = this.viewU32[this.next / this.viewU32.BYTES_PER_ELEMENT]; |
115 this.next += 4; | 112 this.next += this.viewU32.BYTES_PER_ELEMENT; |
116 return result; | 113 return result; |
117 }; | 114 }; |
118 | 115 |
119 Decoder.prototype.read64 = function() { | 116 Decoder.prototype.read64 = function() { |
120 var result = load64(this.memory, this.next); | 117 var low = this.read32(); |
121 this.next += 8; | 118 var high = this.read32(); |
122 return result; | 119 return low + high * 0x100000000; |
123 }; | 120 }; |
124 | 121 |
125 Decoder.prototype.decodePointer = function() { | 122 Decoder.prototype.decodePointer = function() { |
126 // TODO(abarth): To correctly decode a pointer, we need to know the real | 123 // TODO(abarth): To correctly decode a pointer, we need to know the real |
127 // base address of the array buffer. | 124 // base address of the array buffer. |
128 var offsetPointer = this.next; | 125 var offsetPointer = this.next; |
129 var offset = this.read64(); | 126 var offset = this.read64(); |
130 if (!offset) | 127 if (!offset) |
131 return 0; | 128 return 0; |
132 return offsetPointer + offset; | 129 return offsetPointer + offset; |
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 exports.kMessageHeaderSize = kMessageHeaderSize; | 433 exports.kMessageHeaderSize = kMessageHeaderSize; |
437 exports.Uint8 = Uint8; | 434 exports.Uint8 = Uint8; |
438 exports.Uint16 = Uint16; | 435 exports.Uint16 = Uint16; |
439 exports.Uint32 = Uint32; | 436 exports.Uint32 = Uint32; |
440 exports.Uint64 = Uint64; | 437 exports.Uint64 = Uint64; |
441 exports.PointerTo = PointerTo; | 438 exports.PointerTo = PointerTo; |
442 exports.ArrayOf = ArrayOf; | 439 exports.ArrayOf = ArrayOf; |
443 exports.Handle = Handle; | 440 exports.Handle = Handle; |
444 return exports; | 441 return exports; |
445 }); | 442 }); |
OLD | NEW |