This commit is contained in:
2026-01-19 23:10:09 +08:00
parent 1a66d45204
commit 2839c0daff
17 changed files with 2274 additions and 46 deletions

View File

@@ -123,31 +123,45 @@ TEST_CASE("class init is called on declaration", "[class][init]") {
REQUIRE(a_num->value == 18.0);
}
// TEST_CASE("interpreter performance: simple loop", "[perf][script]") {
// State state;
// const char* script = R"(
// func sum_to(number n) -> number {
// number s = 0;
// for (number i = 0; i < n; i = i + 1) {
// s = s + i;
// }
// return s;
// }
// number r = sum_to(1000);
// )";
TEST_CASE("interpreter performance: simple loop", "[perf][script]") {
State state;
State state_vm(ExecutionMode::VM);
const char* script = R"(
func sum_to(number n) -> number {
var s = 0;
for (var i = 0; i < n; i = i + 1) {
s = s + i;
}
return s;
}
var r = sum_to(1000);
)";
// BENCHMARK("sum_to(1000)") {
// if (!state.do_string(script)) {
// auto last_error = state.get_error();
// REQUIRE(last_error.empty());
// }
// auto r_val = state.get_global("r");
// REQUIRE(r_val);
// REQUIRE(r_val->type() == Type::NUMBER);
// auto r_num = std::dynamic_pointer_cast<NumberValue>(r_val);
// REQUIRE(r_num->value == 499500.0);
// };
// }
BENCHMARK("sum_to(1000)") {
if (!state.do_string(script)) {
auto last_error = state.get_error();
REQUIRE(last_error.empty());
}
auto r_val = state.get_global("r");
REQUIRE(r_val);
REQUIRE(r_val->type() == Type::NUMBER);
auto r_num = std::dynamic_pointer_cast<NumberValue>(r_val);
REQUIRE(r_num->value == 499500.0);
};
BENCHMARK("state_vm sum_to(1000)") {
if (!state_vm.do_string(script)) {
auto last_error = state_vm.get_error();
REQUIRE(last_error.empty());
}
auto r_val = state_vm.get_global("r");
REQUIRE(r_val);
REQUIRE(r_val->type() == Type::NUMBER);
auto r_num = std::dynamic_pointer_cast<NumberValue>(r_val);
REQUIRE(r_num->value == 499500.0);
};
}
TEST_CASE("loop break", "[script][loop]") {
State state;