| 1 | use v5; | ||
| 2 | |||
| 3 | package Perlito5::Java::JavaxScript; | ||
| 4 | use Perlito5; | ||
| 5 | use Perlito5::Runtime; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | sub meta_file_name { | ||
| 9 | "META-INF/services/javax.script.ScriptEngineFactory" | ||
| 10 | } | ||
| 11 | |||
| 12 | sub emit_meta_file { | ||
| 13 | "org.perlito.Perlito5.Perlito5ScriptEngineFactory\n" | ||
| 14 | } | ||
| 15 | |||
| 16 | sub emit_java_ScriptContext { | ||
| 17 | return <<'EOT' | ||
| 18 | package org.perlito.Perlito5; | ||
| 19 | |||
| 20 | import javax.script.*; | ||
| 21 | import java.util.*; | ||
| 22 | import java.io.*; | ||
| 23 | |||
| 24 | class Perlito5ScriptContext extends SimpleScriptContext { | ||
| 25 | public Perlito5ScriptContext() { | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | EOT | ||
| 30 | |||
| 31 | } # end of emit_java_ScriptContext() | ||
| 32 | |||
| 33 | |||
| 34 | sub emit_java_EngineFactory { | ||
| 35 | # TODO - get constants from Perlito5::Runtime | ||
| 36 | |||
| 37 | my $engineVersion = $Perlito5::VERSION; | ||
| 38 | my $languageVersion = join ".", @Perlito5::PERL_VERSION; | ||
| 39 | |||
| 40 | return <<'EOT' | ||
| 41 | package org.perlito.Perlito5; | ||
| 42 | |||
| 43 | import javax.script.*; | ||
| 44 | import java.util.*; | ||
| 45 | import java.io.*; | ||
| 46 | |||
| 47 | public final class Perlito5ScriptEngineFactory implements javax.script.ScriptEngineFactory { | ||
| 48 | |||
| 49 | @Override | ||
| 50 | public String getEngineName() { | ||
| 51 | return "perlito5"; | ||
| 52 | } | ||
| 53 | @Override | ||
| 54 | public List<String> getExtensions() { | ||
| 55 | return Arrays.asList("pl"); | ||
| 56 | } | ||
| 57 | @Override | ||
| 58 | public String getLanguageName() { | ||
| 59 | return "Perl"; | ||
| 60 | } | ||
| 61 | EOT | ||
| 62 | . <<"EOT" | ||
| 63 | \@Override | ||
| 64 | public String getEngineVersion() { | ||
| 65 | return "$engineVersion"; | ||
| 66 | } | ||
| 67 | \@Override | ||
| 68 | public String getLanguageVersion() { | ||
| 69 | return "$languageVersion"; | ||
| 70 | } | ||
| 71 | EOT | ||
| 72 | . <<'EOT' | ||
| 73 | @Override | ||
| 74 | public String getMethodCallSyntax(String obj, String m, String[] args) { | ||
| 75 | String ret = obj; | ||
| 76 | ret += "->" + m + "("; | ||
| 77 | for (int i = 0; i < args.length; i++) { | ||
| 78 | ret += args[i]; | ||
| 79 | if (i == args.length - 1) | ||
| 80 | ret += ")"; | ||
| 81 | else | ||
| 82 | ret += ","; | ||
| 83 | } | ||
| 84 | return ret; | ||
| 85 | } | ||
| 86 | @Override | ||
| 87 | public List<String> getMimeTypes() { | ||
| 88 | // https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=229148 | ||
| 89 | return Arrays.asList("text/x-perl", "text/x-perl5"); | ||
| 90 | } | ||
| 91 | @Override | ||
| 92 | public List<String> getNames() { | ||
| 93 | return Arrays.asList("perl", "perl5", "perlito", "perlito5", "Perl", "Perl5", "Perlito", "Perlito5"); | ||
| 94 | } | ||
| 95 | @Override | ||
| 96 | public String getOutputStatement(String o) { | ||
| 97 | return "print " + o; | ||
| 98 | } | ||
| 99 | @Override | ||
| 100 | public Object getParameter(String p) { | ||
| 101 | if (p == null) | ||
| 102 | return null; | ||
| 103 | if (p.equals(ScriptEngine.ENGINE)) | ||
| 104 | return getEngineName(); | ||
| 105 | if (p.equals(ScriptEngine.ENGINE_VERSION)) | ||
| 106 | return getEngineVersion(); | ||
| 107 | if (p.equals(ScriptEngine.NAME)) | ||
| 108 | return "perlito5"; | ||
| 109 | if (p.equals(ScriptEngine.LANGUAGE)) | ||
| 110 | return getLanguageName(); | ||
| 111 | if (p.equals(ScriptEngine.LANGUAGE_VERSION)) | ||
| 112 | return getLanguageVersion(); | ||
| 113 | if (p.equals("THREADING")) | ||
| 114 | return null; // not thread-safe | ||
| 115 | return null; | ||
| 116 | } | ||
| 117 | @Override | ||
| 118 | public String getProgram(String[] lines) { | ||
| 119 | StringBuffer ret = new StringBuffer(); | ||
| 120 | for (int i = 0; i < lines.length; i++) { | ||
| 121 | ret.append(lines[i]); | ||
| 122 | ret.append(";\n"); | ||
| 123 | } | ||
| 124 | return ret.toString(); | ||
| 125 | } | ||
| 126 | @Override | ||
| 127 | public ScriptEngine getScriptEngine() { | ||
| 128 | org.perlito.Perlito5.LibPerl.init(); | ||
| 129 | org.perlito.Perlito5.Main.main(new String[]{"-Cinit"}); | ||
| 130 | |||
| 131 | // turn on autoflush | ||
| 132 | PlV.STDOUT.set_autoflush(PlCx.TRUE); | ||
| 133 | PlV.STDERR.set_autoflush(PlCx.TRUE); | ||
| 134 | |||
| 135 | Perlito5ScriptEngine e = new Perlito5ScriptEngine(); | ||
| 136 | e.setFactory(this); | ||
| 137 | return e; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | EOT | ||
| 141 | |||
| 142 | } # end of emit_java_EngineFactory() | ||
| 143 | |||
| 144 | sub emit_java_Engine { | ||
| 145 | return <<'EOT' | ||
| 146 | package org.perlito.Perlito5; | ||
| 147 | |||
| 148 | import javax.script.*; | ||
| 149 | import java.util.*; | ||
| 150 | import java.io.*; | ||
| 151 | |||
| 152 | class Perlito5ScriptEngine implements javax.script.ScriptEngine { | ||
| 153 | |||
| 154 | protected ScriptContext context; | ||
| 155 | private Perlito5ScriptEngineFactory factory; | ||
| 156 | |||
| 157 | public Perlito5ScriptEngine() { | ||
| 158 | // TODO | ||
| 159 | context = new Perlito5ScriptContext(); | ||
| 160 | } | ||
| 161 | public Perlito5ScriptEngine(Bindings n) { | ||
| 162 | this(); | ||
| 163 | if (n == null) { | ||
| 164 | throw new NullPointerException("n is null"); | ||
| 165 | } | ||
| 166 | context.setBindings(n, ScriptContext.ENGINE_SCOPE); | ||
| 167 | } | ||
| 168 | |||
| 169 | @Override | ||
| 170 | public ScriptEngineFactory getFactory() { | ||
| 171 | synchronized (this) { | ||
| 172 | if (factory == null) { | ||
| 173 | factory = new Perlito5ScriptEngineFactory(); | ||
| 174 | } | ||
| 175 | } | ||
| 176 | return factory; | ||
| 177 | } | ||
| 178 | void setFactory(final Perlito5ScriptEngineFactory owningFactory) { | ||
| 179 | factory = owningFactory; | ||
| 180 | } | ||
| 181 | |||
| 182 | public void setContext(ScriptContext ctxt) { | ||
| 183 | if (ctxt == null) { | ||
| 184 | throw new NullPointerException("null context"); | ||
| 185 | } | ||
| 186 | context = ctxt; | ||
| 187 | } | ||
| 188 | public ScriptContext getContext() { | ||
| 189 | return context; | ||
| 190 | } | ||
| 191 | public Bindings createBindings() { | ||
| 192 | return new SimpleBindings(); | ||
| 193 | } | ||
| 194 | public Bindings getBindings(int scope) { | ||
| 195 | |||
| 196 | if (scope == ScriptContext.GLOBAL_SCOPE) { | ||
| 197 | return context.getBindings(ScriptContext.GLOBAL_SCOPE); | ||
| 198 | } else if (scope == ScriptContext.ENGINE_SCOPE) { | ||
| 199 | return context.getBindings(ScriptContext.ENGINE_SCOPE); | ||
| 200 | } else { | ||
| 201 | throw new IllegalArgumentException("Invalid scope value."); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | public void setBindings(Bindings bindings, int scope) { | ||
| 205 | |||
| 206 | if (scope == ScriptContext.GLOBAL_SCOPE) { | ||
| 207 | context.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);; | ||
| 208 | } else if (scope == ScriptContext.ENGINE_SCOPE) { | ||
| 209 | context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);; | ||
| 210 | } else { | ||
| 211 | throw new IllegalArgumentException("Invalid scope value."); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | public void put(String key, Object value) { | ||
| 215 | |||
| 216 | Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE); | ||
| 217 | if (nn != null) { | ||
| 218 | nn.put(key, value); | ||
| 219 | } | ||
| 220 | |||
| 221 | } | ||
| 222 | public Object get(String key) { | ||
| 223 | Bindings nn = getBindings(ScriptContext.ENGINE_SCOPE); | ||
| 224 | if (nn != null) { | ||
| 225 | return nn.get(key); | ||
| 226 | } | ||
| 227 | return null; | ||
| 228 | } | ||
| 229 | public Object eval(Reader reader, Bindings bindings ) throws ScriptException { | ||
| 230 | ScriptContext ctxt = getScriptContext(bindings); | ||
| 231 | return eval(reader, ctxt); | ||
| 232 | } | ||
| 233 | public Object eval(String script, Bindings bindings) throws ScriptException { | ||
| 234 | ScriptContext ctxt = getScriptContext(bindings); | ||
| 235 | return eval(script , ctxt); | ||
| 236 | } | ||
| 237 | public Object eval(Reader reader) throws ScriptException { | ||
| 238 | return eval(reader, context); | ||
| 239 | } | ||
| 240 | public Object eval(String script) throws ScriptException { | ||
| 241 | return eval(script, context); | ||
| 242 | } | ||
| 243 | public Object eval(Reader reader, ScriptContext ctxt) throws ScriptException { | ||
| 244 | StringBuilder buf = new StringBuilder(); | ||
| 245 | boolean eof = false; | ||
| 246 | while (!eof) { | ||
| 247 | int len = 1000; | ||
| 248 | char[] c = new char[len]; | ||
| 249 | int num_chars = 0; | ||
| 250 | try { | ||
| 251 | num_chars = reader.read(c, 0, len); | ||
| 252 | if (num_chars > 0) { | ||
| 253 | String s = new String(c, 0, num_chars); | ||
| 254 | buf.append(s); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | catch(IOException e) { | ||
| 258 | throw new ScriptException(e); | ||
| 259 | } | ||
| 260 | if (num_chars <= 0) { | ||
| 261 | eof = true; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | String s; | ||
| 265 | s = buf.toString(); | ||
| 266 | return eval(s, ctxt); | ||
| 267 | } | ||
| 268 | public Object eval(String script, ScriptContext ctxt) throws ScriptException { | ||
| 269 | Object[] ret = org.perlito.Perlito5.Main.apply( "Perlito5::eval_string", script ); | ||
| 270 | PlObject perlErr = PlV.Scalar_EVAL_ERROR; | ||
| 271 | if (perlErr.to_boolean()) { | ||
| 272 | throw new ScriptException(perlErr.toString()); | ||
| 273 | } | ||
| 274 | if (ret.length > 0) { | ||
| 275 | return ret[0]; | ||
| 276 | } | ||
| 277 | return null; | ||
| 278 | } | ||
| 279 | |||
| 280 | protected ScriptContext getScriptContext(Bindings nn) { | ||
| 281 | |||
| 282 | Perlito5ScriptContext ctxt = new Perlito5ScriptContext(); | ||
| 283 | Bindings gs = getBindings(ScriptContext.GLOBAL_SCOPE); | ||
| 284 | |||
| 285 | if (gs != null) { | ||
| 286 | ctxt.setBindings(gs, ScriptContext.GLOBAL_SCOPE); | ||
| 287 | } | ||
| 288 | |||
| 289 | if (nn != null) { | ||
| 290 | ctxt.setBindings(nn, | ||
| 291 | ScriptContext.ENGINE_SCOPE); | ||
| 292 | } else { | ||
| 293 | throw new NullPointerException("Engine scope Bindings may not be null."); | ||
| 294 | } | ||
| 295 | |||
| 296 | ctxt.setReader(context.getReader()); | ||
| 297 | ctxt.setWriter(context.getWriter()); | ||
| 298 | ctxt.setErrorWriter(context.getErrorWriter()); | ||
| 299 | |||
| 300 | return ctxt; | ||
| 301 | |||
| 302 | } | ||
| 303 | } | ||
| 304 | EOT | ||
| 305 | |||
| 306 | } # end of emit_java_Engine() | ||
| 307 | |||
| 308 | 1; | ||
| 309 | |||
| 310 | __END__ |