Guest User

Untitled

a guest
Jul 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #if defined _MSC_VER || defined __MINGW32__
  2. # define DLL_IMPORT __declspec(dllimport)
  3. # define DLL_EXPORT __declspec(dllexport)
  4. #elif __GNU__ >= 4
  5. # define DLL_IMPORT __attribute__((visibility("default")))
  6. # define DLL_EXPORT __attribute__((visibility("default")))
  7. #else
  8. # define DLL_IMPORT
  9. # define DLL_EXPORT
  10. #endif
  11.  
  12. #include <iostream>
  13. #include <fstream>
  14. #include <sstream>
  15. #include <iomanip>
  16. #include <cstring>
  17.  
  18. #include <Inline/BasicTypes.h>
  19. #include <Inline/Serialization.h>
  20. #include <Inline/HashMap.h>
  21. #include <IR/Module.h>
  22. #include <IR/Validate.h>
  23. #include <IR/Types.h>
  24. #include <IR/Operators.h>
  25. #include <WASM/WASM.h>
  26. #include <Runtime/Runtime.h>
  27. #include <Runtime/Linker.h>
  28. #include <Runtime/Intrinsics.h>
  29.  
  30. using namespace Serialization;
  31. using namespace IR;
  32. using namespace WASM;
  33. using namespace Runtime;
  34.  
  35. //Define external environment module "env" containing function imports. in our case this is just for testing purposes.
  36. //These macros are defined in Intrinsics.h
  37. DEFINE_INTRINSIC_MODULE(env);
  38. DEFINE_INTRINSIC_FUNCTION(env,"printInteger",void,printInteger,I32 value) { std::cout << "output: " << value << std::endl; }
  39.  
  40. //Resolve name-instance pairs to runtime objects. This struct can be adapted for the EEI
  41. struct WavmResolver : Resolver {
  42. WavmResolver(Compartment* _compartment): compartment(_compartment) {}
  43.  
  44. Compartment *compartment;
  45. HashMap<std::string, ModuleInstance*> moduleNameToInstanceMap;
  46.  
  47. bool resolve(const std::string& moduleName,const std::string& exportName,ObjectType type,Object*& outObject) override
  48. {
  49. auto namedInstance = moduleNameToInstanceMap.get(moduleName);
  50. if (namedInstance) {
  51. outObject = getInstanceExport(*namedInstance, exportName);
  52. if (outObject) {
  53. if(isA(outObject, type)) return true;
  54. else {
  55. std::cout << "Resolved import of incorrect type" << std::endl;
  56. return false;
  57. }
  58. }
  59. }
  60. return false;
  61. }
  62. };
  63.  
  64. int main(int argc, char **argv)
  65. {
  66. std::ifstream infile;
  67. std::string code;
  68.  
  69. assert(argv[1] != nullptr);
  70. infile.open(argv[1], std::ios::binary | std::ios::ate);
  71. assert(infile.is_open());
  72.  
  73. //read file into string and get size
  74. code.resize((unsigned int)infile.tellg());
  75. infile.seekg(0);
  76. infile.read(const_cast<char*>(code.data()), code.size());
  77. infile.close();
  78.  
  79. assert(code.size());
  80.  
  81. //Build Module
  82. Module module;
  83.  
  84. MemoryInputStream stream((const U8*)code.data(), code.size());
  85. WASM::serialize(stream, module);
  86.  
  87. //Prepare WAVM "compartment" and context
  88. Compartment* compartment = createCompartment();
  89. Context* context = createContext(compartment);
  90.  
  91. //Initialize import resolver struct with "env" module
  92. WavmResolver resolver = WavmResolver(compartment);
  93. resolver.moduleNameToInstanceMap.set("env", Intrinsics::instantiateModule(compartment, INTRINSIC_MODULE_REF(env), "env"));
  94.  
  95. //Link module with external functions module
  96. LinkResult result = linkModule(module, resolver);
  97. assert(!result.missingImports.size());
  98. assert(result.success);
  99.  
  100. ModuleInstance* moduleinstance = instantiateModule(compartment, module, std::move(result.resolvedImports), argv[1]);
  101.  
  102. return 0;
  103. }
Add Comment
Please, Sign In to add comment