Subj : Scoping issue with functions and external objects To : netscape.public.mozilla.jseng From : Robert Finger Date : Fri Sep 09 2005 04:09 pm This is a multi-part message in MIME format. ------_=_NextPart_001_01C5B571.FDBEB3A8 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable Hi All, I am fairly new to using Rhino for running so please bear with me if I seem somewhat unknowledgeble about some things. The background: We have a java application that uses JavaScript to perform certain types of calculations. These calculations are user configurable and are stored in our database in XML format. At runtime, we parse the XML, write a script and pass it to Rhino to run. At many points in the generated script, we are calling back into java to do things like execute SQL statements, etc. So far, so good. In the next phase of our app, we'd like to provide the ability to define and call functions in the calculation configurations. This is easy enough if the JS were not calling back into java - we simply create the function definitions as part of the main script. However, the java methods that are called from the generated script need to be able to "see" the variables local to the JS function from which they are called. Our best solution, so far, has been to create FunctionObjects for every defined function and put each one in its own scope. However, we do not know how to find the scope that contains the variables local to the function. If, for example, we have function doCodeLookup: function doCodeLookup( ) { var code =3D ''; code =3D .... // a sequence of statements that set code to some value var sql =3D"Select CodeDescription From AsCode Where CodeValue =3D '[Code]'"; =20 // EXT is a reference to a java object var desc =3D EXT.executeSql( this, sql ); return desc; } , executeSql() then needs to get the value of 'code' and can't. Passing 'this' provides the scope that contains doCodeLookup() but not its contents. So instead of delaring var code =3D ''; in the function we put it in a separate script that compiles in the parent scope of the function. It works but is kind of ugly. The main question is: Is there a way to pass a reference to the scope containing the function contents to the external method? We aren't married to any particular approach here so any ideas are welcome. I'd be happy to expand any explanation here that isn't clear, I didn't want to be any more long winded that I already was. TIA, Bob Finger Senior Developer AdminServer, Inc. ------_=_NextPart_001_01C5B571.FDBEB3A8 Content-Type: text/html; charset="US-ASCII" Content-Transfer-Encoding: quoted-printable
Hi All,
I am fairly new to using Rhino for = running so please bear with me if I seem somewhat unknowledgeble about = some things.
The background:
We have a java application that uses = JavaScript to perform certain types of calculations. These calculations = are user configurable and are stored in our database in XML format. At = runtime, we parse the XML, write a script and pass it to Rhino to run. = At many points in the generated script, we are calling back into java to = do things like execute SQL statements, etc. So far, so = good.
In the next phase of our app, we'd like = to provide the ability to define and call functions in the calculation = configurations. This is easy enough if the JS were not calling back into = java - we simply create the function definitions as part of the main = script. However, the java methods that are called from the generated = script need to be able to "see" the variables local to the JS = function from which they are called.
Our best solution, so far, has been to = create FunctionObjects for every defined function and put each one in = its own scope. However, we do not know how to find the scope that = contains the variables local to the function. If, for example, we have = function doCodeLookup:
function doCodeLookup( ) {
var code =3D '';
code =3D …. // a sequence of =
statements that set code to some value
var sql =3D"Select CodeDescription From =
AsCode Where CodeValue =3D '[Code]'";
=
// EXT is a reference to a java =
object
var desc =3D EXT.executeSql( this, sql =
);
return desc;
}
, executeSql() then needs to get the = value of 'code' and can't. Passing 'this' provides the scope that = contains doCodeLookup() but not its contents. So instead of delaring var = code =3D ''; in the function we put it in a separate script that = compiles in the parent scope of the function. It works but is kind of = ugly.
The main question is: Is there a way to = pass a reference to the scope containing the function contents to the = external method?
We aren't married to any particular = approach here so any ideas are welcome. I'd be happy to expand any = explanation here that isn't clear, I didn't want to be any more long = winded that I already was.
TIA,
Bob Finger
Senior Developer
AdminServer, Inc.