Ruby 1.9.3p327(2012-11-10revision37606)
eval_intern.h
Go to the documentation of this file.
00001 #ifndef RUBY_EVAL_INTERN_H
00002 #define RUBY_EVAL_INTERN_H
00003 
00004 #include "ruby/ruby.h"
00005 #include "vm_core.h"
00006 
00007 #define PASS_PASSED_BLOCK_TH(th) do { \
00008     (th)->passed_block = GC_GUARDED_PTR_REF((rb_block_t *)(th)->cfp->lfp[0]); \
00009     (th)->cfp->flag |= VM_FRAME_FLAG_PASSED; \
00010 } while (0)
00011 
00012 #define PASS_PASSED_BLOCK() do { \
00013     rb_thread_t * const __th__ = GET_THREAD(); \
00014     PASS_PASSED_BLOCK_TH(__th__); \
00015 } while (0)
00016 
00017 #ifdef HAVE_STDLIB_H
00018 #include <stdlib.h>
00019 #endif
00020 #ifndef EXIT_SUCCESS
00021 #define EXIT_SUCCESS 0
00022 #endif
00023 #ifndef EXIT_FAILURE
00024 #define EXIT_FAILURE 1
00025 #endif
00026 
00027 #include <stdio.h>
00028 #include <setjmp.h>
00029 
00030 #ifdef __APPLE__
00031 #include <crt_externs.h>
00032 #endif
00033 
00034 /* Make alloca work the best possible way.  */
00035 #ifdef __GNUC__
00036 # ifndef atarist
00037 #  ifndef alloca
00038 #   define alloca __builtin_alloca
00039 #  endif
00040 # endif /* atarist */
00041 #else
00042 # ifdef HAVE_ALLOCA_H
00043 #  include <alloca.h>
00044 # else
00045 #  ifdef _AIX
00046 #pragma alloca
00047 #  else
00048 #   ifndef alloca               /* predefined by HP cc +Olibcalls */
00049 void *alloca();
00050 #   endif
00051 #  endif /* AIX */
00052 # endif /* HAVE_ALLOCA_H */
00053 #endif /* __GNUC__ */
00054 
00055 #ifndef HAVE_STRING_H
00056 char *strrchr(const char *, const char);
00057 #endif
00058 
00059 #ifdef HAVE_UNISTD_H
00060 #include <unistd.h>
00061 #endif
00062 
00063 #ifdef HAVE_NET_SOCKET_H
00064 #include <net/socket.h>
00065 #endif
00066 
00067 #define ruby_setjmp(env) RUBY_SETJMP(env)
00068 #define ruby_longjmp(env,val) RUBY_LONGJMP((env),(val))
00069 #ifdef __CYGWIN__
00070 # ifndef _setjmp
00071 int _setjmp(jmp_buf);
00072 # endif
00073 # ifndef _longjmp
00074 NORETURN(void _longjmp(jmp_buf, int));
00075 # endif
00076 #endif
00077 
00078 #include <sys/types.h>
00079 #include <signal.h>
00080 #include <errno.h>
00081 
00082 #ifdef HAVE_SYS_SELECT_H
00083 #include <sys/select.h>
00084 #endif
00085 
00086 /*
00087   Solaris sys/select.h switches select to select_large_fdset to support larger
00088   file descriptors if FD_SETSIZE is larger than 1024 on 32bit environment.
00089   But Ruby doesn't change FD_SETSIZE because fd_set is allocated dynamically.
00090   So following definition is required to use select_large_fdset.
00091 */
00092 #ifdef HAVE_SELECT_LARGE_FDSET
00093 #define select(n, r, w, e, t) select_large_fdset((n), (r), (w), (e), (t))
00094 extern int select_large_fdset(int, fd_set *, fd_set *, fd_set *, struct timeval *);
00095 #endif
00096 
00097 #ifdef HAVE_SYS_PARAM_H
00098 #include <sys/param.h>
00099 #endif
00100 
00101 #include <sys/stat.h>
00102 
00103 #define SAVE_ROOT_JMPBUF(th, stmt) do \
00104   if (ruby_setjmp((th)->root_jmpbuf) == 0) { \
00105       stmt; \
00106   } \
00107   else { \
00108       rb_fiber_start(); \
00109   } while (0)
00110 
00111 #define TH_PUSH_TAG(th) do { \
00112   rb_thread_t * const _th = (th); \
00113   struct rb_vm_tag _tag; \
00114   _tag.tag = 0; \
00115   _tag.prev = _th->tag; \
00116   _th->tag = &_tag;
00117 
00118 #define TH_POP_TAG() \
00119   _th->tag = _tag.prev; \
00120 } while (0)
00121 
00122 #define TH_POP_TAG2() \
00123   _th->tag = _tag.prev
00124 
00125 #define PUSH_TAG() TH_PUSH_TAG(GET_THREAD())
00126 #define POP_TAG()      TH_POP_TAG()
00127 
00128 #define TH_EXEC_TAG() ruby_setjmp(_th->tag->buf)
00129 
00130 #define EXEC_TAG() \
00131   TH_EXEC_TAG()
00132 
00133 #define TH_JUMP_TAG(th, st) do { \
00134   ruby_longjmp((th)->tag->buf,(st)); \
00135 } while (0)
00136 
00137 #define JUMP_TAG(st) TH_JUMP_TAG(GET_THREAD(), (st))
00138 
00139 #define INTERNAL_EXCEPTION_P(exc) FIXNUM_P(exc)
00140 
00141 enum ruby_tag_type {
00142     RUBY_TAG_RETURN     = 0x1,
00143     RUBY_TAG_BREAK      = 0x2,
00144     RUBY_TAG_NEXT       = 0x3,
00145     RUBY_TAG_RETRY      = 0x4,
00146     RUBY_TAG_REDO       = 0x5,
00147     RUBY_TAG_RAISE      = 0x6,
00148     RUBY_TAG_THROW      = 0x7,
00149     RUBY_TAG_FATAL      = 0x8,
00150     RUBY_TAG_MASK       = 0xf
00151 };
00152 #define TAG_RETURN      RUBY_TAG_RETURN
00153 #define TAG_BREAK       RUBY_TAG_BREAK
00154 #define TAG_NEXT        RUBY_TAG_NEXT
00155 #define TAG_RETRY       RUBY_TAG_RETRY
00156 #define TAG_REDO        RUBY_TAG_REDO
00157 #define TAG_RAISE       RUBY_TAG_RAISE
00158 #define TAG_THROW       RUBY_TAG_THROW
00159 #define TAG_FATAL       RUBY_TAG_FATAL
00160 #define TAG_MASK        RUBY_TAG_MASK
00161 
00162 #define NEW_THROW_OBJECT(val, pt, st) \
00163   ((VALUE)rb_node_newnode(NODE_LIT, (VALUE)(val), (VALUE)(pt), (VALUE)(st)))
00164 #define SET_THROWOBJ_CATCH_POINT(obj, val) \
00165   (RNODE((obj))->u2.value = (val))
00166 #define SET_THROWOBJ_STATE(obj, val) \
00167   (RNODE((obj))->u3.value = (val))
00168 
00169 #define GET_THROWOBJ_VAL(obj)         ((VALUE)RNODE((obj))->u1.value)
00170 #define GET_THROWOBJ_CATCH_POINT(obj) ((VALUE*)RNODE((obj))->u2.value)
00171 #define GET_THROWOBJ_STATE(obj)       ((int)RNODE((obj))->u3.value)
00172 
00173 #define SCOPE_TEST(f)  (rb_vm_cref()->nd_visi & (f))
00174 #define SCOPE_CHECK(f) (rb_vm_cref()->nd_visi == (f))
00175 #define SCOPE_SET(f)   (rb_vm_cref()->nd_visi = (f))
00176 
00177 #define CHECK_STACK_OVERFLOW(cfp, margin) do \
00178   if ((VALUE *)((char *)(((VALUE *)(cfp)->sp) + (margin)) + sizeof(rb_control_frame_t)) >= ((VALUE *)(cfp))) { \
00179       rb_exc_raise(sysstack_error); \
00180   } \
00181 while (0)
00182 
00183 void rb_thread_cleanup(void);
00184 void rb_thread_wait_other_threads(void);
00185 
00186 enum {
00187     RAISED_EXCEPTION = 1,
00188     RAISED_STACKOVERFLOW = 2,
00189     RAISED_NOMEMORY = 4
00190 };
00191 int rb_threadptr_set_raised(rb_thread_t *th);
00192 int rb_threadptr_reset_raised(rb_thread_t *th);
00193 #define rb_thread_raised_set(th, f)   ((th)->raised_flag |= (f))
00194 #define rb_thread_raised_reset(th, f) ((th)->raised_flag &= ~(f))
00195 #define rb_thread_raised_p(th, f)     (((th)->raised_flag & (f)) != 0)
00196 #define rb_thread_raised_clear(th)    ((th)->raised_flag = 0)
00197 
00198 VALUE rb_f_eval(int argc, VALUE *argv, VALUE self);
00199 VALUE rb_make_exception(int argc, VALUE *argv);
00200 
00201 NORETURN(void rb_fiber_start(void));
00202 
00203 NORETURN(void rb_print_undef(VALUE, ID, int));
00204 NORETURN(void rb_vm_localjump_error(const char *,VALUE, int));
00205 NORETURN(void rb_vm_jump_tag_but_local_jump(int, VALUE));
00206 NORETURN(void rb_raise_method_missing(rb_thread_t *th, int argc, VALUE *argv,
00207                                       VALUE obj, int call_status));
00208 
00209 VALUE rb_vm_make_jump_tag_but_local_jump(int state, VALUE val);
00210 NODE *rb_vm_cref(void);
00211 VALUE rb_vm_call_cfunc(VALUE recv, VALUE (*func)(VALUE), VALUE arg, const rb_block_t *blockptr, VALUE filename);
00212 void rb_vm_set_progname(VALUE filename);
00213 void rb_thread_terminate_all(void);
00214 VALUE rb_vm_top_self();
00215 VALUE rb_vm_cbase(void);
00216 void rb_trap_restore_mask(void);
00217 
00218 #ifndef CharNext                /* defined as CharNext[AW] on Windows. */
00219 #define CharNext(p) ((p) + mblen((p), RUBY_MBCHAR_MAXSIZE))
00220 #endif
00221 
00222 #if defined DOSISH || defined __CYGWIN__
00223 static inline void
00224 translit_char(char *p, int from, int to)
00225 {
00226     while (*p) {
00227         if ((unsigned char)*p == from)
00228             *p = to;
00229         p = CharNext(p);
00230     }
00231 }
00232 #endif
00233 
00234 #endif /* RUBY_EVAL_INTERN_H */
00235