all files / montage/core/serialization/deserializer/ montage-reviver.js

90.07% Statements 263/292
84.18% Branches 149/177
86.27% Functions 44/51
90.07% Lines 263/292
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783               108×       108×       108×         108× 108×   108×           202×     202× 202×   202× 12×     202× 24×     178×           178×       178×     178×     178×   178× 10×     178×                                   108×   108× 108×           1664×   1664× 18× 1646× 93× 1553× 652× 315× 337× 331× 77× 254× 56×       1099×           275×   275×             275×         275×   275×     270×         270×         270×   41×     39×   39×       37×     39×   229×   11×         218×           40×     40× 40×   40×                 54×     54× 54×   54×           218×   216×             216×           216× 178× 178×   178×     216× 10× 10×                       206×     206×                                                               215×       215× 214×   214× 214×     214×   214×     212×             214×   214× 133×       81× 81×   81×         81×               81×   81×         81× 86×       81×           133×   133×   133×   133×   132×   131×             215×   215×   41×   174×   168×               168× 168× 168×   168× 168×   168×                                             133×     133×   133×       131×               131×   131× 263×   263× 261×     263×     212× 13×                 133×         133× 133× 85×   85× 224× 15×   15×                                           1482×   1482× 703× 779× 778× 301× 477× 85× 392× 299× 93× 38×   55×             703× 14×     703×           515×     515× 14×     515× 1236×   1236×           1231×       515× 510×                                   301×     301×           85×     85×     85× 208×   208×         208×       85× 85×                                   55×                             19×                                           178×           178× 157×   21×   21×   14× 14× 14×           21×       21×     178×                     132×                                                                      
var Montage = require("../../core").Montage;
var PropertiesDeserializer = require("./properties-deserializer").PropertiesDeserializer;
var SelfDeserializer = require("./self-deserializer").SelfDeserializer;
var UnitDeserializer = require("./unit-deserializer").UnitDeserializer;
var ModuleReference = require("../../module-reference").ModuleReference;
var Alias = require("../alias").Alias;
 
var Promise = require("../../promise").Promise;
 
var ModuleLoader = Montage.specialize( {
    _require: {value: null},
    _objectRequires: {value: null},
 
    init: {
        value: function (_require, objectRequires) {
            Iif (typeof _require !== "function") {
                throw new Error("Function 'require' missing.");
            }
 
            Iif (typeof _require.location !== "string") {
                throw new Error("Function 'require' location is missing");
            }
 
            Iif (typeof objectRequires !== "object" &&
                typeof objectRequires !== "undefined") {
                throw new Error("Parameter 'objectRequires' should be an object.");
            }
 
            this._require = _require;
            this._objectRequires = objectRequires;
 
            return this;
        }
    },
 
    getExports: {
        value: function (_require, moduleId) {
            var module;
 
            // Transforms relative module ids into absolute module ids
            moduleId = _require.resolve(moduleId);
            module = _require.getModuleDescriptor(moduleId);
 
            while (module.redirect !== void 0) {
                module = _require.getModuleDescriptor(module.redirect);
            }
 
            if (module.mappingRedirect !== void 0) {
                return this.getExports(module.mappingRequire, module.mappingRedirect);
            }
 
            return module.exports;
        }
    },
 
    getModule: {
        value: function (moduleId, label) {
            var objectRequires = this._objectRequires,
                _require,
                module;
 
            Iif (objectRequires && label in objectRequires) {
                _require = objectRequires[label];
            } else {
                _require = this._require;
            }
 
            module = this.getExports(_require, moduleId);
 
            if (!module) {
                module = _require.async(moduleId);
            }
 
            return module;
        }
    }
});
 
/**
 * @class MontageReviver
 */
var MontageReviver = exports.MontageReviver = Montage.specialize(/** @lends MontageReviver# */ {
    moduleLoader: {value: null},
 
    /**
     * @param {Require} _require The require object to load modules
     * @param {Object} objectRequires A dictionary indexed by object label with
     *        the require object to use for a specific object of the
     *        serialization.
     */
    init: {
        value: function (_require, objectRequires) {
            this.moduleLoader = new ModuleLoader()
                                 .init(_require, objectRequires);
            this._require = _require;
            return this;
        }
    },
 
    getTypeOf: {
        value: function (value) {
            var typeOf = typeof value;
 
            if (value === null) {
                return "null";
            } else if (Array.isArray(value)) {
                return "array";
            } else if (typeOf === "object" && Object.keys(value).length === 1) {
                if ("@" in value) {
                    return "reference";
                } else if ("/" in value) {
                    return "regexp";
                } else if ("#" in value) {
                    return "Element";
                } else if ("%" in value) {
                    return "Module";
                } // else return typeOf -> object
            }
 
            return typeOf;
        }
    },
 
    _checkLabel: {
        value: function (label, isTemplateProperty) {
            Iif (isTemplateProperty && label[0] !== ":") {
                return new Error("Aliases can only be defined in template properties (start with a colon (:)), \"" + label + "\".");
            } else if (!isTemplateProperty && label[0] === ":") {
                return new Error("Only aliases are allowed as template properties (start with a colon (:), \"" + label + "\".");
            }
        }
    },
 
    reviveRootObject: {
        value: function (value, context, label) {
            var error,
                isAlias = "alias" in value;
 
            // Only aliases are allowed as template properties, everything else
            // should be rejected as an error.
            error = this._checkLabel(label, isAlias);
 
            if (error) {
                return Promise.reject(error);
            }
 
            var object;
 
            // Check if the optional "debugger" unit is set for this object
            // and stop the execution. This is intended to provide a certain
            // level of debugging in the serialization.
            Iif (value.debugger) {
                console.log("enable debugger statement here");
                //debugger;
            }
 
            if ("value" in value) {
                // it's overriden by a user object
                if (context.hasUserObject(label)) {
                    object = context.getUserObject(label);
                    context.setObjectLabel(object, label);
                    return object;
                }
 
                var revivedValue;
 
                if (this.getTypeOf(value.value) === "Element") {
                    revivedValue = this.reviveElement(value.value, context, label);
 
                    Eif (!Promise.is(revivedValue)) {
                        var montageObjectDesc = this.reviveObjectLiteral(value, context);
                        context.setUnitsToDeserialize(revivedValue, montageObjectDesc, MontageReviver._unitNames);
                    }
                } else {
                    revivedValue = this.reviveValue(value.value, context, label);
                }
 
                return revivedValue;
 
            } else if (Object.keys(value).length === 0) {
                // it's an external object
                if (context.hasUserObject(label)) {
                    object = context.getUserObject(label);
                    context.setObjectLabel(object, label);
                    return object;
                }
 
                return this.reviveExternalObject(value, context, label);
            }
 
            return this.reviveCustomObject(value, context, label);
        }
    },
 
    reviveElement: {
        value: function (value, context, label) {
            var elementId = value["#"],
                element = context.getElementById(elementId);
 
            Eif (element) {
                if (label) {
                    context.setObjectLabel(element, label);
                }
                return element;
            } else {
                return Promise.reject(new Error("Element with id '" + elementId + "' was not found."));
            }
        }
    },
 
    reviveModule: {
        value: function (value, context, label) {
            var moduleId = value["%"],
                _require = context.getRequire();
 
            moduleId = _require.resolve(moduleId);
            var module = _require.getModuleDescriptor(moduleId);
 
            return new ModuleReference().initWithIdAndRequire(module.id, module.require);
        }
    },
 
    reviveCustomObject: {
        value: function (value, context, label) {
            if ("alias" in value) {
                return this.reviveAlias(value, context, label);
            } else {
                return this.reviveMontageObject(value, context, label);
            }
        }
    },
 
    reviveMontageObject: {
        value: function (value, context, label) {
            var self = this,
                module,
                locationDesc,
                locationId = value.prototype || value.object,
                objectName;
 
            if (locationId) {
                locationDesc = MontageReviver.parseObjectLocationId(locationId);
                module = this.moduleLoader.getModule(locationDesc.moduleId,
                    label);
                objectName = locationDesc.objectName;
            }
 
            if (Promise.is(module)) {
                return module.then(function(exports) {
                    if ("object" in value && value.object.endsWith(".mjson")) {
                        return self.instantiateMjsonObject(exports, locationDesc.moduleId);
                    } else {
                        return self.instantiateMontageObject(value, exports, objectName, context, label);
                    }
                }, function (error) {
                    if (error.stack) {
                        console.error(error.stack);
                    }
                    throw new Error('Error deserializing "' + label +
                        '" when loading module "' + locationDesc.moduleId +
                        "' from '" + value.prototype + "' cause: " + error.message);
                });
            } else {
                Iif ("object" in value && value.object.endsWith(".mjson")) {
                    return self.instantiateMjsonObject(module, locationDesc.moduleId);
                } else {
                    return this.instantiateMontageObject(value, module, objectName, context, label);
                }
            }
        }
    },
 
    instantiateMjsonObject: {
        value: function (json, moduleId) {
            var self = this,
                getModelRequire = function (parentRequire, modelId) {
                    // TODO: This utility function is also defined in core/meta/module-blueprint.js.
                    // Maybe it should be a helper module or baked in to deserializers.
                    var topId = parentRequire.resolve(modelId);
                    var module = parentRequire.getModuleDescriptor(topId);
                    while (module.redirect || module.mappingRedirect) {
                        if (module.redirect) {
                            topId = module.redirect;
                        } else {
                            parentRequire = module.mappingRequire;
                            topId = module.mappingRedirect;
                        }
                        module = parentRequire.getModuleDescriptor(topId);
                    }
                    return module.require;
                };
            // Need to require deserializer asynchronously because it depends on montage-interpreter, which
            // depends on this module, montage-reviver. A synchronous require would create a circular dependency.
            // TODO: Maybe this could be passed in from above instead of required here.
            return require.async("core/serialization/deserializer/montage-deserializer")
                .then(function (deserializerModule) {
                    return new deserializerModule.MontageDeserializer()
                        .init(JSON.stringify(json), getModelRequire(self._require, moduleId)) // TODO: MontageDeserializer needs an API to pass in an object instead of the stringified version of the object
                        .deserializeObject();
                });
        }
    },
 
    instantiateMontageObject: {
        value: function (value, module, objectName, context, label) {
            var self = this,
                object,
                montageObjectDesc;
 
            object = this.getMontageObject(value, module, objectName, context, label);
            context.setObjectLabel(object, label);
            
            Eif (object !== null && object !== void 0) {
                object.isDeserializing = true;
            }
 
            montageObjectDesc = this.reviveObjectLiteral(value, context);
 
            if (Promise.is(montageObjectDesc)) {
                return montageObjectDesc.then(function(montageObjectDesc) {
                    return self.deserializeMontageObject(montageObjectDesc, object, context, label);
                });
            } else {
                return this.deserializeMontageObject(montageObjectDesc, object,  context, label);
            }
        }
    },
 
    deserializeMontageObject: {
        value: function (montageObjectDesc, object, context, label) {
            var properties;
 
            if (typeof object.deserializeSelf === "function") {
                return this.deserializeCustomMontageObject(object, montageObjectDesc, context, label);
            } else {
                // Units are deserialized after all objects have been revived.
                // This happens at didReviveObjects.
                context.setUnitsToDeserialize(object, montageObjectDesc, MontageReviver._unitNames);
                properties = this.deserializeMontageObjectProperties(object, montageObjectDesc.properties, context);
 
                Iif (Promise.is(properties)) {
                    return properties.then(function() {
                        return object;
                    });
                } else {
                    return object;
                }
            }
        }
    },
 
    deserializeMontageObjectProperties: {
        value: function (object, properties, context) {
            var value;
 
            Iif (typeof object.deserializeProperties === "function") {
                var propertiesDeserializer = new PropertiesDeserializer()
                    .initWithReviverAndObjects(this, context);
                value = object.deserializeProperties(propertiesDeserializer);
            } else {
                for (var key in properties) {
                    object[key] = properties[key];
                }
            }
 
            return value;
        }
    },
 
    deserializeCustomMontageObject: {
        value: function (object, objectDesc, context, label) {
            var substituteObject;
 
            var selfDeserializer = new SelfDeserializer()
                .initWithObjectAndObjectDescriptorAndContextAndUnitNames(object, objectDesc, context, MontageReviver._unitNames);
            substituteObject = object.deserializeSelf(selfDeserializer);
 
            if (Promise.is(substituteObject)) {
                return substituteObject.then(function(substituteObject) {
                    context.setObjectLabel(substituteObject, label);
                    return substituteObject;
                });
            } else if (typeof substituteObject !== "undefined") {
                context.setObjectLabel(substituteObject, label);
                return substituteObject;
            } else {
                return object;
            }
        }
    },
 
    getMontageObject: {
        value: function (value, module, objectName, context, label) {
            var object;
 
            if (context.hasUserObject(label)) {
 
                return context.getUserObject(label);
 
            } else if ("prototype" in value) {
 
                Iif (!(objectName in module)) {
                    throw new Error('Error deserializing "' + label +
                        '": object named "' + objectName + '"' +
                        ' was not found in "' + value.prototype + '".' +
                        " Available objects are: " + Object.keys(module) + ".");
                }
                // TODO: For now we need this because we need to set
                // isDeserilizing before calling didCreate.
                object = Object.create(module[objectName].prototype);
                object.isDeserializing = true;
                Iif (typeof object.didCreate === "function") {
                    object.didCreate();
                } else Eif (typeof object.constructor === "function") {
                    object.constructor();
                }
                return object;
                //return module[objectName].create();
 
            } else if ("object" in value) {
                if (value.object.endsWith(".json")) {
                    return module;
                }
 
                Iif (!(objectName in module)) {
                    throw new Error('Error deserializing "' + label +
                        '": object named "' + object +
                        "' was not found given '" + value.object + "'");
                }
                return module[objectName];
 
            } else {
                throw new Error("Error deserializing " + JSON.stringify(value) + ", might need \"prototype\" or \"object\" on label " + JSON.stringify(label));
            }
        }
    },
 
    reviveAlias: {
        value: function (value, context, label) {
            var alias = new Alias();
            alias.value = value.alias;
 
            context.setObjectLabel(alias, label);
            return alias;
        }
    },
 
    didReviveObjects: {
        value: function (objects, context) {
            var self = this,
                value;
 
            value = this._deserializeUnits(context);
 
            if (Promise.is(value)) {
                return value.then(function() {
                    self._invokeDeserializedFromSerialization(objects, context);
                });
            } else {
                this._invokeDeserializedFromSerialization(objects, context);
            }
        }
    },
 
    // TODO: can deserializeSelf make deserializedFromSerialization irrelevant?
    _invokeDeserializedFromSerialization: {
        value: function (objects, context) {
            var object;
 
            for (var label in objects) {
                object = objects[label];
 
                if (object !== null && object !== void 0) {
                    delete object.isDeserializing;
                }
 
                if (!context.hasUserObject(label)) {
                    // TODO: merge deserializedFromSerialization with
                    //       deserializedFromTemplate?
                    if (object && typeof object.deserializedFromSerialization === "function") {
                        object.deserializedFromSerialization(label);
                    }
                }
            }
        }
    },
 
    _deserializeUnits: {
        value: function (context) {
            var unitsToDeserialize = context.getUnitsToDeserialize(),
                units = MontageReviver._unitRevivers,
                unitNames,
                unitDeserializer;
 
            try {
                for (var i = 0, unitsDesc; unitsDesc = unitsToDeserialize[i]; i++) {
                    unitNames = unitsDesc.unitNames;
 
                    for (var j = 0, unitName; unitName = unitNames[j]; j++) {
                        if (unitName in unitsDesc.objectDesc) {
                            unitDeserializer = new UnitDeserializer()
                                .initWithContext(context);
                            units[unitName](unitDeserializer, unitsDesc.object, unitsDesc.objectDesc[unitName]);
                        }
                    }
                }
            } catch (ex) {
                return Promise.reject(ex);
            }
        }
    },
 
    _createAssignValueFunction: {
        value: function(object, propertyName) {
            return function(value) {
                object[propertyName] = value;
            }
        }
    },
 
    getCustomObjectTypeOf: {
        writable: true,
        value: function() {}
    },
 
    reviveValue: {
        value: function(value, context, label) {
            var type = this.getTypeOf(value);
 
            if (type === "string" || type === "number" || type === "boolean" || type === "null" || type === "undefined") {
                return this.reviveNativeValue(value, context, label);
            } else if (type === "regexp") {
                return this.reviveRegExp(value, context, label);
            } else if (type === "reference") {
                return this.reviveObjectReference(value, context, label);
            } else if (type === "array") {
                return this.reviveArray(value, context, label);
            } else if (type === "object") {
                return this.reviveObjectLiteral(value, context, label);
            } else if (type === "Element") {
                return this.reviveElement(value, context, label);
            } else {
                return this._callReviveMethod("revive" + type, value, context, label);
            }
        }
    },
 
    reviveNativeValue: {
        value: function(value, context, label) {
            if (label) {
                context.setObjectLabel(value, label);
            }
 
            return value;
        }
    },
 
    reviveObjectLiteral: {
        value: function(value, context, label) {
            var item,
                promises = [];
 
            if (label) {
                context.setObjectLabel(value, label);
            }
 
            for (var propertyName in value) {
                item = this.reviveValue(value[propertyName], context);
 
                if (Promise.is(item)) {
                    promises.push(
                        item.then(this._createAssignValueFunction(
                                value, propertyName)
                        )
                    );
                } else {
                    value[propertyName] = item;
                }
            }
 
            if (promises.length === 0) {
                return value;
            } else {
                return Promise.all(promises).then(function() {
                    return value;
                });
            }
        }
    },
 
    reviveRegExp: {
        value: function(value, context, label) {
            var value = value["/"],
                regexp = new RegExp(value.source, value.flags);
 
            Eif (label) {
                context.setObjectLabel(regexp, label);
            }
 
            return regexp;
        }
    },
 
    reviveObjectReference: {
        value: function(value, context, label) {
            var value = value["@"],
                object = context.getObject(value);
 
            return object;
        }
    },
 
    reviveArray: {
        value: function(value, context, label) {
            var item,
                promises = [];
 
            if (label) {
                context.setObjectLabel(value, label);
            }
 
            for (var i = 0, ii = value.length; i < ii; i++) {
                item = this.reviveValue(value[i], context);
 
                Iif (Promise.is(item)) {
                    promises.push(
                        item.then(this._createAssignValueFunction(value, i))
                    );
                } else {
                    value[i] = item;
                }
            }
 
            Eif (promises.length === 0) {
                return value;
            } else {
                return Promise.all(promises).then(function() {
                    return value;
                });
            }
        }
    },
 
    reviveExternalObject: {
        value: function(value, context, label) {
            return Promise.reject(
                new Error("External object '" + label + "' not found in user objects.")
            );
        }
    },
 
    _callReviveMethod: {
        value: function(methodName, value, context, label) {
            return this[methodName](value, context, label);
        }
    }
 
}, /** @lends MontageReviver. */ {
    _unitRevivers: {value: Object.create(null)},
    _unitNames: {value: []},
 
    _findObjectNameRegExp: {
        value: /([^\/]+?)(\.reel)?$/
    },
    _toCamelCaseRegExp: {
        value: /(?:^|-)([^-])/g
    },
    _replaceToCamelCase: {
        value: function (_, g1) { return g1.toUpperCase(); }
    },
    // Cache of location descriptors indexed by locationId
    _locationDescCache: {value: Object.create(null)},
 
    customObjectRevivers: {value: Object.create(null)},
 
    // Location Id is in the form of <moduleId>[<objectName>] where
    // [<objectName>] is optional. When objectName is missing it is derived
    // from the last path component of moduleId transformed into CamelCase.
    //
    // Example: "event/event-manager" has a default objectName of EventManager.
    //
    // When the last path component ends with ".reel" it is removed before
    // creating the default objectName.
    //
    // Example: "matte/ui/input-range.reel" has a default objectName of
    //          InputRange.
    //
    // @returns {moduleId, objectName}
    parseObjectLocationId: {
        value: function (locationId) {
            var locationDescCache = this._locationDescCache,
                locationDesc,
                bracketIndex,
                moduleId,
                objectName;
 
            if (locationId in locationDescCache) {
                locationDesc = locationDescCache[locationId];
            } else {
                bracketIndex = locationId.indexOf("[");
 
                if (bracketIndex > 0) {
                    moduleId = locationId.substr(0, bracketIndex);
                    objectName = locationId.slice(bracketIndex + 1, -1);
                } else {
                    moduleId = locationId;
                    this._findObjectNameRegExp.test(locationId);
                    objectName = RegExp.$1.replace(
                        this._toCamelCaseRegExp,
                        this._replaceToCamelCase
                    );
                }
 
                locationDesc = {
                    moduleId: moduleId,
                    objectName: objectName
                };
                locationDescCache[locationId] = locationDesc;
            }
 
            return locationDesc;
        }
    },
 
    defineUnitReviver: {
        value: function (name, funktion) {
            this._unitRevivers[name] = funktion;
            this._unitNames.push(name);
        }
    },
 
    getTypeOf: {
        value: function (value) {
            return this.prototype.getTypeOf.call(this, value);
        }
    },
 
    addCustomObjectReviver: {
        value: function(reviver) {
            var customObjectRevivers = this.customObjectRevivers;
 
            for (var methodName in reviver) {
                if (methodName === "getTypeOf") {
                    continue;
                }
 
                if (typeof reviver[methodName] === "function"
                    && /^revive/.test(methodName)) {
                    Eif (typeof customObjectRevivers[methodName] === "undefined") {
                        customObjectRevivers[methodName] = reviver[methodName].bind(reviver);
                    } else {
                        return new Error("Reviver '" + methodName + "' is already registered.");
                    }
                }
            }
 
            this.prototype.getCustomObjectTypeOf = this.makeGetCustomObjectTypeOf(reviver.getTypeOf);
        }
    },
 
    resetCustomObjectRevivers: {
        value: function() {
            this.customObjectRevivers = Object.create(null);
            this.prototype.getCustomObjectTypeOf = function() {};
        }
    },
 
    makeGetCustomObjectTypeOf:{
        value: function (getCustomObjectTypeOf) {
            var previousGetCustomObjectTypeOf = this.prototype.getCustomObjectTypeOf;
 
            return function(value) {
                return getCustomObjectTypeOf(value) || previousGetCustomObjectTypeOf(value);
            }
        }
    }
 
});
 
Eif (typeof exports !== "undefined") {
    exports.MontageReviver = MontageReviver;
}