-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get command line options only if necessary
Rename `main` function according to argument count: * 0: `__main_void` * 2: `__main_argc_argv` The entry point (`_start` function) calls `__main_void` function. If user defines `main` as zero arguments, then it is called. If user defines `main` as two arguments, `__main_void` defined in library code is called then user defined `main` function is called. # Conflicts: # src/wcc/traverse.c
- Loading branch information
Showing
3 changed files
with
36 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include "alloca.h" | ||
|
||
#include "../wasi.h" | ||
|
||
extern int __main_argc_argv(int, char**); | ||
|
||
__attribute__((weak)) | ||
int __main_void(void) { | ||
char **argv; | ||
int argc, len; | ||
int r = args_sizes_get(&argc, &len); | ||
if (r == 0) { | ||
argv = alloca(sizeof(char*) * (argc + 1) + len); | ||
char *str = ((char*)argv) + sizeof(char*) * (argc + 1); | ||
args_get(argv, str); | ||
} else { // Ignore error. | ||
argc = 1; | ||
argv = alloca(sizeof(char*) * (argc + 1)); | ||
argv[0] = "*"; | ||
} | ||
argv[argc] = NULL; | ||
|
||
return __main_argc_argv(argc, argv); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,15 @@ | ||
#include "alloca.h" // alloca | ||
#include "stdlib.h" // exit | ||
|
||
#include "../wasi.h" | ||
|
||
extern void __wasm_call_ctors(void); | ||
|
||
void _start(void) { | ||
#define main __main_argc_argv | ||
extern int main(int, char**); | ||
char **argv; | ||
int argc, len; | ||
int r = args_sizes_get(&argc, &len); | ||
if (r == 0) { | ||
argv = alloca(sizeof(char*) * (argc + 1) + len); | ||
char *str = ((char*)argv) + sizeof(char*) * (argc + 1); | ||
args_get(argv, str); | ||
} else { // Ignore error. | ||
argc = 1; | ||
argv = alloca(sizeof(char*) * (argc + 1)); | ||
argv[0] = "*"; | ||
} | ||
argv[argc] = NULL; | ||
extern int __main_void(void); | ||
|
||
__wasm_call_ctors(); | ||
|
||
int ec = main(argc, argv); | ||
int ec = __main_void(); | ||
exit(ec); | ||
#undef main | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters