Skip to content

Commit 5ff8cf0

Browse files
authored
feat: new component type apis (#240)
1 parent 26a80e1 commit 5ff8cf0

File tree

11 files changed

+183
-7
lines changed

11 files changed

+183
-7
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ root = true
33
[*]
44
end_of_line = lf
55
insert_final_newline = true
6+
indent_size = 4
67

78
[*.{cc,hh,cpp,hpp}]
89
# matching .clang-format IndentWidth

MODULE.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ module(
77
bazel_dep(name = "rules_cc", version = "0.0.9")
88
bazel_dep(name = "bazel_skylib", version = "1.5.0")
99
bazel_dep(name = "magic_enum", version = "0.9.3")
10-
bazel_dep(name = "ecsact_runtime", version = "0.6.9")
11-
bazel_dep(name = "ecsact_parse", version = "0.5.1")
10+
bazel_dep(name = "ecsact_runtime", version = "0.7.0")
11+
bazel_dep(name = "ecsact_parse", version = "0.5.2")
1212

1313
bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
1414
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)

ecsact/interpret/eval.cc

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,46 @@ static ecsact_eval_error eval_component_statement(
726726
return err;
727727
}
728728

729-
if(auto err = disallow_statement_params(statement, context)) {
729+
constexpr auto allowed_params = std::array{"stream"sv, "transient"sv};
730+
if(auto err = allow_statement_params(statement, context, allowed_params)) {
730731
return *err;
731732
}
732733

734+
auto stream_param =
735+
statement_param<bool, std::string_view>(statement, "stream"sv);
736+
auto transient_param = statement_param<bool>(statement, "transient"sv);
737+
auto component_type = ECSACT_COMPONENT_TYPE_NONE;
738+
739+
if(stream_param) {
740+
auto stream_type = std::get_if<std::string_view>(&stream_param.value());
741+
if(stream_type) {
742+
if(*stream_type != "lazy"sv) {
743+
return ecsact_eval_error{
744+
.code = ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE,
745+
.relevant_content = statement.parameters[0].name,
746+
};
747+
}
748+
749+
component_type = ECSACT_COMPONENT_TYPE_LAZY_STREAM;
750+
} else if(std::get<bool>(stream_param.value())) {
751+
component_type = ECSACT_COMPONENT_TYPE_STREAM;
752+
}
753+
}
754+
755+
if(transient_param) {
756+
if(transient_param.value()) {
757+
if(component_type != ECSACT_COMPONENT_TYPE_NONE) {
758+
// can't have transient stream
759+
return ecsact_eval_error{
760+
.code = ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE,
761+
.relevant_content = statement.parameters[0].name,
762+
};
763+
}
764+
765+
component_type = ECSACT_COMPONENT_TYPE_TRANSIENT;
766+
}
767+
}
768+
733769
auto name = std::string(data.component_name.data, data.component_name.length);
734770

735771
auto existing_decl = find_by_name<ecsact_decl_id>(package_id, name);
@@ -740,12 +776,14 @@ static ecsact_eval_error eval_component_statement(
740776
};
741777
}
742778

743-
ecsact_create_component(
779+
auto comp_id = ecsact_create_component(
744780
package_id,
745781
data.component_name.data,
746782
data.component_name.length
747783
);
748784

785+
ecsact_set_component_type(comp_id, component_type);
786+
749787
return {};
750788
}
751789

parse-resolver-runtime/parse-resolver-runtime.cc

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class composite {
3434
struct component_like : composite {};
3535

3636
struct comp_def : component_like {
37-
std::string name;
37+
std::string name;
38+
ecsact_component_type comp_type;
3839
};
3940

4041
struct trans_def : component_like {
@@ -283,6 +284,7 @@ ecsact_component_id ecsact_create_component(
283284
set_package_owner(comp_id, owner);
284285
auto& def = comp_defs[comp_id];
285286
def.name = std::string_view(component_name, component_name_len);
287+
def.comp_type = ECSACT_COMPONENT_TYPE_NONE;
286288
full_names[decl_id] = pkg_def.name + "." + def.name;
287289

288290
return comp_id;
@@ -1132,3 +1134,35 @@ auto ecsact_set_system_notify_component_setting(
11321134
def.notify_settings[component_like_id] = setting;
11331135
}
11341136
}
1137+
1138+
auto ecsact_meta_component_type( //
1139+
ecsact_component_like_id comp_like_id
1140+
) -> ecsact_component_type {
1141+
auto comp_def =
1142+
comp_defs.find(static_cast<ecsact_component_id>(comp_like_id));
1143+
if(comp_def == comp_defs.end()) {
1144+
// NOTE: this is temporary until we remove the transient fns and instead
1145+
// embrace components with transient statement params
1146+
auto trans_def =
1147+
trans_defs.find(static_cast<ecsact_transient_id>(comp_like_id));
1148+
if(trans_def != trans_defs.end()) {
1149+
return ECSACT_COMPONENT_TYPE_TRANSIENT;
1150+
}
1151+
1152+
return ECSACT_COMPONENT_TYPE_NONE;
1153+
}
1154+
1155+
return comp_def->second.comp_type;
1156+
}
1157+
1158+
auto ecsact_set_component_type( //
1159+
ecsact_component_id component_id,
1160+
ecsact_component_type comp_type
1161+
) -> void {
1162+
auto comp_def = comp_defs.find(component_id);
1163+
if(comp_def == comp_defs.end()) {
1164+
return;
1165+
}
1166+
1167+
comp_def->second.comp_type = comp_type;
1168+
}

test/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,19 @@ cc_test(
9090
"@googletest//:gtest_main",
9191
],
9292
)
93+
94+
cc_test(
95+
name = "stream_component",
96+
srcs = ["stream_component.cc"],
97+
copts = copts,
98+
data = [
99+
"stream_component.ecsact",
100+
],
101+
deps = [
102+
":test_lib",
103+
"@ecsact_interpret",
104+
"@bazel_sundry//bazel_sundry:runfiles",
105+
"@googletest//:gtest",
106+
"@googletest//:gtest_main",
107+
],
108+
)

test/MODULE.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module(name = "ecsact_interpret_test")
33
bazel_dep(name = "rules_cc", version = "0.0.9")
44
bazel_dep(name = "bazel_skylib", version = "1.5.0")
55
bazel_dep(name = "googletest", version = "1.14.0")
6-
bazel_dep(name = "ecsact_parse", version = "0.5.1")
7-
bazel_dep(name = "ecsact_runtime", version = "0.6.6")
6+
bazel_dep(name = "ecsact_parse", version = "0.5.2")
7+
bazel_dep(name = "ecsact_runtime", version = "0.7.0")
88

99
bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
1010
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)

test/errors/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ _TESTS = [
66
"duplicate_notice_components",
77
"invalid_assoc_field",
88
"invalid_notify_settings",
9+
"invalid_stream_component_param_value",
910
"no_capabilities",
1011
"no_package_statement_first",
1112
"unknown_association_field",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "gtest/gtest.h"
2+
#include "ecsact/interpret/eval.h"
3+
4+
#include "test_lib.hh"
5+
6+
TEST(UnknownParamValue, UnknownStreamComponentParamValue) {
7+
auto errs = ecsact_interpret_test_files({
8+
"errors/invalid_stream_component_param_value.ecsact",
9+
});
10+
ASSERT_EQ(errs.size(), 1);
11+
ASSERT_EQ(errs[0].eval_error, ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE);
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package unknown_comp_param_value;
2+
3+
component UnknownParamValue(stream: huh) {
4+
f32 a;
5+
}

test/stream_component.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
#include "gtest/gtest.h"
3+
4+
#include "ecsact/runtime/meta.hh"
5+
#include "ecsact/runtime/dynamic.h"
6+
#include "test_lib.hh"
7+
8+
class StreamComponent : public testing::Test {
9+
public:
10+
ecsact_package_id pkg_id;
11+
12+
protected:
13+
void SetUp() override {
14+
auto errs = ecsact_interpret_test_files({"stream_component.ecsact"});
15+
ASSERT_EQ(errs.size(), 0) //
16+
<< "Expected no errors. Instead got: " << errs[0].error_message << "\n";
17+
pkg_id = ecsact::meta::get_package_ids().at(0);
18+
}
19+
20+
void TearDown() override {
21+
// ecsact_destroy_package(pkg_id);
22+
}
23+
};
24+
25+
TEST_F(StreamComponent, SanityCheck) {
26+
auto comp_id = get_component_by_name(pkg_id, "MyNormieComponent");
27+
ASSERT_TRUE(comp_id);
28+
29+
auto comp_like_id = ecsact_meta_component_type(
30+
ecsact_id_cast<ecsact_component_like_id>(*comp_id)
31+
);
32+
33+
ASSERT_EQ(comp_like_id, ECSACT_COMPONENT_TYPE_NONE);
34+
}
35+
36+
TEST_F(StreamComponent, HasStreamType) {
37+
auto comp_id = get_component_by_name(pkg_id, "MyStreamComponent");
38+
ASSERT_TRUE(comp_id);
39+
40+
auto comp_type = ecsact_meta_component_type(
41+
ecsact_id_cast<ecsact_component_like_id>(*comp_id)
42+
);
43+
44+
ASSERT_EQ(comp_type, ECSACT_COMPONENT_TYPE_STREAM);
45+
}
46+
47+
TEST_F(StreamComponent, HasLazyStreamType) {
48+
auto comp_id = get_component_by_name(pkg_id, "MyLazyStreamComponent");
49+
ASSERT_TRUE(comp_id);
50+
51+
auto comp_type = ecsact_meta_component_type(
52+
ecsact_id_cast<ecsact_component_like_id>(*comp_id)
53+
);
54+
55+
ASSERT_EQ(comp_type, ECSACT_COMPONENT_TYPE_LAZY_STREAM);
56+
}

0 commit comments

Comments
 (0)