Skip to content
This repository was archived by the owner on Mar 15, 2025. It is now read-only.

Commit 8cc23fd

Browse files
committed
Add State::dump function.
1 parent 953aceb commit 8cc23fd

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

dang-lua/include/dang-lua/State.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,11 @@ class IndexImpl : public IndexImplBase<TState> {
553553

554554
[[noreturn]] void typeError(const std::string& type_name) { this->state().typeError(index(), type_name); }
555555

556+
// --- Compiling ---
557+
558+
/// @brief Dumps the given function into a binary chunk.
559+
auto dump(bool strip = false) { return this->state().dump(index(), strip); }
560+
556561
// --- Calling ---
557562

558563
/// @brief Calls the element with an arbitrary number of arguments, returning a fixed number of results.
@@ -2554,7 +2559,7 @@ class StateBase {
25542559

25552560
// --- Compiling ---
25562561

2557-
/// @brief Compiles the given chunk and returns it.
2562+
/// @brief Compiles the given chunk and pushes it on the stack.
25582563
Expected<Arg> load(const Chunk& chunk)
25592564
{
25602565
assertPushableAuxiliary();
@@ -2567,6 +2572,30 @@ class StateBase {
25672572
return top().asResult();
25682573
}
25692574

2575+
/// @brief Dumps the given function into a binary chunk.
2576+
std::string dump(int index, bool strip = false)
2577+
{
2578+
auto writer = [](lua_State*, const void* data, std::size_t size, void* chunk) {
2579+
*static_cast<std::string*>(chunk) += std::string_view(static_cast<const char*>(data), size);
2580+
return 0;
2581+
};
2582+
2583+
auto is_top = isIndexTop(index);
2584+
if (!is_top) {
2585+
assertPushable();
2586+
lua_pushvalue(state_, index);
2587+
}
2588+
2589+
std::string chunk;
2590+
lua_dump(state_, writer, &chunk, strip);
2591+
2592+
if (!is_top)
2593+
lua_pop(state_, 1);
2594+
2595+
chunk.shrink_to_fit();
2596+
return chunk;
2597+
}
2598+
25702599
// --- Calling ---
25712600

25722601
/// @brief Calls the given function with the supplied arguments and returns a template specified number of results.

0 commit comments

Comments
 (0)