Skip to content

Commit 794a93c

Browse files
committed
fix: fixed some issues with importing native modules on Windows operating system
1 parent f90de7d commit 794a93c

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

argon/vm/importer/dlwrap.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ using namespace argon::vm::datatype;
2222
using namespace argon::vm::importer;
2323

2424
DLHandle argon::vm::importer::OpenLibrary(const char *path, Error **out_error) {
25-
#ifdef _ARGON_PLATFORM_WINDOWS
26-
HMODULE handle;
25+
*out_error = nullptr;
2726

28-
if ((handle = LoadLibraryA(path)) == nullptr) {
27+
#ifdef _ARGON_PLATFORM_WINDOWS
28+
auto handle = LoadLibraryEx(path, nullptr, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR|LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
29+
if (handle == nullptr) {
2930
auto *winerr = ErrorGetMsgFromWinErr();
3031

3132
*out_error = ErrorNewFormat(kModuleImportError[0],
@@ -62,6 +63,8 @@ DLHandle argon::vm::importer::LoadSymbol(DLHandle handle, const char *sym_name)
6263
}
6364

6465
int argon::vm::importer::CloseLibrary(DLHandle handle, datatype::Error **out_error) {
66+
*out_error = nullptr;
67+
6568
#ifdef _ARGON_PLATFORM_WINDOWS
6669
if (FreeLibrary((HMODULE) handle) == 0) {
6770
auto *winerr = ErrorGetMsgFromWinErr();

argon/vm/importer/import.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ String *FindSourceInPaths(const Import *imp, String *mod_path, String *mod_name)
3838

3939
String *GetModuleName(String *path, const String *sep);
4040

41+
String *SanitizeModulePath(String *name, const String *path_sep);
42+
4143
void DelModuleFromCache(Import *imp, String *name);
4244

4345
// EOF
@@ -645,24 +647,33 @@ Module *argon::vm::importer::LoadModule(Import *imp, const char *name, ImportSpe
645647
Module *argon::vm::importer::LoadModule(Import *imp, String *name, ImportSpec *hint) {
646648
ImportModuleCacheEntry *entry;
647649

650+
if ((name = SanitizeModulePath(name, imp->path_sep)) == nullptr)
651+
return nullptr;
652+
648653
std::unique_lock lock(imp->lock);
649654

650655
if ((entry = imp->module_cache.Lookup(name)) != nullptr) {
651656
if (!AR_TYPEOF(entry->value, type_module_)) {
652657
ErrorFormat(kModuleImportError[0], kModuleImportError[2], ARGON_RAW_STRING(name));
658+
659+
Release(name);
653660
return nullptr;
654661
}
655662

663+
Release(name);
656664
return (Module *) IncRef(entry->value);
657665
}
658666

659667
auto *spec = Locate(imp, name, hint);
660668
if (spec == nullptr) {
661669
ErrorFormat(kModuleImportError[0], kModuleImportError[0], ARGON_RAW_STRING(name));
670+
671+
Release(name);
662672
return nullptr;
663673
}
664674

665675
if (!AddModule2Cache(imp, name, nullptr)) {
676+
Release(name);
666677
Release(spec);
667678
return nullptr;
668679
}
@@ -675,12 +686,15 @@ Module *argon::vm::importer::LoadModule(Import *imp, String *name, ImportSpec *h
675686

676687
if (mod != nullptr) {
677688
if (!AddModule2Cache(imp, name, mod)) {
689+
Release(name);
678690
Release(mod);
679691
return nullptr;
680692
}
681693
} else
682694
DelModuleFromCache(imp, name);
683695

696+
Release(name);
697+
684698
return mod;
685699
}
686700

@@ -819,6 +833,25 @@ String *GetModuleName(String *path, const String *sep) {
819833
return IncRef(path);
820834
}
821835

836+
String *SanitizeModulePath(String *name, const String *path_sep) {
837+
String *altsep;
838+
839+
#ifdef _ARGON_PLATFORM_WINDOWS
840+
altsep = StringIntern("/");
841+
#else
842+
altsep = StringIntern("\\");
843+
#endif
844+
845+
if (altsep == nullptr)
846+
return nullptr;
847+
848+
auto *res = StringReplace(name, altsep, path_sep, -1);
849+
850+
Release(altsep);
851+
852+
return res;
853+
}
854+
822855
void DelModuleFromCache(Import *imp, String *name) {
823856
auto *entry = imp->module_cache.Remove(name);
824857

0 commit comments

Comments
 (0)