Ruby  2.0.0p594(2014-10-27revision48167)
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 = rb_vm_control_frame_block_ptr(th->cfp); \
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 # ifdef HAVE_CRT_EXTERNS_H
00032 #  include <crt_externs.h>
00033 # else
00034 #  include "missing/crt_externs.h"
00035 # endif
00036 #endif
00037 
00038 #ifndef HAVE_STRING_H
00039 char *strrchr(const char *, const char);
00040 #endif
00041 
00042 #ifdef HAVE_UNISTD_H
00043 #include <unistd.h>
00044 #endif
00045 
00046 #ifdef HAVE_NET_SOCKET_H
00047 #include <net/socket.h>
00048 #endif
00049 
00050 #define ruby_setjmp(env) RUBY_SETJMP(env)
00051 #define ruby_longjmp(env,val) RUBY_LONGJMP((env),(val))
00052 #ifdef __CYGWIN__
00053 # ifndef _setjmp
00054 int _setjmp(jmp_buf);
00055 # endif
00056 # ifndef _longjmp
00057 NORETURN(void _longjmp(jmp_buf, int));
00058 # endif
00059 #endif
00060 
00061 #include <sys/types.h>
00062 #include <signal.h>
00063 #include <errno.h>
00064 
00065 #ifdef HAVE_SYS_SELECT_H
00066 #include <sys/select.h>
00067 #endif
00068 
00069 /*
00070   Solaris sys/select.h switches select to select_large_fdset to support larger
00071   file descriptors if FD_SETSIZE is larger than 1024 on 32bit environment.
00072   But Ruby doesn't change FD_SETSIZE because fd_set is allocated dynamically.
00073   So following definition is required to use select_large_fdset.
00074 */
00075 #ifdef HAVE_SELECT_LARGE_FDSET
00076 #define select(n, r, w, e, t) select_large_fdset((n), (r), (w), (e), (t))
00077 extern int select_large_fdset(int, fd_set *, fd_set *, fd_set *, struct timeval *);
00078 #endif
00079 
00080 #ifdef HAVE_SYS_PARAM_H
00081 #include <sys/param.h>
00082 #endif
00083 
00084 #include <sys/stat.h>
00085 
00086 #ifdef _MSC_VER
00087 #define SAVE_ROOT_JMPBUF_BEFORE_STMT \
00088     __try {
00089 #define SAVE_ROOT_JMPBUF_AFTER_STMT \
00090     } \
00091     __except (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW ? \
00092               (rb_thread_raised_set(GET_THREAD(), RAISED_STACKOVERFLOW), \
00093                raise(SIGSEGV), \
00094                EXCEPTION_EXECUTE_HANDLER) : \
00095               EXCEPTION_CONTINUE_SEARCH) { \
00096         /* never reaches here */ \
00097     }
00098 #elif defined(__MINGW32__)
00099 LONG WINAPI rb_w32_stack_overflow_handler(struct _EXCEPTION_POINTERS *);
00100 #define SAVE_ROOT_JMPBUF_BEFORE_STMT \
00101     do { \
00102         PVOID _handler = AddVectoredExceptionHandler(1, rb_w32_stack_overflow_handler);
00103 
00104 #define SAVE_ROOT_JMPBUF_AFTER_STMT \
00105         RemoveVectoredExceptionHandler(_handler); \
00106     } while (0);
00107 #else
00108 #define SAVE_ROOT_JMPBUF_BEFORE_STMT
00109 #define SAVE_ROOT_JMPBUF_AFTER_STMT
00110 #endif
00111 
00112 #define SAVE_ROOT_JMPBUF(th, stmt) do \
00113   if (ruby_setjmp((th)->root_jmpbuf) == 0) { \
00114       SAVE_ROOT_JMPBUF_BEFORE_STMT \
00115       stmt; \
00116       SAVE_ROOT_JMPBUF_AFTER_STMT \
00117   } \
00118   else { \
00119       rb_fiber_start(); \
00120   } while (0)
00121 
00122 #define TH_PUSH_TAG(th) do { \
00123   rb_thread_t * const _th = (th); \
00124   struct rb_vm_tag _tag; \
00125   _tag.tag = 0; \
00126   _tag.prev = _th->tag; \
00127   _th->tag = &_tag;
00128 
00129 #define TH_POP_TAG() \
00130   _th->tag = _tag.prev; \
00131 } while (0)
00132 
00133 #define TH_POP_TAG2() \
00134   _th->tag = _tag.prev
00135 
00136 #define PUSH_TAG() TH_PUSH_TAG(GET_THREAD())
00137 #define POP_TAG()      TH_POP_TAG()
00138 
00139 #define TH_EXEC_TAG() ruby_setjmp(_th->tag->buf)
00140 
00141 #define EXEC_TAG() \
00142   TH_EXEC_TAG()
00143 
00144 #define TH_JUMP_TAG(th, st) do { \
00145   ruby_longjmp((th)->tag->buf,(st)); \
00146 } while (0)
00147 
00148 #define JUMP_TAG(st) TH_JUMP_TAG(GET_THREAD(), (st))
00149 
00150 #define INTERNAL_EXCEPTION_P(exc) FIXNUM_P(exc)
00151 
00152 enum ruby_tag_type {
00153     RUBY_TAG_RETURN     = 0x1,
00154     RUBY_TAG_BREAK      = 0x2,
00155     RUBY_TAG_NEXT       = 0x3,
00156     RUBY_TAG_RETRY      = 0x4,
00157     RUBY_TAG_REDO       = 0x5,
00158     RUBY_TAG_RAISE      = 0x6,
00159     RUBY_TAG_THROW      = 0x7,
00160     RUBY_TAG_FATAL      = 0x8,
00161     RUBY_TAG_MASK       = 0xf
00162 };
00163 #define TAG_RETURN      RUBY_TAG_RETURN
00164 #define TAG_BREAK       RUBY_TAG_BREAK
00165 #define TAG_NEXT        RUBY_TAG_NEXT
00166 #define TAG_RETRY       RUBY_TAG_RETRY
00167 #define TAG_REDO        RUBY_TAG_REDO
00168 #define TAG_RAISE       RUBY_TAG_RAISE
00169 #define TAG_THROW       RUBY_TAG_THROW
00170 #define TAG_FATAL       RUBY_TAG_FATAL
00171 #define TAG_MASK        RUBY_TAG_MASK
00172 
00173 #define NEW_THROW_OBJECT(val, pt, st) \
00174   ((VALUE)rb_node_newnode(NODE_LIT, (VALUE)(val), (VALUE)(pt), (VALUE)(st)))
00175 #define SET_THROWOBJ_CATCH_POINT(obj, val) \
00176   (RNODE((obj))->u2.value = (val))
00177 #define SET_THROWOBJ_STATE(obj, val) \
00178   (RNODE((obj))->u3.value = (val))
00179 
00180 #define GET_THROWOBJ_VAL(obj)         ((VALUE)RNODE((obj))->u1.value)
00181 #define GET_THROWOBJ_CATCH_POINT(obj) ((VALUE*)RNODE((obj))->u2.value)
00182 #define GET_THROWOBJ_STATE(obj)       ((int)RNODE((obj))->u3.value)
00183 
00184 #define SCOPE_TEST(f)  (rb_vm_cref()->nd_visi & (f))
00185 #define SCOPE_CHECK(f) (rb_vm_cref()->nd_visi == (f))
00186 #define SCOPE_SET(f)   (rb_vm_cref()->nd_visi = (f))
00187 
00188 void rb_thread_cleanup(void);
00189 void rb_thread_wait_other_threads(void);
00190 
00191 enum {
00192     RAISED_EXCEPTION = 1,
00193     RAISED_STACKOVERFLOW = 2,
00194     RAISED_NOMEMORY = 4
00195 };
00196 int rb_threadptr_set_raised(rb_thread_t *th);
00197 int rb_threadptr_reset_raised(rb_thread_t *th);
00198 #define rb_thread_raised_set(th, f)   ((th)->raised_flag |= (f))
00199 #define rb_thread_raised_reset(th, f) ((th)->raised_flag &= ~(f))
00200 #define rb_thread_raised_p(th, f)     (((th)->raised_flag & (f)) != 0)
00201 #define rb_thread_raised_clear(th)    ((th)->raised_flag = 0)
00202 
00203 VALUE rb_f_eval(int argc, VALUE *argv, VALUE self);
00204 VALUE rb_make_exception(int argc, VALUE *argv);
00205 
00206 NORETURN(void rb_method_name_error(VALUE, VALUE));
00207 
00208 NORETURN(void rb_fiber_start(void));
00209 
00210 NORETURN(void rb_print_undef(VALUE, ID, int));
00211 NORETURN(void rb_print_undef_str(VALUE, VALUE));
00212 NORETURN(void rb_vm_localjump_error(const char *,VALUE, int));
00213 NORETURN(void rb_vm_jump_tag_but_local_jump(int));
00214 NORETURN(void rb_raise_method_missing(rb_thread_t *th, int argc, VALUE *argv,
00215                                       VALUE obj, int call_status));
00216 
00217 VALUE rb_vm_make_jump_tag_but_local_jump(int state, VALUE val);
00218 NODE *rb_vm_cref(void);
00219 VALUE rb_vm_call_cfunc(VALUE recv, VALUE (*func)(VALUE), VALUE arg, const rb_block_t *blockptr, VALUE filename);
00220 void rb_vm_set_progname(VALUE filename);
00221 void rb_thread_terminate_all(void);
00222 VALUE rb_vm_top_self();
00223 VALUE rb_vm_cbase(void);
00224 
00225 #ifndef CharNext                /* defined as CharNext[AW] on Windows. */
00226 #define CharNext(p) ((p) + mblen((p), RUBY_MBCHAR_MAXSIZE))
00227 #endif
00228 
00229 #if defined DOSISH || defined __CYGWIN__
00230 static inline void
00231 translit_char(char *p, int from, int to)
00232 {
00233     while (*p) {
00234         if ((unsigned char)*p == from)
00235             *p = to;
00236         p = CharNext(p);
00237     }
00238 }
00239 #endif
00240 
00241 #endif /* RUBY_EVAL_INTERN_H */
00242