Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 210 additions & 78 deletions include/scratchcpp/value.h

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions src/blocks/looksblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ void LooksBlocks::compileSwitchCostumeTo(Compiler *compiler)
if (v.type() == Value::Type::Integer) {
compiler->addConstValue(v.toLong() - 1);
compiler->addFunctionCall(&switchCostumeToByIndex);
} else {
compiler->addInput(input);
compiler->addFunctionCall(&switchCostumeTo);
}
}
} else {
Expand Down Expand Up @@ -394,6 +397,9 @@ void LooksBlocks::compileSwitchBackdropTo(Compiler *compiler)
if (v.type() == Value::Type::Integer) {
compiler->addConstValue(v.toLong() - 1);
compiler->addFunctionCall(&switchBackdropToByIndex);
} else {
compiler->addInput(input);
compiler->addFunctionCall(&switchBackdropTo);
}
}
} else {
Expand Down Expand Up @@ -433,6 +439,9 @@ void LooksBlocks::compileSwitchBackdropToAndWait(Compiler *compiler)
if (v.type() == Value::Type::Integer) {
compiler->addConstValue(v.toLong() - 1);
compiler->addFunctionCall(&switchBackdropToByIndexAndWait);
} else {
compiler->addInput(input);
compiler->addFunctionCall(&switchBackdropToAndWait);
}
}
} else {
Expand Down Expand Up @@ -936,7 +945,7 @@ unsigned int LooksBlocks::switchCostumeTo(VirtualMachine *vm)
else if (nameStr == "previous costume")
previousCostume(vm);
else {
if (name->type() == Value::Type::Integer)
if (name->isValidNumber())
setCostumeByIndex(target, name->toLong() - 1);
}
} else
Expand Down Expand Up @@ -994,7 +1003,7 @@ void LooksBlocks::switchBackdropToImpl(VirtualMachine *vm)
else if (nameStr == "random backdrop") {
randomBackdropImpl(vm);
} else {
if (name->type() == Value::Type::Integer)
if (name->isValidNumber())
setCostumeByIndex(stage, name->toLong() - 1);
}
} else
Expand Down
6 changes: 3 additions & 3 deletions src/blocks/soundblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ bool SoundBlocks::compilePlayCommon(Compiler *compiler, bool untilDone, bool *by
if (index == -1) {
Value v(value);

if (v.type() == Value::Type::Integer) {
if (v.isValidNumber() && !v.isInfinity() && !v.isNegativeInfinity() && !(v.isString() && v.toString().empty())) {
compiler->addConstValue(v.toLong() - 1);
compiler->addFunctionCall(untilDone ? &playByIndexUntilDone : &playByIndex);

Expand Down Expand Up @@ -166,7 +166,7 @@ Sound *SoundBlocks::playCommon(VirtualMachine *vm)
return sound;
}

else if (name->type() == Value::Type::Integer) {
if (name->isValidNumber() && !name->isInfinity() && !name->isNegativeInfinity() && !(name->isString() && name->toString().empty())) {
sound = getSoundByIndex(target, name->toLong() - 1);

if (sound) {
Expand Down Expand Up @@ -245,7 +245,7 @@ unsigned int SoundBlocks::checkSound(VirtualMachine *vm)
if (target) {
Sound *sound = target->soundAt(target->findSound(name->toString())).get();

if (!sound && name->type() == Value::Type::Integer)
if (!sound && name->isValidNumber() && !name->isInfinity() && !name->isNegativeInfinity() && !(name->isString() && name->toString().empty()))
sound = getSoundByIndex(target, name->toLong() - 1);

if (sound) {
Expand Down
34 changes: 17 additions & 17 deletions src/engine/virtualmachine_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
DISPATCH();

OP(RANDOM) :
if ((READ_REG(0, 2)->type() == Value::Type::Integer) && (READ_REG(1, 2)->type() == Value::Type::Integer)) REPLACE_RET_VALUE(rng->randint(READ_REG(0, 2)->toInt(), READ_REG(1, 2)->toInt()), 2);
if (READ_REG(0, 2)->isInt() && READ_REG(1, 2)->isInt()) REPLACE_RET_VALUE(rng->randint(READ_REG(0, 2)->toInt(), READ_REG(1, 2)->toInt()), 2);
else REPLACE_RET_VALUE(rng->randintDouble(READ_REG(0, 2)->toDouble(), READ_REG(1, 2)->toDouble()), 2);
FREE_REGS(1);
DISPATCH();
Expand Down Expand Up @@ -621,7 +621,10 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
const Value *indexValue = READ_LAST_REG();
size_t index;
List *list = lists[*++pos];
if (indexValue->isString()) {
if (indexValue->isValidNumber()) {
index = indexValue->toLong();
FIX_LIST_INDEX(index, list->size());
} else {
const std::string &str = indexValue->toString();
if (str == "last") {
index = list->size();
Expand All @@ -633,9 +636,6 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
index = size == 0 ? 0 : rng->randint(1, size);
} else
index = 0;
} else {
index = indexValue->toLong();
FIX_LIST_INDEX(index, list->size());
}
if (index != 0)
list->removeAt(index - 1);
Expand All @@ -652,7 +652,10 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
const Value *indexValue = READ_REG(1, 2);
size_t index;
List *list = lists[*++pos];
if (indexValue->isString()) {
if (indexValue->isValidNumber()) {
index = indexValue->toLong();
FIX_LIST_INDEX(index, list->size());
} else {
const std::string &str = indexValue->toString();
if (str == "last") {
list->push_back(*READ_REG(0, 2));
Expand All @@ -662,9 +665,6 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
index = size == 0 ? 1 : rng->randint(1, size);
} else
index = 0;
} else {
index = indexValue->toLong();
FIX_LIST_INDEX(index, list->size());
}
if ((index != 0) || list->empty()) {
if (list->empty())
Expand All @@ -681,7 +681,10 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
const Value *indexValue = READ_REG(0, 2);
size_t index;
List *list = lists[*++pos];
if (indexValue->isString()) {
if (indexValue->isValidNumber()) {
index = indexValue->toLong();
FIX_LIST_INDEX(index, list->size());
} else {
std::string str = indexValue->toString();
if (str == "last")
index = list->size();
Expand All @@ -690,9 +693,6 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
index = size == 0 ? 0 : rng->randint(1, size);
} else
index = 0;
} else {
index = indexValue->toLong();
FIX_LIST_INDEX(index, list->size());
}
if (index != 0)
list->operator[](index - 1) = *READ_REG(1, 2);
Expand All @@ -705,7 +705,10 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
const Value *indexValue = READ_LAST_REG();
size_t index;
List *list = lists[*++pos];
if (indexValue->isString()) {
if (indexValue->isValidNumber()) {
index = indexValue->toLong();
FIX_LIST_INDEX(index, list->size());
} else {
std::string str = indexValue->toString();
if (str == "last")
index = list->size();
Expand All @@ -714,9 +717,6 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
index = size == 0 ? 0 : rng->randint(1, size);
} else
index = 0;
} else {
index = indexValue->toLong();
FIX_LIST_INDEX(index, list->size());
}
if (index == 0) {
REPLACE_RET_VALUE("", 1);
Expand Down
8 changes: 7 additions & 1 deletion src/internal/reader_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ Value jsonToValue(nlohmann::json value)
return value.get<std::string>();
else if (value.is_boolean())
return value.get<bool>();
else
else if (value.is_number_integer() || value.is_number_unsigned())
return value.get<long>();
else if (value.is_number_float())
return value.get<double>();
else {
assert(!value.is_number());
return value.dump();
}
}

} // namespace libscratchcpp
8 changes: 7 additions & 1 deletion src/scratch/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,15 @@ std::string List::toString() const
bool digits = true;

for (const auto &item : *this) {
if (item.type() == Value::Type::Integer) {
if (item.isValidNumber() && !item.toString().empty()) {
double doubleNum = item.toDouble();
long num = item.toLong();

if (doubleNum != num) {
digits = false;
break;
}

if (num < 0 || num >= 10) {
digits = false;
break;
Expand Down
Loading