Skip to content

Commit a66af5d

Browse files
authored
Use wmain for argv Unicode support (#19)
Fixes: https://jira.mongodb.org/browse/MONGOSH-797
1 parent 4d395dc commit a66af5d

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

resources/main-template.cc

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ 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();
174-
argv = uv_setup_args(argc, argv);
175-
std::vector<std::string> args(argv, argv + argc);
176174
std::vector<std::string> exec_args;
177175
std::vector<std::string> errors;
178176

@@ -205,6 +203,44 @@ int main(int argc, char** argv) {
205203
return ret;
206204
}
207205

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

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)