Skip to content

Commit 5df7f54

Browse files
committed
LLVMCodeBuilder: Sync variables before calling functions
1 parent 987ae10 commit 5df7f54

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

src/engine/internal/llvm/llvmcodebuilder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ std::shared_ptr<ExecutableCode> LLVMCodeBuilder::finalize()
161161
std::vector<llvm::Type *> types;
162162
std::vector<llvm::Value *> args;
163163

164+
// Variables must be synchronized because the function can read them
165+
syncVariables(targetVariables);
166+
164167
// Add execution context arg
165168
if (step.functionCtxArg) {
166169
types.push_back(llvm::PointerType::get(llvm::Type::getInt8Ty(m_llvmCtx), 0));

test/llvm/llvmcodebuilder_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,39 @@ TEST_F(LLVMCodeBuilderTest, ReadVariable)
21062106
ASSERT_EQ(testing::internal::GetCapturedStdout(), expected);
21072107
}
21082108

2109+
TEST_F(LLVMCodeBuilderTest, SyncVariablesBeforeCallingFunction)
2110+
{
2111+
Sprite sprite;
2112+
sprite.setEngine(&m_engine);
2113+
2114+
auto var = std::make_shared<Variable>("", "");
2115+
sprite.addVariable(var);
2116+
2117+
createBuilder(&sprite, true);
2118+
2119+
CompilerValue *v = m_builder->addConstValue("abc");
2120+
m_builder->createVariableWrite(var.get(), v);
2121+
2122+
v = m_builder->addConstValue(123);
2123+
m_builder->createVariableWrite(var.get(), v);
2124+
2125+
m_builder->addTargetFunctionCall("test_print_first_local_variable", Compiler::StaticType::Void, {}, {});
2126+
2127+
v = m_builder->addConstValue(456);
2128+
m_builder->createVariableWrite(var.get(), v);
2129+
2130+
auto code = m_builder->finalize();
2131+
Script script(&sprite, nullptr, nullptr);
2132+
script.setCode(code);
2133+
2134+
Thread thread(&sprite, nullptr, &script);
2135+
auto ctx = code->createExecutionContext(&thread);
2136+
testing::internal::CaptureStdout();
2137+
code->run(ctx.get());
2138+
2139+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "123\n");
2140+
}
2141+
21092142
TEST_F(LLVMCodeBuilderTest, CastNonRawValueToUnknownType)
21102143
{
21112144
Stage stage;

test/llvm/testfunctions.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <scratchcpp/string_functions.h>
33
#include <scratchcpp/stringptr.h>
44
#include <scratchcpp/string_pool.h>
5+
#include <scratchcpp/variable.h>
56
#include <utf8.h>
67
#include <iostream>
78
#include <targetmock.h>
@@ -189,4 +190,9 @@ extern "C"
189190
value_toString(v, &str);
190191
std::cout << str << std::endl;
191192
}
193+
194+
void test_print_first_local_variable(Target *target)
195+
{
196+
std::cout << target->variableAt(0)->value().toString() << std::endl;
197+
}
192198
}

test/llvm/testfunctions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ extern "C"
4848
void test_print_string(const StringPtr *v);
4949
void test_print_pointer(const void *v);
5050
void test_print_unknown(const ValueData *v);
51+
52+
void test_print_first_local_variable(Target *target);
5153
}
5254

5355
} // namespace libscratchcpp

0 commit comments

Comments
 (0)