From:  "Matthias" <Matthias.Igel@gmx.net>
Date:  10 Jul 2006 16:12:01 Hong Kong Time
Newsgroup:  news.mozilla.org/netscape.public.mozilla.jseng
Subject:  

convert java object to js

NNTP-Posting-Host:  212.204.68.178

Hello everyone,

I'm new to rhino (and js ;-)) and have a(n) (understanding) problem
converting java objects to js. Here is my code:

import org.mozilla.javascript.*;
import java.io.*;

public class FirstTry extends ScriptableObject{

	private SecondTry str;

	public String getClassName() {
		return "FirstTry";
	}

	public void jsConstructor(int test) {
		this.str = new SecondTry();
	}

	public SecondTry jsGet_SecondTry() {return str;}

	public FirstTry() {}
}



import org.mozilla.javascript.*;

public class SecondTry extends ScriptableObject {

	public String member = "Hello World";

	public String getClassName() {
		return "SecondTry";
	}

	public SecondTry() {
	}

	public void jsConstructor() {}

	public String jsGet_member () {return member;}
}


import java.io.File;
import java.io.FileReader;

import org.mozilla.javascript.*;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String filename = "firstTry.js";
		Context cx = Context.enter();
		try {
			Scriptable scope = cx.initStandardObjects();
			File file = new File(filename);
			FileReader reader = new FileReader(file);
			ScriptableObject.defineClass(scope, FirstTry.class);
			ScriptableObject.defineClass(scope, SecondTry.class);
			Object result = cx.evaluateReader(scope, reader, "me", 1, null);
			System.out.println(cx.toString(result));
		} catch (Exception e) {
			System.err.println(e.toString());
		}
	}
}

Here is the code of my js- file:

var test = new FirstTry(12);
test.SecondTry.member;

Executing Test simply gives me "undefined".

When I change the content of my js- file to

var test = new SecondTry();
test.member;

it gives me "Hello World" as expected.

So I wonder, what I missed to map java object out of java to js.

Regards,
Matthias