Tencent/ScriptX

demo代码运行异常.failed to run game can't cast value as Object [failed to obtain stacktrace]

Opened this issue · 0 comments

开发环境是VS2022 C++20
void exportHostAbility(const std::shared_ptrscript::ScriptEngine& engine) {
using host_ability::HostImage;

// wrapper inherits from HostImage and ScriptClass
class HostImageWrapper : public HostImage, public script::ScriptClass {
public:
	using script::ScriptClass::ScriptClass;
};

static script::ClassDefine<HostImageWrapper> hostImageDef =
	script::defineClass<HostImageWrapper>("Image")
	.constructor()
	.instanceProperty("src", &HostImage::getSrc, &HostImage::setSrc)
	.instanceProperty("width", &HostImage::getWidth, nullptr)
	.instanceProperty("height", &HostImage::getHeight, nullptr)
	.instanceFunction("drop", &HostImage::drop)
	.build();

engine->registerNativeClass(hostImageDef);

auto drawImageFunc =
	script::Function::newFunction([](HostImageWrapper* img) { host_ability::drawImage(img); });
engine->set("_drawImage", drawImageFunc);

auto sendMessageFunc = script::Function::newFunction(host_ability::sendMessage);
engine->set("_sendMessage", sendMessageFunc);

}
auto engine = createEngine();
// 1. export function
{
script::EngineScope enter(engine.get());
try {
exportHostAbility(engine);//这里异常了
}
catch (const script::Exception& e) {
std::cerr << "failed to run game " << e;
}
}

// 2. inject host base lib
{
	script::EngineScope enter(engine.get());
	try {
		engine->eval(getScriptBaseLibrary());
	}
	catch (const script::Exception& e) {
		std::cerr << "failed to run game " << e;
	}
}

// 3. run downloaded game script
{
	script::EngineScope enter(engine.get());
	try {
		engine->eval(downloadGameScript());
	}
	catch (const script::Exception& e) {
		std::cerr << "failed to run game " << e;
	}
}
// exit and shutdown.