98 lines
2.7 KiB
C++
98 lines
2.7 KiB
C++
#ifndef DLANG_LIBS_DEBUG_HPP
|
|
#define DLANG_LIBS_DEBUG_HPP
|
|
|
|
#include "../vm.hpp"
|
|
#include "../helpers.hpp"
|
|
|
|
static void NativeStackDump(DlangVM* vm)
|
|
{
|
|
std::stringstream ss;
|
|
for (int i = 0; i < dlang_top(vm); i++)
|
|
{
|
|
auto value = vm->stack[i];
|
|
|
|
if (value.type == StackValueType::NIL)
|
|
printf("%i\t%s\n");
|
|
else if (value.type == StackValueType::Int)
|
|
printf("%i\t%s\n", i, std::to_string(std::get<double>(value.data)).c_str());
|
|
else if (value.type == StackValueType::Bool)
|
|
printf("%i\t%s\n", i, std::get<bool>(value.data) ? "True" : "False");
|
|
else if (value.type == StackValueType::String)
|
|
printf("%i\t%s\n", i, std::get<std::string>(value.data).c_str());
|
|
else if (value.type == StackValueType::Function)
|
|
{
|
|
auto func = std::get<std::shared_ptr<DlangFunction>>(value.data);
|
|
|
|
if (func->isNative)
|
|
printf("%i\tfunction\tnative\t0x%llX\n", i, func->nativeFunction);
|
|
else
|
|
{
|
|
printf("%i\tfunction\tnon-native\tlocation: %i\n", i, static_cast<int64_t>(func->bodyAddress));
|
|
printf("\tParameters:\n");
|
|
|
|
for (const auto& param : func->parameters)
|
|
printf("\t\t%s\n", param.c_str());
|
|
}
|
|
}
|
|
else if (value.type == StackValueType::Table)
|
|
printf("%i\ttable\t0x%llX\n", i, std::get<std::shared_ptr<DlangTable>>(value.data).get());
|
|
else
|
|
printf("%i\tunknown\n", i);
|
|
}
|
|
|
|
}
|
|
|
|
static void NativeGlobalsDump(DlangVM* vm)
|
|
{
|
|
std::stringstream ss;
|
|
for (int i = 0; i < vm->globals.top(); i++)
|
|
{
|
|
auto value = vm->globals.Get(i);
|
|
|
|
if (value.type == StackValueType::NIL)
|
|
printf("%i\t%s\n");
|
|
else if (value.type == StackValueType::Int)
|
|
printf("%i\t%s\n", i, std::to_string(std::get<double>(value.data)).c_str());
|
|
else if (value.type == StackValueType::Bool)
|
|
printf("%i\t%s\n", i, std::get<bool>(value.data) ? "True" : "False");
|
|
else if (value.type == StackValueType::String)
|
|
printf("%i\t%s\n", i, std::get<std::string>(value.data).c_str());
|
|
else if (value.type == StackValueType::Function)
|
|
{
|
|
auto func = std::get<std::shared_ptr<DlangFunction>>(value.data);
|
|
|
|
if (func->isNative)
|
|
printf("%i\tfunction\tnative\t0x%llX\n", i, func->nativeFunction);
|
|
else
|
|
{
|
|
printf("%i\tfunction\tnon-native\tlocation: %i\n", i, static_cast<int64_t>(func->bodyAddress));
|
|
printf("\tParameters:\n");
|
|
|
|
for (const auto& param : func->parameters)
|
|
printf("\t\t%s\n", param.c_str());
|
|
}
|
|
}
|
|
else if (value.type == StackValueType::Table)
|
|
printf("%i\ttable\t0x%llX\n", i, std::get<std::shared_ptr<DlangTable>>(value.data).get());
|
|
else
|
|
printf("%i\tunknown\n", i);
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inline void openlib_debug(DlangVM* vm)
|
|
{
|
|
dlang_newtable(vm);
|
|
|
|
dlang_pushcfunction(vm, NativeStackDump);
|
|
dlang_setfield(vm, "stackDump");
|
|
|
|
dlang_pushcfunction(vm, NativeGlobalsDump);
|
|
dlang_setfield(vm, "globalsDump");
|
|
|
|
dlang_setglobal(vm, "debug");
|
|
}
|
|
|
|
#endif |