Skip to content

Commit 74dfb1b

Browse files
update runtime
1 parent 51dd632 commit 74dfb1b

File tree

1 file changed

+16
-88
lines changed

1 file changed

+16
-88
lines changed

src/main/scala/wasm/StagedMiniWasm.scala

Lines changed: 16 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -916,82 +916,20 @@ private:
916916
};
917917
918918
struct Num {
919-
std::unique_ptr<Num_t> num_ptr;
920-
921-
// Constructions and destruction
922-
Num() : num_ptr(nullptr) {}
923-
924-
Num(std::unique_ptr<Num_t> num_ptr_) : num_ptr(std::move(num_ptr_)) {}
925-
926-
Num &operator=(const Num &other) {
927-
if (this != &other) {
928-
num_ptr = other.num_ptr ? other.num_ptr->clone() : nullptr;
929-
}
930-
return *this;
931-
}
932-
933-
Num(const Num &other) {
934-
num_ptr = other.num_ptr ? other.num_ptr->clone() : nullptr;
935-
}
936-
937-
Num(Num &&other) noexcept = default;
938-
939-
Num &operator=(Num &&other) noexcept = default;
940-
941-
~Num() = default;
942-
943-
int32_t toInt() const { return num_ptr->toInt(); }
944-
945-
int32_t toLong() const { return num_ptr->toLong(); }
946-
947-
void display() const { num_ptr->display(); }
948-
949-
Num operator+(const Num &other) const {
950-
if (dynamic_cast<I32V_t *>(num_ptr.get()) &&
951-
dynamic_cast<I32V_t *>(other.num_ptr.get())) {
952-
return Num(
953-
std::make_unique<I32V_t>(I32V_t(this->toInt() + other.toInt())));
954-
} else if (dynamic_cast<I64V_t *>(num_ptr.get()) &&
955-
dynamic_cast<I64V_t *>(other.num_ptr.get())) {
956-
return Num(
957-
std::make_unique<I64V_t>(I64V_t(this->toLong() + other.toLong())));
958-
} else {
959-
throw std::runtime_error("Operands are of different types");
960-
}
961-
}
962-
963-
Num operator-(const Num &other) const {
964-
if (dynamic_cast<I32V_t *>(num_ptr.get()) &&
965-
dynamic_cast<I32V_t *>(other.num_ptr.get())) {
966-
return Num(
967-
std::make_unique<I32V_t>(I32V_t(this->toInt() - other.toInt())));
968-
} else if (dynamic_cast<I64V_t *>(num_ptr.get()) &&
969-
dynamic_cast<I64V_t *>(other.num_ptr.get())) {
970-
return Num(
971-
std::make_unique<I64V_t>(I64V_t(this->toLong() - other.toLong())));
972-
} else {
973-
throw std::runtime_error("Operands are of different types");
974-
}
975-
}
976-
977-
bool operator==(const Num &other) const {
978-
if (dynamic_cast<I32V_t *>(num_ptr.get()) &&
979-
dynamic_cast<I32V_t *>(other.num_ptr.get())) {
980-
return this->toInt() == other.toInt();
981-
} else if (dynamic_cast<I64V_t *>(num_ptr.get()) &&
982-
dynamic_cast<I64V_t *>(other.num_ptr.get())) {
983-
return this->toLong() == other.toLong();
984-
} else {
985-
throw std::runtime_error("Operands are of different types");
986-
}
987-
}
988-
989-
bool operator!=(const Num &other) const { return !(this->operator==(other)); }
919+
Num(int64_t value) : value(value) {}
920+
Num() : value(0) {}
921+
int64_t value;
922+
int32_t toInt() { return static_cast<int32_t>(value); }
923+
924+
bool operator==(const Num &other) const { return value == other.value; }
925+
bool operator!=(const Num &other) const { return !(*this == other); }
926+
Num operator+(const Num &other) const { return Num(value + other.value); }
927+
Num operator-(const Num &other) const { return Num(value - other.value); }
990928
};
991929
992-
static Num I32V(int v) { return Num(std::make_unique<I32V_t>(v)); }
930+
static Num I32V(int v) { return v; }
993931
994-
static Num I64V(int64_t v) { return Num(std::make_unique<I64V_t>(v)); }
932+
static Num I64V(int64_t v) { return v; }
995933
996934
// struct Slice {
997935
// int32_t start;
@@ -1003,22 +941,15 @@ using Slice = std::vector<Num>;
1003941
1004942
class Stack_t {
1005943
public:
1006-
void push(Num &&num) {
1007-
assert(num.num_ptr != nullptr);
1008-
stack_.push_back(std::move(num));
1009-
}
944+
void push(Num &&num) { stack_.push_back(std::move(num)); }
1010945
1011-
void push(Num &num) {
1012-
assert(num.num_ptr != nullptr);
1013-
stack_.push_back(num);
1014-
}
946+
void push(Num &num) { stack_.push_back(num); }
1015947
1016948
Num pop() {
1017949
if (stack_.empty()) {
1018950
throw std::runtime_error("Stack underflow");
1019951
}
1020952
Num num = std::move(stack_.back());
1021-
assert(num.num_ptr != nullptr);
1022953
stack_.pop_back();
1023954
return num;
1024955
}
@@ -1034,7 +965,7 @@ public:
1034965
assert(index >= 0);
1035966
assert(index < stack_.size());
1036967
return stack_[index];
1037-
}
968+
}
1038969
1039970
int32_t size() { return stack_.size(); }
1040971
@@ -1060,7 +991,7 @@ public:
1060991
void print() {
1061992
std::cout << "Stack contents: " << std::endl;
1062993
for (const auto &num : stack_) {
1063-
num.display();
994+
std::cout << num.value << " ";
1064995
}
1065996
}
1066997
@@ -1103,7 +1034,6 @@ public:
11031034
11041035
Num get(std::int32_t index) {
11051036
auto ret = top()[index];
1106-
assert(ret.num_ptr != nullptr);
11071037
return ret;
11081038
}
11091039
@@ -1121,9 +1051,7 @@ public:
11211051
frames.push_back(frame);
11221052
}
11231053
1124-
void putAll(Slice slice) {
1125-
top().putAll(slice);
1126-
}
1054+
void putAll(Slice slice) { top().putAll(slice); }
11271055
11281056
private:
11291057
std::vector<Frame_t> frames;

0 commit comments

Comments
 (0)