public AbstractCompiler(Environment environment, Context context){ this.context = context; this.scope = environment.getScope(); this.environment = environment; }
@Signature public Set<String> getSuperGlobals() { return environment.getScope().superGlobals; }
@Signature public static void addClassPath(Environment env, File file) throws IOException { try { env.getScope().getClassLoader().addLibrary(file.toURI().toURL()); } catch (Throwable t) { throw new IOException("Error, could not add URL to system classloader, " + t.getMessage()); } } }
@Signature(@Arg("name")) public Memory hasSuperGlobal(Environment env, Memory... args) { return environment.getScope().superGlobals.contains(args[0].toString()) ? Memory.TRUE : Memory.FALSE; }
@Signature(@Arg("name")) public static Memory exists(Environment env, Memory... args) throws IOException { String name = args[0].toString(); if (name.startsWith("res:///")) { name = name.substring(7); } else if (name.startsWith("res://")) { name = name.substring(6); } return env.getScope().getClassLoader().getResource(name) == null ? Memory.FALSE : Memory.TRUE; }
@Override @Signature({@Arg("path")}) public Memory __construct(Environment env, Memory... args) throws IOException { super.__construct(env, args[0], StringMemory.valueOf("r")); //long t = System.currentTimeMillis(); String path = this.getPath().replace('\\', '/').replace("//", "/"); if (path.startsWith("/")) { path = path.substring(1); } setPath("res://" + path); url = env.getScope().getClassLoader().getResource(path); if (url == null) { throw new IOException("Resource not found - " + getPath()); } stream = url.openStream(); //System.err.println(path + ", time = " + (System.currentTimeMillis() - t)); return Memory.NULL; }
@Signature(@Arg("name")) public static Memory getResources(Environment env, Memory... args) throws IOException { Enumeration<URL> list = env.getScope().getClassLoader().getResources(args[0].toString()); ArrayMemory r = new ArrayMemory(); while (list.hasMoreElements()) { URL url = list.nextElement(); if (url != null) { ResourceStream rs = new ResourceStream(env, url.openStream()); rs.setPath("res://" + args[0]); r.add(rs); } } return r.toConstant(); } }
@Signature({ @Arg("name"), @Arg(value = "value", optional = @Optional("null")) }) public Memory addSuperGlobal(Environment env, Memory... args) { if (hasSuperGlobal(env, args[0]).toBoolean()) { environment.exception("Super-global variable $%s already exists", args[0]); } environment.getScope().superGlobals.add(args[0].toString()); environment.getGlobals().putAsKeyString(args[0].toString(), args[1].toImmutable()); return Memory.UNDEFINED; }
public static Memory getValue(Debugger context, String value) { DebugTick tick = context.getRegisteredTick(); Environment environment = new Environment(new CompileScope(tick.getEnvironment().getScope())); try { return LangFunctions.eval(environment, tick.getTrace(), tick.getLocals(), "return " + value + ";"); } catch (Throwable throwable) { return null; } }
@SuppressWarnings("unchecked") protected Memory includeResource(String name, ArrayMemory globals){ ByteArrayOutputStream output = new ByteArrayOutputStream(); Environment environment; if (isConcurrent()) { environment = new ConcurrentEnvironment(newScope(), output); } else { environment = new Environment(newScope(), output); } File file = new File(Thread.currentThread().getContextClassLoader().getResource("resources/" + name).getFile()); Context context = new Context(file); JvmCompiler compiler = new JvmCompiler(environment, context, getSyntax(context)); ModuleEntity module = compiler.compile(); environment.getScope().loadModule(module); try { environment.registerModule(module); } catch (Throwable throwable) { throw new RuntimeException(throwable); } if (globals != null) environment.getGlobals().putAll(globals); Memory memory = module.includeNoThrow(environment, environment.getGlobals()); try { environment.doFinal(); } catch (Throwable throwable) { throw new RuntimeException(throwable); } lastOutput = output.toString(); return memory; }
protected Memory runDynamic(String code, boolean returned){ runIndex += 1; Environment environment = new Environment(newScope()); code = (returned ? "return " : "") + code + ";"; Context context = new Context(code); JvmCompiler compiler = new JvmCompiler(environment, context, getSyntax(context)); ModuleEntity module = compiler.compile(); environment.getScope().loadModule(module); try { environment.registerModule(module); } catch (Throwable throwable) { throw new RuntimeException(throwable); } return module.includeNoThrow(environment); }
protected Memory run(String code, boolean returned){ runIndex += 1; Environment environment = new Environment(newScope()); code = "class TestClass { static function test(){ " + (returned ? "return " : "") + code + "; } }"; Context context = new Context(code); JvmCompiler compiler = new JvmCompiler(environment, context, getSyntax(context)); ModuleEntity module = compiler.compile(); environment.getScope().loadModule(module); ClassEntity entity = module.findClass("TestClass"); try { return entity.findMethod("test").invokeStatic(environment); } catch (Throwable throwable) { throw new RuntimeException(throwable); } }
CompileScope compileScope = this.analyzer.getEnvironment().getScope();