From 405e2d1517c6c79ebe9ca11e2940f411df0f449e Mon Sep 17 00:00:00 2001 From: dave caruso Date: Wed, 17 Jan 2024 17:33:02 -0800 Subject: [PATCH] windows: implement os.hostname (#8223) * windows: implement os.hostname * oops --------- Co-authored-by: Jarred Sumner --- .vscode/settings.json | 2 +- src/bun.js/WebKit | 2 +- src/bun.js/node/node_os.zig | 19 +++++++++++++++++-- src/deps/tinycc | 2 +- src/windows.zig | 6 ++++++ 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 43a86eafe4..9c6353d860 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -29,7 +29,7 @@ "editor.defaultFormatter": "esbenp.prettier-vscode" }, "zig.zls.enableInlayHints": false, - "zig.path": "${workspaceFolder}/.cache/zig/zig", + "zig.path": "${workspaceFolder}/.cache/zig/zig.exe", "git.ignoreSubmodules": true, "[jsx]": { "editor.defaultFormatter": "esbenp.prettier-vscode" diff --git a/src/bun.js/WebKit b/src/bun.js/WebKit index 9c501b9aa7..347037014a 160000 --- a/src/bun.js/WebKit +++ b/src/bun.js/WebKit @@ -1 +1 @@ -Subproject commit 9c501b9aa712b7959f80dc99491e8758c151c20e +Subproject commit 347037014ae069eed1c4f4687001a256949b124e diff --git a/src/bun.js/node/node_os.zig b/src/bun.js/node/node_os.zig index ddd647f2d3..cf84b3d506 100644 --- a/src/bun.js/node/node_os.zig +++ b/src/bun.js/node/node_os.zig @@ -353,8 +353,23 @@ pub const Os = struct { JSC.markBinding(@src()); if (comptime Environment.isWindows) { - globalThis.throwTODO("hostname() is not implemented on Windows"); - return .zero; + var name_buffer: [129:0]u16 = undefined; + if (bun.windows.GetHostNameW(&name_buffer, name_buffer.len) == 0) { + const str = bun.String.createUTF16(bun.sliceTo(&name_buffer, 0)); + defer str.deref(); + return str.toJS(globalThis); + } + + var result: std.os.windows.ws2_32.WSADATA = undefined; + if (std.os.windows.ws2_32.WSAStartup(0x202, &result) == 0) { + if (bun.windows.GetHostNameW(&name_buffer, name_buffer.len) == 0) { + const str = bun.String.createUTF16(bun.sliceTo(&name_buffer, 0)); + defer str.deref(); + return str.toJS(globalThis); + } + } + + return JSC.ZigString.init("unknown").withEncoding().toValueGC(globalThis); } var name_buffer: [bun.HOST_NAME_MAX]u8 = undefined; diff --git a/src/deps/tinycc b/src/deps/tinycc index 2d3ad9e0d3..ab631362d8 160000 --- a/src/deps/tinycc +++ b/src/deps/tinycc @@ -1 +1 @@ -Subproject commit 2d3ad9e0d32194ad7fd867b66ebe218dcc8cb5cd +Subproject commit ab631362d839333660a265d3084d8ff060b96753 diff --git a/src/windows.zig b/src/windows.zig index 857b935564..c02358da53 100644 --- a/src/windows.zig +++ b/src/windows.zig @@ -12,6 +12,7 @@ pub const LPCWSTR = windows.LPCWSTR; pub const LPSTR = windows.LPSTR; pub const WCHAR = windows.WCHAR; pub const LPCSTR = windows.LPCSTR; +pub const PWSTR = windows.PWSTR; pub const FALSE = windows.FALSE; pub const TRUE = windows.TRUE; pub const INVALID_HANDLE_VALUE = windows.INVALID_HANDLE_VALUE; @@ -2989,3 +2990,8 @@ pub fn getLastErrno() bun.C.E { }, }; } + +pub extern "kernel32" fn GetHostNameW( + lpBuffer: PWSTR, + nSize: c_int, +) BOOL;