On Apr 3, 4:12 pm, Henry Grippa <[email protected]> wrote:
> Hi everybody,
>
> I have the following problem. I'm trying to do the following
>
> public class Test {
>
> public static class A extends ScriptableObject implements Scriptable {
>
> public A() {
> };
>
> public String getClassName() {
> return "A";
> }
>
> 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 A jsGet_a() {
> return a;
> }
>
> }
>
> 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.doSmth();" +
> "", "", 1, null).exec(cx, scope);
>
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> }
>
> and it gives me back the following output
>
> 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.Test.main(Test.java:55)
>
> which means that the first line ("new A().doSmth();") works fine but the
> second one causes the exception.
>
> As far as i understand it happens because inside jsGet_a() I should
> return something else other than just an instance of A. But i don't know
> what and how.
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
> I will really appreciate any help on this.
>
> Best regards,
> --H
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino