-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/assert rc #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -390,11 +390,91 @@ namespace rt::core | |
| }); | ||
| } | ||
|
|
||
| void assert_rc__function_impl( | ||
| verona::interpreter::FrameObj* frame, size_t args, bool assert_lrc) | ||
| { | ||
| std::string option; | ||
| if (assert_lrc) | ||
| { | ||
| option = "lrc"; | ||
| } | ||
| else | ||
| { | ||
| option = "sbrc"; | ||
| } | ||
|
|
||
|
|
||
| if (args != 2) | ||
| { | ||
| std::stringstream ss; | ||
| ss << "assert_" << option << " expected 2 arguments"; | ||
| ui::error(ss.str()); | ||
| } | ||
|
|
||
| auto count_str = frame->stack_pop("count_str"); | ||
| auto bridge = frame->stack_pop("bridge"); | ||
| // Make sure we're comparing the correct LRC value | ||
| rt::remove_reference(frame->object(), bridge); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can cause undefined behaviour of the RC hits 0. |
||
| if (count_str->get_prototype() != stringPrototypeObject()) | ||
| { | ||
| ui::error("given count is not a string", count_str); | ||
| } | ||
| auto region = objects::get_region(bridge); | ||
| if (region->bridge != bridge) | ||
| { | ||
| std::stringstream ss; | ||
| ss << bridge << " is not the bridge object of the region"; | ||
| ui::error(ss.str(), bridge); | ||
| } | ||
| // Remove string object whitespace | ||
| auto s = count_str->get_name(); | ||
| if (s[0] == '\"') | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be undefined behaviour, if the string is empty |
||
| { | ||
| s.erase(0, 1); | ||
| s.erase(s.size() - 1); | ||
| } | ||
| auto count = std::stoi(s); | ||
| size_t actual; | ||
| if (assert_lrc) | ||
| { | ||
| actual = rt::objects::get_region(bridge)->local_reference_count; | ||
| } | ||
| else | ||
| { | ||
| actual = rt::objects::get_region(bridge)->sub_region_reference_count; | ||
| } | ||
| if (actual != count) | ||
| { | ||
| std::stringstream ss; | ||
| ss << "count: " << count << " did not match " << option << ": " << actual; | ||
| auto msg = ss.str(); | ||
| ui::error(msg, bridge); | ||
| } | ||
|
|
||
| rt::remove_reference(frame->object(), count_str); | ||
| }; | ||
|
|
||
| void test_builtins() | ||
| { | ||
| // Onus is on caller to provide a string object representing an actual integer | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Onus? |
||
| // Fixme: | ||
| // Refactor once integers are implemented | ||
| add_builtin("assert_lrc", [](auto frame, auto args) { | ||
| assert_rc__function_impl(frame, args, true); | ||
| return std::nullopt; | ||
| }); | ||
| add_builtin("assert_sbrc", [](auto frame, auto args) { | ||
| assert_rc__function_impl(frame, args, false); | ||
| return std::nullopt; | ||
| }); | ||
| } | ||
|
|
||
| void init_builtins(ui::UI* ui) | ||
| { | ||
| mermaid_builtins(ui); | ||
| ctor_builtins(); | ||
| action_builtins(); | ||
| pragma_builtins(); | ||
| test_builtins(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,11 @@ | ||||||||
| # Feels incorrect to utilize frank-tests for builtins | ||||||||
| # that are meant to be utilized for frank-tests... | ||||||||
| r1 = Region() | ||||||||
| # Give r1 a LRC of 2 | ||||||||
| a = {} | ||||||||
| r1.a = a | ||||||||
| # Using string object | ||||||||
| expected = "2" | ||||||||
| assert_lrc(r1, expected) | ||||||||
| # Using string directly | ||||||||
| assert_lrc(r1, "2") | ||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Some in the other files |
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Not a bridge | ||
| a = {} | ||
| assert_lrc(a, "2") |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Not a string | ||
| a = {} | ||
| assert_lrc(a, a) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first argument should be a bridge |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| r1 = Region() | ||
| expected = "2" | ||
| # LRC is only 1 | ||
| assert_lrc(r1, expected) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
|
|
||
| r1 = Region() | ||
| r2 = Region() | ||
| r1.r2 = r2 | ||
|
|
||
| assert_sbrc(r1, "1") | ||
| r2 = None | ||
| assert_sbrc(r1, "0") | ||
| assert_lrc(r1.r2, "0") | ||
|
|
||
| # Edge case where r2 does not contribute to sbrc of r1 | ||
| # Assure that lrc is unchanged | ||
| assert_lrc(r1, "1") | ||
| merge(r1.r2, r1) | ||
| assert_lrc(r1, "1") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.