Skip to content

Commit 9ccb9a6

Browse files
committed
fix: add plugin context
1 parent e2deaef commit 9ccb9a6

File tree

8 files changed

+30
-7
lines changed

8 files changed

+30
-7
lines changed

CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,12 @@ set(GOLM_PCH_FILE "src/pch.hpp")
5252
add_library(${PROJECT_NAME} SHARED ${GOLM_SOURCES})
5353
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
5454

55-
set(GOLM_LINK_LIBRARIES plugify::plugify plugify::plugify-assembly plugify::plugify-jit asmjit::asmjit cpptrace::cpptrace)
55+
set(GOLM_LINK_LIBRARIES plugify::plugify plugify::plugify-assembly plugify::plugify-jit asmjit::asmjit cpptrace::cpptrace)
5656

5757
if(NOT COMPILER_SUPPORTS_FORMAT)
5858
set(GOLM_LINK_LIBRARIES ${GOLM_LINK_LIBRARIES} fmt::fmt-header-only)
5959
endif()
6060

61-
target_include_directories(${PROJECT_NAME} PRIVATE ${dyncall_SOURCE_DIR})
62-
6361
target_link_libraries(${PROJECT_NAME} PRIVATE ${GOLM_LINK_LIBRARIES})
6462
target_precompile_headers(${PROJECT_NAME} PRIVATE ${GOLM_PCH_FILE})
6563

src/module.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ LoadResult GoLanguageModule::OnPluginLoad(PluginHandle plugin) {
8686
assemblyPath /= std::format("{}" GOLM_LIBRARY_SUFFIX, plugin.GetDescriptor().GetEntryPoint());
8787

8888
auto assembly = std::make_unique<Assembly>(assemblyPath, LoadFlag::Lazy | LoadFlag::Nodelete | LoadFlag::PinInMemory);
89-
if (!assembly) {
89+
if (!assembly->IsValid()) {
9090
return ErrorData{ std::format("Failed to load assembly: {}", assembly->GetError()) };
9191
}
9292

@@ -103,6 +103,7 @@ LoadResult GoLanguageModule::OnPluginLoad(PluginHandle plugin) {
103103
auto* const startFunc = assembly->GetFunctionByName("Plugify_PluginStart").RCast<StartFunc>();
104104
auto* const updateFunc = assembly->GetFunctionByName("Plugify_PluginUpdate").RCast<UpdateFunc>();
105105
auto* const endFunc = assembly->GetFunctionByName("Plugify_PluginEnd").RCast<EndFunc>();
106+
auto* const contextFunc = assembly->GetFunctionByName("Plugify_PluginContext").RCast<ContextFunc>();
106107

107108
std::vector<std::string_view> funcErrors;
108109

@@ -131,8 +132,10 @@ LoadResult GoLanguageModule::OnPluginLoad(PluginHandle plugin) {
131132
return ErrorData{ std::format("Not supported plugin api {}, max supported {}", resultVersion, kApiVersion) };
132133
}
133134

134-
auto data = _assemblies.emplace_back(std::make_unique<AssemblyHolder>(std::move(assembly), updateFunc, startFunc, endFunc, callFunc)).get();
135-
return LoadResultData{ std::move(methods), data, { updateFunc != nullptr, startFunc != nullptr, endFunc != nullptr, !exportedMethods.empty() } };
135+
auto [hasUpdate, hasStart, hasEnd, _] = contextFunc ? *(contextFunc()) : PluginContext{};
136+
137+
auto data = _assemblies.emplace_back(std::make_unique<AssemblyHolder>(std::move(assembly), updateFunc, startFunc, endFunc, contextFunc, callFunc)).get();
138+
return LoadResultData{ std::move(methods), data, { hasUpdate, hasStart, hasEnd, !exportedMethods.empty() } };
136139
}
137140

138141
void GoLanguageModule::OnPluginStart(PluginHandle plugin) {

src/module.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,25 @@ namespace golm {
4545

4646
constexpr int kApiVersion = 1;
4747

48+
struct PluginContext {
49+
bool hasUpdate{};
50+
bool hasStart{};
51+
bool hasEnd{};
52+
bool hasPanic{};
53+
};
54+
4855
using InitFunc = int (*)(GoSlice, int, const void*);
4956
using StartFunc = void (*)();
5057
using UpdateFunc = void (*)(float);
5158
using EndFunc = void (*)();
59+
using ContextFunc = PluginContext* (*)();
5260

5361
struct AssemblyHolder {
5462
std::unique_ptr<plugify::Assembly> assembly;
5563
UpdateFunc updateFunc;
5664
StartFunc startFunc;
5765
EndFunc endFunc;
66+
ContextFunc contextFunc;
5867
plugify::JitCallback::CallbackHandler callFunc;
5968
};
6069

-7.02 MB
Binary file not shown.
4.07 MB
Binary file not shown.

test/cross_call_worker/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ module plugify-plugin
22

33
go 1.24.0
44

5-
require github.com/untrustedmodders/go-plugify v0.0.0-20250302220657-ee0cab0c0300
5+
require github.com/untrustedmodders/go-plugify v1.0.5

test/cross_call_worker/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
github.com/untrustedmodders/go-plugify v0.0.0-20250302220657-ee0cab0c0300 h1:OuqOGtXXkka7HwdGyHboZDcao64kMSOApo+/UfJM/EQ=
22
github.com/untrustedmodders/go-plugify v0.0.0-20250302220657-ee0cab0c0300/go.mod h1:uPlNwuLP2QQmFGr5JfoU43Kwu84kI6e7CT7pNx+0ulE=
3+
github.com/untrustedmodders/go-plugify v1.0.1 h1:UsGfEjCkvnig4lLBooiQX212MLHwgla3gE2TiU573zE=
4+
github.com/untrustedmodders/go-plugify v1.0.1/go.mod h1:oXbAbZFAOtp53D3d9z5MyZHQ9+cfeuENFiacO/l6AWk=
5+
github.com/untrustedmodders/go-plugify v1.0.3 h1:9Z6U1DmArMMuJv7fiCygLyYkrrKj+fXMvaHY7aAVQdY=
6+
github.com/untrustedmodders/go-plugify v1.0.3/go.mod h1:c5w+u1HBAOxLPJzBwT0OBpfokLuZxGvnjtNSTOmynWc=
7+
github.com/untrustedmodders/go-plugify v1.0.4 h1:qKf7QxjFSlnurnDVev7rzLgNgA5QrahDSlZMXVBrm0U=
8+
github.com/untrustedmodders/go-plugify v1.0.4/go.mod h1:c5w+u1HBAOxLPJzBwT0OBpfokLuZxGvnjtNSTOmynWc=
9+
github.com/untrustedmodders/go-plugify v1.0.5 h1:UCeww6Z2kTmouCRsqqkM3PYDcRL4HzqVWNmzXsz738I=
10+
github.com/untrustedmodders/go-plugify v1.0.5/go.mod h1:c5w+u1HBAOxLPJzBwT0OBpfokLuZxGvnjtNSTOmynWc=

test/cross_call_worker/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/untrustedmodders/go-plugify"
77
"math"
88
"plugify-plugin/cross_call_master"
9+
"runtime/debug"
910
"strconv"
1011
"strings"
1112
"unsafe"
@@ -106,6 +107,10 @@ func init() {
106107
plugify.OnPluginEnd(func() {
107108
fmt.Println("Go: OnPluginEnd")
108109
})
110+
111+
plugify.OnPluginPanic(func() []byte {
112+
return debug.Stack() // workaround for could not import runtime/debug inside plugify package
113+
})
109114
}
110115

111116
func NoParamReturnVoid() {

0 commit comments

Comments
 (0)