I have a jscript stream I *can't change, *like this:
var msg = "hello";
function printMsg(){
alert(msg);
}
And I would like declare an object (ex. "printer") to execute scripts like
"printer.printMsg()".
I've try many things like:
Script scriptObjectPrinter =
(Script)context.compileString([STREAM],"printer",1,null);
Object o = scriptObjectPrinter.exec(context, newScope);
scope.put("printer", scope, o);
//Another option
//ScriptableObject.putProperty(scriptObjectPrinter, "printer",
scriptObjectPrinter);
Script scriptObjectEx =
(Script)context.compileString("printer.printMsg();","script",1,null);
scriptObjectEx.exec(context, newScope);
But I can't create the objects structure in Rhino context.
Anyone have an idea?
Thanks!
--
Alex Fernandez
| ale...@gmail.com |
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.
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);
Hi,
I try with this:
....
Thanks.
_______________________________________________
dev-tech-js-engine-rhino mailing list
dev-tech-js-...@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
It seems that this is not the problem. I have try with this code (more
simple), but rhino maps the script as a function, no an object.
String text = "var msg = 'hello'; \n function printMsg(){ alert
(msg);} ";
Script scriptObjectPrinter =(Script)context.compileString
(text,"printer",0,null);
ScriptableObject.putProperty(scope, "printer",
scriptObjectPrinter);
Script scriptObjectEx = (Script)context.compileString
("printer.printMsg();","script",1,null);
scriptObjectEx.exec(context, scope);
The result is that it can't find a method in the object "printer".