Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if defined _MSC_VER || defined __MINGW32__
- # define DLL_IMPORT __declspec(dllimport)
- # define DLL_EXPORT __declspec(dllexport)
- #elif __GNU__ >= 4
- # define DLL_IMPORT __attribute__((visibility("default")))
- # define DLL_EXPORT __attribute__((visibility("default")))
- #else
- # define DLL_IMPORT
- # define DLL_EXPORT
- #endif
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <iomanip>
- #include <cstring>
- #include <Inline/BasicTypes.h>
- #include <Inline/Serialization.h>
- #include <Inline/HashMap.h>
- #include <IR/Module.h>
- #include <IR/Validate.h>
- #include <IR/Types.h>
- #include <IR/Operators.h>
- #include <WASM/WASM.h>
- #include <Runtime/Runtime.h>
- #include <Runtime/Linker.h>
- #include <Runtime/Intrinsics.h>
- using namespace Serialization;
- using namespace IR;
- using namespace WASM;
- using namespace Runtime;
- //Define external environment module "env" containing function imports. in our case this is just for testing purposes.
- //These macros are defined in Intrinsics.h
- DEFINE_INTRINSIC_MODULE(env);
- DEFINE_INTRINSIC_FUNCTION(env,"printInteger",void,printInteger,I32 value) { std::cout << "output: " << value << std::endl; }
- //Resolve name-instance pairs to runtime objects. This struct can be adapted for the EEI
- struct WavmResolver : Resolver {
- WavmResolver(Compartment* _compartment): compartment(_compartment) {}
- Compartment *compartment;
- HashMap<std::string, ModuleInstance*> moduleNameToInstanceMap;
- bool resolve(const std::string& moduleName,const std::string& exportName,ObjectType type,Object*& outObject) override
- {
- auto namedInstance = moduleNameToInstanceMap.get(moduleName);
- if (namedInstance) {
- outObject = getInstanceExport(*namedInstance, exportName);
- if (outObject) {
- if(isA(outObject, type)) return true;
- else {
- std::cout << "Resolved import of incorrect type" << std::endl;
- return false;
- }
- }
- }
- return false;
- }
- };
- int main(int argc, char **argv)
- {
- std::ifstream infile;
- std::string code;
- assert(argv[1] != nullptr);
- infile.open(argv[1], std::ios::binary | std::ios::ate);
- assert(infile.is_open());
- //read file into string and get size
- code.resize((unsigned int)infile.tellg());
- infile.seekg(0);
- infile.read(const_cast<char*>(code.data()), code.size());
- infile.close();
- assert(code.size());
- //Build Module
- Module module;
- MemoryInputStream stream((const U8*)code.data(), code.size());
- WASM::serialize(stream, module);
- //Prepare WAVM "compartment" and context
- Compartment* compartment = createCompartment();
- Context* context = createContext(compartment);
- //Initialize import resolver struct with "env" module
- WavmResolver resolver = WavmResolver(compartment);
- resolver.moduleNameToInstanceMap.set("env", Intrinsics::instantiateModule(compartment, INTRINSIC_MODULE_REF(env), "env"));
- //Link module with external functions module
- LinkResult result = linkModule(module, resolver);
- assert(!result.missingImports.size());
- assert(result.success);
- ModuleInstance* moduleinstance = instantiateModule(compartment, module, std::move(result.resolvedImports), argv[1]);
- return 0;
- }
Add Comment
Please, Sign In to add comment