javax.script - rhino - how can view or edit native functions?

1.1k views Asked by At

I want to implement a function that is similar to JSON.stringify.

When I call:

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
    Object eval = engine.eval("JSON.stringify");
    System.out.println(eval);

I get sun.org.mozilla.javascript.internal.IdFunctionObject. But how can I really view how is it implemented ? Can I edit or add such "native" functions ?

Don't offer to implement sun.org.mozilla.javascript.internal.Function - I know this ability, but want gain some knowledge about how native functions work.

1

There are 1 answers

2
Nicholas On

You can't eval JSON.stringify because it's not implemented in Rhino. (It's mostly a browser based thing, although I guess Nashorn and node do have it).

You can eval the contents of json2.js which implements JSON.stringify and then your code should work.

But if you just want to stringify, then I would use org.mozilla.javascript.NativeJSON.stringify.