Yes, it should return an object that has its parent scope and
prototype set. Use the Context.toObject() method:
public A jsGet_a() {
return Context.toObject(a, this);
}
Note that this will only work if the parent scope is set up in the
instance this method is called on. Rule of thumb: either use the
JavaScript constructor to create an object, or use the Java
constructor followed by a call to Context.toObject().
hannes
Thanks for reply, and as I understand the following example totally fits
your rule but it still doesn't work.
public class Test {
public static class A extends ScriptableObject implements Scriptable {
public A() {
};
public String getClassName() {
return "A";
}
public void jsConstructor() {
}
public void jsFunction_doSmth() {
System.out.println("I'm doing something");
};
}
public static class B extends ScriptableObject implements Scriptable {
private A a = new A();
public B() {
};
public String getClassName() {
return "B";
}
public void jsConstructor() {
}
public Object jsGet_a() {
return Context.toObject(a, this);
}
}
public static void main(String[] args) {
try {
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects(null, true);
ScriptableObject.defineClass(scope, A.class);
ScriptableObject.defineClass(scope, B.class);
cx.compileString("" +
"new A().doSmth();" +
"new B().a.getSmth();" +
"", "", 1, null).exec(cx, scope);
} catch (Exception e) {
e.printStackTrace();
}
}
}
the output is the same:
I'm doing something
org.mozilla.javascript.EcmaError: TypeError: Cannot find default value
for object. (#1)
at
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3557)
at
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3535)
at
org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3563)
at
org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3575)
at
org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:774)
at
org.mozilla.javascript.ScriptableObject.getDefaultValue(ScriptableObject.java:693)
at org.mozilla.javascript.ScriptRuntime.toString(ScriptRuntime.java:722)
at
org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3644)
at
org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2186)
at
org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:117)
at org.mozilla.javascript.gen.c1._c0(:1)
at org.mozilla.javascript.gen.c1.call()
at
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:401)
at
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3003)
at org.mozilla.javascript.gen.c1.call()
at org.mozilla.javascript.gen.c1.exec()
at com.arcticlake.Test4.main(Test4.java:58)
instead of expected:
I'm doing something
I'm doing something
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino