Пример #1
0
	void serialize(OutputStream& stream,UserSection& userSection)
	{
		serializeConstant(stream,"expected user section (section ID 0)",(U8)SectionType::user);
		ArrayOutputStream sectionStream;
		serialize(sectionStream,userSection.name);
		serializeBytes(sectionStream,userSection.data.data(),userSection.data.size());
		std::vector<U8> sectionBytes = sectionStream.getBytes();
		serialize(stream,sectionBytes);
	}
Пример #2
0
	void serializeSection(OutputStream& stream,SectionType type,SerializeSection serializeSectionBody)
	{
		serializeNativeValue(stream,type);
		ArrayOutputStream sectionStream;
		serializeSectionBody(sectionStream);
		std::vector<U8> sectionBytes = sectionStream.getBytes();
		Uptr sectionNumBytes = sectionBytes.size();
		serializeVarUInt32(stream,sectionNumBytes);
		serializeBytes(stream,sectionBytes.data(),sectionBytes.size());
	}
Пример #3
0
	bool resolve(const char* moduleName,const char* exportName,ObjectType type,Object*& outObject) override
	{
		// Try to resolve an intrinsic first.
		if(IntrinsicResolver::singleton.resolve(moduleName,exportName,type,outObject)) { return true; }

		// Then look for a named module.
		auto namedResolverIt = moduleNameToResolverMap.find(moduleName);
		if(namedResolverIt != moduleNameToResolverMap.end())
		{
			return namedResolverIt->second->resolve(moduleName,exportName,type,outObject);
		}

		// Finally, stub in missing function imports.
		if(type.kind == ObjectKind::function)
		{
			// Generate a function body that just uses the unreachable op to fault if called.
			ArrayOutputStream codeStream;
			OperationEncoder encoder(codeStream);
			encoder.unreachable();
			encoder.end();

			// Generate a module for the stub function.
			Module stubModule;
			DisassemblyNames stubModuleNames;
			stubModule.code = codeStream.getBytes();
			stubModule.types.push_back(type.function);
			stubModule.functionDefs.push_back({{},0,{0,stubModule.code.size()}});
			stubModule.exports.push_back({"importStub",ObjectKind::function,0});
			stubModuleNames.functions.push_back(std::string(moduleName) + "." + exportName);
			setDisassemblyNames(stubModule,stubModuleNames);
			WebAssembly::validate(stubModule);

			// Instantiate the module and return the stub function instance.
			auto stubModuleInstance = instantiateModule(stubModule,{});
			outObject = getInstanceExport(stubModuleInstance,"importStub");
			Log::printf(Log::Category::error,"Generated stub for missing function import %s.%s : %s\n",moduleName,exportName,asString(type).c_str());
			return true;
		}

		return false;
	}