Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add mruby sources

  • Loading branch information...
commit e0d6430f63c4cbe0c71ce82ee23284671389a818 1 parent 54ad561
Hiroshi Mimaki mimaki authored
Showing with 70,374 additions and 4 deletions.
  1. +18 −0 .gitignore
  2. +120 −0 Makefile
  3. +0 −4 README.md
  4. +14 −0 Todo.txt
  5. 0  bin/.gitkeep
  6. 0  doc/.gitkeep
  7. 0  ext/.gitkeep
  8. +42 −0 include/mrbconf.h
  9. +623 −0 include/mruby.h
  10. +42 −0 include/mruby/array.h
  11. +71 −0 include/mruby/class.h
  12. +59 −0 include/mruby/hash.h
  13. +17 −0 include/mruby/numeric.h
  14. +46 −0 include/mruby/object.h
  15. +44 −0 include/mruby/proc.h
  16. +21 −0 include/mruby/range.h
  17. +133 −0 include/mruby/string.h
  18. +16 −0 include/mruby/struct.h
  19. 0  lib/.gitkeep
  20. +62 −0 mrblib/Makefile
  21. +79 −0 mrblib/array.rb
  22. +63 −0 mrblib/compar.rb
  23. +266 −0 mrblib/enum.rb
  24. +9 −0 mrblib/error.rb
  25. +58 −0 mrblib/hash.rb
  26. +17 −0 mrblib/init_mrblib.c
  27. +45 −0 mrblib/kernel.rb
  28. +42 −0 mrblib/numeric.rb
  29. +20 −0 mrblib/print.rb
  30. +30 −0 mrblib/range.rb
  31. +93 −0 mrblib/string.rb
  32. +30 −0 mrblib/struct.rb
  33. +89 −0 src/Makefile
  34. +1,458 −0 src/array.c
  35. +96 −0 src/ascii.c
  36. +197 −0 src/cdump.c
  37. +8 −0 src/cdump.h
  38. +1,049 −0 src/class.c
  39. +2,273 −0 src/codegen.c
  40. +144 −0 src/compar.c
  41. +73 −0 src/compile.h
  42. +28 −0 src/crc.c
  43. +697 −0 src/dump.c
  44. +118 −0 src/dump.h
  45. +1,663 −0 src/encoding.c
  46. +360 −0 src/encoding.h
  47. +1,077 −0 src/enum.c
  48. +479 −0 src/error.c
  49. +21 −0 src/error.h
  50. +280 −0 src/etc.c
  51. +217 −0 src/eval_intern.h
  52. 0  src/ext/.gitkeep
  53. +1,146 −0 src/gc.c
  54. +26 −0 src/gc.h
  55. +1,436 −0 src/hash.c
  56. +105 −0 src/init.c
  57. +10 −0 src/init_ext.c
  58. +23 −0 src/irep.h
  59. +1,530 −0 src/kernel.c
  60. +50 −0 src/keywords
  61. +216 −0 src/lex.def
  62. +642 −0 src/load.c
  63. +53 −0 src/mdata.h
  64. +103 −0 src/method.h
  65. +117 −0 src/minimain.c
  66. +17,985 −0 src/name2ctype.h
  67. +125 −0 src/node.h
  68. +2,018 −0 src/numeric.c
  69. +632 −0 src/object.c
  70. +771 −0 src/oniguruma.h
  71. +148 −0 src/opcode.h
  72. +5,435 −0 src/parse.y
  73. +152 −0 src/pool.c
  74. +19 −0 src/pool.h
  75. +69 −0 src/print.c
  76. +92 −0 src/proc.c
  77. +499 −0 src/range.c
  78. +3,306 −0 src/re.c
  79. +85 −0 src/re.h
  80. +6,286 −0 src/regcomp.c
  81. +909 −0 src/regenc.c
  82. +203 −0 src/regenc.h
  83. +375 −0 src/regerror.c
  84. +32 −0 src/regex.h
  85. +3,757 −0 src/regexec.c
  86. +833 −0 src/regint.h
  87. +5,600 −0 src/regparse.c
  88. +354 −0 src/regparse.h
  89. +203 −0 src/ritehash.h
  90. +1,112 −0 src/sprintf.c
  91. +1,283 −0 src/st.c
  92. +139 −0 src/st.h
  93. +88 −0 src/state.c
Sorry, we could not display the entire diff because it was too big.
18 .gitignore
View
@@ -0,0 +1,18 @@
+# /
+*.bak
+*.dylib
+*.inc
+*.o
+*.orig
+*.rej
+*.sav
+*.swp
+*.d
+*~
+.DS_Store
+.ccmalloc
+.svn
+/.git
+cscope.out
+mruby.exe
+y.tab.c
120 Makefile
View
@@ -0,0 +1,120 @@
+# makefile discription.
+# basic build file for Rite-VM(mruby)
+# 11.Apr.2011 coded by Kenji Yoshimoto.
+# 17.Jan.2012 coded by Hiroshi Mimaki.
+
+# project-specific macros
+# extension of the executable-file is modifiable(.exe .out ...)
+TARGET := bin/mrubysample
+RITEVM := lib/ritevm
+MRUBY := tools/mruby/mruby
+ifeq ($(OS),Windows_NT)
+EXE := $(TARGET).exe
+LIB := $(RITEVM).lib
+MRB := $(MRUBY).exe
+else
+EXE := $(TARGET)
+LIB := $(RITEVM).a
+MRB := $(MRUBY)
+endif
+MSRC := src/minimain.c
+YSRC := src/parse.y
+YC := src/y.tab.c
+EXCEPT1 := $(YC) $(MSRC)
+OBJM := $(patsubst %.c,%.o,$(MSRC))
+OBJY := $(patsubst %.c,%.o,$(YC))
+OBJ1 := $(patsubst %.c,%.o,$(filter-out $(EXCEPT1),$(wildcard src/*.c)))
+#OBJ2 := $(patsubst %.c,%.o,$(wildcard ext/regex/*.c))
+#OBJ3 := $(patsubst %.c,%.o,$(wildcard ext/enc/*.c))
+OBJS := $(OBJ1) $(OBJ2) $(OBJ3)
+# mruby libraries
+EXTC := mrblib/mrblib.c
+EXTRB := $(wildcard mrblib/*.rb)
+EXT0 := $(patsubst %.c,%.o,src/$(EXTC))
+# ext libraries
+EXTS := $(EXT0)
+
+# libraries, includes
+LIBS = $(LIB) -lm
+INCLUDES = -I./src -I./include
+
+# library for iOS
+IOSLIB := $(RITEVM)-ios.a
+IOSSIMLIB := $(RITEVM)-iossim.a
+IOSDEVLIB := $(RITEVM)-iosdev.a
+IOSSIMCC := xcrun -sdk iphoneos llvm-gcc-4.2 -arch i386 -isysroot "/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/"
+IOSDEVCC := xcrun -sdk iphoneos llvm-gcc-4.2 -arch armv7 -isysroot "/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk/"
+
+# compiler, linker (gcc)
+CC = gcc
+LL = gcc
+YACC = bison
+DEBUG_MODE = 1
+ifeq ($(DEBUG_MODE),1)
+CFLAGS = -g
+else
+CFLAGS = -O3
+endif
+ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS)
+MAKE_FLAGS = --no-print-directory CC="$(CC)" LL="$(LL)"
+
+##############################
+# generic build targets, rules
+
+.PHONY : all
+all : $(LIB) $(MRB) $(EXE)
+ @echo "make: built targets of `pwd`"
+
+##############################
+# make library for iOS
+.PHONY : ios
+ios : $(IOSLIB)
+
+$(IOSLIB) : $(IOSSIMLIB) $(IOSDEVLIB)
+ lipo -arch i386 $(IOSSIMLIB) -arch armv7 $(IOSDEVLIB) -create -output $(IOSLIB)
+
+$(IOSSIMLIB) :
+ $(MAKE) clean -C src $(MAKE_FLAGS)
+ $(MAKE) -C src $(MAKE_FLAGS) CC="$(IOSSIMCC)" LL="$(IOSSIMCC)"
+ cp $(LIB) $(IOSSIMLIB)
+
+$(IOSDEVLIB) :
+ $(MAKE) clean -C src $(MAKE_FLAGS)
+ $(MAKE) -C src $(MAKE_FLAGS) CC="$(IOSDEVCC)" LL="$(IOSDEVCC)"
+ cp $(LIB) $(IOSDEVLIB)
+
+# executable constructed using linker from object files
+$(EXE) : $(OBJM) $(LIB)
+ $(LL) -o $@ $(OBJM) $(LIBS)
+
+-include $(OBJS:.o=.d)
+
+# src compile
+$(LIB) : $(EXTS) $(OBJS) $(OBJY)
+ $(MAKE) -C src $(MAKE_FLAGS)
+
+# mruby interpreter compile
+$(MRB) : $(EXTS) $(OBJS) $(OBJY)
+ $(MAKE) -C tools/mruby $(MAKE_FLAGS)
+
+# objects compiled from source
+$(OBJS) :
+ $(MAKE) -C src $(MAKE_FLAGS) && $(MAKE) -C tools/mruby $(MAKE_FLAGS)
+
+# extend libraries complile
+$(EXTS) : $(EXTRB)
+ $(MAKE) -C mrblib $(MAKE_FLAGS)
+
+# test module compile
+$(OBJM) : $(MSRC)
+ $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(MSRC) -o $(OBJM)
+
+# clean up
+.PHONY : clean
+clean :
+ $(MAKE) clean -C src $(MAKE_FLAGS)
+ $(MAKE) clean -C tools/mruby $(MAKE_FLAGS)
+ -rm -f $(EXE) $(OBJM)
+ -rm -f $(OBJM:.o=.d)
+ -rm -f $(IOSLIB) $(IOSSIMLIB) $(IOSDEVLIB)
+ @echo "make: removing targets, objects and depend files of `pwd`"
4 README.md
View
@@ -1,4 +0,0 @@
-mruby
-=====
-
-Lightweight Ruby
14 Todo.txt
View
@@ -0,0 +1,14 @@
+やること(まだできてないこと) / not yet complete
+
+* ヒアドキュメント / here document
+* 特殊変数 ($1,$2..) / special variables
+* super in aliased methods
+* BEGIN/END (対応しないんだっけ?)
+* const_missing
+* respond_to_missing
+
+改善すること(できているが直すこと)
+
+* Hash (サイズを減らす。khashを使うか、順序を保存するか)
+* stringEx (encoding削除、CODERANGE削除、UTF-8 or ASCII以外削除)
+* 気づいたら書き加える
0  bin/.gitkeep
View
No changes.
0  doc/.gitkeep
View
No changes.
0  ext/.gitkeep
View
No changes.
42 include/mrbconf.h
View
@@ -0,0 +1,42 @@
+#ifndef MRUBYCONF_H
+#define MRUBYCONF_H
+
+#include <stdint.h>
+typedef double mrb_float;
+typedef int32_t mrb_int;
+typedef intptr_t mrb_sym;
+
+#define readint(p,base) strtol((p),NULL,(base))
+#define readfloat(p) strtod((p),NULL)
+
+#undef INCLUDE_ENCODING /* not use encoding classes (ascii only) */
+#define INCLUDE_ENCODING /* use UTF-8 encoding classes */
+
+#undef INCLUDE_REGEXP /* not use regular expression classes */
+#define INCLUDE_REGEXP /* use regular expression classes */
+
+#ifdef INCLUDE_REGEXP
+# define INCLUDE_ENCODING /* Regexp depends Encoding */
+#endif
+
+#undef HAVE_UNISTD_H /* WINDOWS */
+#define HAVE_UNISTD_H /* LINUX */
+
+#define SIZEOF_INT 4
+#define SIZEOF_SHORT 2
+#define SIZEOF_LONG 4
+#define SIZEOF_LONG_LONG 8
+#define SIZEOF___INT64 0
+#define SIZEOF_VOIDP 4
+#define SIZEOF_FLOAT 4
+#define SIZEOF_DOUBLE 8
+
+#ifndef FALSE
+# define FALSE 0
+#endif
+
+#ifndef TRUE
+# define TRUE 1
+#endif
+
+#endif /* MRUBYCONF_H */
623 include/mruby.h
View
@@ -0,0 +1,623 @@
+#ifndef MRUBY_H
+#define MRUBY_H
+
+#include <stdlib.h>
+#include "mrbconf.h"
+
+enum mrb_vtype {
+ MRB_TT_FALSE = 0, /* 0 */
+ MRB_TT_FREE, /* 1 */
+ MRB_TT_TRUE, /* 2 */
+ MRB_TT_FIXNUM, /* 3 */
+ MRB_TT_SYMBOL, /* 4 */
+ MRB_TT_UNDEF, /* 5 */
+ MRB_TT_FLOAT, /* 6 */
+ MRB_TT_OBJECT, /* 7 */
+ MRB_TT_CLASS, /* 8 */
+ MRB_TT_MODULE, /* 9 */
+ MRB_TT_ICLASS, /* 10 */
+ MRB_TT_SCLASS, /* 11 */
+ MRB_TT_PROC, /* 12 */
+ MRB_TT_ARRAY, /* 13 */
+ MRB_TT_HASH, /* 14 */
+ MRB_TT_STRING, /* 15 */
+ MRB_TT_RANGE, /* 16 */
+ MRB_TT_REGEX, /* 17 */
+ MRB_TT_STRUCT, /* 18 */
+ MRB_TT_EXCEPTION, /* 19 */
+ MRB_TT_MATCH, /* 20 */
+ MRB_TT_FILE, /* 21 */
+ MRB_TT_ENV, /* 22 */
+ MRB_TT_DATA, /* 23 */
+ MRB_TT_THREAD, /* 24 */
+ MRB_TT_THREADGRP, /* 25 */
+ MRB_TT_MAXDEFINE /* 26 */
+};
+
+typedef struct mrb_value {
+ union {
+ mrb_float f;
+ void *p;
+ mrb_int i;
+ mrb_sym sym;
+ } value;
+ enum mrb_vtype tt:8;
+} mrb_value;
+
+#define mrb_type(o) (o).tt
+#define mrb_nil_p(o) ((o).tt == MRB_TT_FALSE && !(o).value.i)
+#define mrb_test(o) ((o).tt != MRB_TT_FALSE)
+#define mrb_fixnum(o) (o).value.i
+#define mrb_float(o) (o).value.f
+#define mrb_symbol(o) (o).value.sym
+#define mrb_object(o) (o).value.p
+#define FIXNUM_P(o) ((o).tt == MRB_TT_FIXNUM)
+#define UNDEF_P(o) ((o).tt == MRB_TT_UNDEF)
+
+#include "mruby/object.h"
+
+#define IMMEDIATE_P(x) ((mrb_type(x) >= MRB_TT_FALSE) && (mrb_type(x) <= MRB_TT_FLOAT))
+#define SPECIAL_CONST_P(x) IMMEDIATE_P(x)
+#define SYMBOL_P(o) (mrb_type(o) == MRB_TT_SYMBOL)
+#define RTEST(o) mrb_test(o)
+
+#define FL_ABLE(x) (!SPECIAL_CONST_P(x))
+#define FL_TEST(x,f) (FL_ABLE(x)?(RBASIC(x)->flags&(f)):0)
+#define FL_ANY(x,f) FL_TEST(x,f)
+#define FL_ALL(x,f) (FL_TEST(x,f) == (f))
+#define FL_SET(x,f) do {if (FL_ABLE(x)) RBASIC(x)->flags |= (f);} while (0)
+#define FL_UNSET(x,f) do {if (FL_ABLE(x)) RBASIC(x)->flags &= ~(f);} while (0)
+
+static inline mrb_int
+mrb_special_const_p(mrb_value obj)
+{
+ if (SPECIAL_CONST_P(obj)) return 1;
+ return 0;
+}
+static inline mrb_value
+mrb_fixnum_value(mrb_int i)
+{
+ mrb_value v;
+
+ v.tt = MRB_TT_FIXNUM;
+ v.value.i = i;
+ return v;
+}
+
+static inline mrb_value
+mrb_float_value(mrb_float f)
+{
+ mrb_value v;
+
+ v.tt = MRB_TT_FLOAT;
+ v.value.f = f;
+ return v;
+}
+
+static inline mrb_value
+mrb_symbol_value(mrb_sym i)
+{
+ mrb_value v;
+
+ v.tt = MRB_TT_SYMBOL;
+ v.value.sym = i;
+ return v;
+}
+
+static inline mrb_value
+mrb_obj_value(void *p)
+{
+ mrb_value v;
+ struct RBasic *b = p;
+
+ v.tt = b->tt;
+ v.value.p = p;
+ return v;
+}
+
+static inline mrb_value
+mrb_false_value()
+{
+ mrb_value v;
+
+ v.tt = MRB_TT_FALSE;
+ v.value.i = 1;
+ return v;
+}
+
+static inline mrb_value
+mrb_nil_value()
+{
+ mrb_value v;
+
+ v.tt = MRB_TT_FALSE;
+ v.value.p = 0;
+ return v;
+}
+
+static inline mrb_value
+mrb_true_value()
+{
+ mrb_value v;
+
+ v.tt = MRB_TT_TRUE;
+ v.value.i = 1;
+ return v;
+}
+
+static inline mrb_value
+mrb_undef_value()
+{
+ mrb_value v;
+
+ v.tt = MRB_TT_UNDEF;
+ v.value.i = 0;
+ return v;
+}
+
+typedef int32_t mrb_code;
+
+struct mrb_state;
+
+typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t);
+
+#define MRB_ARENA_SIZE 1024 //256 up kusuda 2011/04/30
+#define ruby_debug (mrb_nil_value())
+#define ruby_verbose (mrb_nil_value())
+
+typedef struct {
+ mrb_sym mid;
+ struct RProc *proc;
+ int stackidx;
+ int nregs;
+ int argc;
+ mrb_code *pc;
+ int acc;
+ struct RClass *target_class;
+ int ridx;
+ int eidx;
+ struct REnv *env;
+} mrb_callinfo;
+
+enum gc_state {
+ GC_STATE_NONE = 0,
+ GC_STATE_MARK,
+ GC_STATE_SWEEP
+};
+
+typedef struct mrb_state {
+ void *jmp;
+
+ mrb_allocf allocf;
+
+ mrb_value *stack;
+ mrb_value *stbase, *stend;
+
+ mrb_callinfo *ci;
+ mrb_callinfo *cibase, *ciend;
+
+ mrb_code **rescue;
+ int rsize;
+ struct RProc **ensure;
+ int esize;
+
+ struct RObject *exc;
+ struct kh_iv *globals;
+
+ struct mrb_irep **irep;
+ size_t irep_len, irep_capa;
+
+ struct RClass *object_class;
+ struct RClass *class_class;
+ struct RClass *module_class;
+ struct RClass *proc_class;
+ struct RClass *string_class;
+ struct RClass *array_class;
+ struct RClass *hash_class;
+ struct RClass *range_class;
+#ifdef INCLUDE_REGEXP
+ struct RClass *regex_class;
+ struct RClass *match_class;
+#endif
+#ifdef INCLUDE_ENCODING
+ struct RClass *encode_class;
+ struct RClass *converter_class;
+#endif
+
+ struct RClass *float_class;
+ struct RClass *fixnum_class;
+ struct RClass *true_class;
+ struct RClass *false_class;
+ struct RClass *nil_class;
+ struct RClass *symbol_class;
+
+ struct RClass *kernel_module;
+ struct heap_page *heaps;
+ struct heap_page *sweeps;
+ struct heap_page *free_heaps;
+ size_t live; /* count of live objects */
+ struct RBasic *arena[MRB_ARENA_SIZE];
+ int arena_idx;
+
+ enum gc_state gc_state; /* state of gc */
+ int current_white_part; /* make white object by white_part */
+ struct RBasic *gray_list; /* list of gray objects */
+ struct RBasic *variable_gray_list; /* list of objects to be traversed atomically */
+ size_t gc_live_after_mark;
+ size_t gc_threshold;
+ mrb_int gc_interval_ratio;
+ mrb_int gc_step_ratio;
+
+ mrb_sym symidx;
+ struct kh_n2s *name2sym; /* symbol table */
+ struct kh_s2n *sym2name; /* reverse symbol table */
+ struct RNode *local_svar;/* regexp */
+
+ struct RClass *eException_class;
+ struct RClass *eStandardError_class;
+ struct RClass *eRuntimeError_class;
+} mrb_state;
+
+typedef mrb_value (*mrb_func_t)(mrb_state *mrb, mrb_value);
+typedef mrb_value (*mrb_funcargv_t)(mrb_state *mrb, mrb_value, int argc, mrb_value* argv);
+struct RClass *mrb_define_class(mrb_state *, const char*, struct RClass*);
+struct RClass *mrb_define_module(mrb_state *, const char*);
+mrb_value mrb_singleton_class(mrb_state*, mrb_value);
+void mrb_include_module(mrb_state*, struct RClass*, struct RClass*);
+
+void mrb_define_method(mrb_state*, struct RClass*, const char*, mrb_func_t,int);
+void mrb_define_class_method(mrb_state *, struct RClass *, const char *, mrb_func_t, int);
+void mrb_define_singleton_method(mrb_state*, void*, const char*, mrb_func_t,int);
+void mrb_define_const(mrb_state*, struct RClass*, const char *name, mrb_value);
+mrb_value mrb_instance_new(mrb_state *mrb, mrb_value cv);
+struct RClass * mrb_class_new(mrb_state *mrb, struct RClass *super);
+struct RClass * mrb_module_new(mrb_state *mrb);
+struct RClass * mrb_class_from_sym(mrb_state *mrb, struct RClass *klass, mrb_sym name);
+struct RClass * mrb_class_get(mrb_state *mrb, char *name);
+struct RClass * mrb_class_obj_get(mrb_state *mrb, char *name);
+
+mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
+mrb_value mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method);
+int mrb_obj_respond_to(struct RClass* c, mrb_sym mid);
+struct RClass * mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
+struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
+
+/* required arguments */
+#define ARGS_REQ(n) (((n)&0x1f) << 19)
+/* optional arguments */
+#define ARGS_OPT(n) (((n)&0x1f) << 14)
+/* rest argument */
+#define ARGS_REST() (1 << 13)
+/* required arguments after rest */
+#define ARGS_POST(n) (((n)&0x1f) << 8)
+/* keyword arguments (n of keys, kdict) */
+#define ARGS_KEY(n1,n2) ((((n1)&0x1f) << 3) | ((n2)?(1<<2):0))
+/* block argument */
+#define ARGS_BLOCK() (1 << 1)
+
+/* accept any number of arguments */
+#define ARGS_ANY() ARGS_REST()
+/* accept no arguments */
+#define ARGS_NONE() 0
+
+int mrb_get_args(mrb_state *mrb, const char *format, ...);
+
+mrb_value mrb_funcall(mrb_state*, mrb_value, const char*, int,...);
+mrb_value mrb_funcall_argv(mrb_state*, mrb_value, const char*, int, mrb_value*);
+mrb_value mrb_funcall_with_block(mrb_state*, mrb_value, const char*, int, mrb_value*, struct RProc*);
+mrb_sym mrb_intern(mrb_state*,const char*);
+const char *mrb_sym2name(mrb_state*,mrb_sym);
+mrb_value mrb_str_format(mrb_state *, int, const mrb_value *, mrb_value);
+
+void *mrb_malloc(mrb_state*, size_t);
+void *mrb_calloc(mrb_state*, size_t, size_t);
+void *mrb_realloc(mrb_state*, void*, size_t);
+void *mrb_obj_alloc(mrb_state*, enum mrb_vtype, struct RClass*);
+void *mrb_free(mrb_state*, void*);
+
+mrb_value mrb_str_new_cstr(mrb_state*, const char*);
+
+mrb_state* mrb_open(void);
+mrb_state* mrb_open_allocf(mrb_allocf);
+void mrb_close(mrb_state*);
+int mrb_checkstack(mrb_state*,int);
+
+mrb_value mrb_run(mrb_state*, struct RProc*, mrb_value);
+
+mrb_value mrb_p(mrb_state*, mrb_value);
+int mrb_obj_id(mrb_value obj);
+mrb_sym mrb_to_id(mrb_state *mrb, mrb_value name);
+
+int mrb_obj_equal(mrb_state*, mrb_value, mrb_value);
+int mrb_equal(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
+mrb_value mrb_Integer(mrb_state *mrb, mrb_value val);
+mrb_value mrb_Float(mrb_state *mrb, mrb_value val);
+mrb_value mrb_inspect(mrb_state *mrb, mrb_value obj);
+int mrb_eql(mrb_state *mrb, mrb_value obj1, mrb_value obj2);
+
+void mrb_garbage_collect(mrb_state*);
+void mrb_incremental_gc(mrb_state *);
+int mrb_gc_arena_save(mrb_state*);
+void mrb_gc_arena_restore(mrb_state*,int);
+void mrb_gc_mark(mrb_state*,struct RBasic*);
+#define mrb_gc_mark_value(mrb,val) do {\
+ if ((val).tt >= MRB_TT_OBJECT) mrb_gc_mark((mrb), mrb_object(val));\
+} while (0);
+void mrb_gc_mark_gv(mrb_state*);
+void mrb_gc_free_gv(mrb_state*);
+void mrb_gc_mark_iv(mrb_state*, struct RObject*);
+size_t mrb_gc_mark_iv_size(mrb_state*, struct RObject*);
+void mrb_gc_free_iv(mrb_state*, struct RObject*);
+void mrb_gc_mark_mt(mrb_state*, struct RClass*);
+size_t mrb_gc_mark_mt_size(mrb_state*, struct RClass*);
+void mrb_gc_free_mt(mrb_state*, struct RClass*);
+void mrb_gc_mark_ht(mrb_state*, struct RClass*);
+size_t mrb_gc_mark_ht_size(mrb_state*, struct RClass*);
+void mrb_gc_free_ht(mrb_state*, struct RClass*);
+void mrb_field_write_barrier(mrb_state *, struct RBasic*, struct RBasic*);
+#define mrb_field_write_barrier_value(mrb, obj, val) do{\
+ if ((val.tt >= MRB_TT_OBJECT)) mrb_field_write_barrier((mrb), (obj), mrb_object(val));\
+} while (0);
+void mrb_write_barrier(mrb_state *, struct RBasic*);
+
+#define MRUBY_VERSION "Rite"
+
+#if 0
+#define DEBUG(x) x
+#else
+#define DEBUG(x)
+#endif
+
+mrb_value mrb_check_convert_type(mrb_state *mrb, mrb_value val, mrb_int type, const char *tname, const char *method);
+mrb_value mrb_any_to_s(mrb_state *mrb, mrb_value obj);
+const char * mrb_obj_classname(mrb_state *mrb, mrb_value obj);
+struct RClass* mrb_obj_class(mrb_state *mrb, mrb_value obj);
+mrb_value mrb_class_path(mrb_state *mrb, struct RClass *c);
+mrb_value mrb_convert_type(mrb_state *mrb, mrb_value val, mrb_int type, const char *tname, const char *method);
+mrb_int mrb_obj_is_kind_of(mrb_state *mrb, mrb_value obj, struct RClass *c);
+mrb_value mrb_obj_inspect(mrb_state *mrb, mrb_value self);
+mrb_value mrb_obj_clone(mrb_state *mrb, mrb_value self);
+mrb_value mrb_check_funcall(mrb_state *mrb, mrb_value recv, mrb_sym mid, int argc, mrb_value *argv);
+
+/* need to include <ctype.h> to use these macros */
+#ifndef ISPRINT
+//#define ISASCII(c) isascii((int)(unsigned char)(c))
+#define ISASCII(c) 1
+#undef ISPRINT
+#define ISPRINT(c) (ISASCII(c) && isprint((int)(unsigned char)(c)))
+#define ISSPACE(c) (ISASCII(c) && isspace((int)(unsigned char)(c)))
+#define ISUPPER(c) (ISASCII(c) && isupper((int)(unsigned char)(c)))
+#define ISLOWER(c) (ISASCII(c) && islower((int)(unsigned char)(c)))
+#define ISALNUM(c) (ISASCII(c) && isalnum((int)(unsigned char)(c)))
+#define ISALPHA(c) (ISASCII(c) && isalpha((int)(unsigned char)(c)))
+#define ISDIGIT(c) (ISASCII(c) && isdigit((int)(unsigned char)(c)))
+#define ISXDIGIT(c) (ISASCII(c) && isxdigit((int)(unsigned char)(c)))
+#endif
+
+extern mrb_value mrb_rs;
+extern mrb_value mrb_default_rs;
+
+int mrb_block_given_p(void);
+void mrb_raise(mrb_state *mrb, struct RClass *c, const char *fmt, ...);
+void rb_raise(struct RClass *c, const char *fmt, ...);
+void mrb_warn(const char *fmt, ...);
+void mrb_warning(const char *fmt, ...);
+void mrb_bug(const char *fmt, ...);
+
+#define E_TYPE_ERROR (mrb_class_obj_get(mrb, "TypeError"))
+#define E_ARGUMENT_ERROR (mrb_class_obj_get(mrb, "ArgumentError"))
+#define E_INDEX_ERROR (mrb_class_obj_get(mrb, "IndexError"))
+#define E_RANGE_ERROR (mrb_class_obj_get(mrb, "RangeError"))
+#define E_NAME_ERROR (mrb_class_obj_get(mrb, "NameError"))
+#define E_NOMETHOD_ERROR (mrb_class_obj_get(mrb, "NoMethodError"))
+#define E_SCRIPT_ERROR (mrb_class_obj_get(mrb, "ScriptError"))
+#define E_SYNTAX_ERROR (mrb_class_obj_get(mrb, "SyntaxError"))
+#define E_LOAD_ERROR (mrb_class_obj_get(mrb, "LoadError"))
+#define E_SYSTEMCALL_ERROR (mrb_class_obj_get(mrb, "SystemCallError"))
+#define E_LOCALJUMP_ERROR (mrb_class_obj_get(mrb, "LocalJumpError"))
+#define E_REGEXP_ERROR (mrb_class_obj_get(mrb, "RegexpError"))
+#define E_ZERODIVISION_ERROR (mrb_class_obj_get(mrb, "ZeroDivisionError"))
+
+#define E_ENCODING_ERROR (mrb_class_obj_get(mrb, "EncodingError"))
+#define E_NOTIMP_ERROR (mrb_class_obj_get(mrb, "NotImplementedError"))
+#define E_FLOATDOMAIN_ERROR (mrb_class_obj_get(mrb, "FloatDomainError"))
+
+#define E_KEY_ERROR (mrb_class_obj_get(mrb, "KeyError"))
+
+#define SYM2ID(x) ((x).value.sym)
+
+#define CONST_ID_CACHE(mrb, result, str) \
+ { \
+ static mrb_sym mrb_intern_id_cache;\
+ if (!mrb_intern_id_cache) \
+ mrb_intern_id_cache = mrb_intern(mrb, str); \
+ result mrb_intern_id_cache; \
+ }
+#define CONST_ID(mrb, var, str) \
+ do CONST_ID_CACHE(mrb, var =, str) while (0)
+
+#define NUM2CHR_internal(x) (((mrb_type(x) == MRB_TT_STRING)&&(RSTRING_LEN(x)>=1))?\
+ RSTRING_PTR(x)[0]:(char)(mrb_fixnum_number(x)&0xff))
+#ifdef __GNUC__
+# define NUM2CHR(x) __extension__ ({mrb_value num2chr_x = (x); NUM2CHR_internal(num2chr_x);})
+#else
+static inline char
+NUM2CHR(mrb_value x)
+{
+ return NUM2CHR_internal(x);
+}
+#endif
+mrb_value mrb_io_gets(mrb_state *mrb, mrb_value);
+mrb_value mrb_io_getbyte(mrb_state *mrb, mrb_value);
+mrb_value mrb_io_ungetc(mrb_state *, mrb_value, mrb_value);
+mrb_value mrb_io_ungetbyte(mrb_state *mrb, mrb_value, mrb_value);
+mrb_value mrb_io_close(mrb_state *mrb, mrb_value);
+mrb_value mrb_io_flush(mrb_state *mrb, mrb_value);
+mrb_value mrb_io_eof(mrb_value);
+mrb_value mrb_io_binmode(mrb_state *mrb, mrb_value);
+mrb_value mrb_io_ascii8bit_binmode(mrb_value);
+mrb_value mrb_io_addstr(mrb_state *mrb, mrb_value, mrb_value);
+mrb_value mrb_io_printf(mrb_state *mrb, int, mrb_value*, mrb_value);
+mrb_value mrb_io_print(mrb_state *mrb, int, mrb_value*, mrb_value);
+mrb_value mrb_io_puts(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value);
+mrb_value mrb_io_fdopen(int, int, const char*);
+mrb_value mrb_io_get_io(mrb_state *mrb, mrb_value);
+mrb_value mrb_file_open(mrb_state *mrb, const char*, const char*);
+mrb_value mrb_file_open_str(mrb_value, const char*);
+mrb_value mrb_gets(mrb_state *mrb);
+
+mrb_value mrb_yield(mrb_state *mrb, mrb_value v, mrb_value blk);
+mrb_value mrb_yield_argv(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv);
+mrb_value mrb_yield_with_self(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv, mrb_value self);
+mrb_value mrb_class_new_instance(mrb_state *mrb, int, mrb_value*, struct RClass *);
+mrb_value mrb_class_new_instance_m(mrb_state *mrb, mrb_value klass);
+
+mrb_value mrb_exec_recursive(mrb_state *mrb, mrb_value(*)(mrb_state *, mrb_value, mrb_value, int),mrb_value,void *);
+
+#ifndef xmalloc
+#define xmalloc malloc
+#define xrealloc realloc
+#define xcalloc calloc
+#define xfree free
+#endif
+
+void mrb_gc(void);
+#define thread_debug if(0)printf
+
+#define RUBY_VM 1 /* YARV */
+#define HAVE_NATIVETHREAD
+int ruby_native_thread_p(void);
+
+#define RUBY_EVENT_NONE 0x0000
+#define RUBY_EVENT_LINE 0x0001
+#define RUBY_EVENT_CLASS 0x0002
+#define RUBY_EVENT_END 0x0004
+#define RUBY_EVENT_CALL 0x0008
+#define RUBY_EVENT_RETURN 0x0010
+#define RUBY_EVENT_C_CALL 0x0020
+#define RUBY_EVENT_C_RETURN 0x0040
+#define RUBY_EVENT_RAISE 0x0080
+#define RUBY_EVENT_ALL 0xffff
+#define RUBY_EVENT_VM 0x10000
+#define RUBY_EVENT_SWITCH 0x20000
+#define RUBY_EVENT_COVERAGE 0x40000
+
+typedef unsigned int mrb_event_flag_t;
+typedef void (*mrb_event_hook_func_t)(mrb_state *mrb, mrb_event_flag_t, mrb_value data, mrb_value, mrb_sym, mrb_value klass);
+
+typedef struct mrb_event_hook_struct {
+ mrb_event_flag_t flag;
+ mrb_event_hook_func_t func;
+ mrb_value data;
+ struct mrb_event_hook_struct *next;
+} mrb_event_hook_t;
+
+#define RB_EVENT_HOOKS_HAVE_CALLBACK_DATA 1
+void mrb_add_event_hook(mrb_state *mrb, mrb_event_hook_func_t func, mrb_event_flag_t events,
+ mrb_value data);
+int mrb_remove_event_hook(mrb_event_hook_func_t func);
+mrb_value mrb_to_int(mrb_state *mrb, mrb_value val);
+void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
+
+//#define RUBY_SETJMP(env) ${setjmp_prefix}setjmp(env${setjmp_sigmask+,0})
+//#define RUBY_LONGJMP(env,val) ${setjmp_prefix}longjmp(env,val)
+//#define RUBY_JMP_BUF ${setjmp_sigmask+${setjmp_prefix}}jmp_buf
+#define RUBY_SETJMP(env) __builtin_setjmp(env)
+#define RUBY_LONGJMP(env,val) __builtin_longjmp(env,1)//(env,val)
+//#define RUBY_JMP_BUF ${setjmp_sigmask+${setjmp_prefix}}jmp_buf
+#define select(n, r, w, e, t) select_large_fdset(n, r, w, e, t)
+
+//int RUBY_SETJMP(mrb_jmpbuf_t env); /* add kusuda */
+#define ruby_setjmp(env) RUBY_SETJMP(env)
+#define ruby_longjmp(env,val) RUBY_LONGJMP(env,val)
+
+#if defined PRIdPTR && !defined PRI_VALUE_PREFIX
+#define PRIdVALUE PRIdPTR
+#define PRIiVALUE PRIiPTR
+#define PRIoVALUE PRIoPTR
+#define PRIuVALUE PRIuPTR
+#define PRIxVALUE PRIxPTR
+#define PRIXVALUE PRIXPTR
+#else
+#define PRIdVALUE PRI_VALUE_PREFIX"d"
+#define PRIiVALUE PRI_VALUE_PREFIX"i"
+#define PRIoVALUE PRI_VALUE_PREFIX"o"
+#define PRIuVALUE PRI_VALUE_PREFIX"u"
+#define PRIxVALUE PRI_VALUE_PREFIX"x"
+#define PRIXVALUE PRI_VALUE_PREFIX"X"
+#endif
+#ifndef PRI_VALUE_PREFIX
+# define PRI_VALUE_PREFIX ""
+#endif
+
+#if defined PRIdPTR
+# define PRI_PTRDIFF_PREFIX "t"
+#elif SIZEOF_PTRDIFF_T == SIZEOF_INT
+# define PRI_PTRDIFF_PREFIX
+#elif SIZEOF_PTRDIFF_T == SIZEOF_LONG
+# define PRI_PTRDIFF_PREFIX "l"
+#elif SIZEOF_PTRDIFF_T == SIZEOF_LONG_LONG
+# define PRI_PTRDIFF_PREFIX "ll"
+#else
+# define PRI_PTRDIFF_PREFIX
+#endif
+#define PRIdPTRDIFF PRI_PTRDIFF_PREFIX"d"
+#define PRIiPTRDIFF PRI_PTRDIFF_PREFIX"i"
+#define PRIoPTRDIFF PRI_PTRDIFF_PREFIX"o"
+#define PRIuPTRDIFF PRI_PTRDIFF_PREFIX"u"
+#define PRIxPTRDIFF PRI_PTRDIFF_PREFIX"x"
+#define PRIXPTRDIFF PRI_PTRDIFF_PREFIX"X"
+
+#if defined PRIdPTR
+# define PRI_SIZE_PREFIX "z"
+#elif SIZEOF_SIZE_T == SIZEOF_INT
+# define PRI_SIZE_PREFIX
+#elif SIZEOF_SIZE_T == SIZEOF_LONG
+# define PRI_SIZE_PREFIX "l"
+#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
+# define PRI_SIZE_PREFIX "ll"
+#endif
+#define PRIdSIZE PRI_SIZE_PREFIX"d"
+#define PRIiSIZE PRI_SIZE_PREFIX"i"
+#define PRIoSIZE PRI_SIZE_PREFIX"o"
+#define PRIuSIZE PRI_SIZE_PREFIX"u"
+#define PRIxSIZE PRI_SIZE_PREFIX"x"
+#define PRIXSIZE PRI_SIZE_PREFIX"X"
+#define PRIdPTRDIFF PRI_PTRDIFF_PREFIX"d"
+
+#define KHASH 0
+#define STHASH 1
+#define BASICHASH 2
+#define HASH_CLASS_METHOD BASICHASH
+
+typedef enum call_type {
+ CALL_PUBLIC,
+ CALL_FCALL,
+ CALL_VCALL,
+ CALL_TYPE_MAX
+} call_type;
+
+/* compar.c */
+void mrb_cmperr(mrb_state *mrb, mrb_value x, mrb_value y);
+int mrb_cmpint(mrb_state *mrb, mrb_value val, mrb_value a, mrb_value b);
+
+#define REALLOC_N(mrb,var,type,n) (var)=(type*)mrb_realloc(mrb, (char*)(var), sizeof(type)*(n))
+
+#ifndef ANYARGS
+# ifdef __cplusplus
+# define ANYARGS ...
+# else
+# define ANYARGS
+# endif
+#endif
+void st_foreach_safe(mrb_state *mrb, void *table, int (*func)(ANYARGS), void * a);
+void mrb_define_alias(mrb_state *mrb, struct RClass *klass, const char *name1, const char *name2);
+const char *mrb_class_name(mrb_state *mrb, struct RClass* klass);
+void mrb_define_global_const(mrb_state *mrb, const char *name, mrb_value val);
+
+mrb_value mrb_block_proc(void);
+int mrb_sourceline(void);
+void ruby_default_signal(int sig);
+mrb_value mrb_attr_get(mrb_state *mrb, mrb_value obj, mrb_sym id);
+
+#endif /* MRUBY_H */
42 include/mruby/array.h
View
@@ -0,0 +1,42 @@
+#ifndef MRUBY_ARRAY_H
+#define MRUBY_ARRAY_H
+
+struct RArray {
+ MRUBY_OBJECT_HEADER;
+ size_t len;
+ size_t capa;
+ mrb_value *buf;
+};
+
+#define mrb_ary_ptr(v) ((struct RArray*)((v).value.p))
+#define mrb_ary_value(p) mrb_obj_value((void*)(p))
+#define RARRAY(v) ((struct RArray*)((v).value.p))
+
+#define RARRAY_LEN(a) (RARRAY(a)->len)
+#define RARRAY_PTR(a) (RARRAY(a)->buf)
+
+mrb_value mrb_ary_new_capa(mrb_state*, size_t);
+mrb_value mrb_ary_new(mrb_state *mrb);
+mrb_value mrb_ary_new_elts(mrb_state *mrb, long n, const mrb_value *elts);
+void mrb_ary_concat(mrb_state*, mrb_value, mrb_value);
+mrb_value mrb_ary_splat(mrb_state*, mrb_value);
+void mrb_ary_push(mrb_state*, mrb_value, mrb_value);
+mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
+mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_value *vals, size_t size);
+mrb_value mrb_ary_aget(mrb_state *mrb, mrb_value self);
+mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
+void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
+int mrb_ary_len(mrb_state *mrb, mrb_value ary);
+mrb_value mrb_ary_replace_m(mrb_state *mrb, mrb_value self);
+void mrb_ary_replace(mrb_state *mrb, struct RArray *a, mrb_value *argv, size_t len);
+mrb_value mrb_check_array_type(mrb_state *mrb, mrb_value self);
+mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item);
+mrb_value mrb_ary_new4(mrb_state *mrb, long n, const mrb_value *elts);
+mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr);
+mrb_value mrb_ary_entry(mrb_value ary, long offset);
+void mrb_mem_clear(mrb_value *mem, long size);
+mrb_value mrb_ary_tmp_new(mrb_state *mrb, long capa);
+mrb_value mrb_ary_sort(mrb_state *mrb, mrb_value ary);
+mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self);
+
+#endif /* MRUBY_ARRAY_H */
71 include/mruby/class.h
View
@@ -0,0 +1,71 @@
+#ifndef MRUBY_CLASS_H
+#define MRUBY_CLASS_H
+
+struct RClass {
+ MRUBY_OBJECT_HEADER;
+ struct kh_iv *iv;
+ struct kh_mt *mt;
+ struct RClass *super;
+};
+
+#define mrb_class_ptr(v) ((struct RClass*)((v).value.p))
+#define RCLASS_SUPER(v) (((struct RClass*)((v).value.p))->super)
+#define RCLASS_IV_TBL(v) (((struct RClass*)((v).value.p))->iv)
+#define RCLASS_M_TBL(v) (((struct RClass*)((v).value.p))->mt)
+
+static inline struct RClass*
+mrb_class(mrb_state *mrb, mrb_value v)
+{
+ switch (mrb_type(v)) {
+ case MRB_TT_FALSE:
+ if (v.value.p)
+ return mrb->false_class;
+ return mrb->nil_class;
+ case MRB_TT_TRUE:
+ return mrb->true_class;
+ case MRB_TT_SYMBOL:
+ return mrb->symbol_class;
+ case MRB_TT_FIXNUM:
+ return mrb->fixnum_class;
+ case MRB_TT_FLOAT:
+ return mrb->float_class;
+
+#ifdef INCLUDE_REGEXP
+// case MRB_TT_REGEX:
+// return mrb->regex_class;
+// case MRB_TT_MATCH:
+// return mrb->match_class;
+// case MRB_TT_DATA:
+// return mrb->encode_class;
+#else
+ case MRB_TT_REGEX:
+ case MRB_TT_MATCH:
+ mrb_raise(mrb, E_TYPE_ERROR, "type mismatch: %s given",
+ mrb_obj_classname(mrb, v));
+ return mrb->nil_class; /* not reach */
+#endif
+ default:
+ return ((struct RBasic*)mrb_object(v))->c;
+ }
+}
+
+#define MRB_SET_INSTANCE_TT(c, tt) c->flags = ((c->flags & ~0xff) | (char)tt)
+#define MRB_INSTANCE_TT(c) (enum mrb_vtype)(c->flags & 0xff)
+
+struct RClass* mrb_define_class_id(mrb_state*, mrb_sym, struct RClass*);
+struct RClass* mrb_define_module_id(mrb_state*, mrb_sym);
+struct RClass *mrb_vm_define_class(mrb_state*, mrb_value, mrb_value, mrb_sym);
+struct RClass *mrb_vm_define_module(mrb_state*, mrb_value, mrb_sym);
+void mrb_define_method_vm(mrb_state*, struct RClass*, mrb_sym, mrb_value);
+void mrb_define_method_raw(mrb_state*, struct RClass*, mrb_sym, struct RProc *);
+
+struct RClass *mrb_class_outer_module(mrb_state*, struct RClass *);
+struct RProc *mrb_method_search_vm(mrb_state*, struct RClass**, mrb_sym);
+struct RProc *mrb_method_search(mrb_state*, struct RClass*, mrb_sym);
+
+int mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid);
+void mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t func, int aspec);
+
+void mrb_obj_call_init(mrb_state *mrb, mrb_value obj, int argc, mrb_value *argv);
+
+#endif /* MRUBY_CLASS_H */
59 include/mruby/hash.h
View
@@ -0,0 +1,59 @@
+#ifndef MRUBY_HASH_H
+#define MRUBY_HASH_H
+
+struct RHash {
+ MRUBY_OBJECT_HEADER;
+ struct kh_ht *ht;
+ mrb_value ifnone;
+};
+
+#define N 624
+#define M 397
+#define MATRIX_A 0x9908b0dfU /* constant vector a */
+#define UMASK 0x80000000U /* most significant w-r bits */
+#define LMASK 0x7fffffffU /* least significant r bits */
+#define MIXBITS(u,v) ( ((u) & UMASK) | ((v) & LMASK) )
+#define TWIST(u,v) ((MIXBITS(u,v) >> 1) ^ ((v)&1U ? MATRIX_A : 0U))
+enum {MT_MAX_STATE = N};
+
+struct MT {
+ /* assume int is enough to store 32bits */
+ unsigned int state[N]; /* the array for the state vector */
+ unsigned int *next;
+ int left;
+};
+
+#define mrb_hash_end(h) st_hash_end(h)
+#define mrb_hash_uint(h, i) st_hash_uint(h, i)
+
+#define mrb_hash_ptr(v) ((struct RHash*)((v).value.p))
+#define mrb_hash_value(p) mrb_obj_value((void*)(p))
+
+mrb_value mrb_hash_new_capa(mrb_state*, size_t);
+mrb_value mrb_hash_new(mrb_state *mrb, int capa);
+
+void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val);
+mrb_value mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key);
+mrb_value mrb_hash_getWithDef(mrb_state *mrb, mrb_value hash, mrb_value vkey, mrb_value def);
+mrb_value mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key);
+mrb_value mrb_hash(mrb_state *mrb, mrb_value obj);
+void ruby_setenv(mrb_state *mrb, const char *name, const char *value);
+
+/* RHASH_TBL allocates st_table if not available. */
+#define RHASH(obj) ((struct RHash*)((obj).value.p))
+#define RHASH_TBL(h) mrb_hash_tbl(h)
+#define RHASH_H_TBL(h) (RHASH(h)->ht)
+#define RHASH_SIZE(h) (RHASH_H_TBL(h)->size)
+#define RHASH_EMPTY_P(h) (RHASH_SIZE(h) == 0)
+#define RHASH_IFNONE(h) (RHASH(h)->ifnone)
+#define RHASH_PROCDEFAULT(h) (RHASH(h)->ifnone)
+struct kh_ht * mrb_hash_tbl(mrb_state *mrb, mrb_value hash);
+
+#define MRB_HASH_PROC_DEFAULT 256
+#define MRB_RHASH_PROCDEFAULT_P(h) (RHASH(h)->flags & MRB_HASH_PROC_DEFAULT)
+
+char * ruby_strdup(const char *str);
+void mrb_reset_random_seed(void);
+mrb_value mrb_obj_is_proc(mrb_value proc);
+
+#endif /* MRUBY_HASH_H */
17 include/mruby/numeric.h
View
@@ -0,0 +1,17 @@
+#ifndef MRUBY_NUMERIC_H
+#define MRUBY_NUMERIC_H
+
+#include <limits.h>
+
+#define RSHIFT(x,y) ((x)>>(int)(y))
+#define FIXNUM_MAX (LONG_MAX>>1)
+#define FIXNUM_MIN RSHIFT((long)LONG_MIN,1)
+#define POSFIXABLE(f) ((f) < FIXNUM_MAX+1)
+#define NEGFIXABLE(f) ((f) >= FIXNUM_MIN)
+#define FIXABLE(f) (POSFIXABLE(f) && NEGFIXABLE(f))
+
+mrb_value mrb_dbl2big(mrb_state *mrb, float d);
+void mrb_num_zerodiv(mrb_state *mrb);
+mrb_value mrb_fix2str(mrb_state *mrb, mrb_value x, int base);
+
+#endif /* MRUBY_NUMERIC_H */
46 include/mruby/object.h
View
@@ -0,0 +1,46 @@
+#ifndef MRUBY_OBJECT_H
+#define MRUBY_OBJECT_H
+
+#define MRUBY_OBJECT_HEADER \
+ enum mrb_vtype tt:8;\
+ int color:3;\
+ unsigned int flags:21;\
+ struct RClass *c;\
+ struct RBasic *gcnext;
+
+
+/* white: 011, black: 100, gray: 000 */
+#define MRB_GC_GRAY 0
+#define MRB_GC_WHITE_A 1
+#define MRB_GC_WHITE_B (1 << 1)
+#define MRB_GC_BLACK (1 << 2)
+#define MRB_GC_WHITES (MRB_GC_WHITE_A | MRB_GC_WHITE_B)
+#define MRB_GC_COLOR_MASK 7
+
+#define paint_gray(o) ((o)->color = MRB_GC_GRAY)
+#define paint_black(o) ((o)->color = MRB_GC_BLACK)
+#define paint_white(o) ((o)->color = MRB_GC_WHITES)
+#define paint_partial_white(s, o) ((o)->color = (s)->current_white_part)
+#define is_gray(o) ((o)->color == MRB_GC_GRAY)
+#define is_white(o) ((o)->color & MRB_GC_WHITES)
+#define is_black(o) ((o)->color & MRB_GC_BLACK)
+#define is_dead(s, o) (((o)->color & other_white_part(s) & MRB_GC_WHITES) || (o)->tt == MRB_TT_FREE)
+#define flip_white_part(s) ((s)->current_white_part = other_white_part(s))
+#define other_white_part(s) ((s)->current_white_part ^ MRB_GC_WHITES)
+
+struct RBasic {
+ MRUBY_OBJECT_HEADER;
+};
+
+struct RObject {
+ MRUBY_OBJECT_HEADER;
+ struct kh_iv *iv;
+};
+
+#define mrb_obj_ptr(v) ((struct RObject*)((v).value.p))
+#define RBASIC(obj) ((struct RBasic*)((obj).value.p))
+#define RBASIC_KLASS(v) ((struct RClass *)(((struct RBasic*)((v).value.p))->c))
+#define ROBJECT(v) ((struct RObject*)((v).value.p))
+#define ROBJECT_IVPTR(v) (((struct RObject*)((v).value.p))->iv)
+#define ROBJECT_NUMIV(v) (ROBJECT_IVPTR(v) ? ROBJECT_IVPTR(v)->size : 0)
+#endif /* MRUBY_OBJECT_H */
44 include/mruby/proc.h
View
@@ -0,0 +1,44 @@
+#ifndef MRUBY_PROC_H
+#define MRUBY_PROC_H
+
+#include "mruby.h"
+#include "irep.h"
+
+struct REnv {
+ MRUBY_OBJECT_HEADER;
+ mrb_value *stack;
+ mrb_sym mid;
+ int cioff;
+};
+
+struct RProc {
+ MRUBY_OBJECT_HEADER;
+ union {
+ mrb_irep *irep;
+ mrb_func_t func;
+ } body;
+ struct RClass *target_class;
+ struct REnv *env;
+};
+
+/* aspec access */
+#define ARGS_GETREQ(a) (((a) >> 19) & 0x1f)
+#define ARGS_GETOPT(a) (((a) >> 14) & 0x1f)
+#define ARGS_GETREST(a) ((a) & (1<<13))
+#define ARGS_GETPOST(a) (((a) >> 8) & 0x1f)
+#define ARGS_GETKEY(a) (((a) >> 3) & 0x1f))
+#define ARGS_GETKDICT(a) ((a) & (1<<2))
+#define ARGS_GETBLOCK(a) ((a) & (1<<1))
+
+#define MRB_PROC_CFUNC 128
+#define MRB_PROC_CFUNC_P(p) ((p)->flags & MRB_PROC_CFUNC)
+#define MRB_PROC_STRICT 256
+#define MRB_PROC_STRICT_P(p) ((p)->flags & MRB_PROC_STRICT)
+
+#define mrb_proc_ptr(v) ((struct RProc*)((v).value.p))
+
+struct RProc *mrb_proc_new(mrb_state*, mrb_irep*);
+struct RProc *mrb_proc_new_cfunc(mrb_state*, mrb_func_t);
+struct RProc *mrb_closure_new(mrb_state*, mrb_irep*);
+
+#endif /* MRUBY_STRING_H */
21 include/mruby/range.h
View
@@ -0,0 +1,21 @@
+#ifndef MRUBY_RANGE_H
+#define MRUBY_RANGE_H
+
+struct RRange {
+ MRUBY_OBJECT_HEADER;
+ struct mrb_range_edges {
+ mrb_value beg;
+ mrb_value end;
+ } *edges;
+ int excl;
+};
+
+#define mrb_range_ptr(v) ((struct RRange*)((v).value.p))
+#define mrb_range_value(p) mrb_obj_value((void*)(p))
+
+mrb_value mrb_range_new(mrb_state*, mrb_value, mrb_value, int);
+mrb_int mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_int err);
+int mrb_obj_is_instance_of(mrb_state *mrb, mrb_value obj, struct RClass* c);
+struct RClass* mrb_class_real(struct RClass* cl);
+
+#endif /* MRUBY_RANGE_H */
133 include/mruby/string.h
View
@@ -0,0 +1,133 @@
+#ifndef MRUBY_STRING_H
+#define MRUBY_STRING_H
+
+#ifdef INCLUDE_ENCODING
+#include "encoding.h"
+#endif
+
+#ifndef RB_GC_GUARD
+#define RB_GC_GUARD(v) v
+#endif
+
+#define IS_EVSTR(p,e) ((p) < (e) && (*(p) == '$' || *(p) == '@' || *(p) == '{'))
+
+#define mrb_str_new4 mrb_str_new_frozen
+
+#define STR_BUF_MIN_SIZE 128
+//#define RSTRING_EMBED_LEN_MAX STR_BUF_MIN_SIZE
+
+extern const char ruby_digitmap[];
+
+struct RString {
+ MRUBY_OBJECT_HEADER;
+ size_t len;
+ union {
+ size_t capa;
+ mrb_value shared;
+ } aux;
+ char *buf;
+};
+
+extern struct SCOPE {
+ struct RBasic super;
+ mrb_sym *local_tbl;
+ mrb_value *local_vars;
+ int flags;
+} *ruby_scope;
+
+struct RVarmap {
+ struct RBasic super;
+ mrb_sym id;
+ mrb_value val;
+ struct RVarmap *next;
+};
+extern struct RVarmap *ruby_dyna_vars;
+
+//struct st_hash_type {
+// int (*compare)();
+// int (*hash)();
+//};
+
+#define mrb_str_ptr(s) ((struct RString*)((s).value.p))
+#define RSTRING(s) ((struct RString*)((s).value.p))
+#define RSTRING_PTR(s) (RSTRING(s)->buf)
+#define RSTRING_LEN(s) (RSTRING(s)->len)
+#define RSTRING_CAPA(s) (RSTRING(s)->aux.capa)
+#define RSTRING_SHARED(s) (RSTRING(s)->aux.shared)
+#define RSTRING_END(s) (RSTRING(s)->buf + RSTRING(s)->len)
+
+#define MRB_STR_SHARED 256
+#define MRB_STR_SHARED_P(s) (FL_ALL(s, MRB_STR_SHARED))
+#define MRB_STR_NOCAPA (MRB_STR_SHARED)
+#define MRB_STR_NOCAPA_P(s) (FL_ANY(s, MRB_STR_NOCAPA))
+#define MRB_STR_UNSET_NOCAPA(s) do {\
+ FL_UNSET(s, MRB_STR_NOCAPA);\
+} while (0)
+
+mrb_value mrb_str_literal(mrb_state*, mrb_value);
+void mrb_str_concat(mrb_state*, mrb_value, mrb_value);
+mrb_value mrb_obj_to_str(mrb_state*, mrb_value);
+mrb_value mrb_str_plus(mrb_state*, mrb_value, mrb_value);
+mrb_value mrb_obj_as_string(mrb_state *mrb, mrb_value obj);
+mrb_value mrb_str_new(mrb_state *mrb, const char *p, size_t len); /* mrb_str_new */
+mrb_value mrb_str_resize(mrb_state *mrb, mrb_value str, size_t len); /* mrb_str_resize */
+mrb_value mrb_string_value(mrb_state *mrb, mrb_value *ptr); /* StringValue */
+mrb_value mrb_str_substr(mrb_state *mrb, mrb_value str, mrb_int beg, int len);
+mrb_value mrb_check_string_type(mrb_state *mrb, mrb_value str);
+mrb_value mrb_str_buf_new(mrb_state *mrb, size_t capa);
+mrb_value mrb_str_buf_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
+mrb_value str_buf_cat(mrb_state *mrb, mrb_value str, const char *ptr, size_t len);
+
+char * mrb_string_value_cstr(mrb_state *mrb, mrb_value *ptr);
+char * mrb_string_value_ptr(mrb_state *mrb, mrb_value ptr);
+mrb_value mrb_str_subseq(mrb_state *mrb, mrb_value str, long beg, long len);
+size_t mrb_str_sublen(mrb_state *mrb, mrb_value str, long pos);
+mrb_value mrb_str_size(mrb_state *mrb, mrb_value self);
+long mrb_str_offset(mrb_state *mrb, mrb_value str, long pos);
+mrb_value mrb_str_new2(mrb_state *mrb, const char *p);
+mrb_value mrb_str_dup(mrb_state *mrb, mrb_value str); /* mrb_str_dup */
+mrb_value mrb_str_new_frozen(mrb_state *mrb, mrb_value orig);
+mrb_value mrb_lastline_get(mrb_state *mrb);
+mrb_value mrb_usascii_str_new(mrb_state *mrb, const char *ptr, long len);
+void mrb_lastline_set(mrb_value val);
+mrb_value mrb_str_buf_cat_ascii(mrb_state *mrb, mrb_value str, const char *ptr);
+void mrb_str_modify(mrb_state *mrb, mrb_value str);
+void mrb_str_set_len(mrb_state *mrb, mrb_value str, long len);
+mrb_value mrb_str_intern(mrb_state *mrb, mrb_value self);
+void mrb_str_shared_replace(mrb_state *mrb, mrb_value str, mrb_value str2);
+mrb_value mrb_str_cat2(mrb_state *mrb, mrb_value str, const char *ptr);
+mrb_value mrb_str_catf(mrb_state *mrb, mrb_value str, const char *format, ...);
+mrb_value mrb_str_to_inum(mrb_state *mrb, mrb_value str, int base, int badcheck);
+double mrb_str_to_dbl(mrb_state *mrb, mrb_value str, int badcheck);
+mrb_value mrb_str_to_str(mrb_state *mrb, mrb_value str);
+mrb_value mrb_locale_str_new(mrb_state *mrb, const char *ptr, long len);
+mrb_value mrb_filesystem_str_new_cstr(mrb_state *mrb, const char *ptr);
+mrb_int mrb_str_hash(mrb_state *mrb, mrb_value str);
+int mrb_str_hash_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
+mrb_value str_new3(mrb_state *mrb, struct RClass* klass, mrb_value str);
+mrb_value mrb_str_buf_append(mrb_state *mrb, mrb_value str, mrb_value str2);
+void mrb_str_setter(mrb_state *mrb, mrb_value val, mrb_sym id, mrb_value *var);
+int mrb_str_is_ascii_only_p(mrb_state *mrb, mrb_value str);
+mrb_value mrb_str_inspect(mrb_state *mrb, mrb_value str);
+int mrb_str_equal(mrb_state *mrb, mrb_value str1, mrb_value str2);
+mrb_value str_new4(mrb_state *mrb, enum mrb_vtype ttype, mrb_value str);
+mrb_value * mrb_svar(mrb_int cnt);
+mrb_value mrb_str_drop_bytes(mrb_state *mrb, mrb_value str, long len);
+mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
+mrb_value mrb_str_cat(mrb_state *mrb, mrb_value str, const char *ptr, long len);
+mrb_value mrb_str_append(mrb_state *mrb, mrb_value str, mrb_value str2);
+size_t mrb_str_capacity(mrb_value str);
+
+#ifdef INCLUDE_ENCODING
+int sym_printable(mrb_state *mrb, const char *s, const char *send, mrb_encoding *enc);
+mrb_value mrb_str_conv_enc(mrb_state *mrb, mrb_value str, mrb_encoding *from, mrb_encoding *to);
+mrb_value mrb_str_conv_enc_opts(mrb_state *mrb, mrb_value str, mrb_encoding *from, mrb_encoding *to, int ecflags, mrb_value ecopts);
+mrb_value mrb_enc_str_new(mrb_state *mrb, const char *ptr, long len, mrb_encoding *enc);
+#else
+int mrb_symname_p(const char *name);
+#endif
+
+mrb_value mrb_tainted_str_new(mrb_state *mrb, const char *ptr, long len);
+int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
+
+#endif /* MRUBY_STRING_H */
16 include/mruby/struct.h
View
@@ -0,0 +1,16 @@
+#ifndef MSTRUCT_H
+#define MSTRUCT_H
+
+struct RStruct {
+ struct RBasic basic;
+ long len;
+ mrb_value *ptr;
+};
+#define RSTRUCT(st) ((struct RStruct*)((st).value.p))
+#define RSTRUCT_LEN(st) ((int)(RSTRUCT(st)->len))
+#define RSTRUCT_PTR(st) (RSTRUCT(st)->ptr)
+
+mrb_value mrb_yield_values(int n, ...);
+mrb_value mrb_mod_module_eval(mrb_state *mrb, int argc, mrb_value *argv, mrb_value mod);
+
+#endif //MSTRUCT_H
0  lib/.gitkeep
View
No changes.
62 mrblib/Makefile
View
@@ -0,0 +1,62 @@
+# makefile discription.
+# basic build file for RiteVM library
+# 11.Oct.2011 coded by Hiroshi Mimaki.
+
+# project-specific macros
+# extension of the executable-file is modifiable(.exe .out ...)
+BASEDIR = .
+TARGET := mrblib
+MLIB := $(TARGET).o
+CLIB := $(TARGET).c
+DLIB := $(TARGET).ctmp
+RLIB := $(TARGET).rbtmp
+MRB1 := $(BASEDIR)/*.rb
+MRBS := $(MRB1)
+
+# C compiler (gcc)
+CC = gcc
+DEBUG_MODE = 1
+ifeq ($(DEBUG_MODE),1)
+CFLAGS = -g
+else
+CFLAGS = -O3
+endif
+INCLUDES = -I../src -I../include
+ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS)
+MAKE_FLAGS = --no-print-directory CC="$(CC)" LL="$(LL)"
+
+# mruby compiler
+ifeq ($(OS),Windows_NT)
+MRBC = ../bin/mrbc.exe
+else
+MRBC = ../bin/mrbc
+endif
+
+##############################
+# generic build targets, rules
+
+.PHONY : all
+all : $(MRBC) $(MLIB)
+ @echo "make: built targets of `pwd`"
+
+# Compile mrblib source
+$(MLIB) : $(CLIB)
+ $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(CLIB) -o $(MLIB)
+
+# Compile C source from merged mruby source
+$(CLIB) : $(RLIB) $(MRBC)
+ $(MRBC) -Bmrblib_irep -o$(DLIB) $(RLIB); cat init_$(TARGET).c $(DLIB) > $@
+
+$(MRBC) : ../src/opcode.h ../src/codegen.c ../src/parse.y
+ $(MAKE) -C ../tools/mrbc $(MAKE_FLAGS)
+
+# merge mruby sources
+$(RLIB) : $(MRBS)
+ cat $? > $@
+
+# clean up
+.PHONY : clean
+clean :
+ -rm -f $(MRBC) $(MLIB) $(CLIB) $(RLIB) $(DLIB)
+ @echo "make: removing targets, objects and depend files of `pwd`"
+
79 mrblib/array.rb
View
@@ -0,0 +1,79 @@
+#
+# Array
+#
+class Array
+ # 15.2.12.5.10
+ def each(&block)
+ idx = 0
+ while(idx < length)
+ block.call(self[idx])
+ idx += 1
+ end
+ self
+ end
+
+ # 15.2.12.5.11
+ def each_index(&block)
+ idx = 0
+ while(idx < length)
+ block.call(idx)
+ idx += 1
+ end
+ self
+ end
+
+ # 15.2.12.5.7
+ def collect!(&block)
+ self.each_index{|idx|
+ self[idx] = block.call(self[idx])
+ }
+ self
+ end
+
+ # 15.2.12.5.20
+ # map!(&block)
+ alias map! collect!
+
+ # 15.2.12.5.15
+ def initialize(size=0, obj=nil, &block)
+ raise TypeError, "expected Integer for 1st argument" unless size.kind_of? Integer
+ raise ArgumentError, "negative array size" if size < 0
+
+ self.clear
+ if size > 0
+ self[size - 1] = nil # allocate
+
+ idx = 0
+ while(idx < size)
+ self[idx] = (block)? block.call(idx): obj
+ idx += 1
+ end
+ end
+
+ self
+ end
+
+ def delete(key, &block)
+ while i = self.index(key)
+ self.delete_at(i)
+ ret = key
+ end
+ if ret == nil && block
+ block.call
+ else
+ ret
+ end
+ end
+end
+
+# include modules
+module Enumerable; end
+module Comparable; end
+class Array
+ include Enumerable
+ include Comparable
+
+ def sort!(&block)
+ self.replace(self.sort(&block))
+ end
+end
63 mrblib/compar.rb
View
@@ -0,0 +1,63 @@
+### move to compar.c
+# module Comparable
+ # def == other
+ # cmp = self <=> other
+ # if cmp == 0
+ # true
+ # else
+ # false
+ # end
+ # end
+
+ # def < other
+ # cmp = self <=> other
+ # if cmp.nil?
+ # false
+ # elsif cmp < 0
+ # true
+ # else
+ # false
+ # end
+ # end
+
+ # def <= other
+ # cmp = self <=> other
+ # if cmp.nil?
+ # false
+ # elsif cmp <= 0
+ # true
+ # else
+ # false
+ # end
+ # end
+
+ # def > other
+ # cmp = self <=> other
+ # if cmp.nil?
+ # false
+ # elsif cmp > 0
+ # true
+ # else
+ # false
+ # end
+ # end
+
+ # def >= other
+ # cmp = self <=> other
+ # if cmp.nil?
+ # false
+ # elsif cmp >= 0
+ # true
+ # else
+ # false
+ # end
+ # end
+
+ # def between?(min,max)
+ # if self < min or self > max
+ # false
+ # else
+ # true
+ # end
+ # end
+# end
266 mrblib/enum.rb
View
@@ -0,0 +1,266 @@
+#
+# Enumerable
+#
+module Enumerable
+ # 15.3.2.2.1
+ def all?(&block)
+ st = true
+ if block
+ self.each{|val|
+ unless block.call(val)
+ st = false
+ break
+ end
+ }
+ else
+ self.each{|val|
+ unless val
+ st = false
+ break
+ end
+ }
+ end
+ st
+ end
+
+ # 15.3.2.2.2
+ def any?(&block)
+ st = false
+ if block
+ self.each{|val|
+ if block.call(val)
+ st = true
+ break
+ end
+ }
+ else
+ self.each{|val|
+ if val
+ st = true
+ break
+ end
+ }
+ end
+ st
+ end
+
+ # 15.3.2.2.3
+ def collect(&block)
+ ary = []
+ self.each{|val|
+ ary.push(block.call(val))
+ }
+ ary
+ end
+
+ # 15.3.2.2.4
+ def detect(ifnone=nil, &block)
+ ret = ifnone
+ self.each{|val|
+ if block.call(val)
+ ret = val
+ break
+ end
+ }
+ ret
+ end
+
+ # 15.3.2.2.5
+ def each_with_index(&block)
+ i = 0
+ self.each{|val|
+ block.call(val, i)
+ i += 1
+ }
+ self
+ end
+
+ # 15.3.2.2.6
+ def entries
+ ary = []
+ self.each{|val|
+ ary.push val
+ }
+ ary
+ end
+
+ # 15.3.2.2.7
+ # find(ifnone=nil, &block)
+ alias find detect
+
+ # 15.3.2.2.8
+ def find_all(&block)
+ ary = []
+ self.each{|val|
+ ary.push(val) if block.call(val)
+ }
+ ary
+ end
+
+ # 15.3.2.2.9
+ def grep(pattern, &block)
+ ary = []
+ self.each{|val|
+ if pattern === val
+ ary.push((block)? block.call(val): val)
+ end
+ }
+ ary
+ end
+
+ # 15.3.2.2.10
+ def include?(obj)
+ st = false
+ self.each{|val|
+ if val == obj
+ st = true
+ break
+ end
+ }
+ st
+ end
+
+ # 15.3.2.2.11
+ def inject(*args, &block)
+ raise ArgumentError, "too many arguments" if args.size > 2
+ flag = true # 1st element?
+ result = nil
+ self.each{|val|
+ if flag
+ # 1st element
+ result = (args.empty?)? val: block.call(args[0], val)
+ flag = false
+ else
+ result = block.call(result, val)
+ end
+ }
+ result
+ end
+
+ # 15.3.2.2.12
+ # map(&block)
+ alias map collect
+
+ # 15.3.2.2.13
+ def max(&block)
+ flag = true # 1st element?
+ result = nil
+ self.each{|val|
+ if flag
+ # 1st element
+ result = val
+ flag = false
+ else
+ if block
+ result = val if block.call(val, result) > 0
+ else
+ result = val if (val <=> result) > 0
+ end
+ end
+ }
+ result
+ end
+
+ # 15.3.2.2.14
+ def min(&block)
+ flag = true # 1st element?
+ result = nil
+ self.each{|val|
+ if flag
+ # 1st element
+ result = val
+ flag = false
+ else
+ if block
+ result = val if block.call(val, result) < 0
+ else
+ result = val if (val <=> result) < 0
+ end
+ end
+ }
+ result
+ end
+
+ # 15.3.2.2.15
+ # member?(obj)
+ alias member? include?
+
+ # 15.3.2.2.16
+ def partition(&block)
+ ary_T = []
+ ary_F = []
+ self.each{|val|
+ if block.call(val)
+ ary_T.push(val)
+ else
+ ary_F.push(val)
+ end
+ }
+ [ary_T, ary_F]
+ end
+
+ # 15.3.2.2.17
+ def reject(&block)
+ ary = []
+ self.each{|val|
+ ary.push(val) unless block.call(val)
+ }
+ ary
+ end
+
+ # 15.3.2.2.18
+ # select(&block)
+ alias select find_all
+
+
+ # Does this OK? Please test it.
+ def __sort_sub__(sorted, work, src_ary, head, tail, &block)
+ if head == tail
+ sorted[head] = work[head] if src_ary == 1
+ return
+ end
+
+ # on current step, which is a src ary?
+ if src_ary == 0
+ src, dst = sorted, work
+ else
+ src, dst = work, sorted
+ end
+
+ key = src[head] # key value for dividing values
+ i, j = head, tail # position to store on the dst ary
+
+ (head + 1).upto(tail){|idx|
+ if ((block)? block.call(src[idx], key): (src[idx] <=> key)) > 0
+ # larger than key
+ dst[j] = src[idx]
+ j -= 1
+ else
+ dst[i] = src[idx]
+ i += 1
+ end
+ }
+
+ sorted[i] = key
+
+ # sort each sub-array
+ src_ary = (src_ary + 1) % 2 # exchange a src ary
+ __sort_sub__(sorted, work, src_ary, head, i - 1, &block) if i > head
+ __sort_sub__(sorted, work, src_ary, i + 1, tail, &block) if i < tail
+ end
+# private :__sort_sub__
+
+ # 15.3.2.2.19
+ def sort(&block)
+ ary = []
+ self.each{|val| ary.push(val)}
+ unless ary.empty?
+ __sort_sub__(ary, ::Array.new(ary.size), 0, 0, ary.size - 1, &block)
+ end
+ ary
+ end
+
+ # 15.3.2.2.20
+ # to_a
+ alias to_a entries
+end
9 mrblib/error.rb
View
@@ -0,0 +1,9 @@
+#
+# Exception
+#
+class Exception
+ # 15.2.22.4.1
+ def self.exception(*args, &block)
+ self.new(*args, &block)
+ end
+end
58 mrblib/hash.rb
View
@@ -0,0 +1,58 @@
+#
+# Hash
+#
+class Hash
+ # 15.2.13.4.8
+ def delete(key, &block)
+ if block && ! self.has_key?(key)
+ block.call(key)
+ else
+ self.__delete(key)
+ end
+ end
+
+ # 15.2.13.4.9
+ def each(&block)
+ self.keys.each{|k| block.call([k, self[k]])}
+ self
+ end
+
+ # 15.2.13.4.10
+ def each_key(&block)
+ self.keys.each{|k| block.call(k)}
+ self
+ end
+
+ # 15.2.13.4.11
+ def each_value(&block)
+ self.keys.each{|k| block.call(self[k])}
+ self
+ end
+
+ # 15.2.13.4.16
+ def initialize(*args, &block)
+ self.__init_core(block, *args)
+ end
+
+ # 15.2.13.4.22
+ def merge(other, &block)
+ h = {}
+ raise "can't convert argument into Hash" unless other.respond_to?(:to_hash)
+ other = other.to_hash
+ self.each_key{|k| h[k] = self[k]}
+ if block
+ other.each_key{|k|
+ h[k] = (self.has_key?(k))? block.call(k, self[k], other[k]): other[k]
+ }
+ else
+ other.each_key{|k| h[k] = other[k]}
+ end
+ h
+ end
+end
+
+# include modules
+module Enumerable; end
+class Hash
+ include Enumerable
+end
17 mrblib/init_mrblib.c
View
@@ -0,0 +1,17 @@
+#include "mruby.h"
+#include "irep.h"
+#include "dump.h"
+#include "mruby/string.h"
+#include "mruby/proc.h"
+
+extern const char mrblib_irep[];
+
+void
+mrb_init_mrblib(mrb_state *mrb)
+{
+ int n = mrb_read_irep(mrb, mrblib_irep);
+
+ extern mrb_value mrb_top_self(mrb_state *mrb);
+ mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
+}
+
45 mrblib/kernel.rb
View
@@ -0,0 +1,45 @@
+#
+# Kernel
+#
+module Kernel
+ # 15.3.1.2.6
+ def self.lambda(&block)
+ ### *** TODO *** ###
+ block # dummy
+ end
+
+ # 15.3.1.2.8
+ def self.loop #(&block)
+ while(true)
+ yield
+ end
+ end
+
+ # 15.3.1.3.4
+ def __send__(symbol, *args, &block)
+ ### *** TODO *** ###
+ end
+
+ # 15.3.1.3.18
+ def instance_eval(string=nil, &block)
+ ### *** TODO *** ###
+ end
+
+ # 15.3.1.3.27
+ def lambda(&block)
+ ### *** TODO *** ###
+ block # dummy
+ end
+
+ # 15.3.1.3.29
+ def loop #(&block)
+ while(true)
+ yield
+ end
+ end
+
+ # 15.3.1.3.44
+ def send(symbol, *args, &block)
+ ### *** TODO *** ###
+ end
+end
42 mrblib/numeric.rb
View
@@ -0,0 +1,42 @@
+#
+# Integer
+#
+class Integer
+ # 15.2.8.3.15
+ def downto(num, &block)
+ raise TypeError, "expected Integer" unless num.kind_of? Integer
+ i = self
+ while(i >= num)
+ block.call(i)
+ i -= 1
+ end
+ self
+ end
+
+ # 15.2.8.3.22
+ def times(&block)
+ i = 0
+ while(i < self)
+ block.call(i)
+ i += 1
+ end
+ self
+ end
+
+ # 15.2.8.3.27
+ def upto(num, &block)
+ raise TypeError, "expected Integer" unless num.kind_of? Integer
+ i = self
+ while(i <= num)
+ block.call(i)
+ i += 1
+ end
+ self
+ end
+end
+
+# include modules
+module Comparable; end
+class Numeric
+ include Comparable
+end
20 mrblib/print.rb
View
@@ -0,0 +1,20 @@
+module Kernel
+ def print(*args)
+ i = 0
+ len = args.size
+ while i < len
+ __printstr__ args[i].to_s
+ i += 1
+ end
+ end
+ def puts(*args)
+ i = 0
+ len = args.size
+ while i < len
+ __printstr__ args[i].to_s
+ __printstr__ "\n"
+ i += 1
+ end
+ __printstr__ "\n" if len == 0
+ end
+end
30 mrblib/range.rb
View
@@ -0,0 +1,30 @@
+#
+# Range
+#
+class Range
+ # 15.2.14.4.4
+ def each(&block)
+ val = self.first
+ unless val.respond_to? :succ
+ raise TypeError, "can't iterate"
+ end
+
+ last = self.last
+ return self if (val <=> last) > 0
+
+ while((val <=> last) < 0)
+ block.call(val)
+ val = val.succ
+ end
+
+ block.call(val) unless exclude_end?
+
+ self
+ end
+end
+
+# include modules
+module Enumerable; end
+class Range
+ include Enumerable
+end
93 mrblib/string.rb
View
@@ -0,0 +1,93 @@
+#
+# String
+#
+class String
+ # 15.2.10.5.15
+ def each_line(&block)
+ # expect that str.index accepts an Integer for 1st argument as a byte data
+ offset = 0
+ while(pos = self.index(0x0a, offset))
+ block.call(self[offset, pos + 1 - offset])
+ offset = pos + 1
+ end
+ block.call(self[offset, self.size - offset]) if self.size > offset
+ self
+ end
+
+ # 15.2.10.5.18
+ def gsub(*args, &block)
+ unless (args.size == 1 && block) || args.size == 2
+ raise ArgumentError, "wrong number of arguments"
+ end
+
+ ### *** TODO *** ###
+ end
+
+ # 15.2.10.5.19
+ def gsub!(*args, &block)
+ str = self.gsub(*args, &block)
+ if str != self
+ self.replace(str)
+ self
+ else
+ nil
+ end
+ end
+
+ # 15.2.10.5.32
+ def scan(reg, &block)
+ ### *** TODO *** ###
+ end
+
+ # 15.2.10.5.36
+ def sub(*args, &block)
+ unless (args.size == 1 && block) || args.size == 2
+ raise ArgumentError, "wrong number of arguments"
+ end
+
+ ### *** TODO *** ###
+ end
+
+ # 15.2.10.5.37
+ def sub!(*args, &block)
+ str = self.sub(*args, &block)
+ if str != self
+ self.replace(str)
+ self
+ else
+ nil
+ end
+ end
+
+ def each_char(&block)
+ pos = 0
+ while(pos < self.size)
+ block.call(self[pos])
+ pos += 1
+ end
+ self
+ end
+
+ def each_byte(&block)
+ bytes = self.unpack("C*")
+ pos = 0
+ while(pos < bytes.size)
+ block.call(bytes[pos])
+ pos += 1
+ end
+ self
+ end
+
+ def []=(pos, value)
+ b = self[0, pos]
+ a = self[pos+1..-1]
+ p [b, value, a].join('')
+ self.replace([b, value, a].join(''))
+ end
+end
+
+# include modules
+module Comparable; end
+class String
+ include Comparable
+end
30 mrblib/struct.rb
View
@@ -0,0 +1,30 @@
+#
+# Struct
+#
+class Struct
+ # 15.2.18.4.4
+ def each(&block)
+ self.class.members.each{|field|
+ block.call(self[field])
+ }
+ self
+ end
+
+ # 15.2.18.4.5
+ def each_pair(&block)
+ self.class.members.each{|field|
+ block.call(field.to_sym, self[field])
+ }
+ self
+ end
+
+ # 15.2.18.4.7
+ def select(&block)
+ ary = []
+ self.class.members.each{|field|
+ val = self[field]
+ ary.push(val) if block.call(val)
+ }
+ ary
+ end
+end
89 src/Makefile
View
@@ -0,0 +1,89 @@
+# makefile discription.
+# basic build file for RiteVM library
+# 11.Apr.2011 coded by Kenji Yoshimoto.
+# 31.Aug.2011 coded by Hiroshi Mimaki.
+
+# project-specific macros
+# extension of the executable-file is modifiable(.exe .out ...)
+BASEDIR = .
+TARGET := ../lib/ritevm
+ifeq ($(OS),Windows_NT)
+LIB := $(TARGET).lib
+else
+LIB := $(TARGET).a
+endif
+YSRC := $(BASEDIR)/parse.y
+YC := $(BASEDIR)/y.tab.c
+EXCEPT1 := $(YC) $(BASEDIR)/minimain.c $(BASEDIR)/compile.c $(BASEDIR)/dump.c $(BASEDIR)/cdump.c
+OBJY := $(patsubst %.c,%.o,$(YC))
+OBJ1 := $(patsubst %.c,%.o,$(filter-out $(EXCEPT1),$(wildcard $(BASEDIR)/*.c)))
+#OBJ2 := $(patsubst %.c,%.o,$(wildcard $(BASEDIR)/ext/regex/*.c))
+#OBJ3 := $(patsubst %.c,%.o,$(wildcard $(BASEDIR)/ext/enc/*.c))
+OBJS := $(OBJ1) $(OBJ2) $(OBJ3)
+# mruby libraries
+EXTC := $(BASEDIR)/../mrblib/mrblib.c
+EXTRB := $(wildcard $(BASEDIR)/../mrblib/*.rb)
+EXTM := $(patsubst %.c,%.o,$(EXTC))
+# extend libraries
+#EXT1 := $(patsubst %.c,%.o,$(wildcard $(BASEDIR)/ext/socket/*.c))
+EXTS := $(EXT1)
+
+# libraries, includes
+INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include
+#INCLUDES = -I$(RITEVM_ROOT)
+
+# compiler, linker (gcc)
+CC = gcc
+AR = ar
+YACC = bison
+
+DEBUG_MODE = 1
+ifeq ($(DEBUG_MODE),1)
+CFLAGS = -g
+else
+CFLAGS = -O3
+endif
+ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS)
+MAKE_FLAGS = --no-print-directory CC="$(CC)" LL="$(LL)"
+
+##############################
+# generic build targets, rules
+
+.PHONY : all
+all : $(EXTM) $(LIB)
+ @echo "make: built targets of `pwd`"
+
+# executable constructed using linker from object files
+$(LIB) : $(OBJS) $(OBJY) $(EXTM) $(EXTS)
+ $(AR) r $@ $(OBJS) $(OBJY) $(EXTM) $(EXTS)
+
+-include $(OBJS:.o=.d) $(OBJY:.o=.d)
+
+# objects compiled from source
+$(OBJS) : %.o : %.c
+ $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $< -o $@
+
+# mruby library compile
+$(EXTM) : $(EXTRB) $(OBJS) $(OBJY)
+ $(MAKE) -C ../mrblib $(MAKE_FLAGS)
+
+# extend libraries complile
+$(EXTS) : %.o : %.c
+ $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $< -o $@
+
+# parser complie
+$(OBJY) : $(YC)
+ $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(YC) -o $(OBJY)
+
+# yacc complie
+$(YC) : $(YSRC)
+ $(YACC) -o $(YC) $(YSRC)
+
+# clean up
+.PHONY : clean #cleandep
+clean :
+ $(MAKE) clean -C ../mrblib $(MAKE_FLAGS)
+ -rm -f $(LIB) $(OBJS) $(OBJY) $(YC)
+ -rm -f $(OBJS:.o=.d) $(OBJY:.o=.d)
+ @echo "make: removing targets, objects and depend files of `pwd`"
+
1,458 src/array.c
View
@@ -0,0 +1,1458 @@
+#include "mruby.h"
+#include "mruby/array.h"
+#include <string.h>
+#include "mruby/string.h"
+#include "mdata.h"
+#include "mruby/class.h"
+
+#ifdef INCLUDE_REGEXP
+ #define mrb_usascii_str_new2 mrb_usascii_str_new_cstr
+#else
+ #define mrb_usascii_str_new2 mrb_str_new_cstr
+ #define mrb_usascii_str_new mrb_str_new
+#endif
+mrb_value mrb_exec_recursive_paired(mrb_state *mrb, mrb_value (*func) (mrb_state *, mrb_value, mrb_value, int),
+ mrb_value obj, mrb_value paired_obj, void* arg);
+
+//#define ARY_DEFAULT_LEN 16
+#define ARY_DEFAULT_LEN 4
+#define ARY_SHRINK_RATIO 5 /* must be larger than 2 */
+#ifdef LONG_MAX
+# define ARY_MAX_SIZE (LONG_MAX / sizeof(mrb_value))
+#endif
+
+static inline mrb_value
+ary_elt(mrb_value ary, long offset)
+{
+ if (RARRAY_LEN(ary) == 0) return mrb_nil_value();
+ if (offset < 0 || RARRAY_LEN(ary) <= offset) {
+ return mrb_nil_value();
+ }
+ return RARRAY_PTR(ary)[offset];
+}
+
+mrb_value
+mrb_ary_new_capa(mrb_state *mrb, size_t capa)
+{
+ struct RArray *a;
+
+ if (capa < 0) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "negative ary size (or size too big)");
+ }
+#ifdef LONG_MAX
+ if (capa > ARY_MAX_SIZE) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "ary size too big");
+ }
+#endif
+ if (capa < ARY_DEFAULT_LEN) {
+ capa = ARY_DEFAULT_LEN;
+ }
+
+ a = mrb_obj_alloc(mrb, MRB_TT_ARRAY, mrb->array_class);
+ a->buf = mrb_malloc(mrb, sizeof(mrb_value) * capa);
+ memset(a->buf, 0, sizeof(mrb_value) * capa);
+ a->capa = capa;
+ a->len = 0;
+
+ return mrb_obj_value(a);
+}
+
+mrb_value
+mrb_ary_new(mrb_state *mrb)
+{
+ return mrb_ary_new_capa(mrb, 0);
+}
+
+mrb_value
+mrb_ary_new_from_values(mrb_state *mrb, mrb_value *vals, size_t size)
+{
+ mrb_value ary;
+ struct RArray *a;
+
+ ary = mrb_ary_new_capa(mrb, size);
+ a = mrb_ary_ptr(ary);
+ memcpy(a->buf, vals, sizeof(mrb_value)*size);
+ a->len = size;
+
+ return ary;
+}
+
+mrb_value
+mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr)
+{
+ mrb_value arv[2];
+ arv[0] = car;
+ arv[1] = cdr;
+ return mrb_ary_new_from_values(mrb, arv, 2);
+}
+
+void
+ary_fill_with_nil(mrb_value *buf, size_t size)
+{
+ mrb_value nil = mrb_nil_value();
+
+ while((int)(size--)) {
+ *buf++ = nil;
+ }
+}
+
+void
+mrb_ary_expand_capa(mrb_state *mrb, struct RArray *a, size_t len)
+{
+ size_t capa = a->capa;
+
+#ifdef LONG_MAX
+ if (len > ARY_MAX_SIZE) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "array size too big");
+ }
+#endif
+
+ while(capa < len) {
+ if (capa == 0) {
+ capa = ARY_DEFAULT_LEN;
+ }
+ else {
+ capa *= 2;
+ }
+ }
+
+#ifdef LONG_MAX
+ if (capa > ARY_MAX_SIZE) capa = ARY_MAX_SIZE; /* len <= capa <= ARY_MAX_SIZE */
+#endif
+
+ if (capa > a->capa) {
+ a->capa = capa;
+ a->buf = mrb_realloc(mrb, a->buf, sizeof(mrb_value)*capa);
+ }
+}
+
+void
+mrb_ary_shrink_capa(mrb_state *mrb, struct RArray *a)
+{
+ size_t capa = a->capa;
+
+ if (capa < ARY_DEFAULT_LEN * 2) return;
+ if (capa <= a->len * ARY_SHRINK_RATIO) return;
+
+ do {
+ capa /= 2;
+ if (capa < ARY_DEFAULT_LEN) {
+ capa = ARY_DEFAULT_LEN;
+ break;
+ }
+ } while(capa > a->len * ARY_SHRINK_RATIO);
+
+ if (capa > a->len && capa < a->capa) {
+ a->capa = capa;
+ a->buf = mrb_realloc(mrb, a->buf, sizeof(mrb_value)*capa);
+ }
+}
+
+mrb_value
+mrb_ary_s_create(mrb_state *mrb, mrb_value self)
+{
+ mrb_value *vals;
+ int len;
+
+ mrb_get_args(mrb, "*", &vals, &len);
+ return mrb_ary_new_from_values(mrb, vals, (size_t)len);
+}
+
+void
+mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other)
+{
+ struct RArray *a1 = mrb_ary_ptr(self);
+ struct RArray *a2 = mrb_ary_ptr(other);
+ size_t len = a1->len + a2->len;
+
+ if (a1->capa < len) mrb_ary_expand_capa(mrb, a1, len);
+ memcpy(a1->buf+a1->len, a2->buf, sizeof(mrb_value)*a2->len);
+ mrb_write_barrier(mrb, (struct RBasic*)a1);
+ a1->len = len;
+}
+
+mrb_value
+mrb_ary_concat_m(mrb_state *mrb, mrb_value self)
+{
+ mrb_value other;
+
+ mrb_get_args(mrb, "o", &other);
+ if (mrb_type(other) != MRB_TT_ARRAY) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "expected Array");
+ }
+ mrb_ary_concat(mrb, self, other);
+ return self;
+}
+
+mrb_value
+mrb_ary_plus(mrb_state *mrb, mrb_value self)
+{
+ struct RArray *a1 = mrb_ary_ptr(self);
+ struct RArray *a2;
+ mrb_value other;
+ mrb_value ary;
+
+ mrb_get_args(mrb, "o", &other);
+ if (mrb_type(other) != MRB_TT_ARRAY) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "expected Array");
+ }
+
+ ary = mrb_ary_new_capa(mrb, a1->len + RARRAY_LEN(other));
+ a2 = mrb_ary_ptr(ary);
+ memcpy(a2->buf, a1->buf, sizeof(mrb_value)*a1->len);
+ memcpy(a2->buf + a1->len, RARRAY_PTR(other), sizeof(mrb_value)*RARRAY_LEN(other));
+ a2->len = a1->len + RARRAY_LEN(other);
+
+ return ary;
+}
+
+static mrb_value
+recursive_cmp(mrb_state *mrb, mrb_value ary1, mrb_value ary2, int recur)
+{
+ long i, len;
+
+ if (recur) return mrb_undef_value(); /* Subtle! */
+ len = RARRAY_LEN(ary1);
+ if (len > RARRAY_LEN(ary2)) {
+ len = RARRAY_LEN(ary2);
+ }
+
+ for (i=0; i<len; i++) {
+ mrb_value r = mrb_funcall(mrb, ary_elt(ary1, i), "<=>", 1, ary_elt(ary2, i));
+ if (mrb_type(r) != MRB_TT_FIXNUM || mrb_fixnum(r) != 0) return r;
+ }
+
+ return mrb_undef_value();
+}
+
+/*
+ * call-seq:
+ * ary <=> other_ary -> -1, 0, +1 or nil
+ *
+ * Comparison---Returns an integer (-1, 0, or +1)
+ * if this array is less than, equal to, or greater than <i>other_ary</i>.
+ * Each object in each array is compared (using <=>). If any value isn't
+ * equal, then that inequality is the return value. If all the
+ * values found are equal, then the return is based on a
+ * comparison of the array lengths. Thus, two arrays are
+ * ``equal'' according to <code>Array#<=></code> if and only if they have
+ * the same length and the value of each element is equal to the
+ * value of the corresponding element in the other array.
+ *