diff --git a/src/bytecode/bytecode.hh b/src/bytecode/bytecode.hh index 4946946..f3483f1 100644 --- a/src/bytecode/bytecode.hh +++ b/src/bytecode/bytecode.hh @@ -27,6 +27,8 @@ public: // TODO - debugging info + [[nodiscard]] std::vector const& data() const { return byte_array_.data(); } + private: ByteArray byte_array_; static constexpr uint8_t VERSION = 1; diff --git a/src/bytecode/tests.cc b/src/bytecode/tests.cc index 6dda626..4547771 100644 --- a/src/bytecode/tests.cc +++ b/src/bytecode/tests.cc @@ -1,9 +1,12 @@ #include "gtest/gtest.h" +#include "gmock/gmock.h" #include #include #include "bytearray.hh" +#include "bytecodeprototype.hh" +#include "bytecode.hh" using namespace tyche; @@ -38,6 +41,45 @@ TEST(ByteArray, ByteArray) #undef TESTX } +TEST(Bytecode, Constants) +{ + BytecodePrototype bp; + bp.constants.emplace_back(42); + bp.constants.emplace_back("HELLO"); + + Bytecode bc(bp); + auto binary = bc.data(); + + ByteArray ba; + ba.append_int(42); + + std::vector expected = { + // header + 0x38, 0xc1, 0xb3, 0x74, // magic + 0x01, 0x00, 0x00, 0x00, // version + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + + // index + 0x30, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + + // constant indexes + 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, + + // constant values + 0x54, 'H', 'e', 'l', 'l', 'o', 0x00 + }; + + ASSERT_EQ(binary, expected); +} + int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv);