public void startScript(StaticScope scope) {
    classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);

    // Create the class with the appropriate class name and source file
    classWriter.visit(
        javaVersion == null ? RubyInstanceConfig.JAVA_VERSION : javaVersion,
        ACC_PUBLIC + ACC_SUPER,
        getClassname(),
        null,
        p(AbstractScript.class),
        null);

    // add setPosition impl, which stores filename as constant to speed updates
    SkinnyMethodAdapter method =
        new SkinnyMethodAdapter(
            getClassVisitor(),
            ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC,
            "setPosition",
            sig(Void.TYPE, params(ThreadContext.class, int.class)),
            null,
            null);
    method.start();

    method.aload(0); // thread context
    method.ldc(sourcename);
    method.iload(1); // line number
    method.invokevirtual(
        p(ThreadContext.class), "setFileAndLine", sig(void.class, String.class, int.class));
    method.voidreturn();
    method.end();

    topLevelScope = scope;

    beginInit();

    cacheCompiler = OptoFactory.newCacheCompiler(this);

    // This code was originally used to provide debugging info using JSR-45
    // "SMAP" format. However, it breaks using normal Java traces to
    // generate Ruby traces, since the original path is lost. Reverting
    // to full path for now.
    //        String sourceNoPath;
    //        if (sourcename.indexOf("/") >= 0) {
    //            String[] pathElements = sourcename.split("/");
    //            sourceNoPath = pathElements[pathElements.length - 1];
    //        } else if (sourcename.indexOf("\\") >= 0) {
    //            String[] pathElements = sourcename.split("\\\\");
    //            sourceNoPath = pathElements[pathElements.length - 1];
    //        } else {
    //            sourceNoPath = sourcename;
    //        }

    final File sourceFile = new File(getSourcename());
    // Revert to using original sourcename here, so that jitted traces match
    // interpreted traces.
    classWriter.visitSource(sourcename, sourceFile.getAbsolutePath());
  }
 private void endClassInit() {
   if (clinitMethod != null) {
     clinitMethod.voidreturn();
     clinitMethod.end();
   }
 }
 private void endInit() {
   initMethod.voidreturn();
   initMethod.end();
 }
  public void endScript(boolean generateLoad, boolean generateMain) {
    // add Script#run impl, used for running this script with a specified threadcontext and self
    // root method of a script is always in __file__ method
    String methodName = "__file__";

    String loadSig = sig(IRubyObject.class, ThreadContext.class, IRubyObject.class, boolean.class);

    if (generateLoad || generateMain) {
      // the load method is used for loading as a top-level script, and prepares appropriate scoping
      // around the code
      SkinnyMethodAdapter method =
          new SkinnyMethodAdapter(getClassVisitor(), ACC_PUBLIC, "load", loadSig, null, null);
      method.start();

      // invoke __file__ with threadcontext, self, args (null), and block (null)
      Label tryBegin = new Label();
      Label tryFinally = new Label();

      method.label(tryBegin);
      method.aload(THREADCONTEXT_INDEX);
      String scopeNames = Helpers.encodeScope(topLevelScope);
      method.ldc(scopeNames);
      method.iload(SELF_INDEX + 1);
      method.invokestatic(
          p(Helpers.class),
          "preLoad",
          sig(StaticScope.class, ThreadContext.class, String.class, boolean.class));

      // store root scope
      method.aload(THIS);
      method.swap();
      method.invokevirtual(
          p(AbstractScript.class), "setRootScope", sig(void.class, StaticScope.class));

      method.aload(THIS);
      method.aload(THREADCONTEXT_INDEX);
      method.aload(SELF_INDEX);
      method.getstatic(p(IRubyObject.class), "NULL_ARRAY", ci(IRubyObject[].class));
      method.getstatic(p(Block.class), "NULL_BLOCK", ci(Block.class));

      method.invokestatic(getClassname(), methodName, getStaticMethodSignature(getClassname(), 4));
      method.aload(THREADCONTEXT_INDEX);
      method.invokestatic(p(Helpers.class), "postLoad", sig(void.class, ThreadContext.class));
      method.areturn();

      method.label(tryFinally);
      method.aload(THREADCONTEXT_INDEX);
      method.invokestatic(p(Helpers.class), "postLoad", sig(void.class, ThreadContext.class));
      method.athrow();

      method.trycatch(tryBegin, tryFinally, tryFinally, null);

      method.end();
    }

    if (generateMain) {
      // add main impl, used for detached or command-line execution of this script with a new
      // runtime
      // root method of a script is always in stub0, method0
      SkinnyMethodAdapter method =
          new SkinnyMethodAdapter(
              getClassVisitor(),
              ACC_PUBLIC | ACC_STATIC,
              "main",
              sig(Void.TYPE, params(String[].class)),
              null,
              null);
      method.start();

      // init script filename to simple class name in case we don't assign it (null ClassLoader)
      method.ldc(getClassname().replaceAll("\\\\.", "/"));
      method.astore(1);

      // new instance to invoke run against
      method.newobj(getClassname());
      method.dup();
      method.invokespecial(getClassname(), "<init>", sig(Void.TYPE));

      // guard against null classloader
      method.ldc(Type.getType("L" + getClassname() + ";"));
      method.invokevirtual(p(Class.class), "getClassLoader", sig(ClassLoader.class));
      Label skip = new Label();
      method.ifnull(skip);

      // set filename for the loaded script class (JRUBY-4825)
      method.dup();
      method.ldc(Type.getType("L" + getClassname() + ";"));
      method.invokevirtual(p(Class.class), "getClassLoader", sig(ClassLoader.class));
      method.ldc(getClassname() + ".class");
      method.invokevirtual(p(ClassLoader.class), "getResource", sig(URL.class, String.class));
      method.invokevirtual(p(Object.class), "toString", sig(String.class));
      method.astore(1);
      method.aload(1);
      method.invokevirtual(p(AbstractScript.class), "setFilename", sig(void.class, String.class));

      // ifnull ClassLoader
      method.label(skip);

      // instance config for the script run
      method.newobj(p(RubyInstanceConfig.class));
      method.dup();
      method.invokespecial(p(RubyInstanceConfig.class), "<init>", "()V");

      // set argv from main's args
      method.dup();
      method.aload(0);
      method.invokevirtual(p(RubyInstanceConfig.class), "setArgv", sig(void.class, String[].class));

      // set script filename ($0)
      method.dup();
      method.aload(1);
      method.invokevirtual(
          p(RubyInstanceConfig.class), "setScriptFileName", sig(void.class, String.class));

      // invoke run with threadcontext and topself
      method.invokestatic(p(Ruby.class), "newInstance", sig(Ruby.class, RubyInstanceConfig.class));
      method.dup();

      method.invokevirtual(RUBY, "getCurrentContext", sig(ThreadContext.class));
      method.swap();
      method.invokevirtual(RUBY, "getTopSelf", sig(IRubyObject.class));
      method.ldc(false);

      method.invokevirtual(getClassname(), "load", loadSig);
      method.voidreturn();
      method.end();
    }

    getCacheCompiler().finish();

    endInit();
    endClassInit();
  }