本文整理汇总了C++中QScriptSyntaxCheckResult类的典型用法代码示例。如果您正苦于以下问题:C++ QScriptSyntaxCheckResult类的具体用法?C++ QScriptSyntaxCheckResult怎么用?C++ QScriptSyntaxCheckResult使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QScriptSyntaxCheckResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: execFile
bool Terminal::execFile(QString filePath) {
if(!Utils::isExtension(filePath, QString("js"))) {
std::cout << "File Invalid..."
<< std::endl
<< "Acceptable extension *.js";
return false;
}
QString script = Utils::getContent(filePath);
if(script.isEmpty()) { return false; }
QScriptSyntaxCheckResult result = this->m_engine->checkSyntax(script);
if(result.state() == QScriptSyntaxCheckResult::Error) {
QString error("Error on line %1 column %2\nMessage: %3");
error = error.arg(QString::number(result.errorLineNumber()),
QString::number(result.errorColumnNumber()),
result.errorMessage());
std::cout << error.toAscii().constData() << std::endl;
return false;
}
Jsws *jsws = new Jsws(this->m_engine, script,parent());
QScriptValue value = this->m_engine->newQObject(jsws);
this->m_engine->globalObject().setProperty("WS", value);
this->m_engine->evaluate(script);
return true;
}
示例2: foreach
QScriptValue ScriptTask::runScript(QScriptEngine* engine, const QMap<QString, QScriptValue>& inputParametersMap, const QString& scriptText, TaskStateInfo& stateInfo) {
// create new script engine
QScriptValue result;
// setup all input params as global vars
QScriptValue thiz = engine->globalObject();
foreach(const QString& varName, inputParametersMap.keys()) {
QScriptValue varVal = inputParametersMap.value(varName);
thiz.setProperty(varName, varVal);
}
// check script syntax
QScriptSyntaxCheckResult syntaxResult = engine->checkSyntax(scriptText);
if (syntaxResult.state() != QScriptSyntaxCheckResult::Valid) {
stateInfo.setError(tr("Script syntax check failed! Line: %1, error: %2").arg(syntaxResult.errorLineNumber()).arg(syntaxResult.errorMessage()));
return result;
}
// run the script
result = engine->evaluate(scriptText);
if (engine->hasUncaughtException()) {
stateInfo.setError(tr("Exception during script execution! Line: %1, error: %2")
.arg(engine->uncaughtExceptionLineNumber()).arg(engine->uncaughtExceptionBacktrace().join("\n")));
}
return result;
}
示例3: Q_UNUSED
bool FuncCallScriptAction::check(const QString &xmlFile, const TypeRule *rule,
SymFactory *factory)
{
Q_UNUSED(rule);
Q_UNUSED(factory);
// Delete old program
if (_program) {
delete _program;
_program = 0;
}
// Read the contents of the script file
QFile scriptFile(_scriptFile);
if (!scriptFile.open(QFile::ReadOnly))
ioError(QString("Error opening file \"%1\" for reading.")
.arg(_scriptFile));
_program = new QScriptProgram(scriptFile.readAll(), _scriptFile);
// Basic syntax check
QScriptSyntaxCheckResult result =
QScriptEngine::checkSyntax(_program->sourceCode());
if (result.state() != QScriptSyntaxCheckResult::Valid) {
typeRuleError2(xmlFile, srcLine(), -1,
QString("Syntax error in file %1 line %2 column %3: %4")
.arg(ShellUtil::shortFileName(_scriptFile))
.arg(result.errorLineNumber())
.arg(result.errorColumnNumber())
.arg(result.errorMessage()));
}
// Check if the function exists
ScriptEngine eng(0);
ScriptEngine::FuncExistsResult ret =
eng.functionExists(_function, *_program);
if (ret == ScriptEngine::feRuntimeError) {
QString err;
if (eng.hasUncaughtException()) {
err = QString("Uncaught exception at line %0: %1")
.arg(eng.uncaughtExceptionLineNumber())
.arg(eng.lastError());
QStringList bt = eng.uncaughtExceptionBacktrace();
for (int i = 0; i < bt.size(); ++i)
err += "\n " + bt[i];
}
else
err = eng.lastError();
typeRuleError2(xmlFile, srcLine(), -1,
QString("Runtime error in file %1: %2")
.arg(ShellUtil::shortFileName(_scriptFile))
.arg(err));
}
else if (ret == ScriptEngine::feDoesNotExist) {
typeRuleError2(xmlFile, srcLine(), -1,
QString("Function \"%1\" is not defined in file \"%2\".")
.arg(_function)
.arg(ShellUtil::shortFileName(_scriptFile)));
}
return true;
}
示例4: js_include
//-- \subsection{include(file)}
//-- Includes another source code file at this point. This is experimental, and breaks the
//-- lint tool, so use with care.
static QScriptValue js_include(QScriptContext *context, QScriptEngine *engine)
{
QString path = context->argument(0).toString();
UDWORD size;
char *bytes = NULL;
if (!loadFile(path.toAscii().constData(), &bytes, &size))
{
debug(LOG_ERROR, "Failed to read include file \"%s\"", path.toAscii().constData());
return QScriptValue(false);
}
QString source = QString::fromAscii(bytes, size);
free(bytes);
QScriptSyntaxCheckResult syntax = QScriptEngine::checkSyntax(source);
if (syntax.state() != QScriptSyntaxCheckResult::Valid)
{
debug(LOG_ERROR, "Syntax error in include %s line %d: %s",
path.toAscii().constData(), syntax.errorLineNumber(), syntax.errorMessage().toAscii().constData());
return QScriptValue(false);
}
context->setActivationObject(engine->globalObject());
context->setThisObject(engine->globalObject());
QScriptValue result = engine->evaluate(source, path);
if (engine->hasUncaughtException())
{
int line = engine->uncaughtExceptionLineNumber();
debug(LOG_ERROR, "Uncaught exception at line %d, include file %s: %s",
line, path.toAscii().constData(), result.toString().toAscii().constData());
return QScriptValue(false);
}
return QScriptValue(true);
}
示例5: f
QVariant CBDS::getSettingsFromScript(const QString &filename)
{
QFile f(filename);
if (f.open(QIODevice::ReadOnly))
{
QString appcode(f.readAll());
f.close();
QScriptSyntaxCheckResult res = m_engine.checkSyntax(appcode);
if (res.state() != QScriptSyntaxCheckResult::Valid)
{
emit error(QString("SyntaxCheck Failed: Line: %1 Column: %2: %3").arg(res.errorLineNumber()).arg(res.errorColumnNumber()).arg(res.errorMessage()));
return QVariant();
}
QScriptContext *ctx = m_engine.pushContext();
CBJSObject js;
CBObjectBase cbo(&m_engine, m_roomowner);
ctx->activationObject().setProperty("cb", m_engine.newQObject(&cbo));
ctx->activationObject().setProperty("cbjs", m_engine.newQObject(&js));
QScriptValue ret = m_engine.evaluate(appcode, QFileInfo(filename).fileName());
if (ret.isError())
{
emit error(ret.toString());
m_engine.popContext();
return QVariant();
}
m_engine.popContext();
return cbo.getSettingsChoices().toVariant();
}
emit error("Can't open file: " + filename);
return QVariant();
}
示例6: script
void ScriptingAsset::load()
{
if( !isLoaded() )
{
QFile script( file().toLocalFile() );
if( script.open( QIODevice::ReadOnly ) )
{
d->script = script.readAll();
mimeData()->setText( d->script );
}
// Don't attempt to do anything if the script is empty
if( d->script.isEmpty() )
return;
QScriptSyntaxCheckResult result = ScriptingEngine::instance()->registerAsset( this );
if( result.state() != QScriptSyntaxCheckResult::Valid )
{
debug( tr( "Script error %1 (%2,%3): %4" ).arg( fullyQualifiedName() ).arg( result.errorLineNumber() ).arg( result.errorColumnNumber() ).arg( result.errorMessage() ) );
}
else
{
Asset::load();
}
}
}
示例7: PROFILE
void JavascriptInstance::Load()
{
PROFILE(JSInstance_Load);
if (!engine_)
CreateEngine();
if (sourceFile.isEmpty() && scriptRefs_.empty())
{
LogError("JavascriptInstance::Load: No script content to load!");
return;
}
// Can't specify both a file source and an Asset API source.
if (!sourceFile.isEmpty() && !scriptRefs_.empty())
{
LogError("JavascriptInstance::Load: Cannot specify both an local input source file and a list of script refs to load!");
return;
}
bool useAssetAPI = !scriptRefs_.empty();
size_t numScripts = useAssetAPI ? scriptRefs_.size() : 1;
// Determine based on code origin whether it can be trusted with system access or not
if (useAssetAPI)
{
trusted_ = true;
for(unsigned i = 0; i < scriptRefs_.size(); ++i)
trusted_ = trusted_ && scriptRefs_[i]->IsTrusted();
}
else // Local file: always trusted.
{
program_ = LoadScript(sourceFile);
trusted_ = true; // This is a file on the local filesystem. We are making an assumption nobody can inject untrusted code here.
// Actually, we are assuming the attacker does not know the absolute location of the asset cache locally here, since if he makes
// the client to load a script into local cache, he could use this code path to automatically load that unsafe script from cache, and make it trusted. -jj.
}
// Check the validity of the syntax in the input.
for (size_t i = 0; i < numScripts; ++i)
{
QString scriptSourceFilename = (useAssetAPI ? scriptRefs_[i]->Name() : sourceFile);
QString &scriptContent = (useAssetAPI ? scriptRefs_[i]->scriptContent : program_);
QScriptSyntaxCheckResult syntaxResult = engine_->checkSyntax(scriptContent);
if (syntaxResult.state() != QScriptSyntaxCheckResult::Valid)
{
LogError("Syntax error in script " + scriptSourceFilename + "," + QString::number(syntaxResult.errorLineNumber()) +
": " + syntaxResult.errorMessage());
// Delete our loaded script content (if any exists).
program_ == "";
}
}
}
示例8: loadPlayerScript
bool loadPlayerScript(QString path, int player, int difficulty)
{
ASSERT_OR_RETURN(false, player < MAX_PLAYERS, "Player index %d out of bounds", player);
QScriptEngine *engine = new QScriptEngine();
UDWORD size;
char *bytes = NULL;
if (!loadFile(path.toAscii().constData(), &bytes, &size))
{
debug(LOG_ERROR, "Failed to read script file \"%s\"", path.toAscii().constData());
return false;
}
QString source = QString::fromAscii(bytes, size);
free(bytes);
QScriptSyntaxCheckResult syntax = QScriptEngine::checkSyntax(source);
ASSERT_OR_RETURN(false, syntax.state() == QScriptSyntaxCheckResult::Valid, "Syntax error in %s line %d: %s",
path.toAscii().constData(), syntax.errorLineNumber(), syntax.errorMessage().toAscii().constData());
// Remember internal, reserved names
QScriptValueIterator it(engine->globalObject());
while (it.hasNext())
{
it.next();
internalNamespace.insert(it.name(), 1);
}
QScriptValue result = engine->evaluate(source, path);
ASSERT_OR_RETURN(false, !engine->hasUncaughtException(), "Uncaught exception at line %d, file %s: %s",
engine->uncaughtExceptionLineNumber(), path.toAscii().constData(), result.toString().toAscii().constData());
// Special functions
engine->globalObject().setProperty("setTimer", engine->newFunction(js_setTimer));
engine->globalObject().setProperty("queue", engine->newFunction(js_queue));
engine->globalObject().setProperty("removeTimer", engine->newFunction(js_removeTimer));
engine->globalObject().setProperty("include", engine->newFunction(js_include));
engine->globalObject().setProperty("bind", engine->newFunction(js_bind));
// Special global variables
engine->globalObject().setProperty("me", player, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("selectedPlayer", selectedPlayer, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("gameTime", gameTime, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("difficulty", difficulty, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("mapName", game.map, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("baseType", game.base, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("alliancesType", game.alliance, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("powerType", game.power, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("maxPlayers", game.maxPlayers, QScriptValue::ReadOnly | QScriptValue::Undeletable);
engine->globalObject().setProperty("scavengers", game.scavengers, QScriptValue::ReadOnly | QScriptValue::Undeletable);
// Regular functions
registerFunctions(engine);
// Register script
scripts.push_back(engine);
return true;
}
示例9: performSyntaxCheck
bool ScriptLoader::performSyntaxCheck(const QString & script)
{
QScriptSyntaxCheckResult syntaxCheck = p_engine->checkSyntax(script);
if(syntaxCheck.state() != QScriptSyntaxCheckResult::Valid)
{
qDebug() << "ScriptLoader::load: Syntax error in the script";
qDebug() << "Line" << syntaxCheck.errorLineNumber() << ": error:" << syntaxCheck.errorMessage();
return false;
}
return true;
}
示例10: QObject
ScriptSandbox::ScriptSandbox(const PluginManager *parent, const QString &file, const QString &script, AdamantPlugin *owner)
: QObject()
, _manager(parent)
, _script(script)
, _owner(owner)
, _engine()
, _engineDebugger()
, _errorString()
{
_engineDebugger.attachTo(&_engine);
_program = QScriptProgram(_script, file);
// Register MetaTypes, sometimes Q_DECL doesn't seem to cut it...
qRegisterMetaType<Session::Request*>("Session::Request");
qRegisterMetaType<AdamantUI*>("AdamantUI");
qRegisterMetaType<ItemManager*>("ItemManager");
qRegisterMetaType<AdamantUI::ApplicationTheme>("ApplicationTheme");
// qScriptRegisterMetaType<AdamantUI::ApplicationTheme>(&_engine, [](QScriptEngine *engine, const AdamantUI::ApplicationTheme &t) -> QScriptValue {
// return QScriptValue(static_cast<int>(t));
// }, [](const QScriptValue &obj, AdamantUI::ApplicationTheme &t) {
// t = static_cast<AdamantUI::ApplicationTheme>(obj.toInt32());
// });
QScriptSyntaxCheckResult syntax = _engine.checkSyntax(_script);
if (syntax.state() != QScriptSyntaxCheckResult::Valid){
_errorString = syntax.errorMessage();
}
else {
// Default Globals
addGlobalObject("manager", (QObject*)_manager);
addGlobalObject("script", this);
addGlobalObject("core", _manager->core());
addGlobalObject("app", qApp);
}
{
QScriptValue func = _engine.newFunction(ScriptSandbox::runFunc, this);
_engine.globalObject().setProperty("run", func);
}
{
QScriptValue func = _engine.newFunction(ScriptSandbox::importFunc, this);
_engine.globalObject().setProperty("using", func);
}
{
QScriptValue func = _engine.newFunction(ScriptSandbox::printFunc, this);
_engine.globalObject().setProperty("print", func);
}
}
示例11: setObjectName
Script::Script(QString script)
{
setObjectName(metaObject()->className());
addOutputPort("ERROR");
//From http://www.qtcentre.org/threads/10425-QtScript-newFunction-won-t-work
QScriptValue that = scriptEngine.newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeChildObjects
| QScriptEngine::ExcludeSuperClassMethods | QScriptEngine::ExcludeSuperClassProperties);
scriptEngine.globalObject().setProperty("self",that);
//Log in console
scriptEngine.evaluate("function echo(str){self.scriptEcho(str);};");
//Add input
scriptEngine.evaluate("function addInputPort(str){self.scriptAddInputPort(str);};");
//Add output
scriptEngine.evaluate("function addOutputPort(str){self.scriptAddOutputPort(str);};");
//Set self starting
scriptEngine.evaluate("function setSelfStarting(str){self.scriptSetSelfStarting(str);};");
//Receive
scriptEngine.evaluate("function receive(str){return self.scriptReceive(str);};");
//Send
scriptEngine.evaluate("function send(str, val){self.scriptSend(str, val);};");
//Read file
scriptEngine.evaluate("function readFile(str){return self.scriptReadFile(str);};");
//------------------------------------------------------------------
// EVALUATE THE SCRIPT
//------------------------------------------------------------------
QScriptSyntaxCheckResult check = scriptEngine.checkSyntax(script);
if(check.state() != QScriptSyntaxCheckResult::Valid)
{
std::cerr << QString("Error in script line %1, column %2: %3")
.arg(check.errorLineNumber())
.arg(check.errorColumnNumber())
.arg(check.errorMessage())
.toStdString() << std::endl;
}
else if(!scriptEngine.canEvaluate(script))
{
std::cerr << QString("Can't evaluate script: unknown error.")
.toStdString() << std::endl;
}
else
{
scriptEngine.evaluate(script);
scriptEngine.evaluate("init()");
}
}
示例12: isApplied
bool QtScriptFemCondition3D::setCondition(QString script)
{
QString func = "function isApplied(x, y, z){\n";
func += "return " + script + ";\n}";
script_ = script;
QScriptSyntaxCheckResult chk = QScriptEngine::checkSyntax(func);
if (chk.state() != QScriptSyntaxCheckResult::Valid)
{
std::cout << "Ошибка анализа граничного условия: " << chk.errorMessage().toStdString() << std::endl;
return false;
}
object_ = engine_.evaluate(func);
if (object_.isError())return false;
return true;
}
示例13: QScriptValue
QScriptValue EntityTreeRenderer::loadEntityScript(EntityItem* entity) {
if (!entity) {
return QScriptValue(); // no entity...
}
EntityItemID entityID = entity->getEntityItemID();
if (_entityScripts.contains(entityID)) {
EntityScriptDetails details = _entityScripts[entityID];
// check to make sure our script text hasn't changed on us since we last loaded it
if (details.scriptText == entity->getScript()) {
return details.scriptObject; // previously loaded
}
// if we got here, then we previously loaded a script, but the entity's script value
// has changed and so we need to reload it.
_entityScripts.remove(entityID);
}
if (entity->getScript().isEmpty()) {
return QScriptValue(); // no script
}
QString scriptContents = loadScriptContents(entity->getScript());
QScriptSyntaxCheckResult syntaxCheck = QScriptEngine::checkSyntax(scriptContents);
if (syntaxCheck.state() != QScriptSyntaxCheckResult::Valid) {
qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID;
qDebug() << " " << syntaxCheck.errorMessage() << ":"
<< syntaxCheck.errorLineNumber() << syntaxCheck.errorColumnNumber();
qDebug() << " SCRIPT:" << entity->getScript();
return QScriptValue(); // invalid script
}
QScriptValue entityScriptConstructor = _entitiesScriptEngine->evaluate(scriptContents);
if (!entityScriptConstructor.isFunction()) {
qDebug() << "EntityTreeRenderer::loadEntityScript() entity:" << entityID;
qDebug() << " NOT CONSTRUCTOR";
qDebug() << " SCRIPT:" << entity->getScript();
return QScriptValue(); // invalid script
}
QScriptValue entityScriptObject = entityScriptConstructor.construct();
EntityScriptDetails newDetails = { entity->getScript(), entityScriptObject };
_entityScripts[entityID] = newDetails;
return entityScriptObject; // newly constructed
}
示例14: switch
void CConsole::evaluate() {
QScriptSyntaxCheckResult result = m_engine->checkSyntax( m_text->toPlainText() );
switch (result.state()) {
case QScriptSyntaxCheckResult::Valid:
qDebug() << "Valid Syntax";
break;
case QScriptSyntaxCheckResult::Error:
case QScriptSyntaxCheckResult::Intermediate:
qDebug() << "Invalid Syntax at" << result.errorLineNumber() << result.errorMessage();
return;
break;
};
if(m_engine->canEvaluate( m_text->toPlainText() )) {
qDebug() << "Evaluatable";
} else {
qDebug() << "Non-evaluatable";
}
}
示例15: LogDebug
void JavascriptInstance::IncludeFile(const QString &path)
{
for(uint i = 0; i < includedFiles.size(); ++i)
if (includedFiles[i].toLower() == path.toLower())
{
LogDebug("JavascriptInstance::IncludeFile: Not including already included file " + path);
return;
}
QString script = LoadScript(path);
QScriptContext *context = engine_->currentContext();
assert(context);
if (!context)
{
LogError("JavascriptInstance::IncludeFile: QScriptEngine::currentContext() returned null!");
return;
}
QScriptContext *parent = context->parentContext();
if (!parent)
{
LogError("JavascriptInstance::IncludeFile: QScriptEngine::parentContext() returned null!");
return;
}
context->setActivationObject(context->parentContext()->activationObject());
context->setThisObject(context->parentContext()->thisObject());
QScriptSyntaxCheckResult syntaxResult = engine_->checkSyntax(script);
if(syntaxResult.state() != QScriptSyntaxCheckResult::Valid)
{
LogError("JavascriptInstance::IncludeFile: Syntax error in " + path + ". " + syntaxResult.errorMessage() +
" In line:" + QString::number(syntaxResult.errorLineNumber()));
return;
}
QScriptValue result = engine_->evaluate(script, path);
includedFiles.push_back(path);
if (engine_->hasUncaughtException())
LogError(result.toString());
}