Skip to content

Commit

Permalink
Fixed issues. [skip CI]
Browse files Browse the repository at this point in the history
* Improved logging by adding additional ImGui error messages.
* Fixed potential crash issues with C++ callbacks invoked in WASM.
  • Loading branch information
pigpigyyy committed Jan 6, 2025
1 parent 5ca29ab commit 2c5ee06
Show file tree
Hide file tree
Showing 23 changed files with 120 additions and 56 deletions.
2 changes: 2 additions & 0 deletions Source/3rdParty/imgui/imconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,5 @@ namespace ImGui

#define IMGUI_DISABLE_OBSOLETE_KEYIO
#define IMGUI_DEFINE_MATH_OPERATORS

#define IMGUI_DEBUG_PRINTF(_FMT,...) Dora::LogErrorThreaded(Dora::sprintf(_FMT, __VA_ARGS__))
10 changes: 6 additions & 4 deletions Source/Common/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,17 @@ class Logger : public NonCopyable {
}

void log(spdlog::level::level_enum level, const std::string& msg) {
_logger->log(level, msg);
auto m = Slice(msg).trimSpace().toString();
_logger->log(level, m);
}

void logAsync(spdlog::level::level_enum level, const std::string& msg) {
auto m = Slice(msg).trimSpace().toString();
if (Singleton<AsyncThread>::isDisposed()) {
_logger->log(level, msg);
_logger->log(level, m);
} else {
_thread->run([this, level, msg]() {
_logger->log(level, msg);
_thread->run([this, level, m]() {
_logger->log(level, m);
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions Source/Common/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ bool IsInLuaOrWasm();
#define WarnIf(...) DORA_DUMMY
#define ErrorIf(...) DORA_DUMMY
#else
#define Info(...) LogInfoThreaded(fmt::format(__VA_ARGS__))
#define Warn(...) LogWarnThreaded(fmt::format(__VA_ARGS__))
#define Error(...) LogErrorThreaded(fmt::format(__VA_ARGS__))
#define Info(...) Dora::LogInfoThreaded(fmt::format(__VA_ARGS__))
#define Warn(...) Dora::LogWarnThreaded(fmt::format(__VA_ARGS__))
#define Error(...) Dora::LogErrorThreaded(fmt::format(__VA_ARGS__))
#define InfoIf(cond, ...) \
if (cond) { \
Info(__VA_ARGS__); \
Expand Down
32 changes: 32 additions & 0 deletions Source/Common/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,36 @@ void threadLoop(const std::function<Job()>& work) {
SharedDirector.getScheduler()->schedule(loop(work));
}

std::string sprintf(const char* fmt, ...) {
int size = 1024;
std::vector<char> buffer(size);

va_list args;
va_start(args, fmt);
int n = vsnprintf(buffer.data(), size, fmt, args);
va_end(args);

if (n < 0) {
// vsnprintf 失败
return "";
} else if (n < size) {
// 格式化后的字符串已成功放入缓冲区
return std::string(buffer.data(), n);
} else {
// 缓冲区不足,需要重新分配
size = n + 1;
buffer.resize(size);

va_start(args, fmt);
n = vsnprintf(buffer.data(), size, fmt, args);
va_end(args);

if (n < 0 || n >= size) {
return "";
}

return std::string(buffer.data(), n);
}
}

NS_DORA_END
2 changes: 2 additions & 0 deletions Source/Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,6 @@ std::function<bool(double)> loop(const std::function<Job()>& work);
void thread(const std::function<Job()>& work);
void threadLoop(const std::function<Job()>& work);

std::string sprintf(const char* fmt, ...);

NS_DORA_END
2 changes: 1 addition & 1 deletion Source/Wasm/Dora/BodyWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void body_on_contact_filter(int64_t self, int32_t func0, int64_t stack0) {
args0->clear();
args0->push(body);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
});
}
int64_t body_new(int64_t def, int64_t world, int64_t pos, float rot) {
Expand Down
4 changes: 2 additions & 2 deletions Source/Wasm/Dora/ContentWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void content_zip_async(int64_t folder_path, int64_t zip_file, int32_t func0, int
args0->clear();
args0->push(file);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}, [func1, args1, deref1](bool success) {
args1->clear();
args1->push(success);
Expand All @@ -134,7 +134,7 @@ void content_unzip_async(int64_t zip_file, int64_t folder_path, int32_t func0, i
args0->clear();
args0->push(file);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}, [func1, args1, deref1](bool success) {
args1->clear();
args1->push(success);
Expand Down
4 changes: 2 additions & 2 deletions Source/Wasm/Dora/DirectorWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void director_schedule(int32_t func0, int64_t stack0) {
args0->clear();
args0->push(deltaTime);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
});
}
void director_schedule_posted(int32_t func0, int64_t stack0) {
Expand All @@ -56,7 +56,7 @@ void director_schedule_posted(int32_t func0, int64_t stack0) {
args0->clear();
args0->push(deltaTime);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
});
}
void director_push_camera(int64_t camera) {
Expand Down
2 changes: 1 addition & 1 deletion Source/Wasm/Dora/EntityGroupWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int64_t entitygroup_find(int64_t self, int32_t func0, int64_t stack0) {
args0->clear();
args0->push(e);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}));
}
int64_t entitygroup_new(int64_t components) {
Expand Down
2 changes: 1 addition & 1 deletion Source/Wasm/Dora/HttpClientWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void httpclient_download_async(int64_t url, int64_t full_path, float timeout, in
args0->push(current);
args0->push(total);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
});
}
} // extern "C"
Expand Down
2 changes: 1 addition & 1 deletion Source/Wasm/Dora/ModelWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int32_t model_each_node(int64_t self, int32_t func0, int64_t stack0) {
args0->clear();
args0->push(node);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}) ? 1 : 0;
}
int64_t model_new(int64_t filename) {
Expand Down
10 changes: 5 additions & 5 deletions Source/Wasm/Dora/NodeWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void node_schedule(int64_t self, int32_t func0, int64_t stack0) {
args0->clear();
args0->push(deltaTime);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
});
}
void node_unschedule(int64_t self) {
Expand Down Expand Up @@ -307,7 +307,7 @@ int32_t node_each_child(int64_t self, int32_t func0, int64_t stack0) {
args0->clear();
args0->push(child);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
}) ? 1 : 0;
}
int32_t node_traverse(int64_t self, int32_t func0, int64_t stack0) {
Expand All @@ -319,7 +319,7 @@ int32_t node_traverse(int64_t self, int32_t func0, int64_t stack0) {
args0->clear();
args0->push(child);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
}) ? 1 : 0;
}
int32_t node_traverse_all(int64_t self, int32_t func0, int64_t stack0) {
Expand All @@ -331,7 +331,7 @@ int32_t node_traverse_all(int64_t self, int32_t func0, int64_t stack0) {
args0->clear();
args0->push(child);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
}) ? 1 : 0;
}
float node_run_action_def(int64_t self, int64_t def, int32_t looped) {
Expand Down Expand Up @@ -425,7 +425,7 @@ void node_on_update(int64_t self, int32_t func0, int64_t stack0) {
args0->clear();
args0->push(deltaTime);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
});
}
int64_t node_new() {
Expand Down
4 changes: 2 additions & 2 deletions Source/Wasm/Dora/PhysicsWorldWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int32_t physicsworld_query(int64_t self, int64_t rect, int32_t func0, int64_t st
args0->clear();
args0->push(body);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}) ? 1 : 0;
}
int32_t physicsworld_raycast(int64_t self, int64_t start, int64_t stop, int32_t closest, int32_t func0, int64_t stack0) {
Expand All @@ -34,7 +34,7 @@ int32_t physicsworld_raycast(int64_t self, int64_t start, int64_t stop, int32_t
args0->push(point);
args0->push(normal);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}) ? 1 : 0;
}
void physicsworld_set_iterations(int64_t self, int32_t velocity_iter, int32_t position_iter) {
Expand Down
2 changes: 1 addition & 1 deletion Source/Wasm/Dora/Platformer/Behavior/LeafWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int64_t platformer_behavior_leaf_con(int64_t name, int32_t func0, int64_t stack0
args0->clear();
args0->push(r_cast<int64_t>(blackboard));
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}));
}
int64_t platformer_behavior_leaf_act(int64_t action_name) {
Expand Down
4 changes: 2 additions & 2 deletions Source/Wasm/Dora/Platformer/Decision/LeafWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int64_t platformer_decision_leaf_con(int64_t name, int32_t func0, int64_t stack0
args0->clear();
args0->push(unit);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}));
}
int64_t platformer_decision_leaf_act(int64_t action_name) {
Expand All @@ -41,7 +41,7 @@ int64_t platformer_decision_leaf_act_dynamic(int32_t func0, int64_t stack0) {
args0->clear();
args0->push(unit);
SharedWasmRuntime.invoke(func0);
return std::get<std::string>(args0->pop());
return args0->empty() ? ""s : std::get<std::string>(args0->pop());
}));
}
int64_t platformer_decision_leaf_accept() {
Expand Down
2 changes: 1 addition & 1 deletion Source/Wasm/Dora/Platformer/FaceWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int64_t platformer_face_with_func(int32_t func0, int64_t stack0, int64_t point,
return Object_From(Platformer::Face::create([func0, args0, deref0]() {
args0->clear();
SharedWasmRuntime.invoke(func0);
return s_cast<Node*>(std::get<Object*>(args0->pop()));
return args0->empty() ? Node::create() : s_cast<Node*>(std::get<Object*>(args0->pop()));
}, Vec2_From(point), scale, angle));
}
} // extern "C"
Expand Down
4 changes: 2 additions & 2 deletions Source/Wasm/Dora/Platformer/UnitActionWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ void platformer_unitaction_add(int64_t name, int32_t priority, float reaction, f
args0->push(owner);
args0->push(r_cast<int64_t>(action));
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(false);
}, [func1, args1, deref1](Platformer::Unit* owner, Platformer::UnitAction* action) {
args1->clear();
args1->push(owner);
args1->push(r_cast<int64_t>(action));
SharedWasmRuntime.invoke(func1);
return s_cast<Platformer::WasmActionUpdate*>(std::get<Object*>(args1->pop()));
return args1->empty()? Platformer::WasmActionUpdate::create([](Platformer::Unit*, Platformer::UnitAction*, float) { return true; }) : s_cast<Platformer::WasmActionUpdate*>(std::get<Object*>(args1->pop()));
}, [func2, args2, deref2](Platformer::Unit* owner, Platformer::UnitAction* action) {
args2->clear();
args2->push(owner);
Expand Down
2 changes: 1 addition & 1 deletion Source/Wasm/Dora/Platformer/WasmActionUpdateWasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int64_t platformer_wasmactionupdate_new(int32_t func0, int64_t stack0) {
args0->push(r_cast<int64_t>(action));
args0->push(deltaTime);
SharedWasmRuntime.invoke(func0);
return std::get<bool>(args0->pop());
return args0->pop_bool_or(true);
}));
}
} // extern "C"
Expand Down
14 changes: 13 additions & 1 deletion Source/Wasm/WasmRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ dora_val_t CallStack::pop() {
return var;
}

bool CallStack::pop_bool_or(bool def) {
if (_stack.empty()) {
return def;
}
auto var = _stack.front();
_stack.pop_front();
if (std::holds_alternative<bool>(var)) {
return std::get<bool>(var);
}
return def;
}

dora_val_t& CallStack::front() {
return _stack.front();
}
Expand Down Expand Up @@ -1939,7 +1951,7 @@ void WasmRuntime::invoke(int32_t funcId) {
DEFER(_callFromWasm--);
_callFunc->call(funcId);
} catch (std::runtime_error& e) {
Error("failed to execute wasm module due to: {}{}", e.what(), _runtime->get_error_message() == Slice::Empty ? Slice::Empty : ": "s + _runtime->get_error_message());
Error("failed to execute wasm callback due to: {}{}", e.what(), _runtime->get_error_message() == Slice::Empty ? Slice::Empty : ": "s + _runtime->get_error_message());
}
}

Expand Down
1 change: 1 addition & 0 deletions Source/Wasm/WasmRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CallStack {
bool empty() const;
dora_val_t pop();
dora_val_t& front();
bool pop_bool_or(bool def);
void clear();

private:
Expand Down
Loading

0 comments on commit 2c5ee06

Please sign in to comment.