Haiyang-Sun / nodeprof.js

Instrumentation framework for Node.js compliant to ECMAScript 2020 based on GraalVM.
Apache License 2.0
53 stars 22 forks source link

`this` is not being bound inside analysis methods #90

Closed thepalbi closed 3 years ago

thepalbi commented 3 years ago

Hi!

I'm implementing a couple of analysis in NodeProf. However, when trying to replicate the example of branch coverage like the on in this Jalangi tutorial, I'm getting the following kind of errors:

[e] TypeError: Cannot read property 'branches' of undefined
TypeError: Cannot read property 'branches' of undefined
conditional (/persistent2/tsm-setup/taser/src/SimpleAnalysis.js:11:26)
Object.<anonymous> (/persistent2/tsm-setup/graalvm-nodeprof-java8-20.2.0-dev/nodeprof/tests/helloworld.js:9:19)
Module._compile (internal/modules/cjs/loader.js:1138:30)
Object.<anonymous> (internal/modules/cjs/loader.js:1158:10)
Module.load (internal/modules/cjs/loader.js:986:32)
Function._load (internal/modules/cjs/loader.js:879:14)
Function.executeUserEntryPoint (internal/modules/run_main.js:71:12)
Object.<anonymous> (/persistent2/tsm-setup/graalvm-nodeprof-java8-20.2.0-dev/nodeprof/nodeprof.js:260:19)
Module._compile (internal/modules/cjs/loader.js:1138:30)
Object.<anonymous> (internal/modules/cjs/loader.js:1158:10)

The analysis source code is the following:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var util = require('util');
//DO NOT INSTRUMENT
var SimpleAnalysis = /** @class */ (function () {
    function SimpleAnalysis() {
        this.branches = {};
    }
    SimpleAnalysis.prototype.conditional = function (iid, result) {
        var globalIID = J$.getGlobalIID(iid);
        var branchInfo = this.branches[globalIID];
        if (!branchInfo) {
            branchInfo = { trueCount: 0, falseCount: 0 };
        }
        if (result) {
            branchInfo.trueCount++;
        }
        else {
            branchInfo.falseCount++;
        }
        this.branches[globalIID] = branchInfo;
    };
    SimpleAnalysis.prototype.endExecution = function () {
        for (var id in this.branches) {
            var branchInfo = this.branches[id];
            var location = J$.iidToLocation(id);
            console.log(util.format('At location %s true branch was taken %d times, and false branch %d times.', location, branchInfo.trueCount, branchInfo.falseCount));
        }
    };
    return SimpleAnalysis;
}());
var analysis = new SimpleAnalysis();
J$.analysis = analysis;

For running the analysis, I've used the latest release, an the following command:

./bin/node --experimental-options --nodeprof ./nodeprof/nodeprof.js --analysis /persistent2/tsm-setup/taser/src/SimpleAnalysis.js ./nodeprof/tests/helloworld.js

Note that it is a Typescript generated javascript snippet, but since the usage of this inside prototype defined functions is normal, I assumed this would work. Any insights on what's happending?