Example #1
0
QVariant LuaEngine::invokeFunction(const char* object, const char* method, const QVariantList& arguments /*= QVariantList()*/, QLSCallback callback /*= nullptr*/)
{
	int argc = arguments.count();
#ifdef Q_OS_WIN
	foreach(const QVariant & argv, arguments)
#else
	for (const QVariant & argv : arguments)
#endif
	{
		luaPush(argv);
	}

	invokeFunction(std::string(object), std::string(method), argc, 1, callback != nullptr);

	QVariant result;

	if (lua_isnumber(L, -1))
	{
		result = luaPopNumber();
	}
	else if (lua_isstring(L, -1))
	{
		result = QString(luaPopString().c_str());
	}
	else if (lua_isuserdata(L, -1))
	{
		result = (qulonglong)lua_touserdata(L, -1);
	}
	else if (lua_istable(L, -1))
	{
		QHash<QString, QVariant> table;
		lua_pushnil(L);
		while (lua_next(L, -2) != 0)
		{
			table[lua_tostring(L, -2)] = lua_tostring(L, -1);
			lua_pop(L, 1);
		}
		lua_pop(L, 1); // remove table

		result = table;
	}

	return result;
}
Example #2
0
	bool loadModule(const AST::Module* module)
	{
		// Free any existing memory.
		vmShrinkMemory(vmGrowMemory(0));

		// Initialize the module's requested initial memory.
		if(vmGrowMemory((int32)module->initialNumPagesMemory * AST::numBytesPerPage) != 0)
		{
			std::cerr << "Failed to commit the requested initial memory for module instance (" << module->initialNumPagesMemory*AST::numBytesPerPage/1024 << "KB requested)" << std::endl;
			return false;
		}

		// Copy the module's data segments into VM memory.
		if(module->initialNumPagesMemory * AST::numBytesPerPage >= (1ull<<32)) { throw; }
		for(auto dataSegment : module->dataSegments)
		{
			if(dataSegment.baseAddress + dataSegment.numBytes > module->initialNumPagesMemory * AST::numBytesPerPage)
			{
				std::cerr << "Module data segment exceeds initial memory allocation" << std::endl;
				return false;
			}
			memcpy(instanceMemoryBase + dataSegment.baseAddress,dataSegment.data,dataSegment.numBytes);
		}

		// Generate machine code for the module.
		if(!LLVMJIT::compileModule(module)) { return false; }

		// Initialize the intrinsics.
		initEmscriptenIntrinsics(module);
		initWebAssemblyIntrinsics();
		initWAVMIntrinsics();

		// Call the module's start function.
		if(module->startFunctionIndex != UINTPTR_MAX)
		{
			assert(module->functions[module->startFunctionIndex]->type == AST::FunctionType());
			invokeFunction(module,module->startFunctionIndex,nullptr);
		}

		return true;
	}
Example #3
0
 /**
  * Invokes this object's function with the provided arguments.
  * @sa Wintermute::Util::Invokable::invokeFunction
  * @return The value returned from said function.
  * @throw std::invalid_argument Throws this exception if no function is
  * stored.
  *
  * Using the provided arguments, this invokes the stored function.
  */
 ReturnType operator()(const Arguments... arguments)
 {
   return invokeFunction(arguments...);
 }
Example #4
0
	void runInstanceStartFunc(ModuleInstance* moduleInstance) {
		if(moduleInstance->startFunctionIndex != UINTPTR_MAX)
			invokeFunction(moduleInstance->functions[moduleInstance->startFunctionIndex],{});
	}