src/js/instrument/esnstrument.js
function instrumentCode(options) {
        var aret, skip = false;
        var isEval = options.isEval,
            code = options.code, thisIid = options.thisIid, inlineSource = options.inlineSource, url = options.url;

        iidSourceInfo = {};
        initializeIIDCounters(isEval);
        instCodeFileName = options.instCodeFileName ? options.instCodeFileName : (options.isDirect?"eval":"evalIndirect");
        origCodeFileName = options.origCodeFileName ? options.origCodeFileName : (options.isDirect?"eval":"evalIndirect");


        if (sandbox.analysis && sandbox.analysis.instrumentCodePre) {
            aret = sandbox.analysis.instrumentCodePre(thisIid, code, options.isDirect);
            if (aret) {
                code = aret.code;
                skip = aret.skip;
            }
        }

        if (!skip && typeof code === 'string' && code.indexOf(noInstr) < 0) {
            try {
                code = removeShebang(code);
                iidSourceInfo = {};
                var newAst;
                if (Config.ENABLE_SAMPLING) {
                    newAst = transformString(code, [visitorCloneBodyPre, visitorRRPost, visitorOps, visitorMergeBodyPre], [undefined, visitorRRPre, undefined, undefined]);
                } else {
                    newAst = transformString(code, [visitorRRPost, visitorOps], [visitorRRPre, undefined]);
                }
                // post-process AST to hoist function declarations (required for Firefox)
                var hoistedFcts = [];
                newAst = hoistFunctionDeclaration(newAst, hoistedFcts);
                var newCode = esotope.generate(newAst, {comment: true ,parse: acorn.parse});
                code = newCode + "\n" + noInstr + "\n";
            } catch(ex) {
                console.log("Failed to instrument", code);
                throw ex;
            }
        }

        var tmp = {};

        tmp.nBranches = iidSourceInfo.nBranches = (condIid / IID_INC_STEP - 1) * 2;
        tmp.originalCodeFileName = iidSourceInfo.originalCodeFileName = origCodeFileName;
        tmp.instrumentedCodeFileName = iidSourceInfo.instrumentedCodeFileName = instCodeFileName;
        if (url) {
            tmp.url = iidSourceInfo.url = url;
        }
        if (isEval) {
            tmp.evalSid = iidSourceInfo.evalSid = sandbox.sid;
            tmp.evalIid = iidSourceInfo.evalIid = thisIid;
        }
        if (inlineSource) {
            tmp.code = iidSourceInfo.code = options.code;
        }

        var prepend = JSON.stringify(iidSourceInfo);
        var instCode;
        if (options.inlineSourceMap) {
            instCode = JALANGI_VAR + ".iids = " + prepend + ";\n" + code;
        } else {
            instCode = JALANGI_VAR + ".iids = " + JSON.stringify(tmp) + ";\n" + code;
        }

        if (isEval && sandbox.analysis && sandbox.analysis.instrumentCode) {
            aret = sandbox.analysis.instrumentCode(thisIid, instCode, newAst, options.isDirect);
            if (aret) {
                instCode = aret.result;
            }
        }

        return {code: instCode, instAST: newAst, sourceMapObject: iidSourceInfo, sourceMapString: prepend};

    }
function instrumentCode(options) {
        var aret, skip = false;
        var isEval = options.isEval,
            code = options.code, thisIid = options.thisIid, inlineSource = options.inlineSource, url = options.url;

        iidSourceInfo = {};
        initializeIIDCounters(isEval);
        instCodeFileName = options.instCodeFileName ? options.instCodeFileName : (options.isDirect?"eval":"evalIndirect");
        origCodeFileName = options.origCodeFileName ? options.origCodeFileName : (options.isDirect?"eval":"evalIndirect");


        if (sandbox.analysis && sandbox.analysis.instrumentCodePre) {
            aret = sandbox.analysis.instrumentCodePre(thisIid, code, options.isDirect);
            if (aret) {
                code = aret.code;
                skip = aret.skip;
            }
        }

        if (!skip && typeof code === 'string' && code.indexOf(noInstr) < 0) {
            try {
                code = removeShebang(code);
                iidSourceInfo = {};
                var newAst;
                if (Config.ENABLE_SAMPLING) {
                    newAst = transformString(code, [visitorCloneBodyPre, visitorRRPost, visitorOps, visitorMergeBodyPre], [undefined, visitorRRPre, undefined, undefined]);
                } else {
                    newAst = transformString(code, [visitorRRPost, visitorOps], [visitorRRPre, undefined]);
                }
                // post-process AST to hoist function declarations (required for Firefox)
                var hoistedFcts = [];
                newAst = hoistFunctionDeclaration(newAst, hoistedFcts);
                var newCode = esotope.generate(newAst, {comment: true ,parse: acorn.parse});
                code = newCode + "\n" + noInstr + "\n";
            } catch(ex) {
                console.log("Failed to instrument", code);
                throw ex;
            }
        }

        var tmp = {};

        tmp.nBranches = iidSourceInfo.nBranches = (condIid / IID_INC_STEP - 1) * 2;
        tmp.originalCodeFileName = iidSourceInfo.originalCodeFileName = origCodeFileName;
        tmp.instrumentedCodeFileName = iidSourceInfo.instrumentedCodeFileName = instCodeFileName;
        if (url) {
            tmp.url = iidSourceInfo.url = url;
        }
        if (isEval) {
            tmp.evalSid = iidSourceInfo.evalSid = sandbox.sid;
            tmp.evalIid = iidSourceInfo.evalIid = thisIid;
        }
        if (inlineSource) {
            tmp.code = iidSourceInfo.code = options.code;
        }

        var prepend = JSON.stringify(iidSourceInfo);
        var instCode;
        if (options.inlineSourceMap) {
            instCode = JALANGI_VAR + ".iids = " + prepend + ";\n" + code;
        } else {
            instCode = JALANGI_VAR + ".iids = " + JSON.stringify(tmp) + ";\n" + code;
        }

        if (isEval && sandbox.analysis && sandbox.analysis.instrumentCode) {
            aret = sandbox.analysis.instrumentCode(thisIid, instCode, newAst, options.isDirect);
            if (aret) {
                instCode = aret.result;
            }
        }

        return {code: instCode, instAST: newAst, sourceMapObject: iidSourceInfo, sourceMapString: prepend};

    }
function instrumentCode ( options ) { var aret , skip = false ; var isEval = options . isEval , code = options . code , thisIid = options . thisIid , inlineSource = options . inlineSource , url = options . url ; iidSourceInfo = { } ; initializeIIDCounters ( isEval ) ; instCodeFileName = options . instCodeFileName ? options . instCodeFileName : ( options . isDirect ? "eval" : "evalIndirect" ) ; origCodeFileName = options . origCodeFileName ? options . origCodeFileName : ( options . isDirect ? "eval" : "evalIndirect" ) ; if ( sandbox . analysis && sandbox . analysis . instrumentCodePre ) { aret = sandbox . analysis . instrumentCodePre ( thisIid , code , options . isDirect ) ; if ( aret ) { code = aret . code ; skip = aret . skip ; } } if ( ! skip && typeof code === 'string' && code . indexOf ( noInstr ) < 0 ) { try { code = removeShebang ( code ) ; iidSourceInfo = { } ; var newAst ; if ( Config . ENABLE_SAMPLING ) { newAst = transformString ( code , [ visitorCloneBodyPre , visitorRRPost , visitorOps , visitorMergeBodyPre ] , [ undefined , visitorRRPre , undefined , undefined ] ) ; } else { newAst = transformString ( code , [ visitorRRPost , visitorOps ] , [ visitorRRPre , undefined ] ) ; } // post-process AST to hoist function declarations (required for Firefox) var hoistedFcts = [ ] ; newAst = hoistFunctionDeclaration ( newAst , hoistedFcts ) ; var newCode = esotope . generate ( newAst , { comment : true , parse : acorn . parse } ) ; code = newCode + "\n" + noInstr + "\n" ; } catch ( ex ) { console . log ( "Failed to instrument" , code ) ; throw ex ; } } var tmp = { } ; tmp . nBranches = iidSourceInfo . nBranches = ( condIid / IID_INC_STEP - 1 ) * 2 ; tmp . originalCodeFileName = iidSourceInfo . originalCodeFileName = origCodeFileName ; tmp . instrumentedCodeFileName = iidSourceInfo . instrumentedCodeFileName = instCodeFileName ; if ( url ) { tmp . url = iidSourceInfo . url = url ; } if ( isEval ) { tmp . evalSid = iidSourceInfo . evalSid = sandbox . sid ; tmp . evalIid = iidSourceInfo . evalIid = thisIid ; } if ( inlineSource ) { tmp . code = iidSourceInfo . code = options . code ; } var prepend = JSON . stringify ( iidSourceInfo ) ; var instCode ; if ( options . inlineSourceMap ) { instCode = JALANGI_VAR + ".iids = " + prepend + ";\n" + code ; } else { instCode = JALANGI_VAR + ".iids = " + JSON . stringify ( tmp ) + ";\n" + code ; } if ( isEval && sandbox . analysis && sandbox . analysis . instrumentCode ) { aret = sandbox . analysis . instrumentCode ( thisIid , instCode , newAst , options . isDirect ) ; if ( aret ) { instCode = aret . result ; } } return { code : instCode , instAST : newAst , sourceMapObject : iidSourceInfo , sourceMapString : prepend } ; }
Instruments the provided code.

@param {{isEval: boolean, code: string, thisIid: int, origCodeFileName: string, instCodeFileName: string, inlineSourceMap: boolean, inlineSource: boolean, url: string, isDirect: boolean }} options
@return {{code:string, instAST: object, sourceMapObject: object, sourceMapString: string}}
Instruments the provided code .