Skip to content

Commit f66699f

Browse files
committed
Implement operator_join
1 parent 4ee628a commit f66699f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/dev/blocks/operatorblocks.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void OperatorBlocks::registerBlocks(IEngine *engine)
3131
engine->addCompileFunction(this, "operator_and", &compileAnd);
3232
engine->addCompileFunction(this, "operator_or", &compileOr);
3333
engine->addCompileFunction(this, "operator_not", &compileNot);
34+
engine->addCompileFunction(this, "operator_join", &compileJoin);
3435
}
3536

3637
CompilerValue *OperatorBlocks::compileAdd(Compiler *compiler)
@@ -89,3 +90,27 @@ CompilerValue *OperatorBlocks::compileNot(Compiler *compiler)
8990
{
9091
return compiler->createNot(compiler->addInput("OPERAND"));
9192
}
93+
94+
CompilerValue *OperatorBlocks::compileJoin(Compiler *compiler)
95+
{
96+
auto string1 = compiler->addInput("STRING1");
97+
auto string2 = compiler->addInput("STRING2");
98+
return compiler->addFunctionCall("operator_join", Compiler::StaticType::String, { Compiler::StaticType::String, Compiler::StaticType::String }, { string1, string2 });
99+
}
100+
101+
extern "C" char *operator_join(const char *string1, const char *string2)
102+
{
103+
const size_t len1 = strlen(string1);
104+
const size_t len2 = strlen(string2);
105+
106+
char *ret = (char *)malloc((len1 + len2 + 1) * sizeof(char));
107+
size_t i;
108+
109+
for (i = 0; i < len1; i++)
110+
ret[i] = string1[i];
111+
112+
for (i = 0; i < len2 + 1; i++) // +1: null-terminate
113+
ret[len1 + i] = string2[i];
114+
115+
return ret;
116+
}

src/dev/blocks/operatorblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class OperatorBlocks : public IExtension
2929
static CompilerValue *compileAnd(Compiler *compiler);
3030
static CompilerValue *compileOr(Compiler *compiler);
3131
static CompilerValue *compileNot(Compiler *compiler);
32+
static CompilerValue *compileJoin(Compiler *compiler);
3233
};
3334

3435
} // namespace libscratchcpp

test/dev/blocks/operator_blocks_test.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,28 @@ TEST_F(OperatorBlocksTest, Not)
353353
ASSERT_EQ(Value(values[0]), true);
354354
ASSERT_EQ(Value(values[1]), false);
355355
}
356+
357+
TEST_F(OperatorBlocksTest, Join)
358+
{
359+
auto target = std::make_shared<Sprite>();
360+
ScriptBuilder builder(m_extension.get(), m_engine, target);
361+
362+
builder.addBlock("operator_join");
363+
builder.addValueInput("STRING1", "abc");
364+
builder.addValueInput("STRING2", "def");
365+
builder.captureBlockReturnValue();
366+
367+
builder.addBlock("operator_join");
368+
builder.addValueInput("STRING1", "Hello ");
369+
builder.addValueInput("STRING2", "world");
370+
builder.captureBlockReturnValue();
371+
372+
builder.build();
373+
builder.run();
374+
375+
List *valueList = builder.capturedValues();
376+
ValueData *values = valueList->data();
377+
ASSERT_EQ(valueList->size(), 2);
378+
ASSERT_EQ(Value(values[0]), "abcdef");
379+
ASSERT_EQ(Value(values[1]), "Hello world");
380+
}

0 commit comments

Comments
 (0)