123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- // write-core.js
- var ExtBuffer = require("./ext-buffer").ExtBuffer;
- var ExtPacker = require("./ext-packer");
- var WriteType = require("./write-type");
- var CodecBase = require("./codec-base");
- CodecBase.install({
- addExtPacker: addExtPacker,
- getExtPacker: getExtPacker,
- init: init
- });
- exports.preset = init.call(CodecBase.preset);
- function getEncoder(options) {
- var writeType = WriteType.getWriteType(options);
- return encode;
- function encode(encoder, value) {
- var func = writeType[typeof value];
- if (!func) throw new Error("Unsupported type \"" + (typeof value) + "\": " + value);
- func(encoder, value);
- }
- }
- function init() {
- var options = this.options;
- this.encode = getEncoder(options);
- if (options && options.preset) {
- ExtPacker.setExtPackers(this);
- }
- return this;
- }
- function addExtPacker(etype, Class, packer) {
- packer = CodecBase.filter(packer);
- var name = Class.name;
- if (name && name !== "Object") {
- var packers = this.extPackers || (this.extPackers = {});
- packers[name] = extPacker;
- } else {
- // fallback for IE
- var list = this.extEncoderList || (this.extEncoderList = []);
- list.unshift([Class, extPacker]);
- }
- function extPacker(value) {
- if (packer) value = packer(value);
- return new ExtBuffer(value, etype);
- }
- }
- function getExtPacker(value) {
- var packers = this.extPackers || (this.extPackers = {});
- var c = value.constructor;
- var e = c && c.name && packers[c.name];
- if (e) return e;
- // fallback for IE
- var list = this.extEncoderList || (this.extEncoderList = []);
- var len = list.length;
- for (var i = 0; i < len; i++) {
- var pair = list[i];
- if (c === pair[0]) return pair[1];
- }
- }
|