Found that for me it's much easy works with jdk1.6 and its features.
Now my java code looks such:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
engine.put("args", params);
try {
engine.eval(JSON1);
engine.eval(code);
Invocable invocableEngine = (Invocable) engine;
return invocableEngine.invokeFunction("main").toString();
} catch (ScriptException e) {
throw new RuntimeException("Error in JS code", e);
} catch (NoSuchMethodException e) {
throw new RuntimeException("Can't find main method", e);
}
On 7 июл, 15:24, DarkIT <[EMAIL PROTECTED]> wrote:
> I tried to execute such code:
>
> final Scriptable scope = cx.initStandardObjects();
> final Script json = cx.compileString(JSON_CODE, "JSON", 1, null);
> scope.put("JSON", scope, json);
> scope.put("args", scope, params);
> final Function script = cx.compileFunction(scope, code, "main", 1,
> null);
> return Context.toString(script.call(cx, scope, scope, null));
>
> Where JSON_CODE is a string which contains JSON JavaScript code.
> "params" - string with json object. (for example: { "name":"test22"})
> "code" is my cusomt JS function:
>
> function main() {
> var myJSON = eval('('+args+')');
> return JSON.stringify(myJSON));
>
> }
>
> And it doesn't work. I received:
>
> org.mozilla.javascript.EcmaError: TypeError: Cannot find default value
> for object. (main#3)
>
> On 6 ÉÀÌ, 00:21, Attila Szegedi <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Jul 5, 2008, at 10:48 PM, DarkIT wrote:
>
> > > Thank's
> > > But how can i include this library to rhino ? that all other functions
> > > in different contexts and scopes can use it.
>
> > Well, simplest approach is: you can load it into a Script object, and š
> > then execute it in a scope, and then execute the Script object š
> > representing your actual program in the same scope object.
>
> > > Also it's more general question how can I write Java and JavaScript
> > > function which can comunicate without efforts - just write "brigde"
> > > which will do all work for given object type.
>
> > Well, Rhino does quite a lot of automatic marshaling of values from JS š
> > to Java and back already. I think you'd find what's in there already š
> > will be sufficient.
>
> > Attila.
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino