Skip to content

Commit 3b6d658

Browse files
committed
Fix type casting in VirtualMachine
1 parent af4522a commit 3b6d658

File tree

2 files changed

+62
-22
lines changed

2 files changed

+62
-22
lines changed

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);

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,11 @@ TEST(VirtualMachineTest, OP_RANDOM)
412412
static unsigned int bytecode2[] = { OP_START, OP_CONST, 1, OP_CONST, 2, OP_RANDOM, OP_HALT };
413413
static unsigned int bytecode3[] = { OP_START, OP_CONST, 3, OP_CONST, 0, OP_RANDOM, OP_HALT };
414414
static unsigned int bytecode4[] = { OP_START, OP_CONST, 2, OP_CONST, 3, OP_RANDOM, OP_HALT };
415-
static Value constValues[] = { -45, 12, 6.05, -78.686 };
415+
static unsigned int bytecode5[] = { OP_START, OP_CONST, 4, OP_CONST, 5, OP_RANDOM, OP_HALT };
416+
static unsigned int bytecode6[] = { OP_START, OP_CONST, 5, OP_CONST, 6, OP_RANDOM, OP_HALT };
417+
static unsigned int bytecode7[] = { OP_START, OP_CONST, 7, OP_CONST, 4, OP_RANDOM, OP_HALT };
418+
static unsigned int bytecode8[] = { OP_START, OP_CONST, 6, OP_CONST, 7, OP_RANDOM, OP_HALT };
419+
static Value constValues[] = { -45, 12, 6.05, -78.686, "-45", "12", "6.05", "-78.686" };
416420

417421
VirtualMachinePrivate vm(nullptr, nullptr, nullptr, nullptr);
418422
vm.constValues = constValues;
@@ -455,6 +459,42 @@ TEST(VirtualMachineTest, OP_RANDOM)
455459

456460
ASSERT_EQ(vm.regCount, 1);
457461
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -28.648764);
462+
463+
EXPECT_CALL(rng, randint(-45, 12)).WillOnce(Return(-18));
464+
vm.bytecode = bytecode5;
465+
vm.pos = bytecode5;
466+
vm.regCount = 0;
467+
vm.run(vm.pos);
468+
469+
ASSERT_EQ(vm.regCount, 1);
470+
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -18);
471+
472+
EXPECT_CALL(rng, randintDouble(12, 6.05)).WillOnce(Return(3.486789));
473+
vm.bytecode = bytecode6;
474+
vm.pos = bytecode6;
475+
vm.regCount = 0;
476+
vm.run(vm.pos);
477+
478+
ASSERT_EQ(vm.regCount, 1);
479+
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), 3.486789);
480+
481+
EXPECT_CALL(rng, randintDouble(-78.686, -45)).WillOnce(Return(-59.468873));
482+
vm.bytecode = bytecode7;
483+
vm.pos = bytecode7;
484+
vm.regCount = 0;
485+
vm.run(vm.pos);
486+
487+
ASSERT_EQ(vm.regCount, 1);
488+
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -59.468873);
489+
490+
EXPECT_CALL(rng, randintDouble(6.05, -78.686)).WillOnce(Return(-28.648764));
491+
vm.bytecode = bytecode8;
492+
vm.pos = bytecode8;
493+
vm.regCount = 0;
494+
vm.run(vm.pos);
495+
496+
ASSERT_EQ(vm.regCount, 1);
497+
ASSERT_EQ(vm.regs[vm.regCount - 1]->toDouble(), -28.648764);
458498
}
459499

460500
TEST(VirtualMachineTest, OP_ROUND)
@@ -955,7 +995,7 @@ TEST(VirtualMachineTest, OP_LIST_DEL)
955995
0, OP_CONST, 9, OP_LIST_DEL, 0, OP_READ_LIST, 0, OP_CONST, 10, OP_LIST_DEL, 0, OP_READ_LIST, 0, OP_CONST, 11, OP_LIST_DEL, 0, OP_READ_LIST,
956996
0, OP_CONST, 12, OP_LIST_DEL, 0, OP_READ_LIST, 0, OP_CONST, 13, OP_LIST_DEL, 0, OP_READ_LIST, 0, OP_HALT
957997
};
958-
static Value constValues[] = { 3, 1, 6, 0, 7, -1, 9, Value::SpecialValue::NegativeInfinity, Value::SpecialValue::Infinity, Value::SpecialValue::NaN, "invalid", "last", "random", "all" };
998+
static Value constValues[] = { 3, 1, "6", 0, 7, -1, 9, Value::SpecialValue::NegativeInfinity, Value::SpecialValue::Infinity, Value::SpecialValue::NaN, "invalid", "last", "random", "all" };
959999
List list1("", "list1");
9601000
list1.push_back("a");
9611001
list1.push_back("b");
@@ -1027,7 +1067,7 @@ TEST(VirtualMachineTest, OP_LIST_INSERT)
10271067
0, OP_CONST, 0, OP_CONST, 10, OP_LIST_INSERT, 0, OP_READ_LIST, 0, OP_CONST, 0, OP_CONST, 11, OP_LIST_INSERT, 0, OP_READ_LIST, 0, OP_CONST,
10281068
0, OP_CONST, 12, OP_LIST_INSERT, 0, OP_READ_LIST, 0, OP_CONST, 0, OP_CONST, 13, OP_LIST_INSERT, 0, OP_READ_LIST, 0, OP_HALT
10291069
};
1030-
static Value constValues[] = { "new item", 3, 1, 10, 0, 12, -1, 14, Value::SpecialValue::NegativeInfinity, Value::SpecialValue::Infinity, Value::SpecialValue::NaN, "last", "random", "invalid" };
1070+
static Value constValues[] = { "new item", "3", 1, 10, 0, 12, -1, 14, Value::SpecialValue::NegativeInfinity, Value::SpecialValue::Infinity, Value::SpecialValue::NaN, "last", "random", "invalid" };
10311071
List list1("", "list1");
10321072
list1.push_back("a");
10331073
list1.push_back("b");
@@ -1079,7 +1119,7 @@ TEST(VirtualMachineTest, OP_LIST_REPLACE)
10791119
12, OP_CONST, 14, OP_LIST_REPLACE, 0, OP_READ_LIST, 0, OP_CONST, 13, OP_CONST, 0, OP_LIST_REPLACE, 0, OP_READ_LIST, 0, OP_HALT
10801120
};
10811121
static Value constValues[] = {
1082-
"new item", 3, 1, 8, 0, 9, -1, 12, Value::SpecialValue::NegativeInfinity, Value::SpecialValue::Infinity, Value::SpecialValue::NaN, "last", "random", "invalid", "test"
1122+
"new item", 3, "1", 8, 0, 9, -1, 12, Value::SpecialValue::NegativeInfinity, Value::SpecialValue::Infinity, Value::SpecialValue::NaN, "last", "random", "invalid", "test"
10831123
};
10841124
List list1("", "list1");
10851125
list1.push_back("a");
@@ -1128,7 +1168,7 @@ TEST(VirtualMachineTest, OP_LIST_GET_ITEM)
11281168
0, OP_CONST, 5, OP_LIST_GET_ITEM, 0, OP_CONST, 6, OP_LIST_GET_ITEM, 0, OP_CONST, 7, OP_LIST_GET_ITEM, 0, OP_CONST, 8, OP_LIST_GET_ITEM, 0, OP_CONST, 9, OP_LIST_GET_ITEM,
11291169
0, OP_CONST, 10, OP_LIST_GET_ITEM, 0, OP_CONST, 11, OP_LIST_GET_ITEM, 0, OP_CONST, 12, OP_LIST_GET_ITEM, 0, OP_HALT
11301170
};
1131-
static Value constValues[] = { 3, 1, 8, 0, 9, -1, 12, Value::SpecialValue::NegativeInfinity, Value::SpecialValue::Infinity, Value::SpecialValue::NaN, "last", "random", "invalid" };
1171+
static Value constValues[] = { 3, 1, "8", 0, 9, -1, 12, Value::SpecialValue::NegativeInfinity, Value::SpecialValue::Infinity, Value::SpecialValue::NaN, "last", "random", "invalid" };
11321172
List list1("", "list1");
11331173
list1.push_back("a");
11341174
list1.push_back("b");

0 commit comments

Comments
 (0)