diff --git a/ext/libriscv b/ext/libriscv index db73460..2f78139 160000 --- a/ext/libriscv +++ b/ext/libriscv @@ -1 +1 @@ -Subproject commit db7346086557a951fb516e2657380168cba5e439 +Subproject commit 2f78139c03918898ca71fc372099818cf6d548f9 diff --git a/src/elf/script_instance.cpp b/src/elf/script_instance.cpp index 66ddf59..6c0bc88 100644 --- a/src/elf/script_instance.cpp +++ b/src/elf/script_instance.cpp @@ -504,7 +504,7 @@ void ELFScriptInstance::reload(ELFScript *p_script) { auto old_script = std::move(this->script); this->script = Ref(p_script); this->update_methods(); - bool updated_program = false; + bool update_program = false; auto it = sandbox_instances.find(old_script.ptr()); if (it != sandbox_instances.end()) { @@ -513,26 +513,27 @@ void ELFScriptInstance::reload(ELFScript *p_script) { // Remove the old script instance and insert the new one sandbox_instances.erase(it); sandbox_instances.insert_or_assign(p_script, sandbox_ptr); - // Set the new program - sandbox_ptr->set_program(this->script); - updated_program = true; + update_program = true; } - if (!updated_program) { + if (!update_program) { Sandbox *sandbox_ptr = Object::cast_to(this->owner); if (sandbox_ptr != nullptr) { this->current_sandbox = sandbox_ptr; - sandbox_ptr->set_program(this->script); - updated_program = true; + update_program = true; } } - if (!updated_program) { + if (!update_program) { ERR_PRINT("ELFScriptInstance: Did not reload a Sandbox instance"); if constexpr (VERBOSE_LOGGING) { fprintf(stderr, "ELFScriptInstance: owner is instead a '%s'!\n", this->owner->get_class().utf8().get_data()); } } else { + // Set the new program, with potentially new parent Node + current_sandbox->set_tree_base(godot::Object::cast_to(this->owner)); + current_sandbox->set_program(this->script); + if constexpr (VERBOSE_LOGGING) { ERR_PRINT("ELFScriptInstance: reloaded " + Object::cast_to(owner)->get_name()); } diff --git a/src/sandbox.cpp b/src/sandbox.cpp index 2c40667..4395fec 100644 --- a/src/sandbox.cpp +++ b/src/sandbox.cpp @@ -159,7 +159,6 @@ std::vector Sandbox::create_sandbox_property_list() const { void Sandbox::constructor_initialize() { this->m_current_state = &this->m_states[0]; - this->m_tree_base = this; this->m_use_unboxed_arguments = SandboxProjectSettings::use_native_types(); // For each call state, reset the state for (size_t i = 0; i < this->m_states.size(); i++) { @@ -191,20 +190,17 @@ Sandbox::Sandbox() { dummy_machine = new machine_t{}; } this->constructor_initialize(); + this->m_tree_base = this; this->m_global_instance_count += 1; // In order to reduce checks we guarantee that this // class is well-formed at all times. this->reset_machine(); } -Sandbox::Sandbox(const PackedByteArray &buffer) { - this->constructor_initialize(); - this->m_global_instance_count += 1; +Sandbox::Sandbox(const PackedByteArray &buffer) : Sandbox() { this->load_buffer(buffer); } -Sandbox::Sandbox(Ref program) { - this->constructor_initialize(); - this->m_global_instance_count += 1; - this->set_program(std::move(program)); +Sandbox::Sandbox(Ref program) : Sandbox() { + this->set_program(program); } Sandbox::~Sandbox() { @@ -247,7 +243,7 @@ void Sandbox::set_program(Ref program) { property_values.push_back(value); } - this->m_program_data = std::move(program); + this->m_program_data = program; this->m_program_bytes = {}; // Unload program and reset the machine @@ -595,7 +591,7 @@ GuestVariant *Sandbox::setup_arguments(gaddr_t &sp, const Variant **args, int ar } Variant Sandbox::vmcall_internal(gaddr_t address, const Variant **args, int argc) { this->m_current_state += 1; - if (this->m_current_state >= this->m_states.end()) { + if (UNLIKELY(this->m_current_state >= this->m_states.end())) { ERR_PRINT("Too many VM calls in progress"); this->m_exceptions ++; this->m_global_exceptions ++; diff --git a/src/sandbox.h b/src/sandbox.h index 6143a17..8f16721 100644 --- a/src/sandbox.h +++ b/src/sandbox.h @@ -465,7 +465,7 @@ class Sandbox : public Node { // State stack, with the permanent (initial) state at index 0. // That means eg. static Variant values are held stored in the state at index 0, // so that they can be accessed by future VM calls, and not lost when a call ends. - std::array m_states; + std::array m_states; // Properties mutable std::vector m_properties;