<commit_before>// BooJs Runtime - Debugging extensions
// MIT license
// Copyright 2012-2013 Iván -DrSlump- Montes <<EMAIL>>

/*jshint indent:4, lastsemic:false, curly:false */
/*global Boo:true */

(function (exports, undefined) {
    var sourcemaps = [];

    // Override the sourcemap registry implementation
    Boo.sourcemap = function (srcmap) {
        sourcemaps.push(srcmap);
    };

    var _define = Boo.define;
    Boo.define = function () {
        // HACK: Delay triggering to allow the sourcemap to be registered
        var args = arguments;
        setTimeout(function () { run_and_process_exception(_define, args); }, 0);
    };

    var _require = Boo.require;
    Boo.require = function () {
        // HACK: Delay triggering to allow the sourcemap to be registered
        var args = arguments;
        setTimeout(function () { run_and_process_exception(_require, args); }, 0);
    };

    function norm(file) {
        return file.toLowerCase().replace('\\', '/');
    }

    function run_and_process_exception(fn, args) {
        try {
            fn.apply(Boo, args);
        } catch (e) {
            console.log(e.stack);
            /*
            NOTE: Check for format examples: https://github.com/eriwen/javascript-stacktrace/blob/master/test/CapturedExceptions.js

            Opera 12:
                <anonymous function>([arguments not available])@http://localhost:63342/javascript-stacktrace/test/functional/ExceptionLab.js:4
                createException([arguments not available])@http://localhost:63342/javascript-stacktrace/test/functional/ExceptionLab.js:2

            Firefox 22:
                @file:///E:/javascript-stacktrace/test/functional/ExceptionLab.js:4
                createException@file:///E:/javascript-stacktrace/test/functional/ExceptionLab.js:8
        
            Safari 6:
                @file:///Users/eric/src/javascript-stacktrace/test/functional/ExceptionLab.html:48
                onclick@file:///Users/eric/src/javascript-stacktrace/test/functional/ExceptionLab.html:82
                [native code]

            Chrome 27:
                TypeError: Cannot call method 'undef' of null
                    at file:///E:/javascript-stacktrace/test/functional/ExceptionLab.js:4:9
                    at createException (file:///E:/javascript-stacktrace/test/functional/ExceptionLab.js:8:5)
                    at HTMLButtonElement.onclick (file:///E:/javascript-stacktrace/test/functional/ExceptionLab.html:83:126)

            Chrome 30:
                ReferenceError: value_ is not defined
                    at file://localhost/Users/drslump/www/boojs/hash-3.js:36:26
                    at Object.boo_each [as each] (file://localhost/Users/drslump/www/boojs/src/Boo.js:203:47)
                    at Object.Boo.require (file://localhost/Users/drslump/www/boojs/src/Boo.js:180:18)
                    at run_and_process_exception (file://localhost/Users/drslump/www/boojs/src/Boo.debug.js:32:16)

            Internet Explorer 10:
                TypeError: Unable to get property 'undef' of undefined or null reference
                   at Anonymous function (http://jenkins.eriwen.com/job/stacktrace.js/ws/test/functional/ExceptionLab.html:48:13)
                   at onclick (http://jenkins.eriwen.com/job/stacktrace.js/ws/test/functional/ExceptionLab.html:82:1)       
            */
            if (e.stack) {
                var stack = [], frames = e.stack.split(/\n/);
                
                for (var i = 0; i < frames.length; i++) {
                    var ident, file, line, column, m,
                        frame = frames[i];

                    // Ignore runtime files
                    if (/\/Boo(\.\w+)?\.js\b/.test(frame)) {
                        continue;
                    }

                    // Chrome: 'at func (file:///path/to/file.js:4:9)'
                    if (m = frame.match(/^\s+at\s+([^\s]+)\s+\((.+?):(\d+):(\d+)\)$/)) {
                        ident = m[1].replace('Object.<anonymous>', '<anonymous>').replace('Object.', '');
                        file = m[2];
                        line = parseInt(m[3], 10) - 1;
                        column = parseInt(m[4], 10) - 1;
                    // Firefox, Opera, Safari: 'func@file:///path/to/file.js:4'
                    } else if (m = frame.match(/^\s+(\w+)?@(.+?):(\d+)([:,](\d+))?$/)) {
                        ident = m[1] || '<anonymous>';
                        file = m[2];
                        line = parseInt(m[3], 10) - 1;
                        column = m[4] ? parseInt(m[5], 10) - 1 : -1;
                    // Unrecognized
                    } else if (m = frame.match(/((?:\w+:\/\/)?[\w\d\.\/\\_=\+-]+):(\d+)([:,](\d+))?/)) {
                        ident = '<anonymous>';
                        file = m[1];
                        line = parseInt(m[2], 10) - 1;
                        column = m[3] ? parseInt(m[4], 10) - 1 : -1;
                    // Unfortunately nothing useful was found
                    } else {
                        // If it's indented it looks like a frame so perhaps we may be interested in it
                        if (/^\s+/.test(frame)) {
                            stack.push(frame.replace(/^\s+/, ''));
                        }
                        continue;
                    }

                    var srcmap;
                    for (var j = 0; j < sourcemaps.length; j++) {
                        if (-1 !== norm(file).indexOf(norm(sourcemaps[j].file))) {
                            srcmap = SrcMap(sourcemaps[j]);
                            break;
                        }
                    }

                    if (!srcmap) {
                        continue;
                    }

                    // TODO: don't output column if it's -1 in the Javascript stack
                    var found = srcmap.find(line, column);
                    if (found) {
                        file = found.source;
                        line = found.line;
                        column = column > -1 ? found.column : -1;
                    }
                    stack.push('at ' + (ident ? ident + ' ' : '') + '(' + file + ':' + (line + 1) + ':' + (column + 1) + ')');
                }

                var msg = e.name + ': ' + e.message + '\n   ' + stack.join('\n   ');
                if ('warn' in console) {
                    console.warn(msg);
                } else if ('log' in console) {
                    console.log(msg);
                } else {
                    // If the console is not available replace the exception message
                    e.message = msg;
                }
            }

            throw e;
        }
    }

    // Reduced implementation of a source map lookup method
    function SrcMap(srcmap) {

        // Consumes a VLQ encoded segment. It must be called sequentially from the first
        // segment in the map.
        function consume(segment, prev) {
            var values = [],
                pair = [null, segment];
            
            while (pair[1].length) {
                pair = vlq(pair[1]);
                values.push(pair[0]);<commit_msg>Allow to mock the console object used in boo.debug<commit_after>// BooJs Runtime - Debugging extensions
// MIT license
// Copyright 2012-2013 Iván -DrSlump- Montes <<EMAIL>>

/*jshint indent:4, lastsemic:false, curly:false */
/*global Boo:true */

(function (exports, undefined) {
    var sourcemaps = [];

    // Allow to override the console with a mock
    Boo.console = console;

    // Override the sourcemap registry implementation
    Boo.sourcemap = function (srcmap) {
        sourcemaps.push(srcmap);
    };

    var _define = Boo.define;
    Boo.define = function () {
        // HACK: Delay triggering to allow the sourcemap to be registered
        var args = arguments;
        setTimeout(function () { run_and_process_exception(_define, args); }, 0);
    };

    var _require = Boo.require;
    Boo.require = function () {
        // HACK: Delay triggering to allow the sourcemap to be registered
        var args = arguments;
        setTimeout(function () { run_and_process_exception(_require, args); }, 0);
    };

    function norm(file) {
        return file.toLowerCase().replace('\\', '/');
    }

    function run_and_process_exception(fn, args) {
        try {
            fn.apply(Boo, args);
        } catch (e) {
            /*
            NOTE: Check for format examples: https://github.com/eriwen/javascript-stacktrace/blob/master/test/CapturedExceptions.js

            Opera 12:
                <anonymous function>([arguments not available])@http://localhost:63342/javascript-stacktrace/test/functional/ExceptionLab.js:4
                createException([arguments not available])@http://localhost:63342/javascript-stacktrace/test/functional/ExceptionLab.js:2

            Firefox 22:
                @file:///E:/javascript-stacktrace/test/functional/ExceptionLab.js:4
                createException@file:///E:/javascript-stacktrace/test/functional/ExceptionLab.js:8
        
            Safari 6:
                @file:///Users/eric/src/javascript-stacktrace/test/functional/ExceptionLab.html:48
                onclick@file:///Users/eric/src/javascript-stacktrace/test/functional/ExceptionLab.html:82
                [native code]

            Chrome 27:
                TypeError: Cannot call method 'undef' of null
                    at file:///E:/javascript-stacktrace/test/functional/ExceptionLab.js:4:9
                    at createException (file:///E:/javascript-stacktrace/test/functional/ExceptionLab.js:8:5)
                    at HTMLButtonElement.onclick (file:///E:/javascript-stacktrace/test/functional/ExceptionLab.html:83:126)

            Chrome 30:
                ReferenceError: value_ is not defined
                    at file://localhost/Users/drslump/www/boojs/hash-3.js:36:26
                    at Object.boo_each [as each] (file://localhost/Users/drslump/www/boojs/src/Boo.js:203:47)
                    at Object.Boo.require (file://localhost/Users/drslump/www/boojs/src/Boo.js:180:18)
                    at run_and_process_exception (file://localhost/Users/drslump/www/boojs/src/Boo.debug.js:32:16)

            Internet Explorer 10:
                TypeError: Unable to get property 'undef' of undefined or null reference
                   at Anonymous function (http://jenkins.eriwen.com/job/stacktrace.js/ws/test/functional/ExceptionLab.html:48:13)
                   at onclick (http://jenkins.eriwen.com/job/stacktrace.js/ws/test/functional/ExceptionLab.html:82:1)       
            */
            if (e.stack) {
                var stack = [], frames = e.stack.split(/\n/);
                
                for (var i = 0; i < frames.length; i++) {
                    var ident, file, line, column, m,
                        frame = frames[i];

                    // Ignore runtime files
                    if (/\/Boo(\.\w+)?\.js\b/.test(frame)) {
                        continue;
                    }

                    // Chrome: 'at func (file:///path/to/file.js:4:9)'
                    if (m = frame.match(/^\s+at\s+([^\s]+)\s+\((.+?):(\d+):(\d+)\)$/)) {
                        ident = m[1].replace('Object.<anonymous>', '<anonymous>').replace('Object.', '');
                        file = m[2];
                        line = parseInt(m[3], 10) - 1;
                        column = parseInt(m[4], 10) - 1;
                    // Firefox, Opera, Safari: 'func@file:///path/to/file.js:4'
                    } else if (m = frame.match(/^\s+(\w+)?@(.+?):(\d+)([:,](\d+))?$/)) {
                        ident = m[1] || '<anonymous>';
                        file = m[2];
                        line = parseInt(m[3], 10) - 1;
                        column = m[4] ? parseInt(m[5], 10) - 1 : -1;
                    // Unrecognized
                    } else if (m = frame.match(/((?:\w+:\/\/)?[\w\d\.\/\\_=\+-]+):(\d+)([:,](\d+))?/)) {
                        ident = '<anonymous>';
                        file = m[1];
                        line = parseInt(m[2], 10) - 1;
                        column = m[3] ? parseInt(m[4], 10) - 1 : -1;
                    // Unfortunately nothing useful was found
                    } else {
                        // If it's indented it looks like a frame so perhaps we may be interested in it
                        if (/^\s+/.test(frame)) {
                            stack.push(frame.replace(/^\s+/, ''));
                        }
                        continue;
                    }

                    var srcmap;
                    for (var j = 0; j < sourcemaps.length; j++) {
                        if (-1 !== norm(file).indexOf(norm(sourcemaps[j].file))) {
                            srcmap = SrcMap(sourcemaps[j]);
                            break;
                        }
                    }

                    if (!srcmap) {
                        continue;
                    }

                    // TODO: don't output column if it's -1 in the Javascript stack
                    var found = srcmap.find(line, column);
                    if (found) {
                        file = found.source;
                        line = found.line;
                        column = column > -1 ? found.column : -1;
                    }
                    stack.push('at ' + (ident ? ident + ' ' : '') + '(' + file + ':' + (line + 1) + ':' + (column + 1) + ')');
                }

                var msg = e.name + ': ' + e.message + '\n   ' + stack.join('\n   ');
                if ('warn' in Boo.console) {
                    Boo.console.warn(msg);
                } else if ('log' in Boo.console) {
                    Boo.console.log(msg);
                } else {
                    // If the console is not available replace the exception message
                    e.message = msg;
                }
            }

            throw e;
        }
    }

    // Reduced implementation of a source map lookup method
    function SrcMap(srcmap) {

        // Consumes a VLQ encoded segment. It must be called sequentially from the first
        // segment in the map.
        function consume(segment, prev) {
            var values = [],
                pair = [null, segment];
            
            while (pair[1].length) {
                pair = vlq(pair[1]);
                values.push(pair[0]);
{'fraction_non_alphanumeric': 0.13782361899487747, 'fraction_numerical': 0.01689048871659975, 'mean_word_length': 1.3494877215807448, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4392820', 'n_tokens_mistral': 4271, 'n_tokens_neox': 3868, 'n_words': 842}