Skip to content
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

refact(rivetc.codegen): generate tagged enums using unions in the C backend #54

Merged
merged 7 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions lib/core/src/utils.ri
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,22 @@
// Use of this source code is governed by an MIT license that can
// be found in the LICENSE file.

struct EnumInfo {
_rc_: uint;
_id_: uint;
obj: rawptr;
}

func enum_cast(ptr: rawptr, expected_id: uint) -> rawptr {
enum_info := unsafe { @as(&EnumInfo, ptr) };
if enum_info._id_ != expected_id {
func tagged_enum_cast(expected_id: uint, got_id: uint) {
if got_id != expected_id {
// TODO: replace indexes with names
console_ewriteln(
"runtime error: enum cast: cannot cast ID({}) to ID({})", enum_info._id_, expected_id
"runtime error: cannot cast tagged enum ID({}) to variant ID({})", got_id, expected_id
);
bt_print(2);
process_exit(1);
}
return enum_info.obj;
}

func trait_cast(got_obj: rawptr, got_id: uint, expected_id: uint) -> rawptr {
if got_id != expected_id {
// TODO: replace indexes with names
console_ewriteln(
"runtime error: trait cast: cannot cast ID({}) to ID({})", got_id, expected_id
"runtime error: cannot cast trait ID({}) to type ID({})", got_id, expected_id
);
bt_print(2);
process_exit(1);
Expand Down
Loading