Skip to content

Commit 97ceb93

Browse files
committed
fix: improve native system
1 parent d6f5a5e commit 97ceb93

File tree

2 files changed

+9
-42
lines changed

2 files changed

+9
-42
lines changed

src/module.cpp

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ Result<InitData> RustLanguageModule::Initialize(const Provider& provider, [[mayb
5353
}
5454

5555
void RustLanguageModule::Shutdown() {
56-
for (MemAddr* addr : _addresses) {
57-
*addr = nullptr;
58-
}
59-
_nativesMap.clear();
60-
_addresses.clear();
6156
_assemblies.clear();
6257
_provider.reset();
6358
}
@@ -70,10 +65,13 @@ bool RustLanguageModule::IsDebugBuild() {
7065
}
7166

7267
void RustLanguageModule::OnMethodExport(const Extension& plugin) {
73-
const auto& methods = plugin.GetMethodsData();
74-
_nativesMap.reserve(_nativesMap.size() + methods.size());
75-
for (const auto& [method, addr] :methods) {
76-
_nativesMap.try_emplace(std::format("{}.{}", plugin.GetName(), method.GetName()), addr);
68+
for (const auto& [method, addr] : plugin.GetMethodsData()) {
69+
auto variableName = std::format("__{}_{}", plugin.GetName(), method.GetName());
70+
for (const auto& assembly : _assemblies) {
71+
if (auto function = assembly->assembly->GetSymbol(variableName)) {
72+
*function->RCast<MemAddr*>() = addr;
73+
}
74+
}
7775
}
7876
}
7977

@@ -168,32 +166,10 @@ void RustLanguageModule::OnPluginEnd(const Extension& plugin) {
168166
plugin.GetUserData().RCast<AssemblyHolder*>()->endFunc();
169167
}
170168

171-
MemAddr RustLanguageModule::GetNativeMethod(std::string_view methodName) const {
172-
if (const auto it = _nativesMap.find(methodName); it != _nativesMap.end()) {
173-
return std::get<MemAddr>(*it);
174-
}
175-
_provider->Log(std::format(LOG_PREFIX "GetNativeMethod failed to find: '{}'", methodName), Severity::Fatal);
176-
return nullptr;
177-
}
178-
179-
void RustLanguageModule::GetNativeMethod(std::string_view methodName, MemAddr* addressDest) {
180-
if (const auto it = _nativesMap.find(methodName); it != _nativesMap.end()) {
181-
*addressDest = std::get<MemAddr>(*it);
182-
_addresses.emplace_back(addressDest);
183-
return;
184-
}
185-
_provider->Log(std::format(LOG_PREFIX "GetNativeMethod failed to find: '{}'", methodName), Severity::Fatal);
186-
}
187-
188169
namespace rustlm {
189170
RustLanguageModule g_rustlm;
190171
}
191172

192-
193-
void* GetMethodPtr(const char* mstr, size_t mlen) {
194-
return g_rustlm.GetNativeMethod(std::string_view(mstr, mlen));
195-
}
196-
197173
bool IsExtensionLoaded(const char* nstr, size_t nlen, const char* cstr, size_t clen) {
198174
std::string_view name(nstr, nlen);
199175
std::string_view constraint(cstr, clen);
@@ -442,8 +418,7 @@ void AssignVectorVector3(plg::vector<plg::vec3>* ptr, plg::vec3* arr, size_t len
442418
void AssignVectorVector4(plg::vector<plg::vec4>* ptr, plg::vec4* arr, size_t len) { AssignVector(ptr, arr, len); }
443419
void AssignVectorMatrix4x4(plg::vector<plg::mat4x4>* ptr, plg::mat4x4* arr, size_t len) { AssignVector(ptr, arr, len); }
444420

445-
const std::array<void*, 123> RustLanguageModule::_pluginApi = {
446-
reinterpret_cast<void*>(&::GetMethodPtr),
421+
const std::array<void*, 122> RustLanguageModule::_pluginApi = {
447422
reinterpret_cast<void*>(&::GetBaseDir),
448423
reinterpret_cast<void*>(&::GetExtensionsDir),
449424
reinterpret_cast<void*>(&::GetConfigsDir),

src/module.hpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,11 @@ namespace rustlm {
5151

5252
const std::unique_ptr<Provider>& GetProvider() { return _provider; }
5353

54-
MemAddr GetNativeMethod(std::string_view methodName) const;
55-
void GetNativeMethod(std::string_view methodName, MemAddr* addressDest);
56-
std::shared_ptr<Method> FindMethod(std::string_view name);
57-
5854
private:
5955
std::unique_ptr<Provider> _provider;
60-
6156
std::vector<std::unique_ptr<AssemblyHolder>> _assemblies;
62-
std::unordered_map<std::string, MemAddr, plg::string_hash, std::equal_to<>> _nativesMap;
63-
64-
std::vector<MemAddr*> _addresses;
6557

66-
static const std::array<void*, 123> _pluginApi;
58+
static const std::array<void*, 122> _pluginApi;
6759
};
6860

6961
extern RustLanguageModule g_golm;

0 commit comments

Comments
 (0)