serialize.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <inttypes.h>
  5. #include "wasm_c_api.h"
  6. #define own
  7. // A function to be called from Wasm code.
  8. own wasm_trap_t *
  9. hello_callback(const wasm_val_vec_t *args, wasm_val_vec_t *results)
  10. {
  11. printf("Calling back...\n");
  12. printf("> Hello World!\n");
  13. return NULL;
  14. }
  15. int
  16. main(int argc, const char *argv[])
  17. {
  18. // Initialize.
  19. printf("Initializing...\n");
  20. wasm_engine_t *engine = wasm_engine_new();
  21. wasm_store_t *store = wasm_store_new(engine);
  22. // Load binary.
  23. printf("Loading binary...\n");
  24. FILE *file = fopen("serialize.wasm", "rb");
  25. if (!file) {
  26. printf("> Error loading module!\n");
  27. return 1;
  28. }
  29. fseek(file, 0L, SEEK_END);
  30. size_t file_size = ftell(file);
  31. fseek(file, 0L, SEEK_SET);
  32. wasm_byte_vec_t binary;
  33. wasm_byte_vec_new_uninitialized(&binary, file_size);
  34. if (fread(binary.data, file_size, 1, file) != 1) {
  35. printf("> Error loading module!\n");
  36. return 1;
  37. }
  38. fclose(file);
  39. // Compile.
  40. printf("Compiling module...\n");
  41. own wasm_module_t *module = wasm_module_new(store, &binary);
  42. if (!module) {
  43. printf("> Error compiling module!\n");
  44. return 1;
  45. }
  46. wasm_byte_vec_delete(&binary);
  47. // Serialize module.
  48. printf("Serializing module...\n");
  49. own wasm_byte_vec_t serialized;
  50. wasm_module_serialize(module, &serialized);
  51. wasm_module_delete(module);
  52. // Deserialize module.
  53. printf("Deserializing module...\n");
  54. own wasm_module_t *deserialized =
  55. wasm_module_deserialize(store, &serialized);
  56. if (!deserialized) {
  57. printf("> Error deserializing module!\n");
  58. return 1;
  59. }
  60. wasm_byte_vec_delete(&serialized);
  61. // Create external print functions.
  62. printf("Creating callback...\n");
  63. own wasm_functype_t *hello_type = wasm_functype_new_0_0();
  64. own wasm_func_t *hello_func =
  65. wasm_func_new(store, hello_type, hello_callback);
  66. wasm_functype_delete(hello_type);
  67. // Instantiate.
  68. printf("Instantiating deserialized module...\n");
  69. wasm_extern_t *externs[] = { wasm_func_as_extern(hello_func) };
  70. wasm_extern_vec_t imports = WASM_ARRAY_VEC(externs);
  71. own wasm_instance_t *instance =
  72. wasm_instance_new(store, deserialized, &imports, NULL);
  73. if (!instance) {
  74. printf("> Error instantiating module!\n");
  75. return 1;
  76. }
  77. wasm_func_delete(hello_func);
  78. // Extract export.
  79. printf("Extracting export...\n");
  80. own wasm_extern_vec_t exports;
  81. wasm_instance_exports(instance, &exports);
  82. if (exports.size == 0) {
  83. printf("> Error accessing exports!\n");
  84. return 1;
  85. }
  86. const wasm_func_t *run_func = wasm_extern_as_func(exports.data[0]);
  87. if (run_func == NULL) {
  88. printf("> Error accessing export!\n");
  89. return 1;
  90. }
  91. wasm_module_delete(deserialized);
  92. wasm_instance_delete(instance);
  93. // Call.
  94. printf("Calling export...\n");
  95. wasm_val_vec_t empty = WASM_EMPTY_VEC;
  96. if (wasm_func_call(run_func, &empty, &empty)) {
  97. printf("> Error calling function!\n");
  98. return 1;
  99. }
  100. wasm_extern_vec_delete(&exports);
  101. // Shut down.
  102. printf("Shutting down...\n");
  103. wasm_store_delete(store);
  104. wasm_engine_delete(engine);
  105. // All done.
  106. printf("Done.\n");
  107. return 0;
  108. }