<commit_before>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*jslint sub: true*/
/**
 * @description Represents a Aura client-side context, created during HTTP requests for component definitions. A context
 *            can include a mode, such as "DEV" for development mode or "PROD" for production mode.
 * @constructor
 * @protected
 * @class AuraContext
 * @param {Object} config the 'founding' config for the context from the server.
 * @param {Function} initCallback an optional callback invoked after the config has finished its asynchronous initialization.
 * @export
 */
Aura.Context.AuraContext = function AuraContext(config, initCallback) {
    this.mode = config["mode"];
    this.loaded = config["loaded"];
    if (this.loaded === undefined) {
        this.loaded = {};
    }
    this.fwuid = config["fwuid"];
    this.num = 0;
    // To keep track of re-rendering service call
    this.renderNum = 0;
    this.transaction = 0;
    this.transactionName = "";
    this.lastGlobalId = 0;
    this.componentConfigs = {};
    this.app = config["app"];
    this.cmp = config["cmp"];
    this.test = config["test"];
    this.contextPath = config["contextPath"] || "";
    this.allowedGlobals = config["allowedGlobals"];
    this.globals = config["globals"];
    this.accessStack=[];

    var that = this;
    if(!config["globalValueProviders"]){
        config["globalValueProviders"]={};
    }
    $A.util.apply(config["globalValueProviders"],$A.globalValueProviders);

    this.globalValueProviders = new Aura.Provider.GlobalValueProviders(config["globalValueProviders"], function() {
        var i, defs;
        
        // Careful now, the def is null, this fake action sets up our paths.
        that.currentAction = new Action(null, ""+that.num, null, null, false, null, false);
        
        if(config["libraryDefs"]) {
            defs = config["libraryDefs"];
            for (i = 0; i < defs.length; i++) {
                $A.componentService.createLibraryDef(defs[i]);
            }
        }
        
        if (config["componentDefs"]) {
            defs = config["componentDefs"];
            for (i = 0; i < defs.length; i++) {
                if (defs[i]["descriptor"]) {
                    $A.componentService.saveComponentConfig(defs[i]);
                }
            }
        }
        if (config["eventDefs"]) {
            defs = config["eventDefs"];
            for (i = 0; i < defs.length; i++) {
                $A.eventService.createEventDef(defs[i]);
            }
        }
        that.joinComponentConfigs(config["components"], that.currentAction.getId());

        if (initCallback) {
            initCallback();
        }
    });
    this.contextGlobals = this.globalValueProviders.getValueProvider("Global");
};

/**
 * Returns the mode for the current request. Defaults to "PROD" for production mode and "DEV" for development mode.
 * The HTTP request format is <code>http://<your server>/namespace/component?aura.mode=PROD</code>.
 *
 * @return {string} the mode from the server.
 * @export
 */
Aura.Context.AuraContext.prototype.getMode = function() {
    return this.mode;
};

Aura.Context.AuraContext.prototype.getCurrentAccess=function(){
    var access=this.accessStack[this.accessStack.length-1];
    while(access instanceof PassthroughValue){
        access=access.getComponent();
    }
    return access;
};

Aura.Context.AuraContext.prototype.getCurrentAccessCaller=function(){
    var access=this.accessStack[this.accessStack.length-2];
    while(access instanceof PassthroughValue){
        access=access.getComponent();
    }
    return access;
};

Aura.Context.AuraContext.prototype.setCurrentAccess=function(component){
    return this.accessStack.push(component?component:this.getCurrentAccess());
};

Aura.Context.AuraContext.prototype.releaseCurrentAccess=function(){
    this.accessStack.pop();
};

Aura.Context.AuraContext.prototype.getAccessVersion = function(name) {
    var currentAccessCaller = this.getCurrentAccessCaller();
    if (currentAccessCaller) {
        return currentAccessCaller.getDef().getRequiredVersionDefs().getDef(name);
    }

    return null;
};

/**
 * Adds a new global value provider.
 * @param type The key to identify the valueProvider.
 * @param valueProvider The valueProvider to add.
 * @private
 */
Aura.Context.AuraContext.prototype.addGlobalValueProvider = function(type,valueProvider) {
    this.globalValueProviders.addValueProvider(type,valueProvider);
};

/**
 * Provides access to global value providers.
 * For example, <code>$A.get("$Label.Related_Lists.task_mode_today");</code> gets the label value.
 * 
 * @return {GlobalValueProviders}
 * @private
 */
Aura.Context.AuraContext.prototype.getGlobalValueProvider = function(type) {
    return this.globalValueProviders.getValueProvider(type);
};

/**
 * JSON representation of context for server requests.
 *
 * This must remain in sync with AuraTestingUtil so that we can accurately test.
 * 
 * @return {String} json representation
 * @private
 */
Aura.Context.AuraContext.prototype.encodeForServer = function() {
    return aura.util.json.encode({
        "mode" : this.mode,
        "loaded" : this.loaded,
        "dn" : $A.services.component.getDynamicNamespaces(),
        "app" : this.app,
        "cmp" : this.cmp,
        "fwuid" : this.fwuid,
        "globals" : this.globalValueProviders.getValueProvider("$Global").serializeForServer(),
        "test" : this.test
    });
};

/**
 * @param {Object} otherContext the context from the server to join in to this one.
 * @export
 */
Aura.Context.AuraContext.prototype.merge = function(otherContext) {
    var i, defs;

    if (otherContext["mode"] !== this.getMode()) {
        throw new Error("Mode mismatch");
    }
    if ($A.util.isUndefinedOrNull(this.fwuid)) {
        this.fwuid = otherContext["fwuid"];
    }
    if (otherContext["fwuid"] !== this.fwuid) {
        throw new Error("framework mismatch");
    }
    this.globalValueProviders.merge(otherContext["globalValueProviders"]);
    $A.localizationService.init();
    
    if(otherContext["libraryDefs"]) {
        defs = otherContext["libraryDefs"];
        for (i = 0; i < defs.length; i++) {
            $A.componentService.createLibraryDef(defs[i]);
        }
    }
    
    if (otherContext["componentDefs"]) {
        defs = otherContext["componentDefs"];
        for (i = 0; i < defs.length; i++) {
            // only create when component def is an object with descriptor key
            // there are occasions when defs are just references (descriptor name)
            if (defs[i]["descriptor"]) {
                $A.componentService.createDef(defs[i]);
            }
        }<commit_msg>Debugging with bad error messages sucks<commit_after>/*
 * Copyright (C) 2013 salesforce.com, inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*jslint sub: true*/
/**
 * @description Represents a Aura client-side context, created during HTTP requests for component definitions. A context
 *            can include a mode, such as "DEV" for development mode or "PROD" for production mode.
 * @constructor
 * @protected
 * @class AuraContext
 * @param {Object} config the 'founding' config for the context from the server.
 * @param {Function} initCallback an optional callback invoked after the config has finished its asynchronous initialization.
 * @export
 */
Aura.Context.AuraContext = function AuraContext(config, initCallback) {
    this.mode = config["mode"];
    this.loaded = config["loaded"];
    if (this.loaded === undefined) {
        this.loaded = {};
    }
    this.fwuid = config["fwuid"];
    this.num = 0;
    // To keep track of re-rendering service call
    this.renderNum = 0;
    this.transaction = 0;
    this.transactionName = "";
    this.lastGlobalId = 0;
    this.componentConfigs = {};
    this.app = config["app"];
    this.cmp = config["cmp"];
    this.test = config["test"];
    this.contextPath = config["contextPath"] || "";
    this.allowedGlobals = config["allowedGlobals"];
    this.globals = config["globals"];
    this.accessStack=[];

    var that = this;
    if(!config["globalValueProviders"]){
        config["globalValueProviders"]={};
    }
    $A.util.apply(config["globalValueProviders"],$A.globalValueProviders);

    this.globalValueProviders = new Aura.Provider.GlobalValueProviders(config["globalValueProviders"], function() {
        var i, defs;
        
        // Careful now, the def is null, this fake action sets up our paths.
        that.currentAction = new Action(null, ""+that.num, null, null, false, null, false);
        
        if(config["libraryDefs"]) {
            defs = config["libraryDefs"];
            for (i = 0; i < defs.length; i++) {
                $A.componentService.createLibraryDef(defs[i]);
            }
        }
        
        if (config["componentDefs"]) {
            defs = config["componentDefs"];
            for (i = 0; i < defs.length; i++) {
                if (defs[i]["descriptor"]) {
                    $A.componentService.saveComponentConfig(defs[i]);
                }
            }
        }
        if (config["eventDefs"]) {
            defs = config["eventDefs"];
            for (i = 0; i < defs.length; i++) {
                $A.eventService.createEventDef(defs[i]);
            }
        }
        that.joinComponentConfigs(config["components"], that.currentAction.getId());

        if (initCallback) {
            initCallback();
        }
    });
    this.contextGlobals = this.globalValueProviders.getValueProvider("Global");
};

/**
 * Returns the mode for the current request. Defaults to "PROD" for production mode and "DEV" for development mode.
 * The HTTP request format is <code>http://<your server>/namespace/component?aura.mode=PROD</code>.
 *
 * @return {string} the mode from the server.
 * @export
 */
Aura.Context.AuraContext.prototype.getMode = function() {
    return this.mode;
};

Aura.Context.AuraContext.prototype.getCurrentAccess=function(){
    var access=this.accessStack[this.accessStack.length-1];
    while(access instanceof PassthroughValue){
        access=access.getComponent();
    }
    return access;
};

Aura.Context.AuraContext.prototype.getCurrentAccessCaller=function(){
    var access=this.accessStack[this.accessStack.length-2];
    while(access instanceof PassthroughValue){
        access=access.getComponent();
    }
    return access;
};

Aura.Context.AuraContext.prototype.setCurrentAccess=function(component){
    return this.accessStack.push(component?component:this.getCurrentAccess());
};

Aura.Context.AuraContext.prototype.releaseCurrentAccess=function(){
    this.accessStack.pop();
};

Aura.Context.AuraContext.prototype.getAccessVersion = function(name) {
    var currentAccessCaller = this.getCurrentAccessCaller();
    if (currentAccessCaller) {
        return currentAccessCaller.getDef().getRequiredVersionDefs().getDef(name);
    }

    return null;
};

/**
 * Adds a new global value provider.
 * @param type The key to identify the valueProvider.
 * @param valueProvider The valueProvider to add.
 * @private
 */
Aura.Context.AuraContext.prototype.addGlobalValueProvider = function(type,valueProvider) {
    this.globalValueProviders.addValueProvider(type,valueProvider);
};

/**
 * Provides access to global value providers.
 * For example, <code>$A.get("$Label.Related_Lists.task_mode_today");</code> gets the label value.
 * 
 * @return {GlobalValueProviders}
 * @private
 */
Aura.Context.AuraContext.prototype.getGlobalValueProvider = function(type) {
    return this.globalValueProviders.getValueProvider(type);
};

/**
 * JSON representation of context for server requests.
 *
 * This must remain in sync with AuraTestingUtil so that we can accurately test.
 * 
 * @return {String} json representation
 * @private
 */
Aura.Context.AuraContext.prototype.encodeForServer = function() {
    return aura.util.json.encode({
        "mode" : this.mode,
        "loaded" : this.loaded,
        "dn" : $A.services.component.getDynamicNamespaces(),
        "app" : this.app,
        "cmp" : this.cmp,
        "fwuid" : this.fwuid,
        "globals" : this.globalValueProviders.getValueProvider("$Global").serializeForServer(),
        "test" : this.test
    });
};

/**
 * @param {Object} otherContext the context from the server to join in to this one.
 * @export
 */
Aura.Context.AuraContext.prototype.merge = function(otherContext) {
    var i, defs;

    if (otherContext["mode"] !== this.getMode()) {
        throw new Error("[Mode mismatch] Expected '" + this.getMode() + "' instead tried to merge mode '" + otherContext["mode"] + "'");
    }
    if ($A.util.isUndefinedOrNull(this.fwuid)) {
        this.fwuid = otherContext["fwuid"];
    }
    if (otherContext["fwuid"] !== this.fwuid) {
        throw new Error("framework mismatch");
    }
    this.globalValueProviders.merge(otherContext["globalValueProviders"]);
    $A.localizationService.init();
    
    if(otherContext["libraryDefs"]) {
        defs = otherContext["libraryDefs"];
        for (i = 0; i < defs.length; i++) {
            $A.componentService.createLibraryDef(defs[i]);
        }
    }
    
    if (otherContext["componentDefs"]) {
        defs = otherContext["componentDefs"];
        for (i = 0; i < defs.length; i++) {
            // only create when component def is an object with descriptor key
            // there are occasions when defs are just references (descriptor name)
            if (defs[i]["descriptor"]) {
                $A.componentService.createDef(defs[i]);
            }
        }
{'fraction_non_alphanumeric': 0.12806837606837607, 'fraction_numerical': 0.002598290598290598, 'mean_word_length': 3.0414479137883395, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6610834', 'n_tokens_mistral': 4209, 'n_tokens_neox': 4144, 'n_words': 1039}