QSA-interest Archive, February 2006
eval'ing a file
Message 1 in thread
I am trying to load and evaluate a file from a script. The loaded file
contains a function declaration. However that function is unknown
after eval. Isn't this supposed to work, or what might be wrong here?
"Main" script:
--------------------------------------------------
eval(File.read('foo.qs'));
foo(); // <-- this fails because function unknown
--------------------------------------------------
foo.qs:
--------------------------------------------------
function foo()
{
debug('executing foo');
}
--------------------------------------------------
Seneca
To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx
Message 2 in thread
Seneca wrote:
> I am trying to load and evaluate a file from a script. The loaded file
> contains a function declaration. However that function is unknown
> after eval. Isn't this supposed to work, or what might be wrong here?
>
>
> "Main" script:
> --------------------------------------------------
> eval(File.read('foo.qs'));
>
> foo(); // <-- this fails because function unknown
> --------------------------------------------------
>
> foo.qs:
> --------------------------------------------------
> function foo()
> {
> debug('executing foo');
> }
> --------------------------------------------------
Unfortunatly no, because eval opens a separate scope for its execution
context. You have two options:
1. Make sure the functions you declare end up in the global scope. You
can do this by assigning them as variables in the eval'ed file.
foo = function() {
debug('executing foo()');
}
Since foo is not declared it will be stored in the global scope in which
case you can later on call foo.
2. Implement a QObject that has an evaluate() slot that calls
QSInterpreter::evaluate() on the current interpreter. This code will be
executed in the global context so you don't have to change the syntax of
the foo.qs file.
-
Gunnar
To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx
Message 3 in thread
I have implemented it as a static class which can be used in scripts as:
Script.load(code, scriptname, multiple = false);
Script.loadFile(filename, multiple = false);
Works perfectly this way. In case somebody needs the same I can
provide the code (it's no rocketscience though).
--
[ signature omitted ]
Message 4 in thread
Seneca, We encountered exactly the same problem yesterday.
Actually we would be glad if you could share the code.
Gunnar, we would like to request this as a feature
for QSA: support include files.
Main purpose on our side:
We would like to re-use in different scripts classes and
functions that have been code once.
thx
Wolfgang
Seneca wrote:
>
> I have implemented it as a static class which can be used in scripts as:
>
> Script.load(code, scriptname, multiple = false);
> Script.loadFile(filename, multiple = false);
>
> Works perfectly this way. In case somebody needs the same I can
> provide the code (it's no rocketscience though).
>
> --
> Seneca
>
> GS> Seneca wrote:
> >> I am trying to load and evaluate a file from a script. The loaded file
> >> contains a function declaration. However that function is unknown
> >> after eval. Isn't this supposed to work, or what might be wrong here?
> >>
> >>
> >> "Main" script:
> >> --------------------------------------------------
> >> eval(File.read('foo.qs'));
> >>
> >> foo(); // <-- this fails because function unknown
> >> --------------------------------------------------
> >>
> >> foo.qs:
> >> --------------------------------------------------
> >> function foo()
> >> {
> >> debug('executing foo');
> >> }
> >> --------------------------------------------------
>
> GS> Unfortunatly no, because eval opens a separate scope for its execution
> GS> context. You have two options:
>
> GS> 1. Make sure the functions you declare end up in the global scope. You
> GS> can do this by assigning them as variables in the eval'ed file.
>
> GS> foo = function() {
> GS> debug('executing foo()');
> GS> }
>
> GS> Since foo is not declared it will be stored in the global scope in which
> GS> case you can later on call foo.
>
> GS> 2. Implement a QObject that has an evaluate() slot that calls
> GS> QSInterpreter::evaluate() on the current interpreter. This code will be
> GS> executed in the global context so you don't have to change the syntax of
> GS> the foo.qs file.
>
> GS> -
> GS> Gunnar
>
> GS> To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx
>
> To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx
____________
Virus checked: AVK 16.5557 from 15.02.2006
To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx
Message 5 in thread
Wolfgang Trautenberg wrote:
> Seneca, We encountered exactly the same problem yesterday.
> Actually we would be glad if you could share the code.
>
> Gunnar, we would like to request this as a feature
> for QSA: support include files.
> Main purpose on our side:
> We would like to re-use in different scripts classes and
> functions that have been code once.
>
> thx
>
> Wolfgang
We already have this on our feature list for the next major version of
QSA. In the mean time I'll try to add an example to the 1.x packages
show how it can be done.
-
Gunnar
To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx
Message 6 in thread
WT> Seneca, We encountered exactly the same problem yesterday.
WT> Actually we would be glad if you could share the code.
WT> Gunnar, we would like to request this as a feature
WT> for QSA: support include files.
WT> Main purpose on our side:
WT> We would like to re-use in different scripts classes and
WT> functions that have been code once.
Hello Wolfgang,
I extracted it from my application and made a distinct objectfactory
for convenience use. Please not that I created this with Qt 4.1.0 and
QSA 1.2.0, so if you are on other versions it may need some tweaking.
Also since I am using it primarly on windows, comparisation for
allready loaded files is done case-insensitive.
To use the script class add it to your interpreter with:
#include "qsscriptfactory.h"
interpreter->addObjectFactory(new QSScriptFactory);
Then you can use it in your scripts as:
Script.load(code, scriptname, multiple = false);
Load script code from a string in 'code'.
'scriptname' is mandatory and must be unique for distinct scripts.
'multiple' false = ignore attemts to load same script twice.
true = allow multiple loading of same script.
Script.loadFile(filename, multiple = false);
Load script from file 'filename'.
'multiple' false = ignore attemts to load same script twice.
true = allow multiple loading of same script.
Example:
Script.loadFile('foo.qs'); // load/evaluate script in file foo.qs
Enjoy!
--
[ signature omitted ]
Message 7 in thread
Small bugfix:
void Script::load(const QString& code, const QString& name, bool multiple)
{
bool loaded(isLoaded(name));
if (multiple || !loaded) { // <--- this line changed
interpreter()->evaluate(code, 0, name);
if (!loaded) aNames.append(name);
} // if
} // load
--
[ signature omitted ]