Subj : Adding an existing ScriptableObject instance to the scope? To : netscape.public.mozilla.jseng From : JPsychic Date : Fri Feb 04 2005 10:52 pm I have two classes SO1 and SO2 that subclass ScriptableObject I pre-create an instance of SO2.class called ISO2 using: ISO2 = new SO2(); Both classes SO1 and SO2 define a method: public void jsFunction_hello() { // Do nothing } Now, I register a new instance of SO1 into the scope using: --- final Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); try { ScriptableObject.defineClass(scope, SO1.class); } catch (Exception ex) { ex.printStackTrace(); System.exit(0); } Scriptable so = cx.newObject(scope, "com.package.SO1", null); scope.put("o1", scope, so); Context.exit(); --- I invoke the hello() function on 'o1' using: Object result = cx.evaluateString(sh.scope, "o1.hello()", "", 1, null); This works great. --- Now, I register the 'existing instance' of SO2 called ISO2 using: final Context cx = Context.enter(); Scriptable scope = cx.initStandardObjects(); scope.put("o2", scope, ISO2); Context.exit(); However, when I attempt to invoke the hello() function on 'o2': Object result = cx.evaluateString(sh.scope, "o1.hello()", "", 1, null); I get the following exception: org.mozilla.javascript.EcmaError: TypeError: hello is not a function. (#1) at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3240) at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3230) at org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3246) at org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3258) at org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3317) at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:1987) at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:114) at org.mozilla.javascript.gen.c1._c0(:1) at org.mozilla.javascript.gen.c1.call() at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:304) at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2769) at org.mozilla.javascript.gen.c1.call() at org.mozilla.javascript.gen.c1.exec() at org.mozilla.javascript.Context.evaluateString(Context.java:1220) --- So how do I add an existing instance of a ScriptableObject subclass into the scope and get access to its methods? Instead of: scope.put("o2", scope, ISO2); I tried: Object oISO2 = Context.javaToJS(ISO2, scope); ScriptableObject.putProperty(scope, "o2", oISO2); .... still didn't work. Then, I tried: try { ScriptableObject.defineClass(scope, so.getClass()); } catch (Exception ex) { throw new ScriptException(ex); } scope.put("o2", scope, ISO2); .... this didn't work either. Help? .