Don't #include inside extern "C" blocks.

Also free the malloc'd error string in print_wasmer_error().

Run the C code through clang-format.
This commit is contained in:
Nick Lewycky
2021-02-10 18:00:58 -08:00
parent fd3373a971
commit d589d83d09
4 changed files with 86 additions and 103 deletions

View File

@@ -76,7 +76,10 @@ pub fn generate_header_file(
) -> String { ) -> String {
let mut c_statements = vec![]; let mut c_statements = vec![];
c_statements.push(CStatement::LiteralConstant { c_statements.push(CStatement::LiteralConstant {
value: "#include <stdlib.h>\n\n".to_string(), value: "#include <stdlib.h>\n#include <string.h>\n\n".to_string(),
});
c_statements.push(CStatement::LiteralConstant {
value: "#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n".to_string(),
}); });
c_statements.push(CStatement::Declaration { c_statements.push(CStatement::Declaration {
name: "module_bytes_len".to_string(), name: "module_bytes_len".to_string(),
@@ -285,11 +288,9 @@ pub fn generate_header_file(
value: HELPER_FUNCTIONS.to_string(), value: HELPER_FUNCTIONS.to_string(),
}); });
let inner_c = generate_c(&c_statements); c_statements.push(CStatement::LiteralConstant {
value: "\n#ifdef __cplusplus\n}\n#endif\n\n".to_string(),
});
// we wrap the inner C to work with C++ too generate_c(&c_statements)
format!(
"#ifdef __cplusplus\nextern \"C\" {{\n#endif\n\n{}\n\n#ifdef __cplusplus\n}}\n#endif\n",
inner_c
)
} }

View File

@@ -1,10 +1,6 @@
#ifdef __cplusplus
extern "C" {
#endif
#include "wasmer_wasm.h"
#include "wasm.h"
#include "my_wasm.h" #include "my_wasm.h"
#include "wasm.h"
#include "wasmer_wasm.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -14,21 +10,17 @@ extern "C" {
// TODO: make this define templated so that the Rust code can toggle it on/off // TODO: make this define templated so that the Rust code can toggle it on/off
#define WASI #define WASI
#ifdef __cplusplus static void print_wasmer_error() {
}
#endif
void print_wasmer_error()
{
int error_len = wasmer_last_error_length(); int error_len = wasmer_last_error_length();
printf("Error len: `%d`\n", error_len); printf("Error len: `%d`\n", error_len);
char *error_str = (char *)malloc(error_len); char *error_str = (char *)malloc(error_len);
wasmer_last_error_message(error_str, error_len); wasmer_last_error_message(error_str, error_len);
printf("%s\n", error_str); printf("%s\n", error_str);
free(error_str);
} }
#ifdef WASI #ifdef WASI
int find_colon(char* string) { static int find_colon(char *string) {
int colon_location = 0; int colon_location = 0;
for (int j = 0; j < strlen(string); ++j) { for (int j = 0; j < strlen(string); ++j) {
if (string[j] == ':') { if (string[j] == ':') {
@@ -39,7 +31,7 @@ int find_colon(char* string) {
return colon_location; return colon_location;
} }
void pass_mapdir_arg(wasi_config_t* wasi_config, char* mapdir) { static void pass_mapdir_arg(wasi_config_t *wasi_config, char *mapdir) {
int colon_location = find_colon(mapdir); int colon_location = find_colon(mapdir);
if (colon_location == 0) { if (colon_location == 0) {
// error malformed argument // error malformed argument
@@ -66,40 +58,41 @@ void pass_mapdir_arg(wasi_config_t* wasi_config, char* mapdir) {
// We try to parse out `--dir` and `--mapdir` ahead of time and process those // We try to parse out `--dir` and `--mapdir` ahead of time and process those
// specially. All other arguments are passed to the guest program. // specially. All other arguments are passed to the guest program.
void handle_arguments(wasi_config_t* wasi_config, int argc, char* argv[]) { static void handle_arguments(wasi_config_t *wasi_config, int argc,
char *argv[]) {
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
// We probably want special args like `--dir` and `--mapdir` to not be passed directly // We probably want special args like `--dir` and `--mapdir` to not be
// passed directly
if (strcmp(argv[i], "--dir") == 0) { if (strcmp(argv[i], "--dir") == 0) {
// next arg is a preopen directory // next arg is a preopen directory
if ((i + 1) < argc) { if ((i + 1) < argc) {
i++; i++;
wasi_config_preopen_dir(wasi_config, argv[i]); wasi_config_preopen_dir(wasi_config, argv[i]);
} else { } else {
fprintf(stderr, "--dir expects a following argument specifying which directory to preopen\n"); fprintf(stderr, "--dir expects a following argument specifying which "
"directory to preopen\n");
exit(-1); exit(-1);
} }
} } else if (strcmp(argv[i], "--mapdir") == 0) {
else if (strcmp(argv[i], "--mapdir") == 0) {
// next arg is a mapdir // next arg is a mapdir
if ((i + 1) < argc) { if ((i + 1) < argc) {
i++; i++;
pass_mapdir_arg(wasi_config, argv[i]); pass_mapdir_arg(wasi_config, argv[i]);
} else { } else {
fprintf(stderr, "--mapdir expects a following argument specifying which directory to preopen in the form alias:directory\n"); fprintf(stderr,
"--mapdir expects a following argument specifying which "
"directory to preopen in the form alias:directory\n");
exit(-1); exit(-1);
} }
} } else if (strncmp(argv[i], "--dir=", strlen("--dir=")) == 0) {
else if (strncmp(argv[i], "--dir=", strlen("--dir=")) == 0 ) {
// this arg is a preopen dir // this arg is a preopen dir
char *dir = argv[i] + strlen("--dir="); char *dir = argv[i] + strlen("--dir=");
wasi_config_preopen_dir(wasi_config, dir); wasi_config_preopen_dir(wasi_config, dir);
} } else if (strncmp(argv[i], "--mapdir=", strlen("--mapdir=")) == 0) {
else if (strncmp(argv[i], "--mapdir=", strlen("--mapdir=")) == 0 ) {
// this arg is a mapdir // this arg is a mapdir
char *mapdir = argv[i] + strlen("--mapdir="); char *mapdir = argv[i] + strlen("--mapdir=");
pass_mapdir_arg(wasi_config, mapdir); pass_mapdir_arg(wasi_config, mapdir);
} } else {
else {
// guest argument // guest argument
wasi_config_arg(wasi_config, argv[i]); wasi_config_arg(wasi_config, argv[i]);
} }
@@ -120,7 +113,8 @@ int main(int argc, char* argv[]) {
return -1; return -1;
} }
// We have now finished the memory buffer book keeping and we have a valid Module. // We have now finished the memory buffer book keeping and we have a valid
// Module.
#ifdef WASI #ifdef WASI
wasi_config_t *wasi_config = wasi_config_new(argv[0]); wasi_config_t *wasi_config = wasi_config_new(argv[0]);

View File

@@ -25,10 +25,6 @@ Target: x86_64-apple-darwin
Now lets create a program to link with this object file. Now lets create a program to link with this object file.
```C ```C
#ifdef __cplusplus
extern "C" {
#endif
#include "wasmer_wasm.h" #include "wasmer_wasm.h"
#include "wasm.h" #include "wasm.h"
#include "my_wasm.h" #include "my_wasm.h"
@@ -38,17 +34,14 @@ extern "C" {
#define own #define own
#ifdef __cplusplus static void print_wasmer_error()
}
#endif
void print_wasmer_error()
{ {
int error_len = wasmer_last_error_length(); int error_len = wasmer_last_error_length();
printf("Error len: `%d`\n", error_len); printf("Error len: `%d`\n", error_len);
char* error_str = (char*) malloc(error_len); char* error_str = (char*) malloc(error_len);
wasmer_last_error_message(error_str, error_len); wasmer_last_error_message(error_str, error_len);
printf("Error str: `%s`\n", error_str); printf("Error str: `%s`\n", error_str);
free(error_str);
} }
int main() { int main() {

View File

@@ -1,27 +1,19 @@
#ifdef __cplusplus
extern "C" {
#endif
#include "wasmer_wasm.h"
#include "wasm.h"
#include "my_wasm.h" #include "my_wasm.h"
#include "wasm.h"
#include "wasmer_wasm.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#define own #define own
#ifdef __cplusplus static void print_wasmer_error() {
}
#endif
void print_wasmer_error()
{
int error_len = wasmer_last_error_length(); int error_len = wasmer_last_error_length();
printf("Error len: `%d`\n", error_len); printf("Error len: `%d`\n", error_len);
char *error_str = (char *)malloc(error_len); char *error_str = (char *)malloc(error_len);
wasmer_last_error_message(error_str, error_len); wasmer_last_error_message(error_str, error_len);
printf("Error str: `%s`\n", error_str); printf("Error str: `%s`\n", error_str);
free(error_str);
} }
int main() { int main() {
@@ -39,12 +31,15 @@ int main() {
return -1; return -1;
} }
// We have now finished the memory buffer book keeping and we have a valid Module. // We have now finished the memory buffer book keeping and we have a valid
// Module.
// In this example we're passing some JavaScript source code as a command line argument // In this example we're passing some JavaScript source code as a command line
// to a WASI module that can evaluate JavaScript. // argument to a WASI module that can evaluate JavaScript.
wasi_config_t *wasi_config = wasi_config_new("constant_value_here"); wasi_config_t *wasi_config = wasi_config_new("constant_value_here");
const char* js_string = "function greet(name) { return JSON.stringify('Hello, ' + name); }; print(greet('World'));"; const char *js_string =
"function greet(name) { return JSON.stringify('Hello, ' + name); }; "
"print(greet('World'));";
wasi_config_arg(wasi_config, "--eval"); wasi_config_arg(wasi_config, "--eval");
wasi_config_arg(wasi_config, js_string); wasi_config_arg(wasi_config, js_string);
wasi_env_t *wasi_env = wasi_env_new(wasi_config); wasi_env_t *wasi_env = wasi_env_new(wasi_config);