Skip to content

Commit

Permalink
Merge pull request #40 from mjp41/0-pippi-langstrump
Browse files Browse the repository at this point in the history
Mermaid: Add taint function and more colors to diagrams
  • Loading branch information
xFrednet authored Oct 25, 2024
2 parents 5fb69bd + 20abbff commit 03b22b2
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 32 deletions.
66 changes: 66 additions & 0 deletions docs/builtin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Built-in Functions

## Debugging

#### `breakpoint()`

Causes the interpreter to enter *interactive mode*.

#### `unreachable()`

Aborts the interpreter process. It's intended to indicate that an execution branch is not reachable.

## Constructors

#### `region()`

Creates a new region object.

#### `cown(take region)`

Creates a new `cown` object.

The region must have a local reference count of one. The `take` keyword is used to replace the local value with `None`.

### `create(proto)`

Creates a new object from the given prototype.

## Memory Management

#### `freeze(obj)`

Performs a deep freeze of the object and all referenced objects.

This will move the objects out of their current region into the immutable region. Cowns will stop the freeze propagation, as they can be safely shared across threads and behaviors.

## Mermaid

#### `mermaid_hide(obj, ..)`

Hides the given arguments from the mermaid graph.

#### `mermaid_show(obj, ..)`

Shows the given arguments in the mermaid diagram.

#### `mermaid_show_all()`

Makes all nodes visible in the mermaid diagram.

#### `mermaid_show_tainted(obj, ...)`

Draws a mermaid diagram with the given objects marked as tainted. This will show which objects are reachable at this point.

#### `mermaid_taint(obj, ...)`

Marks the given objects as tainted, this will highlight, which nodes are reachable from the given objects.

The tainted status will remain until `mermaid_untaint` is called.

`mermaid_show_tainted()` can be used to only taint the current snapshot.

#### `mermaid_untaint(obj, ...)`

Marks the given objects as untainted, thereby removing the highlights from the mermaid diagram.

55 changes: 55 additions & 0 deletions src/rt/core/builtin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,61 @@ namespace rt::core
return std::nullopt;
});

add_builtin(
"mermaid_show_tainted", [mermaid](auto frame, auto stack, auto args) {
assert(args >= 1);

std::vector<rt::objects::DynObject*> taint;
for (int i = 0; i < args; i++)
{
auto value = stack->back();
mermaid->add_taint(value);
taint.push_back(value);
rt::remove_reference(frame, value);
stack->pop_back();
}

// Mermaid output
std::vector<rt::objects::DynObject*> roots{frame};
mermaid->output(roots, "Builtin: display taint");

for (auto tainted : taint)
{
mermaid->remove_taint(tainted);
}

return std::nullopt;
});

add_builtin("mermaid_taint", [mermaid](auto frame, auto stack, auto args) {
assert(args >= 1);

for (int i = 0; i < args; i++)
{
auto value = stack->back();
mermaid->add_taint(value);
rt::remove_reference(frame, value);
stack->pop_back();
}

return std::nullopt;
});

add_builtin(
"mermaid_untaint", [mermaid](auto frame, auto stack, auto args) {
assert(args >= 1);

for (int i = 0; i < args; i++)
{
auto value = stack->back();
mermaid->remove_taint(value);
rt::remove_reference(frame, value);
stack->pop_back();
}

return std::nullopt;
});

add_builtin("breakpoint", [mermaid](auto, auto, auto args) {
assert(args == 0);

Expand Down
13 changes: 13 additions & 0 deletions src/rt/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace rt::ui
std::set<objects::DynObject*> unreachable_hide;
/// Nodes that should never be visible.
std::set<objects::DynObject*> always_hide;
/// @brief Nodes that should be tainted, meaning they and all reachable
/// nodes are highlighted.
std::set<rt::objects::DynObject*> taint;

public:
MermaidUI(int step_counter);
Expand Down Expand Up @@ -90,6 +93,16 @@ namespace rt::ui
{
always_hide.erase(obj);
}

void add_taint(objects::DynObject* obj)
{
taint.insert(obj);
}

void remove_taint(objects::DynObject* obj)
{
taint.erase(obj);
}
};

[[noreturn]] inline void error(const std::string& msg)
Expand Down
Loading

0 comments on commit 03b22b2

Please sign in to comment.