[Qtscript-bindings] a 'include' method
Ian Monroe
ian at monroe.nu
Tue Aug 12 19:54:09 CEST 2008
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
More information about the Qtscript-bindings
mailing list