OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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("mojo/public/js/bindings/codec", [ | 5 define("mojo/public/js/bindings/codec", [ |
6 "mojo/public/js/bindings/unicode" | 6 "mojo/public/js/bindings/unicode" |
7 ], function(unicode) { | 7 ], function(unicode) { |
8 | 8 |
9 var kErrorUnsigned = "Passing negative value to unsigned"; | 9 var kErrorUnsigned = "Passing negative value to unsigned"; |
10 | 10 |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 Decoder.prototype.decodeArray = function(cls) { | 222 Decoder.prototype.decodeArray = function(cls) { |
223 var numberOfBytes = this.readUint32(); | 223 var numberOfBytes = this.readUint32(); |
224 var numberOfElements = this.readUint32(); | 224 var numberOfElements = this.readUint32(); |
225 var val = new Array(numberOfElements); | 225 var val = new Array(numberOfElements); |
226 for (var i = 0; i < numberOfElements; ++i) { | 226 for (var i = 0; i < numberOfElements; ++i) { |
227 val[i] = cls.decode(this); | 227 val[i] = cls.decode(this); |
228 } | 228 } |
229 return val; | 229 return val; |
230 }; | 230 }; |
231 | 231 |
| 232 Decoder.prototype.decodeStruct = function(cls) { |
| 233 return cls.decode(this); |
| 234 }; |
| 235 |
232 Decoder.prototype.decodeStructPointer = function(cls) { | 236 Decoder.prototype.decodeStructPointer = function(cls) { |
233 return cls.decode(this.decodeAndCreateDecoder()); | 237 return cls.decode(this.decodeAndCreateDecoder()); |
234 }; | 238 }; |
235 | 239 |
236 Decoder.prototype.decodeArrayPointer = function(cls) { | 240 Decoder.prototype.decodeArrayPointer = function(cls) { |
237 return this.decodeAndCreateDecoder().decodeArray(cls); | 241 return this.decodeAndCreateDecoder().decodeArray(cls); |
238 }; | 242 }; |
239 | 243 |
240 Decoder.prototype.decodeStringPointer = function() { | 244 Decoder.prototype.decodeStringPointer = function() { |
241 return this.decodeAndCreateDecoder().decodeString(); | 245 return this.decodeAndCreateDecoder().decodeString(); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 Encoder.prototype.encodeArray = function(cls, val) { | 355 Encoder.prototype.encodeArray = function(cls, val) { |
352 var numberOfElements = val.length; | 356 var numberOfElements = val.length; |
353 var numberOfBytes = kArrayHeaderSize + cls.encodedSize * numberOfElements; | 357 var numberOfBytes = kArrayHeaderSize + cls.encodedSize * numberOfElements; |
354 this.writeUint32(numberOfBytes); | 358 this.writeUint32(numberOfBytes); |
355 this.writeUint32(numberOfElements); | 359 this.writeUint32(numberOfElements); |
356 for (var i = 0; i < numberOfElements; ++i) { | 360 for (var i = 0; i < numberOfElements; ++i) { |
357 cls.encode(this, val[i]); | 361 cls.encode(this, val[i]); |
358 } | 362 } |
359 }; | 363 }; |
360 | 364 |
| 365 Encoder.prototype.encodeStruct = function(cls, val) { |
| 366 return cls.encode(this, val); |
| 367 }; |
| 368 |
361 Encoder.prototype.encodeStructPointer = function(cls, val) { | 369 Encoder.prototype.encodeStructPointer = function(cls, val) { |
362 var encoder = this.createAndEncodeEncoder(cls.encodedSize); | 370 var encoder = this.createAndEncodeEncoder(cls.encodedSize); |
363 cls.encode(encoder, val); | 371 cls.encode(encoder, val); |
364 }; | 372 }; |
365 | 373 |
366 Encoder.prototype.encodeArrayPointer = function(cls, val) { | 374 Encoder.prototype.encodeArrayPointer = function(cls, val) { |
367 var encodedSize = kArrayHeaderSize + cls.encodedSize * val.length; | 375 var encodedSize = kArrayHeaderSize + cls.encodedSize * val.length; |
368 var encoder = this.createAndEncodeEncoder(encodedSize); | 376 var encoder = this.createAndEncodeEncoder(encodedSize); |
369 encoder.encodeArray(cls, val); | 377 encoder.encodeArray(cls, val); |
370 }; | 378 }; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 | 489 |
482 function Int8() { | 490 function Int8() { |
483 } | 491 } |
484 | 492 |
485 Int8.encodedSize = 1; | 493 Int8.encodedSize = 1; |
486 | 494 |
487 Int8.decode = function(decoder) { | 495 Int8.decode = function(decoder) { |
488 return decoder.readInt8(); | 496 return decoder.readInt8(); |
489 }; | 497 }; |
490 | 498 |
| 499 Int8.encode = function(encoder, val) { |
| 500 encoder.writeInt8(val); |
| 501 }; |
| 502 |
491 Uint8.encode = function(encoder, val) { | 503 Uint8.encode = function(encoder, val) { |
492 encoder.writeUint8(val); | 504 encoder.writeUint8(val); |
493 }; | 505 }; |
494 | 506 |
495 function Uint8() { | 507 function Uint8() { |
496 } | 508 } |
497 | 509 |
498 Uint8.encodedSize = 1; | 510 Uint8.encodedSize = 1; |
499 | 511 |
500 Uint8.decode = function(decoder) { | 512 Uint8.decode = function(decoder) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 Uint64.encodedSize = 8; | 588 Uint64.encodedSize = 8; |
577 | 589 |
578 Uint64.decode = function(decoder) { | 590 Uint64.decode = function(decoder) { |
579 return decoder.readUint64(); | 591 return decoder.readUint64(); |
580 }; | 592 }; |
581 | 593 |
582 Uint64.encode = function(encoder, val) { | 594 Uint64.encode = function(encoder, val) { |
583 encoder.writeUint64(val); | 595 encoder.writeUint64(val); |
584 }; | 596 }; |
585 | 597 |
586 // TODO(abarth): Add missing types: | 598 function String() { |
587 // * String | 599 }; |
| 600 |
| 601 String.encodedSize = 8; |
| 602 |
| 603 String.decode = function(decoder) { |
| 604 return decoder.decodeStringPointer(); |
| 605 }; |
| 606 |
| 607 String.encode = function(encoder, val) { |
| 608 encoder.encodeStringPointer(val); |
| 609 }; |
| 610 |
588 | 611 |
589 function Float() { | 612 function Float() { |
590 } | 613 } |
591 | 614 |
592 Float.encodedSize = 4; | 615 Float.encodedSize = 4; |
593 | 616 |
594 Float.decode = function(decoder) { | 617 Float.decode = function(decoder) { |
595 return decoder.readFloat(); | 618 return decoder.readFloat(); |
596 }; | 619 }; |
597 | 620 |
(...skipping 29 matching lines...) Expand all Loading... |
627 this.cls.encode(objectEncoder, val); | 650 this.cls.encode(objectEncoder, val); |
628 }; | 651 }; |
629 | 652 |
630 function ArrayOf(cls) { | 653 function ArrayOf(cls) { |
631 this.cls = cls; | 654 this.cls = cls; |
632 } | 655 } |
633 | 656 |
634 ArrayOf.prototype.encodedSize = 8; | 657 ArrayOf.prototype.encodedSize = 8; |
635 | 658 |
636 ArrayOf.prototype.decode = function(decoder) { | 659 ArrayOf.prototype.decode = function(decoder) { |
637 return decoder.decodeArrayPointer(self.cls); | 660 return decoder.decodeArrayPointer(this.cls); |
638 }; | 661 }; |
639 | 662 |
640 ArrayOf.prototype.encode = function(encoder, val) { | 663 ArrayOf.prototype.encode = function(encoder, val) { |
641 encoder.encodeArrayPointer(self.cls, val); | 664 encoder.encodeArrayPointer(this.cls, val); |
642 }; | 665 }; |
643 | 666 |
644 function Handle() { | 667 function Handle() { |
645 } | 668 } |
646 | 669 |
647 Handle.encodedSize = 4; | 670 Handle.encodedSize = 4; |
648 | 671 |
649 Handle.decode = function(decoder) { | 672 Handle.decode = function(decoder) { |
650 return decoder.decodeHandle(); | 673 return decoder.decodeHandle(); |
651 }; | 674 }; |
(...skipping 17 matching lines...) Expand all Loading... |
669 exports.Int8 = Int8; | 692 exports.Int8 = Int8; |
670 exports.Uint8 = Uint8; | 693 exports.Uint8 = Uint8; |
671 exports.Int16 = Int16; | 694 exports.Int16 = Int16; |
672 exports.Uint16 = Uint16; | 695 exports.Uint16 = Uint16; |
673 exports.Int32 = Int32; | 696 exports.Int32 = Int32; |
674 exports.Uint32 = Uint32; | 697 exports.Uint32 = Uint32; |
675 exports.Int64 = Int64; | 698 exports.Int64 = Int64; |
676 exports.Uint64 = Uint64; | 699 exports.Uint64 = Uint64; |
677 exports.Float = Float; | 700 exports.Float = Float; |
678 exports.Double = Double; | 701 exports.Double = Double; |
| 702 exports.String = String; |
679 exports.PointerTo = PointerTo; | 703 exports.PointerTo = PointerTo; |
680 exports.ArrayOf = ArrayOf; | 704 exports.ArrayOf = ArrayOf; |
681 exports.Handle = Handle; | 705 exports.Handle = Handle; |
682 return exports; | 706 return exports; |
683 }); | 707 }); |
OLD | NEW |