Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_main.cc
Go to the documentation of this file.
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #include "node.h"
23 
24 #ifdef _WIN32
25 #include <VersionHelpers.h>
26 #include <WinError.h>
27 
28 int wmain(int argc, wchar_t *wargv[]) {
29  if (!IsWindows7OrGreater()) {
30  fprintf(stderr, "This application is only supported on Windows 7, "
31  "Windows Server 2008 R2, or higher.");
32  exit(ERROR_EXE_MACHINE_TYPE_MISMATCH);
33  }
34 
35  // Convert argv to to UTF8
36  char** argv = new char*[argc + 1];
37  for (int i = 0; i < argc; i++) {
38  // Compute the size of the required buffer
39  DWORD size = WideCharToMultiByte(CP_UTF8,
40  0,
41  wargv[i],
42  -1,
43  nullptr,
44  0,
45  nullptr,
46  nullptr);
47  if (size == 0) {
48  // This should never happen.
49  fprintf(stderr, "Could not convert arguments to utf8.");
50  exit(1);
51  }
52  // Do the actual conversion
53  argv[i] = new char[size];
54  DWORD result = WideCharToMultiByte(CP_UTF8,
55  0,
56  wargv[i],
57  -1,
58  argv[i],
59  size,
60  nullptr,
61  nullptr);
62  if (result == 0) {
63  // This should never happen.
64  fprintf(stderr, "Could not convert arguments to utf8.");
65  exit(1);
66  }
67  }
68  argv[argc] = nullptr;
69  // Now that conversion is done, we can finally start.
70  return node::Start(argc, argv);
71 }
72 #else
73 // UNIX
74 #ifdef __linux__
75 #include <elf.h>
76 #ifdef __LP64__
77 #define Elf_auxv_t Elf64_auxv_t
78 #else
79 #define Elf_auxv_t Elf32_auxv_t
80 #endif // __LP64__
81 extern char** environ;
82 #endif // __linux__
83 
84 namespace node {
85  extern bool linux_at_secure;
86 } // namespace node
87 
88 int main(int argc, char *argv[]) {
89 #if defined(__linux__)
90  char** envp = environ;
91  while (*envp++ != nullptr) {}
92  Elf_auxv_t* auxv = reinterpret_cast<Elf_auxv_t*>(envp);
93  for (; auxv->a_type != AT_NULL; auxv++) {
94  if (auxv->a_type == AT_SECURE) {
95  node::linux_at_secure = auxv->a_un.a_val;
96  break;
97  }
98  }
99 #endif
100  // Disable stdio buffering, it interacts poorly with printf()
101  // calls elsewhere in the program (e.g., any logging from V8.)
102  setvbuf(stdout, nullptr, _IONBF, 0);
103  setvbuf(stderr, nullptr, _IONBF, 0);
104  return node::Start(argc, argv);
105 }
106 #endif
int main(int argc, char *argv[])
Definition: node_main.cc:88
char ** environ
bool linux_at_secure
Definition: node.cc:251
int Start(Isolate *isolate, IsolateData *isolate_data, int argc, const char *const *argv, int exec_argc, const char *const *exec_argv)
Definition: node.cc:4536