1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cerrno>
17 #include <cstdlib>
18 #include <iostream>
19 #include <getopt.h>
20
21 #include "ecmascript/mem/mem_common.h"
22 #include "js_runtime_options.h"
23
24 namespace panda::ecmascript {
25 const std::string PUBLIC_API COMMON_HELP_HEAD_MSG =
26 "Usage: panda [OPTIONS] [file1:file2:file3] [entrypoint] -- [arguments]\n"
27 "\n"
28 "optional arguments:\n";
29
30 const std::string PUBLIC_API STUB_HELP_HEAD_MSG =
31 "Usage: ark_stub_compiler [OPTIONS]\n"
32 "\n"
33 "optional arguments:\n";
34
35 const std::string PUBLIC_API HELP_OPTION_MSG =
36 "--aot-file: Path (file suffix not needed) to AOT output file. Default: \"aot_file\"\n"
37 "--ark-properties: set ark properties\n"
38 "--ark-bundle-name: set ark bundle name\n"
39 "--asm-interpreter: Enable asm interpreter. Default: true\n"
40 "--asm-opcode-disable-range: Opcode range when asm interpreter is enabled.\n"
41 "--assert-types: Enable type assertion for type inference tests. Default: false\n"
42 "--builtins-dts: builtins.d.abc file path for AOT.\n"
43 "--compiler-log: log Option For aot compiler and stub compiler,\n"
44 " \"none\": no log,\n"
45 " \"allllircirasm or all012\": print llIR file, CIR log and asm log for all methods,\n"
46 " \"allcir or all0\": print cir info for all methods,\n"
47 " \"allllir or all1\": print llir info for all methods,\n"
48 " \"allasm or all2\": print asm log for all methods,\n"
49 " \"alltype or all3\": print type infer log for all methods,\n"
50 " \"cerllircirasm or cer0112\": print llIR file, CIR log and asm log for certain method defined "
51 "in 'mlist-for-log',\n"
52 " \"cercir or cer0\": print cir info for certain method illustrated in 'mlist-for-log',\n"
53 " \"cerasm or cer2\": print asm log for certain method illustrated in 'mlist-for-log',\n"
54 " Default: \"none\"\n"
55 "--compiler-log-methods: specific method list for compiler log output, only used when compiler-log."
56 "Default: \"none\"\n"
57 "--enable-ark-tools: Enable ark tools to debug. Default: false\n"
58 "--trace-bc: enable tracing bytecode for aot runtime. Default: false\n"
59 "--trace-deopt: enable tracing deopt for aot runtime. Default: false\n"
60 "--deopt-threshold: set max count which aot function can occur deoptimization. Default: 10\n"
61 "--opt-code-profiler: enable opt code Bytecode Statistics for aot runtime. Default: false\n"
62 "--enable-force-gc: enable force gc when allocating object. Default: true\n"
63 "--enable-ic: switch of inline cache. Default: true\n"
64 "--enable-runtime-stat: enable statistics of runtime state. Default: false\n"
65 "--enable-type-lowering: enable TSTypeLowering and TypeLowering for aot runtime. Default:true\n"
66 "--entry-point: full name of entrypoint function or method. Default: _GLOBAL::func_main_0\n"
67 "--force-full-gc: if true trigger full gc, else trigger semi and old gc. Default: true\n"
68 "--framework-abc-file: snapshot file. Default: \"strip.native.min.abc\"\n"
69 "--gcThreadNum: set gcThreadNum. Default: 7\n"
70 "--heap-size-limit: Max heap size. Default: 512MB\n"
71 "--help: Print this message and exit\n"
72 "--icu-data-path: Path to generated icu data file. Default: \"default\"\n"
73 "--IsWorker: whether is worker vm. Default: false\n"
74 "--log-level: Log level. Possible values: [\"debug\", \"info\", \"warning\", \"error\", \"fatal\"].\n"
75 "--log-components: Enable logs from specified components. Possible values: [\"all\", \"gc\", \"ecma\","
76 "\"interpreter\", \"debugger\", \"compiler\", \"builtins\", \"all\"]. Default: [\"all\"]\n"
77 "--log-debug: Enable debug or above logs from specified components. Possible values: [\"all\", \"gc\", \"ecma\","
78 "\"interpreter\", \"debugger\", \"compiler\", \"builtins\", \"all\"]. Default: [\"all\"]\n"
79 "--log-error: Enable error or above logs from specified components. Possible values: [\"all\", \"gc\", \"ecma\","
80 "\"interpreter\", \"debugger\", \"compiler\", \"builtins\", \"all\"]. Default: [\"all\"]\n"
81 "--log-fatal: Enable fatal logs from specified components. Possible values: [\"all\", \"gc\", \"ecma\","
82 "\"interpreter\", \"debugger\", \"compiler\", \"builtins\", \"all\"]. Default: [\"all\"]\n"
83 "--log-info: Enable info or above logs from specified components. Possible values: [\"all\", \"gc\", \"ecma\","
84 "\"interpreter\", \"debugger\", \"compiler\", \"builtins\", \"all\"]. Default: [\"all\"]\n"
85 "--log-warning: Enable warning or above logs from specified components. Possible values: [\"all\", \"gc\","
86 "\"ecma\", \"interpreter\", \"debugger\", \"compiler\", \"builtins\", \"all\"]. Default: [\"all\"]\n"
87 "--longPauseTime: set longPauseTime. Default: 40ms\n"
88 "--maxAotMethodSize: enable aot to skip too large method. Default size: 32 KB\n"
89 "--maxNonmovableSpaceCapacity: set max nonmovable space capacity\n"
90 "--merge-abc: abc file is merge abc. Default: false\n"
91 "--opt-level: Optimization level configuration on llvm back end. Default: \"3\"\n"
92 "--options: Print compiler and runtime options\n"
93 "--print-any-types: Enable TypeFilter to print any types after type inference. Default: false\n"
94 "--reloc-mode: Relocation configuration on llvm back end. Default: \"2\"\n"
95 "--serializer-buffer-size-limit: Max serializer buffer size used by the VM. Default: 2GB\n"
96 "--snapshot-file: snapshot file. Default: \"/system/etc/snapshot\"\n"
97 "--startup-time: Print the start time of command execution. Default: false\n"
98 "--stub-file: Path of file includes common stubs module compiled by stub compiler. Default: \"stub.an\"\n"
99 "--enable-pgo-profiler: Enable pgo profiler to sample jsfunction call and output to file. Default: false\n"
100 "--pgo-hotness-threshold: set hotness threshold for pgo in aot compiler. Default: 2\n"
101 "--pgo-profiler-path: The pgo sampling profiler file output dir for application or ark_js_vm runtime,"
102 "or the sampling profiler file input dir for AOT PGO compiler. Default: ""\n"
103 "--target-triple: target triple for aot compiler or stub compiler.\n"
104 "--enable-print-execute-time: enable print execute pandafile spent time\"\n"
105 " Possible values: [\"x86_64-unknown-linux-gnu\", \"arm-unknown-linux-gnu\", "
106 "\"aarch64-unknown-linux-gnu\"].\n"
107 " Default: \"x86_64-unknown-linux-gnu\"\n";
108
109 const std::string PUBLIC_API HELP_TAIL_MSG =
110 "Tail arguments:\n"
111 "files: path to pandafiles\n";
112
ParseCommand(const int argc,const char ** argv)113 bool JSRuntimeOptions::ParseCommand(const int argc, const char **argv)
114 {
115 const struct option longOptions[] = {
116 {"aot-file", required_argument, nullptr, OPTION_AOT_FILE},
117 {"ark-properties", required_argument, nullptr, OPTION_ARK_PROPERTIES},
118 {"ark-bundleName", required_argument, nullptr, OPTION_ARK_BUNDLENAME},
119 {"asm-interpreter", required_argument, nullptr, OPTION_ENABLE_ASM_INTERPRETER},
120 {"asm-opcode-disable-range", required_argument, nullptr, OPTION_ASM_OPCODE_DISABLE_RANGE},
121 {"assert-types", required_argument, nullptr, OPTION_ASSERT_TYPES},
122 {"builtins-dts", required_argument, nullptr, OPTION_BUILTINS_DTS},
123 {"compiler-log", required_argument, nullptr, OPTION_COMPILER_LOG_OPT},
124 {"compiler-log-methods", required_argument, nullptr, OPTION_COMPILER_LOG_METHODS},
125 {"compiler-log-snapshot", required_argument, nullptr, OPTION_COMPILER_LOG_SNAPSHOT},
126 {"compiler-log-time", required_argument, nullptr, OPTION_COMPILER_LOG_TIME},
127 {"enable-ark-tools", required_argument, nullptr, OPTION_ENABLE_ARK_TOOLS},
128 {"trace-bc", required_argument, nullptr, OPTION_TRACE_BC},
129 {"trace-deopt", required_argument, nullptr, OPTION_TRACE_DEOPT},
130 {"deopt-threshold", required_argument, nullptr, OPTION_DEOPT_THRESHOLD},
131 {"opt-code-profiler", required_argument, nullptr, OPTION_OPT_CODE_PROFILER},
132 {"enable-force-gc", required_argument, nullptr, OPTION_ENABLE_FORCE_GC},
133 {"enable-ic", required_argument, nullptr, OPTION_ENABLE_IC},
134 {"enable-runtime-stat", required_argument, nullptr, OPTION_ENABLE_RUNTIME_STAT},
135 {"enable-type-lowering", required_argument, nullptr, OPTION_ENABLE_TYPE_LOWERING},
136 {"entry-point", required_argument, nullptr, OPTION_ENTRY_POINT},
137 {"force-full-gc", required_argument, nullptr, OPTION_FORCE_FULL_GC},
138 {"framework-abc-file", required_argument, nullptr, OPTION_FRAMEWORK_ABC_FILE},
139 {"gcThreadNum", required_argument, nullptr, OPTION_GC_THREADNUM},
140 {"heap-size-limit", required_argument, nullptr, OPTION_HEAP_SIZE_LIMIT},
141 {"help", no_argument, nullptr, OPTION_HELP},
142 {"icu-data-path", required_argument, nullptr, OPTION_ICU_DATA_PATH},
143 {"IsWorker", required_argument, nullptr, OPTION_IS_WORKER},
144 {"log-components", required_argument, nullptr, OPTION_LOG_COMPONENTS},
145 {"log-debug", required_argument, nullptr, OPTION_LOG_DEBUG},
146 {"log-error", required_argument, nullptr, OPTION_LOG_ERROR},
147 {"log-fatal", required_argument, nullptr, OPTION_LOG_FATAL},
148 {"log-info", required_argument, nullptr, OPTION_LOG_INFO},
149 {"log-level", required_argument, nullptr, OPTION_LOG_LEVEL},
150 {"log-warning", required_argument, nullptr, OPTION_LOG_WARNING},
151 {"longPauseTime", required_argument, nullptr, OPTION_LONG_PAUSE_TIME},
152 {"maxAotMethodSize", required_argument, nullptr, OPTION_MAX_AOTMETHODSIZE},
153 {"maxNonmovableSpaceCapacity", required_argument, nullptr, OPTION_MAX_NONMOVABLE_SPACE_CAPACITY},
154 {"merge-abc", required_argument, nullptr, OPTION_MERGE_ABC},
155 {"opt-level", required_argument, nullptr, OPTION_ASM_OPT_LEVEL},
156 {"options", no_argument, nullptr, OPTION_OPTIONS},
157 {"print-any-types", required_argument, nullptr, OPTION_PRINT_ANY_TYPES},
158 {"reloc-mode", required_argument, nullptr, OPTION_RELOCATION_MODE},
159 {"serializer-buffer-size-limit", required_argument, nullptr, OPTION_SERIALIZER_BUFFER_SIZE_LIMIT},
160 {"snapshot-file", required_argument, nullptr, OPTION_SNAPSHOT_FILE},
161 {"startup-time", required_argument, nullptr, OPTION_STARTUP_TIME},
162 {"stub-file", required_argument, nullptr, OPTION_STUB_FILE},
163 {"target-triple", required_argument, nullptr, OPTION_TARGET_TRIPLE},
164 {"enable-print-execute-time", required_argument, nullptr, OPTION_PRINT_EXECUTE_TIME},
165 {"enable-pgo-profiler", required_argument, nullptr, OPTION_ENABLE_PGO_PROFILER},
166 {"pgo-profiler-path", required_argument, nullptr, OPTION_PGO_PROFILER_PATH},
167 {"pgo-hotness-threshold", required_argument, nullptr, OPTION_PGO_HOTNESS_THRESHOLD},
168 {nullptr, 0, nullptr, 0},
169 };
170
171 int index = 0;
172 opterr = 0;
173 bool ret = false;
174 int option = 0;
175 arg_list_t argListStr;
176 uint32_t argUint32;
177 int argInt;
178 bool argBool;
179 static std::string COLON = ":";
180
181 if (argc <= 1) {
182 return true;
183 }
184
185 while (true) {
186 option = getopt_long_only(argc, const_cast<char **>(argv), "", longOptions, &index);
187 LOG_ECMA(INFO) << "option: " << option << ", optopt: " << optopt << ", optind: " << optind;
188
189 if (optind <= 0 || optind > argc) {
190 return false;
191 }
192
193 if (option == -1) {
194 return true;
195 }
196
197 // unknown option or required_argument option has no argument
198 if (option == '?') {
199 ret = SetDefaultValue(const_cast<char *>(argv[optind - 1]));
200 if (ret) {
201 continue;
202 } else {
203 return ret;
204 }
205 }
206
207 WasSet(option);
208 switch (option) {
209 case OPTION_AOT_FILE:
210 SetAOTOutputFile(optarg);
211 break;
212 case OPTION_ARK_PROPERTIES:
213 ret = ParseIntParam("ark-properties", &argInt);
214 if (ret) {
215 SetArkProperties(argInt);
216 } else {
217 return false;
218 }
219 break;
220 case OPTION_ARK_BUNDLENAME:
221 SetArkBundleName(optarg);
222 break;
223 case OPTION_ENABLE_ASM_INTERPRETER:
224 ret = ParseBoolParam(&argBool);
225 if (ret) {
226 SetEnableAsmInterpreter(argBool);
227 } else {
228 return false;
229 }
230 break;
231 case OPTION_ASM_OPCODE_DISABLE_RANGE:
232 SetAsmOpcodeDisableRange(optarg);
233 break;
234 case OPTION_ASSERT_TYPES:
235 ret = ParseBoolParam(&argBool);
236 if (ret) {
237 SetAssertTypes(argBool);
238 } else {
239 return false;
240 }
241 break;
242 case OPTION_BUILTINS_DTS:
243 SetBuiltinsDTS(optarg);
244 break;
245 case OPTION_COMPILER_LOG_OPT:
246 SetCompilerLogOption(optarg);
247 break;
248 case OPTION_COMPILER_LOG_SNAPSHOT:
249 ret = ParseBoolParam(&argBool);
250 if (ret) {
251 SetCompilerLogSnapshot(argBool);
252 } else {
253 return false;
254 }
255 break;
256 case OPTION_COMPILER_LOG_TIME:
257 ret = ParseBoolParam(&argBool);
258 if (ret) {
259 SetCompilerLogTime(argBool);
260 } else {
261 return false;
262 }
263 break;
264 case OPTION_ENABLE_ARK_TOOLS:
265 ret = ParseBoolParam(&argBool);
266 if (ret) {
267 SetEnableArkTools(argBool);
268 } else {
269 return false;
270 }
271 break;
272 case OPTION_TRACE_BC:
273 ret = ParseBoolParam(&argBool);
274 if (ret) {
275 SetTraceBc(argBool);
276 } else {
277 return false;
278 }
279 break;
280 case OPTION_TRACE_DEOPT:
281 ret = ParseBoolParam(&argBool);
282 if (ret) {
283 SetTraceDeopt(argBool);
284 } else {
285 return false;
286 }
287 break;
288 case OPTION_OPT_CODE_PROFILER:
289 ret = ParseBoolParam(&argBool);
290 if (ret) {
291 SetOptCodeProfiler(argBool);
292 } else {
293 return false;
294 }
295 break;
296 case OPTION_ENABLE_FORCE_GC:
297 ret = ParseBoolParam(&argBool);
298 if (ret) {
299 SetEnableForceGC(argBool);
300 } else {
301 return false;
302 }
303 break;
304 case OPTION_FORCE_FULL_GC:
305 ret = ParseBoolParam(&argBool);
306 if (ret) {
307 SetForceFullGC(argBool);
308 } else {
309 return false;
310 }
311 break;
312 case OPTION_ENABLE_IC:
313 ret = ParseBoolParam(&argBool);
314 if (ret) {
315 SetEnableIC(argBool);
316 } else {
317 return false;
318 }
319 break;
320 case OPTION_ENABLE_RUNTIME_STAT:
321 ret = ParseBoolParam(&argBool);
322 if (ret) {
323 SetEnableRuntimeStat(argBool);
324 } else {
325 return false;
326 }
327 break;
328 case OPTION_FRAMEWORK_ABC_FILE:
329 SetFrameworkAbcFile(optarg);
330 break;
331 case OPTION_GC_THREADNUM:
332 ret = ParseUint32Param("gcThreadNum", &argUint32);
333 if (ret) {
334 SetGcThreadNum(argUint32);
335 } else {
336 return false;
337 }
338 break;
339 case OPTION_HEAP_SIZE_LIMIT:
340 ret = ParseUint32Param("heap-size-limit", &argUint32);
341 if (ret) {
342 SetHeapSizeLimit(argUint32);
343 } else {
344 return false;
345 }
346 break;
347 case OPTION_HELP:
348 return false;
349 case OPTION_ICU_DATA_PATH:
350 SetIcuDataPath(optarg);
351 break;
352 case OPTION_IS_WORKER:
353 ret = ParseBoolParam(&argBool);
354 if (ret) {
355 SetIsWorker(argBool);
356 } else {
357 return false;
358 }
359 break;
360 case OPTION_LOG_COMPONENTS:
361 ParseListArgParam(optarg, &argListStr, COLON);
362 SetLogComponents(argListStr);
363 break;
364 case OPTION_LOG_DEBUG:
365 ParseListArgParam(optarg, &argListStr, COLON);
366 SetLogDebug(argListStr);
367 break;
368 case OPTION_LOG_ERROR:
369 ParseListArgParam(optarg, &argListStr, COLON);
370 SetLogError(argListStr);
371 break;
372 case OPTION_LOG_FATAL:
373 ParseListArgParam(optarg, &argListStr, COLON);
374 SetLogFatal(argListStr);
375 break;
376 case OPTION_LOG_INFO:
377 ParseListArgParam(optarg, &argListStr, COLON);
378 SetLogInfo(argListStr);
379 break;
380 case OPTION_LOG_LEVEL:
381 SetLogLevel(optarg);
382 break;
383 case OPTION_LOG_WARNING:
384 ParseListArgParam(optarg, &argListStr, COLON);
385 SetLogWarning(argListStr);
386 break;
387 case OPTION_LONG_PAUSE_TIME:
388 ret = ParseUint32Param("longPauseTime", &argUint32);
389 if (ret) {
390 SetLongPauseTime(argUint32);
391 } else {
392 return false;
393 }
394 break;
395 case OPTION_MAX_AOTMETHODSIZE:
396 ret = ParseUint32Param("maxAotMethodSize", &argUint32);
397 if (ret) {
398 SetMaxAotMethodSize(argUint32);
399 } else {
400 return false;
401 }
402 break;
403 case OPTION_MAX_NONMOVABLE_SPACE_CAPACITY:
404 ret = ParseUint32Param("maxNonmovableSpaceCapacity", &argUint32);
405 if (ret) {
406 SetMaxNonmovableSpaceCapacity(argUint32);
407 } else {
408 return false;
409 }
410 break;
411 case OPTION_COMPILER_LOG_METHODS:
412 SetMethodsListForLog(optarg);
413 break;
414 case OPTION_ASM_OPT_LEVEL:
415 ret = ParseUint32Param("opt-level", &argUint32);
416 if (ret) {
417 SetOptLevel(argUint32);
418 } else {
419 return false;
420 }
421 break;
422 case OPTION_PRINT_ANY_TYPES:
423 ret = ParseBoolParam(&argBool);
424 if (ret) {
425 SetPrintAnyTypes(argBool);
426 } else {
427 return false;
428 }
429 break;
430 case OPTION_PRINT_EXECUTE_TIME:
431 ret = ParseBoolParam(&argBool);
432 if (ret) {
433 SetEnablePrintExecuteTime(argBool);
434 } else {
435 return false;
436 }
437 break;
438 case OPTION_ENABLE_PGO_PROFILER:
439 ret = ParseBoolParam(&argBool);
440 if (ret) {
441 SetEnablePGOProfiler(argBool);
442 } else {
443 return false;
444 }
445 break;
446 case OPTION_PGO_PROFILER_PATH:
447 if (*optarg == '\0') {
448 return false;
449 }
450 SetPGOProfilerPath(optarg);
451 break;
452 case OPTION_PGO_HOTNESS_THRESHOLD:
453 ret = ParseUint32Param("pgo-hotness-threshold", &argUint32);
454 if (ret) {
455 SetPGOHotnessThreshold(argUint32);
456 } else {
457 return false;
458 }
459 break;
460 case OPTION_RELOCATION_MODE:
461 ret = ParseUint32Param("reloc-mode", &argUint32);
462 if (ret) {
463 SetRelocMode(argUint32);
464 } else {
465 return false;
466 }
467 break;
468 case OPTION_SERIALIZER_BUFFER_SIZE_LIMIT:
469 uint64_t argUInt64;
470 ret = ParseUint64Param("serializer-buffer-size-limit", &argUInt64);
471 if (ret) {
472 SetSerializerBufferSizeLimit(argUInt64);
473 } else {
474 return false;
475 }
476 break;
477 case OPTION_SNAPSHOT_FILE:
478 SetSnapshotFile(optarg);
479 break;
480 case OPTION_STARTUP_TIME:
481 ret = ParseBoolParam(&argBool);
482 if (ret) {
483 SetStartupTime(argBool);
484 } else {
485 return false;
486 }
487 break;
488 case OPTION_STUB_FILE:
489 SetStubFile(optarg);
490 break;
491 case OPTION_TARGET_TRIPLE:
492 SetTargetTriple(optarg);
493 break;
494 case OPTION_ENTRY_POINT:
495 SetEntryPoint(optarg);
496 break;
497 case OPTION_MERGE_ABC:
498 ret = ParseBoolParam(&argBool);
499 if (ret) {
500 SetMergeAbc(argBool);
501 } else {
502 return false;
503 }
504 break;
505 case OPTION_ENABLE_TYPE_LOWERING:
506 ret = ParseBoolParam(&argBool);
507 if (ret) {
508 SetEnableTypeLowering(argBool);
509 } else {
510 return false;
511 }
512 break;
513 default:
514 std::cerr << "Invalid option\n"<< std::endl;
515 return false;
516 }
517 }
518 }
519
SetDefaultValue(char * argv)520 bool JSRuntimeOptions::SetDefaultValue(char* argv)
521 {
522 WasSet(optopt);
523
524 if (optopt == OPTION_DEFAULT) { // unknown option
525 std::cerr << " Invalid option \"" << argv << "\"" << std::endl;
526 return false;
527 }
528
529 if (optopt > OPTION_OPTIONS) { // unknown argument
530 std::cerr << "getopt: \"" << argv <<"\" argument has invalid parameter value \n" << std::endl;
531 return false;
532 }
533 return true;
534 }
535
ParseBoolParam(bool * argBool)536 bool JSRuntimeOptions::ParseBoolParam(bool* argBool)
537 {
538 if ((strcmp(optarg, "false") == 0) || (strcmp(optarg, "0") == 0)) {
539 *argBool = false;
540 } else {
541 *argBool = true;
542 }
543 return true;
544 }
545
ParseIntParam(const std::string & option,int * argInt)546 bool JSRuntimeOptions::ParseIntParam(const std::string &option, int* argInt)
547 {
548 if (StartsWith(optarg, "0x")) {
549 const int HEX = 16;
550 *argInt = std::stoi(optarg, nullptr, HEX);
551 } else {
552 *argInt = std::stoi(optarg);
553 }
554
555 if (errno == ERANGE) {
556 std::cerr << "getopt: \"" << option <<"\" argument has invalid parameter value \""
557 << optarg <<"\"\n" << std::endl;
558 return false;
559 }
560 return true;
561 }
562
ParseUint32Param(const std::string & option,uint32_t * argUint32)563 bool JSRuntimeOptions::ParseUint32Param(const std::string &option, uint32_t *argUint32)
564 {
565 if (StartsWith(optarg, "0x")) {
566 const int HEX = 16;
567 *argUint32 = std::strtoull(optarg, nullptr, HEX);
568 } else {
569 const int DEC = 10;
570 *argUint32 = std::strtoull(optarg, nullptr, DEC);
571 }
572
573 if (errno == ERANGE) {
574 std::cerr << "getopt: \"" << option <<"\" argument has invalid parameter value \""
575 << optarg <<"\"\n" << std::endl;
576 return false;
577 }
578 return true;
579 }
580
ParseUint64Param(const std::string & option,uint64_t * argUInt64)581 bool JSRuntimeOptions::ParseUint64Param(const std::string &option, uint64_t *argUInt64)
582 {
583 if (StartsWith(optarg, "0x")) {
584 const int HEX = 16;
585 *argUInt64 = std::strtoull(optarg, nullptr, HEX);
586 } else {
587 const int DEC = 10;
588 *argUInt64 = std::strtoull(optarg, nullptr, DEC);
589 }
590
591 if (errno == ERANGE) {
592 std::cerr << "getopt: \"" << option <<"\" argument has invalid parameter value \""
593 << optarg <<"\"\n" << std::endl;
594 return false;
595 }
596 return true;
597 }
598
ParseListArgParam(const std::string & option,arg_list_t * argListStr,std::string delimiter)599 void JSRuntimeOptions::ParseListArgParam(const std::string &option, arg_list_t *argListStr, std::string delimiter)
600 {
601 argListStr->clear();
602 std::size_t strIndex = 0;
603 std::size_t pos = option.find_first_of(delimiter, strIndex);
604 while ((pos < option.size()) && (pos > strIndex)) {
605 argListStr->push_back(option.substr(strIndex, pos - strIndex));
606 strIndex = pos;
607 strIndex = option.find_first_not_of(delimiter, strIndex);
608 pos = option.find_first_of(delimiter, strIndex);
609 }
610 if (pos > strIndex) {
611 argListStr->push_back(option.substr(strIndex, pos - strIndex));
612 }
613 return;
614 }
615 }
616