Подтвердить что ты не робот

Могу ли я переопределить объект функции Javascript для регистрации всех вызовов функций?

Могу ли я переопределить поведение объекта Function, чтобы я мог вводить поведение до каждого вызова функции, а затем продолжать как обычно? В частности, (хотя общая идея сама по себе интригует), я могу войти в консоль каждый вызов функции без необходимости вставлять инструкции console.log повсюду? И тогда нормальное поведение продолжается?

Я действительно признаю, что это, вероятно, будет иметь серьезные проблемы с производительностью; Я не собираюсь запускать этот запуск, даже в моей среде разработки. Но если это работает, кажется, это элегантное решение получить 1000-метровый вид на текущий код. И я подозреваю, что ответ покажет мне что-то более глубокое о javascript.

4b9b3361

Ответ 1

Очевидным ответом является следующее:

var origCall = Function.prototype.call;
Function.prototype.call = function (thisArg) {
    console.log("calling a function");

    var args = Array.prototype.slice.call(arguments, 1);
    origCall.apply(thisArg, args);
};

Но это фактически немедленно входит в бесконечный цикл, потому что сам акт вызова console.log выполняет вызов функции, который вызывает console.log, который выполняет вызов функции, который вызывает console.log, который...

Точка, я не уверен, что это возможно.

Ответ 2

Перехват вызовов функций

Многие здесь пытались переопределить .call. Некоторые из них потерпели неудачу, некоторые преуспели. Я отвечаю на этот старый вопрос, поскольку он был поднят на моем рабочем месте, причем этот пост используется в качестве ссылки.

Для нас доступны только две функции, связанные с функциями: мы можем изменить:.call и .apply. Я продемонстрирую успешное переопределение обоих.

TL; DR: то, что спрашивает OP, невозможно. Некоторые из отчетов об успехах в ответах объясняются тем, что консоль вызывает .call внутри непосредственно перед оценкой, а не из-за вызова, который мы хотим перехватить.

Переопределение функции .prototype.call

Это, кажется, первая идея, которую люди придумали. Некоторые из них были более успешными, чем другие, но вот реализация, которая работает:

// Store the original
var origCall = Function.prototype.call;
Function.prototype.call = function () {
    // If console.log is allowed to stringify by itself, it will
    // call .call 9 gajillion times. Therefore, lets do it by ourselves.
    console.log("Calling",
                Function.prototype.toString.apply(this, []),
                "with:",
                Array.prototype.slice.apply(arguments, [1]).toString()
               );

    // A trace, for fun
   console.trace.apply(console, []);

   // The call. Apply is the only way we can pass all arguments, so don't touch that!
   origCall.apply(this, arguments);
};

Это успешно перехватывает функцию .prototype.call

Возьмем его для спина, будем ли мы?

// Some tests
console.log("1"); // Does not show up
console.log.apply(console,["2"]); // Does not show up
console.log.call(console, "3"); // BINGO!

Важно, чтобы это не запускалось с консоли. В разных браузерах есть всевозможные консольные инструменты, которые очень часто вызывают .call, в том числе один раз для каждого входа, который может напугать пользователя в данный момент. Другая ошибка - просто аргументы console.log, которые проходят через консоль api для строкования, что, в свою очередь, вызывает бесконечный цикл.

Переопределение функции .prototype.apply также

Ну, а как же тогда применить? Они - единственные магические функции вызова, которые у нас есть, поэтому давайте попробуем это. Здесь идет версия, которая ловит оба:

// Store apply and call
var origApply = Function.prototype.apply;
var origCall = Function.prototype.call;

// We need to be able to apply the original functions, so we need
// to restore the apply locally on both, including the apply itself.
origApply.apply = origApply;
origCall.apply = origApply;

// Some utility functions we want to work
Function.prototype.toString.apply = origApply;
Array.prototype.slice.apply = origApply;
console.trace.apply = origApply;

function logCall(t, a) {
    // If console.log is allowed to stringify by itself, it will
    // call .call 9 gajillion times. Therefore, do it ourselves.
    console.log("Calling",
                Function.prototype.toString.apply(t, []),
                "with:",
                Array.prototype.slice.apply(a, [1]).toString()
               );
    console.trace.apply(console, []);
}

Function.prototype.call = function () {
   logCall(this, arguments);
   origCall.apply(this, arguments);
};

Function.prototype.apply = function () {
    logCall(this, arguments);
    origApply.apply(this, arguments);
}

... И попробуем попробовать!

// Some tests
console.log("1"); // Passes by unseen
console.log.apply(console,["2"]); // Caught
console.log.call(console, "3"); // Caught

Как вы можете видеть, вызывающая скобка остается незамеченной.

Заключение

К счастью, вызывающие скобки не могут быть перехвачены с помощью JavaScript. Но даже если .call перехватит оператор скобки на объектах функции, как бы мы могли называть оригинал, не вызывая бесконечного цикла?

Единственное, что переопределяет .call/.apply, это перехват явных вызовов этих прототипов. Если консоль используется с этим взломом на месте, будет много и много спама. Кроме того, необходимо быть очень осторожным, если он используется, поскольку использование API-интерфейса консоли может быстро вызвать бесконечный цикл (console.log будет использовать .call внутри, если он дает ему нестроку).

Ответ 3

Я получаю НЕКОТОРЫЕ результаты и никаких сбоев страницы со следующим:

(function () {
  var 
    origCall = Function.prototype.call,
    log = document.getElementById ('call_log');  

  // Override call only if call_log element is present    
  log && (Function.prototype.call = function (self) {
    var r = (typeof self === 'string' ? '"' + self + '"' : self) + '.' + this + ' ('; 
    for (var i = 1; i < arguments.length; i++) r += (i > 1 ? ', ' : '') + arguments[i];  
    log.innerHTML += r + ')<br/>';



    this.apply (self, Array.prototype.slice.apply (arguments, [1]));
  });
}) ();

Проверено только в версии Chrome версии 9.xxx.

Это, конечно, не протоколирование всех вызовов функций, но это регистрация некоторых! Я подозреваю, что обрабатываются только фактические вызовы 'call' int

Ответ 4

Только быстрый тест, но он работает для меня. Возможно, это не так полезно, но я в основном восстанавливаю прототип, пока в моем заменяющем теле, а затем "невосстанавливаю" его перед выходом.

Этот пример просто регистрирует все вызовы функций, хотя может быть некоторый фатальный недостаток, который я еще не обнаружил; делая это за кофе-брейком

реализация

callLog = [];

/* set up an override for the Function call prototype
 * @param func the new function wrapper
 */
function registerOverride(func) {
   oldCall = Function.prototype.call;
   Function.prototype.call = func;
}

/* restore you to your regular programming 
 */
function removeOverride() {
   Function.prototype.call = oldCall;
}

/* a simple example override
 * nb: if you use this from the node.js REPL you'll get a lot of buffer spam
 *     as every keypress is processed through a function
 * Any useful logging would ideally compact these calls
 */
function myCall() { 
   // first restore the normal call functionality
   Function.prototype.call = oldCall;

   // gather the data we wish to log
   var entry = {this:this, name:this.name, args:{}};
   for (var key in arguments) {
     if (arguments.hasOwnProperty(key)) {
      entry.args[key] = arguments[key];
     }
   }
   callLog.push(entry);

   // call the original (I may be doing this part naughtily, not a js guru)
   this(arguments);

   // put our override back in power
   Function.prototype.call = myCall;
}

Использование

У меня были некоторые проблемы, включая призывы к этому в одной большой паре, поэтому вот что я набрал в REPL, чтобы протестировать вышеперечисленные функции:

/* example usage
 * (only tested through the node.js REPL)
 */
registerOverride(myCall);
console.log("hello, world!");
removeOverride(myCall);
console.log(callLog);

Ответ 5

Вы можете переопределить Function.prototype.call, просто убедитесь, что в вашем переопределении есть только функции apply.

window.callLog = [];
Function.prototype.call = function() {
    Array.prototype.push.apply(window.callLog, [[this, arguments]]);
    return this.apply(arguments[0], Array.prototype.slice.apply(arguments,[1]));
};

Ответ 6

Мне было проще всего обрабатывать файл, используя автоматический процесс. Я построил этот маленький инструмент, чтобы сделать его проще для себя. Возможно, кто-то другой найдет это полезным. Это в основном awk, но проще для использования Javascript-программистом.

// This tool reads a file and builds a buffer of say ten lines.  
// When a line falls off the end of the buffer, it gets written to the output file. 
// When a line is read from the input file, it gets written to the first line of the buffer. 
// After each occurrence of a line being read from the input file and/or written to the output 
// file, a routine is given control.  The routine has the option of operating on the buffer.  
// It can insert a line before or after a line that is there, based on the lines surrounding. 
// 
// The immediate case is that if I have a set of lines like this: 
// 
//             getNum: function (a, c) {
//                 console.log(`getNum: function (a, c) {`);
//                 console.log(`arguments.callee = ${arguments.callee.toString().substr(0,100)}`);
//                 console.log(`arguments.length = ${arguments.length}`);
//                 for (var i = 0; i < arguments.length; i++) { console.log(`arguments[${i}] = ${arguments[i] ? arguments[i].toString().substr(0,100) : 'falsey'}`); }
//                 var d = b.isStrNum(a) ? (c && b.isString(c) ? RegExp(c) : b.getNumRegx).exec(a) : null;
//                 return d ? d[0] : null
//             },
//             compareNums: function (a, c, d) {
//                 console.log(`arguments.callee = ${arguments.callee.toString().substr(0,100)}`);
// 
// I want to change that to a set of lines like this: 
// 
//             getNum: function (a, c) {
//                 console.log(`getNum: function (a, c) {`);
//                 console.log(`arguments.callee = ${arguments.callee.toString().substr(0,100)}`);
//                 console.log(`arguments.length = ${arguments.length}`);
//                 for (var i = 0; i < arguments.length; i++) { console.log(`arguments[${i}] = ${arguments[i] ? arguments[i].toString().substr(0,100) : 'falsey'}`); }
//                 var d = b.isStrNum(a) ? (c && b.isString(c) ? RegExp(c) : b.getNumRegx).exec(a) : null;
//                 return d ? d[0] : null
//             },
//             compareNums: function (a, c, d) {
//                 console.log(`compareNums: function (a, c, d) {`);
//                 console.log(`arguments.callee = ${arguments.callee.toString().substr(0,100)}`);
// 
// We are trying to figure out how a set of functions work, and I want each function to report 
// its name when we enter it.
// 
// To save time, options and the function that is called on each cycle appear at the beginning 
// of this file.  Ideally, they would be --something options on the command line. 


const readline = require('readline');


//------------------------------------------------------------------------------------------------

// Here are the things that would properly be options on the command line.  Put here for 
// speed of building the tool. 

const frameSize = 10;
const shouldReportFrame = false;

function reportFrame() {
    for (i = frame.length - 1; i >= 0; i--) {
        console.error(`${i}.  ${frame[i]}`);  // Using the error stream because the stdout stream may have been coopted. 
    }
}

function processFrame() {
    // console.log(`********  ${frame[0]}`);
    // if (frame[0].search('console.log(\`arguments.callee = \$\{arguments.callee.toString().substr(0,100)\}\`);') !== -1) {
    // if (frame[0].search('arguments.callee') !== -1) {
    // if (frame[0].search(/console.log\(`arguments.callee = \$\{arguments.callee.toString\(\).substr\(0,100\)\}`\);/) !== -1) {
    var matchArray = frame[0].match(/([ \t]*)console.log\(`arguments.callee = \$\{arguments.callee.toString\(\).substr\(0,100\)\}`\);/);
    if (matchArray) {
        // console.log('********  Matched');
        frame.splice(1, 0, `${matchArray[1]}console.log('${frame[1]}');`);
    }
}

//------------------------------------------------------------------------------------------------


var i;
var frame = [];

const rl = readline.createInterface({
    input: process.stdin
});

rl.on('line', line => {
    if (frame.length > frameSize - 1) {
        for (i = frame.length - 1; i > frameSize - 2; i--) {
            process.stdout.write(`${frame[i]}\n`);
        }
    }
    frame.splice(frameSize - 1, frame.length - frameSize + 1);
    frame.splice(0, 0, line);
    if (shouldReportFrame) reportFrame();
    processFrame();
    // process.stdout.write(`${line}\n`);  // readline gives us the line with the newline stripped off
});

rl.on('close', () => {
    for (i = frame.length - 1; i > -1; i--) {
        process.stdout.write(`${frame[i]}\n`);
    }
});


// Notes
// 
// We are not going to control the writing to the output stream.  In particular, we are not 
// going to listen for drain events.  Nodejs' buffering may get overwhelmed. 
//