JaroslavTulach commented on a change in pull request #1011: Enhanced wrapper for Scripting API URL: https://github.com/apache/incubator-netbeans/pull/1011#discussion_r248576049
########## File path: platform/api.scripting/src/org/netbeans/modules/scripting/GraalEngine.java ########## @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.netbeans.modules.scripting; + +import java.io.Reader; +import javax.script.Bindings; +import javax.script.Invocable; +import javax.script.ScriptContext; +import javax.script.ScriptEngine; +import javax.script.ScriptException; +import org.graalvm.polyglot.PolyglotException; +import org.graalvm.polyglot.Source; +import org.graalvm.polyglot.Value; + +final class GraalEngine implements ScriptEngine, Invocable { + + private final GraalEngineFactory factory; + + GraalEngine(GraalEngineFactory f) { + this.factory = f; + } + + private String id() { + return factory.id; + } + + @Override + public GraalContext getContext() { + return factory.ctx; + } + + @Override + public GraalEngineFactory getFactory() { + return factory; + } + + @Override + public Object eval(String src, ScriptContext arg1) throws ScriptException { + Value result = evalImpl(arg1, src); + return unbox(result); + } + + private Value evalImpl(ScriptContext arg1, String src) throws ScriptException { + + try { + return ((GraalContext) arg1).ctx().eval(id(), src); + } catch (PolyglotException e) { + throw new ScriptException(e); + } + } + + @Override + public Object eval(Reader arg0, ScriptContext arg1) throws ScriptException { + Source src = Source.newBuilder(id(), arg0, null).buildLiteral(); + Value result = ((GraalContext)arg1).ctx().eval(src); + return unbox(result); + } + + @Override + public Object eval(String arg0) throws ScriptException { + return eval(arg0, factory.ctx); + } + + @Override + public Object eval(Reader arg0) throws ScriptException { + return eval(arg0, factory.ctx); + } + + @Override + public Object eval(String arg0, Bindings arg1) throws ScriptException { + return eval(arg0); + } + + @Override + public Object eval(Reader arg0, Bindings arg1) throws ScriptException { + return eval(arg0); + } + + @Override + public void put(String arg0, Object arg1) { + } + + @Override + public Object get(String arg0) { + return null; + } + + @Override + public Bindings getBindings(int scope) { + return getContext().getBindings(scope); + } + + @Override + public void setBindings(Bindings arg0, int arg1) { + } + + @Override + public Bindings createBindings() { + return null; + } + + @Override + public void setContext(ScriptContext arg0) { + throw new IllegalStateException(); + } + + @Override + public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { + final Value thisValue = (Value) thiz; Review comment: Good idea: 2ecb7b92d2 ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists
