[Qtscript-bindings] a 'include' method
Peter Hackett
Peter.Hackett at icmanage.com
Tue Aug 12 21:14:07 CEST 2008
This is what I do (right or less than right :-)
//static
QScriptValue
IqtScriptKit::include(QScriptContext* context,QScriptEngine* engine,
bool localScope)
{
QString funcName("include");
if ( localScope ) {
funcName = "localInclude";
}
QScriptValue arg = context->argument(0);
if (!arg.isString()) {
return context->throwError(
QScriptContext::TypeError,
QString(funcName + "(): filename expected"));
}
// grab current script path
QString prevScriptPath = getScriptPath(engine);
QString filePath = arg.toString();
exportCurScriptPath(engine,/*QString&*/filePath,/*validate*/true);
QFile file(filePath);
if ( ! file.open(QFile::ReadOnly) ) {
return context->throwError(
QString(funcName + "(): could not open `%s'")
.arg(filePath));
}
QTextStream textStream(&file);
QString contents = textStream.readAll();
file.close();
if ( localScope ) {
context->setActivationObject(
context->parentContext()->activationObject());
} else {
//global
context->setActivationObject(engine->globalObject());
context->setThisObject(engine->globalObject());
}
QScriptValue rtn;
try {
rtn = engine->evaluate(contents,filePath);
} catch ( IqtException e ) {
//rtn = context->throwError(QScriptContext::UnknownError,
//no! QString(e.what()));
IqtConsole::statusOut(IqtErr) << "IqtScriptKit::include:
IqtException: "
<< e.what() << endl; // TODO re-init script engine(?)
}
// restore previous script path
exportCurScriptPath(engine,/*QString&*/prevScriptPath,/*validate*/false);
return rtn;
}
//static
void
IqtScriptKit::exportCurScriptPath(QScriptEngine* engine,
QString& scriptPath,bool check)
{
if ( check ) {
QFileInfo fileInfo(scriptPath);
scriptPath = fileInfo.absoluteFilePath();
if ( ! fileInfo.exists() ) {
return;
}
if ( ! fileInfo.isReadable() ) {
return;
}
}
// put path into script env:
QScriptValue scriptObj(engine,scriptPath);
QScriptValue globalObj = engine->globalObject();
globalObj.setProperty(s_curScriptPath,scriptObj);
}
//static
QString
IqtScriptKit::getScriptPath(QScriptEngine* engine)
{
QScriptValue globalObj = engine->globalObject();
return globalObj.property(s_curScriptPath,
QScriptValue::ResolveLocal).toString();
}
Ian Monroe wrote:
> First off, has anyone written a 'include' function that (ideally)
> evals the given file in the current context? The less ideal of evaling
> it into the global context would work too.
>
> Here is my attempt at it. I can't figure out how to get it to eval in
> the global context, let alone the calling context. I tried putting the
> file.readAll() within a javascript and then running .call(null) on it,
> but that didn't work. Any global variables defined work. It's possible
> that with javascript thats the only way its going to work I suppose.
> It would be nice to allow imported javascript files to use the
> standard "function blah()" syntax; currently only "blah = function()"
> works.
>
> m_scriptUrl is the url of the currently running script.
>
> bool
> ScriptImporter::include( const QString& relativeFilename )
> {
> KUrl includeUrl = m_scriptUrl.upUrl();
> includeUrl.addPath( relativeFilename );
> QFile file( includeUrl.toLocalFile() );
> if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
> return false;
> m_scriptEngine->evaluate( file.readAll() ), relativeFilename );
> return true;
> }
>
> Thanks,
> Ian
> _______________________________________________
> Qtscript-bindings mailing list
> Qtscript-bindings at kde.org
> https://mail.kde.org/mailman/listinfo/qtscript-bindings
>
More information about the Qtscript-bindings
mailing list