Skip to content

Commit b9db5e6

Browse files
committed
fix: compilation on Windows
1 parent 8298ff9 commit b9db5e6

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

jetpack/src/ModuleProvider.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace jetpack {
3434

3535
std::string source_path = module_path.string();
3636

37-
if (unlikely(source_path.rfind(base_path_, 0) != 0)) { // is not under working dir
37+
if (unlikely(source_path.rfind(base_path_.string(), 0) != 0)) { // is not under working dir
3838
std::cerr << fmt::format("path {} is not under working dir: {}", source_path, base_path_.string()) << std::endl;
3939
return std::nullopt;
4040
}

jetpack/src/ModuleResolver.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ namespace jetpack {
9898
}
9999
}
100100

101-
std::optional<ghc::filesystem::path> base_path = base_path_override.empty() ? FindPathOfPackageJson(target_p) : ghc::filesystem::path(base_path_override);
101+
std::optional<ghc::filesystem::path> base_path = base_path_override.empty() ? FindPathOfPackageJson(target_p.string()) : ghc::filesystem::path(base_path_override);
102102
if (unlikely(!base_path.has_value())) {
103103
ghc::filesystem::path p = target_p.parent_path();
104104
base_path = { p.string() };
@@ -108,7 +108,7 @@ namespace jetpack {
108108
auto fileProvider = std::make_shared<FileModuleProvider>(*base_path);
109109
providers_.push_back(fileProvider);
110110

111-
pBeginFromEntry(fileProvider, config, target_p.lexically_relative(*base_path));
111+
pBeginFromEntry(fileProvider, config, target_p.lexically_relative(*base_path).string());
112112
}
113113

114114
void ModuleResolver::BeginFromEntryString(const parser::Config& config,
@@ -227,10 +227,11 @@ namespace jetpack {
227227
return nullptr;
228228
}
229229

230-
mf->resolved_map[path] = match_result.second;
230+
std::string match_path_str = match_result.second.string();
231+
mf->resolved_map[path] = match_path_str;
231232

232233
bool isNew = false;
233-
Sp<ModuleFile> childMod = modules_table_.CreateNewIfNotExists(match_result.second, isNew);
234+
Sp<ModuleFile> childMod = modules_table_.CreateNewIfNotExists(match_path_str, isNew);
234235
if (!isNew) {
235236
mf->ref_mods.push_back(childMod);
236237
return childMod;

jetpack/src/utils/io/FileIO.cpp

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,29 @@ namespace jetpack::io {
151151
#ifdef _WIN32
152152
hFile = ::CreateFileA(path_.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
153153
if (hFile == INVALID_HANDLE_VALUE) {
154-
std::cerr << "open file failed: " << filename << ", " << ::GetLastError() << std::endl;
154+
std::cerr << fmt::format("open file {} failed: {}", path_, ::GetLastError()) << std::endl;
155155
return IOError::OpenFailed;
156156
}
157157

158158
DWORD file_size;
159159
::GetFileSize(hFile, &file_size);
160-
size = int64_t(file_size);
160+
current_size_ = int64_t(file_size);
161161

162-
hMapping = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, size, NULL);
162+
hMapping = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, current_size_, NULL);
163163
if (hMapping == NULL) {
164164
::CloseHandle(hFile);
165-
std::cerr << "read file failed: " << filename << ", " << ::GetLastError() << std::endl;
165+
std::cerr << fmt::format("read file {} failed: {}", path_, ::GetLastError()) << std::endl;
166166
return IOError::ReadFailed;
167167
}
168168

169169
void* p = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);
170170
if (p == NULL) {
171171
::CloseHandle(hMapping);
172172
::CloseHandle(hFile);
173-
std::cerr << "read file failed: " << filename << ", " << ::GetLastError() << std::endl;
173+
std::cerr << fmt::format("read file {} failed: {}", path_, ::GetLastError()) << std::endl;
174174
return IOError::ReadFailed;
175175
}
176-
mapped_mem = reinterpret_cast<uintptr_t>(p);
176+
mapped_mem_ = reinterpret_cast<unsigned char*>(p);
177177
return IOError::Ok;
178178
#else
179179
fd = ::open(path_.c_str(), O_RDWR | O_CREAT, 0644);
@@ -202,6 +202,46 @@ namespace jetpack::io {
202202
}
203203

204204
IOError FileWriterInternal::Resize(uint64_t size) {
205+
#ifdef _WIN32
206+
::UnmapViewOfFile(mapped_mem_);
207+
::CloseHandle(hMapping);
208+
209+
bool ok = false;
210+
211+
LARGE_INTEGER large_size;
212+
large_size.QuadPart = size;
213+
ok = ::SetFilePointerEx(hFile, large_size, NULL, FILE_BEGIN);
214+
if (!ok) {
215+
std::cerr << fmt::format("can not set file pointer of {}", path_) << std::endl;
216+
return IOError::ResizeFailed;
217+
}
218+
219+
ok = ::SetEndOfFile(hFile);
220+
if (!ok) {
221+
std::cerr << fmt::format("can not set end of {}", path_) << std::endl;
222+
return IOError::ResizeFailed;
223+
}
224+
225+
current_size_ = size;
226+
227+
hMapping = ::CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, current_size_, NULL);
228+
if (hMapping == NULL) {
229+
::CloseHandle(hFile);
230+
std::cerr << fmt::format("create file mapping {} failed: {}", path_, ::GetLastError()) << std::endl;
231+
return IOError::ResizeFailed;
232+
}
233+
234+
void* p = MapViewOfFile(hMapping, FILE_MAP_WRITE, 0, 0, 0);
235+
if (p == NULL) {
236+
::CloseHandle(hMapping);
237+
::CloseHandle(hFile);
238+
std::cerr << fmt::format("map file {} failed: {}", path_, ::GetLastError()) << std::endl;
239+
return IOError::ResizeFailed;
240+
}
241+
mapped_mem_ = reinterpret_cast<unsigned char*>(p);
242+
243+
return IOError::Ok;
244+
#else
205245
::munmap(mapped_mem_, current_size_);
206246

207247
if (::ftruncate(fd, size) != 0) {
@@ -217,6 +257,7 @@ namespace jetpack::io {
217257
}
218258

219259
return IOError::Ok;
260+
#endif
220261
}
221262

222263
IOError FileWriterInternal::EnsureSize(uint64_t size) {
@@ -253,9 +294,9 @@ namespace jetpack::io {
253294

254295
FileWriterInternal::~FileWriterInternal() {
255296
#ifdef _WIN32
297+
::UnmapViewOfFile(mapped_mem_);
256298
::CloseHandle(hMapping);
257-
::UnmapViewOfFile(hFile);
258-
::CloseHandle(hFile);
299+
::CloseHandle(hFile);
259300
#else
260301
if (likely(mapped_mem_ != nullptr)) {
261302
::munmap(mapped_mem_, current_size_);

0 commit comments

Comments
 (0)