@abaplint/runtime
Version:
Transpiler - Runtime
90 lines • 3.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Trace = void 0;
const statements_1 = require("./statements");
// Polyfill for detecting async functions without relying on Node's "util/types"
// Works in Node and browser builds.
const isAsyncFunction = (fn) => {
if (typeof fn !== "function") {
return false;
}
// Reliable brand check
const tag = Object.prototype.toString.call(fn);
if (tag === "[object AsyncFunction]") {
return true;
}
// Fallback via constructor name (covers most environments)
const ctorName = fn?.constructor?.name;
return ctorName === "AsyncFunction";
};
class Trace {
traceTotals = {};
setTrace(min, totals, statements) {
const candidates = [...Object.keys(statements), ...Object.getOwnPropertyNames(statements_1.Statements.prototype)];
for (const c of candidates) {
if (c === "context" || c === "constructor" || c.startsWith("_") || c === "loop") {
continue;
}
const func = statements[c];
if (isAsyncFunction(func)) {
statements[c] = this._traceAsync(func, c, min, totals);
}
else {
statements[c] = this._trace(func, c, min, totals);
}
}
return this;
}
getTotals() {
return this.traceTotals;
}
//////////////////////////////////////////
_trace(func, name, min, totals) {
const tt = this.traceTotals;
const exec1 = (...options) => {
const start = Date.now();
const result = func.bind(this)(...options);
const runtime = Date.now() - start;
if (totals === true) {
if (tt[name] === undefined) {
tt[name] = { calls: 0, totalRuntime: 0 };
}
tt[name].totalRuntime += runtime;
tt[name].calls++;
}
if (min > 0 && runtime >= min) {
console.log(`STATEMENT: ${name}, ${runtime} ms`);
if (totals === true) {
console.log(JSON.stringify(tt));
}
}
return result;
};
return exec1;
}
_traceAsync(func, name, min, totals) {
const tt = this.traceTotals;
const exec2 = async (...options) => {
const start = Date.now();
const result = await func.bind(this)(...options);
const runtime = Date.now() - start;
if (totals === true) {
if (tt[name] === undefined) {
tt[name] = { calls: 0, totalRuntime: 0 };
}
tt[name].totalRuntime += runtime;
tt[name].calls++;
}
if (min > 0 && runtime >= min) {
console.log(`STATEMENT: ${name}, ${runtime} ms`);
if (totals === true) {
console.log(JSON.stringify(tt));
}
}
return result;
};
return exec2;
}
}
exports.Trace = Trace;
//# sourceMappingURL=trace.js.map