code: public static Scriptable getTopLevelScope(Scriptable obj)
      {
          for (;;) {
              Scriptable parent = obj.getParentScope();
              if (parent == null) {
                  return obj;
              }
              obj = parent;
          }
      }
docs: Get the global scope.
  
  <p>Walks the parent scope chain to find an object with a null
  parent scope (the global object).
  
  @param obj a JavaScript object
  @return the corresponding global scope