Last active
December 9, 2015 16:38
-
-
Save think49/bfca562039aa29fa5766 to your computer and use it in GitHub Desktop.
debug-code.js: コードを実行し、評価値or例外エラーをプログラムを強制終了せずに返す
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* debug-code.js | |
* | |
* @version 1.0.3 | |
* @author think49 | |
* @url https://gist.github.com/think49/bfca562039aa29fa5766 | |
* @license http://www.opensource.org/licenses/mit-license.php (The MIT License) | |
*/ | |
'use strict'; | |
var DebugCode = (function (Object, keys, defineProperty, String) { | |
/** | |
* convertNoPrototypeObject | |
* | |
* @function | |
* @param {Object} object | |
* @returns no prototype object. | |
* @type Object | |
*/ | |
var convertNoPrototypeObject = (function (create, keys) { | |
function convertNoPrototypeObject (object) { | |
var newObject = create(null), | |
properties, property, i, l; | |
object = Object(object); | |
properties = keys(object); | |
i = 0; | |
l = properties.length; | |
while (i < l) { | |
property = properties[i++]; | |
newObject[property] = object[property]; | |
} | |
return newObject; | |
} | |
return convertNoPrototypeObject; | |
}(Object.create, Object.keys)); | |
/** | |
* DebugCode | |
* | |
* @constructor | |
* @param {Object} [valiables] object map of valiables. | |
* @type Object | |
*/ | |
function DebugCode (/* [valiables] */) { | |
defineProperty(this, 'valiables', { | |
writable: true, | |
enumerable: false, | |
configurable: false, | |
value: convertNoPrototypeObject(arguments[0]) | |
}); | |
} | |
return DebugCode; | |
}(Object, Object.keys, Object.defineProperty, String)); | |
(function (keys, String, console) { | |
/** | |
* Object.values() (ES7 stage3) | |
* @function | |
* @param {Object} object | |
* @type Object | |
*/ | |
var getObjectValues = (function (keys, mapfn) { | |
return function getObjectValues (object) { | |
return keys(object).map(mapfn, object); | |
}; | |
}(Object.keys, function mapfn (key) { | |
return this[key]; | |
})); | |
Object.defineProperties(this, { | |
console: { | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
value: Object.create(null, { | |
options: { // default parameter (for console.log, console.error, console.logAll) | |
writable: true, | |
enumerable: false, | |
configurable: false, | |
value: Object.create(null, { | |
useStack: { // use error.stack of default parameter (for console.log, console.error, console.logAll) | |
writable: true, | |
enumerable: false, | |
configurable: false, | |
value: false | |
} | |
}) | |
}, | |
pastLog: { | |
writable: true, | |
enumerable: false, | |
configurable: false, | |
value: [] | |
}, | |
log: { | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
/** | |
* DebugCode.prototype.console.log() | |
* @function | |
* @param {String} stringCode | |
* @param {String} message | |
* @param {Boolean} [useStack] use error.stack. default parameter is console.options.useStack. (true = use / false = unuse) | |
*/ | |
value: function log (stringCode, message /* [, useStack] */) { | |
var currentLog = {type: 'log', code: stringCode, value: message}, | |
useStack = arguments.length > 2 ? !!arguments[2] : this.options.useStack, | |
error; | |
if (typeof Error.captureStackTrace === 'function') { // JavaScript Stack Trace API (for Google Chrome only) | |
error = {name: 'CustomError'}; | |
Error.captureStackTrace(error, log); | |
currentLog.stack = error.stack; | |
} | |
this.pastLog.push(currentLog); | |
return useStack && error ? console.log(message, error.stack) : console.log(message); | |
} | |
}, | |
error: { | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
/** | |
* DebugCode.prototype.console.error() | |
* @function | |
* @param {String} stringCode | |
* @param {String} error | |
* @param {Boolean} [useStack] use error.stack. default parameter is console.options.useStack. (true = use / false = unuse) | |
*/ | |
value: function error (stringCode, error /* [, useStack] */) { | |
var useStack = arguments.length > 2 ? !!arguments[2] : this.options.useStack; | |
this.pastLog.push({type: 'error', code: stringCode, value: error, stack: error.stack}); | |
return console.error(useStack ? error.stack : error); | |
} | |
}, | |
logAll: { | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
/** | |
* DebugCode.prototype.console.logAll() | |
* @function | |
* @param {Boolean} [useStack] use error.stack. default parameter is console.options.useStack. (true = use / false = unuse) | |
*/ | |
value: function logAll (/* [useStack] */) { | |
var pastLog = this.pastLog, | |
i = 0, | |
l = pastLog.length, | |
messages = [], | |
useStack = arguments.length > 0 ? !!arguments[0] : this.options.useStack, | |
key = useStack ? 'stack' : 'value', | |
value, log, stringCode; | |
while (i < l) { | |
log = pastLog[i++]; | |
stringCode = log.code; | |
if (stringCode[stringCode.length - 1] !== ';') { | |
stringCode += ';'; | |
} | |
value = log[key]; | |
if (!useStack && log.type === 'log' && typeof value === 'string') { | |
value = '"' + value.replace(/"/g, '\\"') + '"'; | |
} | |
messages.push(stringCode + ' // ' + value); | |
} | |
return console.log(messages.join('\r\n')); | |
} | |
} | |
}) | |
}, | |
test: { | |
writable: true, | |
enumerable: false, | |
configurable: true, | |
/** | |
* DebugCode.prototype.test() | |
* @function | |
* @param {String} code | |
* @param {Boolean} [useStack] use error.stack. default parameter is console.options.useStack. (true = use / false = unuse) | |
*/ | |
value: function test (code /* [, useStack] */) { | |
var valiables = this.valiables, | |
args = keys(valiables), | |
useStack = arguments.length > 1 ? !!arguments[1] : this.console.options.useStack, | |
stringCode; | |
code = String(code); | |
stringCode = '\x27' + code.replace(/\x27/g, '\\x27') + '\x27'; | |
args.push('try {this.console.log(' + stringCode + ',' + code + ',' + useStack + ');} catch (error) { this.console.error(' + stringCode + ',error,' + useStack + '); }'); | |
return Function.apply(null, args).apply(this, getObjectValues(valiables)); | |
} | |
} | |
}); | |
}.call(DebugCode.prototype, Object.keys, String, console)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考リンク
V8専用 JavaScript Stack Trace API (参考: Stack trace collection for custom exceptions)。