Skip to content

Commit 5269847

Browse files
authored
Merge pull request #520 from scratchcpp/value_fixes
Fix Value issues
2 parents 43e6f0b + 3b6d658 commit 5269847

File tree

11 files changed

+751
-196
lines changed

11 files changed

+751
-196
lines changed

include/scratchcpp/value.h

Lines changed: 210 additions & 78 deletions
Large diffs are not rendered by default.

src/blocks/looksblocks.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ void LooksBlocks::compileSwitchCostumeTo(Compiler *compiler)
350350
if (v.type() == Value::Type::Integer) {
351351
compiler->addConstValue(v.toLong() - 1);
352352
compiler->addFunctionCall(&switchCostumeToByIndex);
353+
} else {
354+
compiler->addInput(input);
355+
compiler->addFunctionCall(&switchCostumeTo);
353356
}
354357
}
355358
} else {
@@ -394,6 +397,9 @@ void LooksBlocks::compileSwitchBackdropTo(Compiler *compiler)
394397
if (v.type() == Value::Type::Integer) {
395398
compiler->addConstValue(v.toLong() - 1);
396399
compiler->addFunctionCall(&switchBackdropToByIndex);
400+
} else {
401+
compiler->addInput(input);
402+
compiler->addFunctionCall(&switchBackdropTo);
397403
}
398404
}
399405
} else {
@@ -433,6 +439,9 @@ void LooksBlocks::compileSwitchBackdropToAndWait(Compiler *compiler)
433439
if (v.type() == Value::Type::Integer) {
434440
compiler->addConstValue(v.toLong() - 1);
435441
compiler->addFunctionCall(&switchBackdropToByIndexAndWait);
442+
} else {
443+
compiler->addInput(input);
444+
compiler->addFunctionCall(&switchBackdropToAndWait);
436445
}
437446
}
438447
} else {
@@ -936,7 +945,7 @@ unsigned int LooksBlocks::switchCostumeTo(VirtualMachine *vm)
936945
else if (nameStr == "previous costume")
937946
previousCostume(vm);
938947
else {
939-
if (name->type() == Value::Type::Integer)
948+
if (name->isValidNumber())
940949
setCostumeByIndex(target, name->toLong() - 1);
941950
}
942951
} else
@@ -994,7 +1003,7 @@ void LooksBlocks::switchBackdropToImpl(VirtualMachine *vm)
9941003
else if (nameStr == "random backdrop") {
9951004
randomBackdropImpl(vm);
9961005
} else {
997-
if (name->type() == Value::Type::Integer)
1006+
if (name->isValidNumber())
9981007
setCostumeByIndex(stage, name->toLong() - 1);
9991008
}
10001009
} else

src/blocks/soundblocks.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ bool SoundBlocks::compilePlayCommon(Compiler *compiler, bool untilDone, bool *by
6666
if (index == -1) {
6767
Value v(value);
6868

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

@@ -166,7 +166,7 @@ Sound *SoundBlocks::playCommon(VirtualMachine *vm)
166166
return sound;
167167
}
168168

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

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

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

251251
if (sound) {

src/engine/virtualmachine_p.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
438438
DISPATCH();
439439

440440
OP(RANDOM) :
441-
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);
441+
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);
442442
else REPLACE_RET_VALUE(rng->randintDouble(READ_REG(0, 2)->toDouble(), READ_REG(1, 2)->toDouble()), 2);
443443
FREE_REGS(1);
444444
DISPATCH();
@@ -621,7 +621,10 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
621621
const Value *indexValue = READ_LAST_REG();
622622
size_t index;
623623
List *list = lists[*++pos];
624-
if (indexValue->isString()) {
624+
if (indexValue->isValidNumber()) {
625+
index = indexValue->toLong();
626+
FIX_LIST_INDEX(index, list->size());
627+
} else {
625628
const std::string &str = indexValue->toString();
626629
if (str == "last") {
627630
index = list->size();
@@ -633,9 +636,6 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
633636
index = size == 0 ? 0 : rng->randint(1, size);
634637
} else
635638
index = 0;
636-
} else {
637-
index = indexValue->toLong();
638-
FIX_LIST_INDEX(index, list->size());
639639
}
640640
if (index != 0)
641641
list->removeAt(index - 1);
@@ -652,7 +652,10 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
652652
const Value *indexValue = READ_REG(1, 2);
653653
size_t index;
654654
List *list = lists[*++pos];
655-
if (indexValue->isString()) {
655+
if (indexValue->isValidNumber()) {
656+
index = indexValue->toLong();
657+
FIX_LIST_INDEX(index, list->size());
658+
} else {
656659
const std::string &str = indexValue->toString();
657660
if (str == "last") {
658661
list->push_back(*READ_REG(0, 2));
@@ -662,9 +665,6 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
662665
index = size == 0 ? 1 : rng->randint(1, size);
663666
} else
664667
index = 0;
665-
} else {
666-
index = indexValue->toLong();
667-
FIX_LIST_INDEX(index, list->size());
668668
}
669669
if ((index != 0) || list->empty()) {
670670
if (list->empty())
@@ -681,7 +681,10 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
681681
const Value *indexValue = READ_REG(0, 2);
682682
size_t index;
683683
List *list = lists[*++pos];
684-
if (indexValue->isString()) {
684+
if (indexValue->isValidNumber()) {
685+
index = indexValue->toLong();
686+
FIX_LIST_INDEX(index, list->size());
687+
} else {
685688
std::string str = indexValue->toString();
686689
if (str == "last")
687690
index = list->size();
@@ -690,9 +693,6 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
690693
index = size == 0 ? 0 : rng->randint(1, size);
691694
} else
692695
index = 0;
693-
} else {
694-
index = indexValue->toLong();
695-
FIX_LIST_INDEX(index, list->size());
696696
}
697697
if (index != 0)
698698
list->operator[](index - 1) = *READ_REG(1, 2);
@@ -705,7 +705,10 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
705705
const Value *indexValue = READ_LAST_REG();
706706
size_t index;
707707
List *list = lists[*++pos];
708-
if (indexValue->isString()) {
708+
if (indexValue->isValidNumber()) {
709+
index = indexValue->toLong();
710+
FIX_LIST_INDEX(index, list->size());
711+
} else {
709712
std::string str = indexValue->toString();
710713
if (str == "last")
711714
index = list->size();
@@ -714,9 +717,6 @@ unsigned int *VirtualMachinePrivate::run(unsigned int *pos, bool reset)
714717
index = size == 0 ? 0 : rng->randint(1, size);
715718
} else
716719
index = 0;
717-
} else {
718-
index = indexValue->toLong();
719-
FIX_LIST_INDEX(index, list->size());
720720
}
721721
if (index == 0) {
722722
REPLACE_RET_VALUE("", 1);

src/internal/reader_common.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ Value jsonToValue(nlohmann::json value)
1515
return value.get<std::string>();
1616
else if (value.is_boolean())
1717
return value.get<bool>();
18-
else
18+
else if (value.is_number_integer() || value.is_number_unsigned())
19+
return value.get<long>();
20+
else if (value.is_number_float())
21+
return value.get<double>();
22+
else {
23+
assert(!value.is_number());
1924
return value.dump();
25+
}
2026
}
2127

2228
} // namespace libscratchcpp

src/scratch/list.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,15 @@ std::string List::toString() const
7474
bool digits = true;
7575

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

81+
if (doubleNum != num) {
82+
digits = false;
83+
break;
84+
}
85+
8086
if (num < 0 || num >= 10) {
8187
digits = false;
8288
break;

0 commit comments

Comments
 (0)