sdedic commented on a change in pull request #1011: Enhanced wrapper for Scripting API URL: https://github.com/apache/incubator-netbeans/pull/1011#discussion_r238710918
########## File path: platform/api.scripting/src/org/netbeans/modules/scripting/GraalContext.java ########## @@ -0,0 +1,128 @@ +/* + * 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.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import javax.script.Bindings; +import javax.script.ScriptContext; +import javax.script.SimpleBindings; +import org.graalvm.polyglot.Context; + +final class GraalContext implements ScriptContext { + private Context ctx; + private final WriterOutputStream writer = new WriterOutputStream(new OutputStreamWriter(System.out)); + private final WriterOutputStream errorWriter = new WriterOutputStream(new OutputStreamWriter(System.err)); + private SimpleBindings bindings; + + synchronized final Context ctx() { + if (ctx == null) { + final Context.Builder b = Context.newBuilder(); + b.out(writer); + b.err(errorWriter); + // XXX: workaround until python is fixed + b.allowAllAccess(true); + ctx = b.build(); + } + return ctx; + } + + + @Override + public void setBindings(Bindings bindings, int scope) { + throw new UnsupportedOperationException(); + } + + @Override + public Bindings getBindings(int scope) { + if (scope != GLOBAL_SCOPE) { + throw new IllegalArgumentException(); + } + if (bindings == null) { + Map<String,Object> map = ctx().getPolyglotBindings().as(Map.class); + bindings = new SimpleBindings(map); + } + return bindings; + } + + @Override + public void setAttribute(String name, Object value, int scope) { + throw new UnsupportedOperationException(); + } + + @Override + public Object getAttribute(String name, int scope) { + return null; + } + + @Override + public Object removeAttribute(String name, int scope) { + throw new UnsupportedOperationException(); + } + + @Override + public Object getAttribute(String name) { + return null; + } + + @Override + public int getAttributesScope(String name) { + return -1; + } + + @Override + public Writer getWriter() { + return writer.getWriter(); + } + + @Override + public Writer getErrorWriter() { + return errorWriter.getWriter(); + } + + @Override + public void setWriter(Writer writer) { + this.writer.setWriter(writer); + } + + @Override + public void setErrorWriter(Writer writer) { + this.errorWriter.setWriter(writer); + } + + @Override + public Reader getReader() { + throw new UnsupportedOperationException(); Review comment: I would rather recommend to return an empty reader, which immediately reports an `EOF`. ---------------------------------------------------------------- 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
