Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script functions to script object

9 views
Skip to first unread message

Alex Fernandez Nogueira

unread,
Oct 22, 2009, 4:55:26 PM10/22/09
to dev-tech-js-...@lists.mozilla.org
Hi all,
I have a question about dynamically create javascript objects:

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 |

Alex

unread,
Oct 24, 2009, 7:12:49 PM10/24/09
to
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.

David Parks

unread,
Oct 25, 2009, 2:46:08 PM10/25/09
to dev-tech-js-...@lists.mozilla.org
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);

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

Alex

unread,
Oct 25, 2009, 4:16:40 PM10/25/09
to
Hi,

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".

0 new messages