Skip to content

Commit

Permalink
Add a chapter about r2js plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
radare committed Jul 10, 2024
1 parent 2b3ba16 commit b68d633
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
* [Analysis plugins](plugins/dev-anal.md)
* [Bin plugins](plugins/dev-bin.md)
* [Charset plugins](plugins/dev-charset.md)
* [R2JS plugins](plugins/r2js.md)
* [Python plugins](plugins/python.md)
* [Other plugins](plugins/dev-other.md)
* [Troubleshooting](plugins/troubles.md)
Expand Down
38 changes: 38 additions & 0 deletions src/plugins/r2js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## R2JS Plugins

The javascript runtime embedded in radare2 provides a way to implement different types of plugins.

Check out the `r2skel` project for more examples, but we will cover the basics now.

```js
(function () {
r2.unload("core", "mycore");
r2.plugin("core", function () {
console.log("==> The 'mycore' plugin has been instantiated. Type 'mycore' to test it");
function coreCall(cmd) {
if (cmd.startsWith("mycore")) {
console.log("Hello From My Core!");
return true;
}
return false;
}
return {
"name": "mycore",
"license": "MIT",
"desc": "simple core plugin in typescript for radare2",
"call": coreCall,
};
});
})();
```

Some notes on this code:

* The whole code is wrapped inside an anonymous function call, this way we don't polute the global scope.
* Use r2.unload() and r2.plugin() to unload and register new plugins
* Register a plugin by passing the plugin type and a function
* The initialization function returns a object describing it

This code runs inside radare2, this means that it will be *fast*, and by fast I mean faster than Python, r2pipe and will be closer to the `C` plugins. Not just for running, but also for loading, because the js runtime is already there, use r2js plugins if possible if you care about performance.

You can find other plugin examples in the examples directory in radare2.

0 comments on commit b68d633

Please sign in to comment.