Um, I could be missing something here because I didn't quite follow every
detail of your code (I'm going to blame it on the fact that I'm reading this
late at night and I'm sleep). But I think you are passing the wrong scope
into your exec and putProperty methods, am I reading it wrong?
Original:
==========
ScriptableObject.putProperty(scriptableObject,"myObject", scriptObject);
Script scriptObject2 = (Script)
context.compileString("myObject.test();","script",0,null);
scriptObject2.exec(context, scriptableObject);
Updated (note variable "scope" in 1st and 3rd line):
=====================================================
ScriptableObject.putProperty(scriptableObject,"myObject", scope);
Script scriptObject2 = (Script)
context.compileString("myObject.test();","script",0,null);
scriptObject2.exec(context, scope);
-----Original Message-----
From:
dev-tech-js-engine-rhino-bounces+davidparks21=yahoo....@lists.mozilla.org
[mailto:dev-tech-js-engine-rhino-bounces+davidparks21=yahoo....@lists.mozill
a.org] On Behalf Of Alex
Sent: Sunday, October 25, 2009 6:13 AM
To: [email protected]
Subject: Re: Script functions to script object
Hi,
I try with this:
Context context = Context.enter();
ScriptableObject scope =
context.initStandardObjects();
....
CompilerEnvirons compilerEnv = new
CompilerEnvirons();
compilerEnv.initFromContext(context);
compilerEnv.setGenerateDebugInfo(true);
variableElement.setScript("function
test(){return \"hello
\";}");
ClassCompiler compiler = new
ClassCompiler(compilerEnv);
compiler.setMainMethodClass(variableElement.getName());
Object[] classes =
compiler.compileToClassFiles
(variableElement.getScript(), variableElement.getName(), 0,
variableElement.getName());
GeneratedClassLoader loader =
context.createClassLoader
(context.getApplicationClassLoader());
Class<?> clazz = null;
for (int i = 0; i < classes.length; i += 2)
{
final String clazzName = (String) classes[i];
final byte[] clazzBytes = (byte[]) classes[i
+1];
Class<?> c = loader.defineClass(clazzName,
clazzBytes);
loader.linkClass(c);
if (i == 0) {
clazz = c;
}
}
Script scriptObject = (Script)Context.javaToJS
(clazz.newInstance(), scriptableObject);
ScriptableObject.putProperty(scriptableObject,
"myObject", scriptObject);
Script scriptObject2 = (Script)
context.compileString("myObject.test();","script",0,null);
scriptObject2.exec(context,
scriptableObject);
But the intrepeter don't found the function "test()" in the script
object "myObject".
Someone can tell me if this is possible?
Thanks.
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino