improve rtrace and move it to lib

This commit is contained in:
dcode
2019-05-23 16:41:26 +02:00
parent fbba76ef2c
commit a49ab7a706
35 changed files with 3435 additions and 3307 deletions

26
lib/rtrace/README.md Normal file
View File

@ -0,0 +1,26 @@
# ![AS](https://avatars1.githubusercontent.com/u/28916798?s=48) rtrace
A tiny utility that records allocations, retains, releases and frees performed by the runtime and emits an error if something is off. Also checks for leaks.
Instructions
------------
Compile your module that uses the full or half runtime with `-use ASC_RTRACE=1` and include an instance of this module as the import named `rtrace`.
```js
var rtr = rtrace(e => {
// handle error
});
WebAssembly.instantiate(..., { rtrace: rtr, ... });
...
if (rtr.active) {
let leakCount = rtr.leakCount;
if (leakCount) {
// handle error
}
}
```
Note that references retained in globals which are not cleared before execution concludes appear as leaks, including their inner members. A TypedArray would leak itself and its backing ArrayBuffer in this case for example. This is perfectly normal and clearing all globals avoids this.

62
lib/rtrace/index.js Normal file
View File

@ -0,0 +1,62 @@
function rtrace(onerror) {
var blocks = new Map();
var rtrace = {
allocCount: 0,
freeCount: 0,
incrementCount: 0,
decrementCount: 0,
onalloc: function(block) {
++rtrace.allocCount;
if (blocks.has(block)) {
onerror(Error("duplicate alloc: " + block));
} else {
blocks.set(block, 0);
}
},
onfree: function(block) {
++rtrace.freeCount;
if (!blocks.has(block)) {
onerror(Error("invalid free: " + block));
} else {
blocks.delete(block);
}
},
onincrement: function(block) {
++rtrace.incrementCount;
if (!blocks.has(block)) {
onerror(Error("invalid increment: " + block));
} else {
let rc = blocks.get(block) + 1;
blocks.set(block, rc);
}
},
ondecrement: function(block) {
++rtrace.decrementCount;
if (!blocks.has(block)) {
onerror(Error("invalid decrement: " + block));
} else {
let rc = blocks.get(block) - 1;
if (rc < 0) {
onerror(Error("invalid decrement: " + block));
} else {
blocks.set(block, rc);
}
}
},
get active() {
return Boolean(rtrace.allocCount + rtrace.freeCount + rtrace.incrementCount + rtrace.decrementCount);
},
get leakCount() {
return blocks.size;
}
};
return rtrace;
}
module.exports = rtrace;

6
lib/rtrace/package.json Normal file
View File

@ -0,0 +1,6 @@
{
"name": "@assemblyscript/rtrace",
"version": "0.1.0",
"license": "Apache-2.0",
"main": "index.js"
}