

#include "setjmp.h"
#include "stdio.h"
#include "stdlib.h"
#include "unistd.h"
#include "string.h"
#include "stdarg.h"
#include "exception.h"
#include "mystdlib.h"
#include "debug.h"
#include "mythread.h"



exception_def *exception(){
  exception_def *frame;
  frame=&myth_self()->except;
  return frame;
}





void __throw(char *file,int line,const char *fmt,...){
  exception_def *f=exception();
  jmp_fram *h=f->jmp_head;
  char *buff=0;
  int length=0;
  va_list args;
  f->file=file;
  f->line=line;
  buff=f->log;
  va_start(args, fmt);
  length += vsnprintf(buff + length, THROW_LOG__MAX_SIZE - length - 1, fmt, args);
  va_end(args);
  longjmp((h->fram),1);
}




char *err_(){
  exception_def *f=exception();
  return f->log;
}
char *file_(){
  exception_def *f=exception();
  return f->file;
}
int line_(){
  exception_def *f=exception();
  return f->line;
}
void func_two();

int test_add(int a, int b)
{
  DBG_INFO("setjmp test.\n");
  try_{
    DBG_INFO("first enter setjmp.\n");

    try_{
      DBG_INFO("try in try.\n");
      func_two();
      // throw_("throw two err.");
    }catch_{
      DBG_WARN("file=%s,line=%d,err=%s.\n",file_(),line_(),err_());
    }
    throw_("throw a err.");

  }catch_{
    DBG_WARN("file=%s,line=%d,err=%s.\n",file_(),line_(),err_());
    try_{
      DBG_INFO("try in catch.\n");
      func_two();
      // throw_("throw two err.");
    }catch_{
      DBG_WARN("file=%s,line=%d,err=%s.\n",file_(),line_(),err_());
    }
  }
  DBG_INFO("setjmp end.\n");
  return 0;
}





void func_two(){
  DBG_INFO("in func:%s\n",__func__);
  void *p=mem_malloc(512);
  throw_("throw in func_two. in func:%s",__func__);
  mem_free(p);
}



