Skip to content

Commit

Permalink
feat: new component type apis (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaucy authored Oct 14, 2024
1 parent 26a80e1 commit d8b2205
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 7 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ root = true
[*]
end_of_line = lf
insert_final_newline = true
indent_size = 4

[*.{cc,hh,cpp,hpp}]
# matching .clang-format IndentWidth
Expand Down
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module(
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "magic_enum", version = "0.9.3")
bazel_dep(name = "ecsact_runtime", version = "0.6.9")
bazel_dep(name = "ecsact_parse", version = "0.5.1")
bazel_dep(name = "ecsact_runtime", version = "0.7.0")
bazel_dep(name = "ecsact_parse", version = "0.5.2")

bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
Expand Down
42 changes: 40 additions & 2 deletions ecsact/interpret/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,46 @@ static ecsact_eval_error eval_component_statement(
return err;
}

if(auto err = disallow_statement_params(statement, context)) {
constexpr auto allowed_params = std::array{"stream"sv, "transient"sv};
if(auto err = allow_statement_params(statement, context, allowed_params)) {
return *err;
}

auto stream_param =
statement_param<bool, std::string_view>(statement, "stream"sv);
auto transient_param = statement_param<bool>(statement, "transient"sv);
auto component_type = ECSACT_COMPONENT_TYPE_NONE;

if(stream_param) {
auto stream_type = std::get_if<std::string_view>(&stream_param.value());
if(stream_type) {
if(*stream_type != "lazy"sv) {
return ecsact_eval_error{
.code = ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE,
.relevant_content = statement.parameters[0].name,
};
}

component_type = ECSACT_COMPONENT_TYPE_LAZY_STREAM;
} else if(std::get<bool>(stream_param.value())) {
component_type = ECSACT_COMPONENT_TYPE_STREAM;
}
}

if(transient_param) {
if(transient_param.value()) {
if(component_type != ECSACT_COMPONENT_TYPE_NONE) {
// can't have transient stream
return ecsact_eval_error{
.code = ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE,
.relevant_content = statement.parameters[0].name,
};
}

component_type = ECSACT_COMPONENT_TYPE_TRANSIENT;
}
}

auto name = std::string(data.component_name.data, data.component_name.length);

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

ecsact_create_component(
auto comp_id = ecsact_create_component(
package_id,
data.component_name.data,
data.component_name.length
);

ecsact_set_component_type(comp_id, component_type);

return {};
}

Expand Down
36 changes: 35 additions & 1 deletion parse-resolver-runtime/parse-resolver-runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class composite {
struct component_like : composite {};

struct comp_def : component_like {
std::string name;
std::string name;
ecsact_component_type comp_type;
};

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

return comp_id;
Expand Down Expand Up @@ -1132,3 +1134,35 @@ auto ecsact_set_system_notify_component_setting(
def.notify_settings[component_like_id] = setting;
}
}

auto ecsact_meta_component_type( //
ecsact_component_like_id comp_like_id
) -> ecsact_component_type {
auto comp_def =
comp_defs.find(static_cast<ecsact_component_id>(comp_like_id));
if(comp_def == comp_defs.end()) {
// NOTE: this is temporary until we remove the transient fns and instead
// embrace components with transient statement params
auto trans_def =
trans_defs.find(static_cast<ecsact_transient_id>(comp_like_id));
if(trans_def != trans_defs.end()) {
return ECSACT_COMPONENT_TYPE_TRANSIENT;
}

return ECSACT_COMPONENT_TYPE_NONE;
}

return comp_def->second.comp_type;
}

auto ecsact_set_component_type( //
ecsact_component_id component_id,
ecsact_component_type comp_type
) -> void {
auto comp_def = comp_defs.find(component_id);
if(comp_def == comp_defs.end()) {
return;
}

comp_def->second.comp_type = comp_type;
}
16 changes: 16 additions & 0 deletions test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,19 @@ cc_test(
"@googletest//:gtest_main",
],
)

cc_test(
name = "stream_component",
srcs = ["stream_component.cc"],
copts = copts,
data = [
"stream_component.ecsact",
],
deps = [
":test_lib",
"@ecsact_interpret",
"@bazel_sundry//bazel_sundry:runfiles",
"@googletest//:gtest",
"@googletest//:gtest_main",
],
)
9 changes: 7 additions & 2 deletions test/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module(name = "ecsact_interpret_test")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "googletest", version = "1.14.0")
bazel_dep(name = "ecsact_parse", version = "0.5.1")
bazel_dep(name = "ecsact_runtime", version = "0.6.6")
bazel_dep(name = "ecsact_parse", version = "0.5.2")
bazel_dep(name = "ecsact_runtime", version = "0.7.0")

bazel_dep(name = "toolchains_llvm", version = "1.0.0", dev_dependency = True)
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
Expand Down Expand Up @@ -36,3 +36,8 @@ local_path_override(
module_name = "ecsact_interpret",
path = "..",
)

local_path_override(
module_name = "ecsact_runtime",
path = "../../ecsact_runtime",
)
1 change: 1 addition & 0 deletions test/errors/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _TESTS = [
"duplicate_notice_components",
"invalid_assoc_field",
"invalid_notify_settings",
"invalid_stream_component_param_value",
"no_capabilities",
"no_package_statement_first",
"unknown_association_field",
Expand Down
12 changes: 12 additions & 0 deletions test/errors/invalid_stream_component_param_value.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "gtest/gtest.h"
#include "ecsact/interpret/eval.h"

#include "test_lib.hh"

TEST(UnknownParamValue, UnknownStreamComponentParamValue) {
auto errs = ecsact_interpret_test_files({
"errors/invalid_stream_component_param_value.ecsact",
});
ASSERT_EQ(errs.size(), 1);
ASSERT_EQ(errs[0].eval_error, ECSACT_EVAL_ERR_INVALID_PARAMETER_VALUE);
}
5 changes: 5 additions & 0 deletions test/errors/invalid_stream_component_param_value.ecsact
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package unknown_comp_param_value;

component UnknownParamValue(stream: huh) {
f32 a;
}
56 changes: 56 additions & 0 deletions test/stream_component.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

#include "gtest/gtest.h"

#include "ecsact/runtime/meta.hh"
#include "ecsact/runtime/dynamic.h"
#include "test_lib.hh"

class StreamComponent : public testing::Test {
public:
ecsact_package_id pkg_id;

protected:
void SetUp() override {
auto errs = ecsact_interpret_test_files({"stream_component.ecsact"});
ASSERT_EQ(errs.size(), 0) //
<< "Expected no errors. Instead got: " << errs[0].error_message << "\n";
pkg_id = ecsact::meta::get_package_ids().at(0);
}

void TearDown() override {
// ecsact_destroy_package(pkg_id);
}
};

TEST_F(StreamComponent, SanityCheck) {
auto comp_id = get_component_by_name(pkg_id, "MyNormieComponent");
ASSERT_TRUE(comp_id);

auto comp_like_id = ecsact_meta_component_type(
ecsact_id_cast<ecsact_component_like_id>(*comp_id)
);

ASSERT_EQ(comp_like_id, ECSACT_COMPONENT_TYPE_NONE);
}

TEST_F(StreamComponent, HasStreamType) {
auto comp_id = get_component_by_name(pkg_id, "MyStreamComponent");
ASSERT_TRUE(comp_id);

auto comp_type = ecsact_meta_component_type(
ecsact_id_cast<ecsact_component_like_id>(*comp_id)
);

ASSERT_EQ(comp_type, ECSACT_COMPONENT_TYPE_STREAM);
}

TEST_F(StreamComponent, HasLazyStreamType) {
auto comp_id = get_component_by_name(pkg_id, "MyLazyStreamComponent");
ASSERT_TRUE(comp_id);

auto comp_type = ecsact_meta_component_type(
ecsact_id_cast<ecsact_component_like_id>(*comp_id)
);

ASSERT_EQ(comp_type, ECSACT_COMPONENT_TYPE_LAZY_STREAM);
}
13 changes: 13 additions & 0 deletions test/stream_component.ecsact
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
main package eval.stream_component;

component MyStreamComponent(stream) {
f32 a;
}

component MyLazyStreamComponent(stream: lazy) {
f32 a;
}

component MyNormieComponent(stream: false) {
f32 a;
}

0 comments on commit d8b2205

Please sign in to comment.