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

How to create a 'real' array that passes 'instanceof Array'

41 views
Skip to first unread message

Raphael Pigulla

unread,
Jun 22, 2010, 5:00:09 AM6/22/10
to dev-tech-js-...@lists.mozilla.org
Hello,

I'm trying to run JSLint using Rhino, but I've encountered a problem that has somewhat stumped me. How can I create a 'real' array from Java? Here's what I've been trying:

Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
String src = "function f(a) { return a instanceof Array; };";

cx.evaluateString(scope, src, "<src>", 0, null);

Function f = (Function) scope.get("f", scope);
Object[] fArgs = new Object[]{ new NativeArray(0) };
Object result = f.call(cx, scope, scope, fArgs);

System.out.println(Context.toString(result));
Context.exit();

But result is false. What am I doing wrong?

P.S.: Since I don't want to mess with JSLint's source, it is essential that I can create an object that passes the "instanceof" test.

Attila Szegedi

unread,
Jun 22, 2010, 5:35:43 AM6/22/10
to Raphael Pigulla, dev-tech-js-...@lists.mozilla.org
Try

cx.newArray(scope, 0)

instead. Constructing a NativeArray object directly won't work, 'cause it still needs to have its prototype set to the value of "Array.prototype" from your scope. newArray() will take care of all of that.

Attila.

Raphael Pigulla

unread,
Jun 22, 2010, 5:48:50 AM6/22/10
to dev-tech-js-...@lists.mozilla.org
> Try
> cx.newArray(scope, 0)
> instead.

That did it. Thank you very much.

0 new messages