Example #1
0
 @Override
 public Task awaitTask() throws InterruptedException {
   for (; ; ) {
     try {
       com.github.anba.es6draft.ast.Script parsedScript = read(realm, line);
       if (parsedScript == null) {
         return null;
       }
       if (parsedScript.getStatements().isEmpty()) {
         continue;
       }
       return new EvalPrintTask(realm, parsedScript);
     } catch (RuntimeException e) {
       return new ThrowExceptionTask<>(e);
     } catch (Error e) {
       return new ThrowErrorTask<>(e);
     }
   }
 }
Example #2
0
 /**
  * REPL: Read
  *
  * @param realm the realm instance
  * @param line the current line
  * @return the parsed script node or {@coden null} if the end of stream has been reached
  */
 private com.github.anba.es6draft.ast.Script read(Realm realm, int[] line) {
   StringBuilder sourceBuffer = new StringBuilder();
   for (String prompt = PROMPT; ; prompt = "") {
     String s = console.readLine(prompt);
     if (s == null) {
       return null;
     }
     sourceBuffer.append(s).append('\n');
     String sourceCode = sourceBuffer.toString();
     Source source = new Source(Paths.get(".").toAbsolutePath(), "typein", line[0]);
     try {
       com.github.anba.es6draft.ast.Script script = parse(realm, source, sourceCode);
       line[0] += script.getEndLine() - script.getBeginLine();
       return script;
     } catch (ParserEOFException e) {
       continue;
     } catch (ParserException e) {
       throw new ParserExceptionWithSource(e, source, sourceCode);
     }
   }
 }
Example #3
0
  private String sourceMap(Script script) {
    if (!compilerOptions.contains(Option.SourceMap)) {
      return null;
    }
    String sourceFile = script.getSourceFile();
    Path path;
    try {
      path = Paths.get(sourceFile);
    } catch (InvalidPathException e) {
      // return here if 'sourceFile' is not a valid path
      return null;
    }
    // line numbers are limited to uint16 in bytecode, valid line count not needed for smap
    final int maxLineCount = 0xffff;
    try (Formatter smap = new Formatter(Locale.ROOT)) {
      // Header
      // - ID
      smap.format("SMAP%n");
      // - OutputFileName
      smap.format("%s%n", script.getSourceFile());
      // - DefaultStratumId
      smap.format("Script%n");
      // Section
      // - StratumSection
      smap.format("*S Script%n");
      // - FileSection
      smap.format("*F%n");
      // -- FileInfo
      smap.format("+ 1 %s%n%s%n", path.getFileName(), path);
      // - LineSection
      smap.format("*L%n");
      // -- LineInfo
      smap.format("%d#1,%d:%d%n", script.getBeginLine(), maxLineCount, script.getBeginLine());
      // EndSection
      smap.format("*E%n");

      return smap.toString();
    }
  }
Example #4
0
  public CompiledScript compile(Script script, String className) {
    try (CodeSizeAnalysis analysis = new CodeSizeAnalysis()) {
      analysis.submit(script);
    } catch (CodeSizeException e) {
      e.printStackTrace();
      throw new CompilationException(e.getMessage());
    }

    // set-up
    // prepend '#' to mark generated classes, cf. ErrorPrototype
    String clazzName = "#" + className;
    String superClassName = Types.CompiledScript.getInternalName();
    Code code = new Code(clazzName, superClassName, script.getSourceFile(), sourceMap(script));

    // generate code
    try (CodeGenerator codegen = new CodeGenerator(code, script.getOptions())) {
      codegen.compile(script);
    }

    // finalize
    CodeLoader loader = new CodeLoader();
    List<ClassCode> classes = code.getClasses();
    for (ClassCode classCode : classes) {
      byte[] bytes = classCode.toByteArray();
      if (compilerOptions.contains(Option.Debug)) {
        debug(bytes);
      }
      // System.out.printf("define class '%s'%n", classCode.className);
      loader.defineClass(classCode.className, bytes);
    }

    try {
      Class<?> c = loader.loadClass(clazzName);
      return (CompiledScript) c.newInstance();
    } catch (ReflectiveOperationException e) {
      throw new RuntimeException(e);
    }
  }