Skip to content

Commit ac7ae56

Browse files
committed
Use wmain for argv Unicode support
Fixes: https://jira.mongodb.org/browse/MONGOSH-797
1 parent 85f9011 commit ac7ae56

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

resources/main-template.cc

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,9 @@ static int RunNodeInstance(MultiIsolatePlatform* platform,
169169
return exit_code;
170170
}
171171

172-
int main(int argc, char** argv) {
172+
static int BoxednodeMain(std::vector<std::string> args) {
173173
boxednode::InitializeOncePerProcess();
174174
argv = uv_setup_args(argc, argv);
175-
std::vector<std::string> args(argv, argv + argc);
176175
std::vector<std::string> exec_args;
177176
std::vector<std::string> errors;
178177

@@ -205,6 +204,41 @@ int main(int argc, char** argv) {
205204
return ret;
206205
}
207206

207+
#ifdef _WIN32
208+
int wmain(int argc, wchar_t* wargv[]) {
209+
// Convert argv to UTF8
210+
std::vector<std::string> args;
211+
for (int i = 0; i < argc; i++) {
212+
DWORD size = WideCharToMultiByte(CP_UTF8,
213+
0,
214+
wargv[i],
215+
-1,
216+
nullptr,
217+
0,
218+
nullptr,
219+
nullptr);
220+
assert(size > 0);
221+
std::string arg(size, '\0');
222+
DWORD result = WideCharToMultiByte(CP_UTF8,
223+
0,
224+
wargv[i],
225+
-1,
226+
&arg[0],
227+
size,
228+
nullptr,
229+
nullptr);
230+
assert(result > 0);
231+
}
232+
return BoxednodeMain(std::move(args));
233+
}
234+
235+
#else
236+
int main(int argc, char** argv) {
237+
std::vector<std::string> args(argv, argv + argc);
238+
return BoxednodeMain(std::move(args));
239+
}
240+
#endif
241+
208242
// The code below is mostly lifted directly from node.cc
209243
// TODO(addaleax): Expose these APIs on the Node.js side.
210244

test/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ describe('basic functionality', () => {
4040
assert.strictEqual(stdout, '42\n');
4141
}
4242

43+
{
44+
const { stdout } = await execFile(
45+
path.resolve(__dirname, `resources/example${exeSuffix}`), ['🐈'],
46+
{ encoding: 'utf8' });
47+
assert.strictEqual(stdout, '🐈\n');
48+
}
49+
4350
{
4451
const { stdout } = await execFile(
4552
path.resolve(__dirname, `resources/example${exeSuffix}`), ['process.argv.length'],

0 commit comments

Comments
 (0)