mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Merge branch 'master' of github.com:mruby/mruby
This commit is contained in:
+14
-3
@@ -5,6 +5,10 @@ os: Visual Studio 2015
|
||||
clone_depth: 50
|
||||
|
||||
|
||||
cache:
|
||||
- win_flex_bison-2.5.10.zip
|
||||
|
||||
|
||||
environment:
|
||||
matrix:
|
||||
# Visual Studio 2015 64bit
|
||||
@@ -18,10 +22,17 @@ environment:
|
||||
|
||||
init:
|
||||
- call "%visualcpp%" %machine%
|
||||
# For using bison.exe
|
||||
- set PATH=%PATH%;C:\cygwin\bin;
|
||||
# For using Rubyinstaller's Ruby 2.4 64bit
|
||||
- set PATH=C:\Ruby24-x64\bin;%PATH%
|
||||
- ruby --version
|
||||
|
||||
|
||||
install:
|
||||
- if not exist win_flex_bison-2.5.10.zip appveyor DownloadFile "https://github.com/lexxmark/winflexbison/releases/download/v.2.5.10/win_flex_bison-2.5.10.zip"
|
||||
- 7z x -y -owin_flex_bison win_flex_bison-2.5.10.zip > nul
|
||||
|
||||
|
||||
build_script:
|
||||
- set YACC=.\win_flex_bison\win_bison.exe
|
||||
- set MRUBY_CONFIG=appveyor_config.rb
|
||||
- ruby .\minirake test
|
||||
- ruby .\minirake test all
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ MRuby::Build.new('debug') do |conf|
|
||||
# include all core GEMs
|
||||
conf.gembox 'full-core'
|
||||
conf.compilers.each do |c|
|
||||
c.defines += %w(MRB_GC_STRESS MRB_GC_FIXED_ARENA)
|
||||
c.defines += %w(MRB_GC_STRESS MRB_GC_FIXED_ARENA MRB_METHOD_CACHE)
|
||||
end
|
||||
|
||||
build_mrbc_exec
|
||||
|
||||
+11
-12
@@ -124,17 +124,17 @@ MRuby::Build.new('test') do |conf|
|
||||
conf.gembox 'default'
|
||||
end
|
||||
|
||||
MRuby::Build.new('bench') do |conf|
|
||||
# Gets set by the VS command prompts.
|
||||
if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR']
|
||||
toolchain :visualcpp
|
||||
else
|
||||
toolchain :gcc
|
||||
conf.cc.flags << '-O3'
|
||||
end
|
||||
|
||||
conf.gembox 'default'
|
||||
end
|
||||
#MRuby::Build.new('bench') do |conf|
|
||||
# # Gets set by the VS command prompts.
|
||||
# if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR']
|
||||
# toolchain :visualcpp
|
||||
# else
|
||||
# toolchain :gcc
|
||||
# conf.cc.flags << '-O3'
|
||||
# end
|
||||
#
|
||||
# conf.gembox 'default'
|
||||
#end
|
||||
|
||||
# Define cross build settings
|
||||
# MRuby::CrossBuild.new('32bit') do |conf|
|
||||
@@ -148,5 +148,4 @@ end
|
||||
# conf.gem 'examples/mrbgems/c_and_ruby_extension_example'
|
||||
#
|
||||
# conf.test_runner.command = 'env'
|
||||
#
|
||||
# end
|
||||
|
||||
+10
-7
@@ -862,11 +862,14 @@ mrb_get_mid(mrb_state *mrb) /* get method symbol */
|
||||
return mrb->c->ci->mid;
|
||||
}
|
||||
|
||||
static inline mrb_int
|
||||
mrb_get_argc(mrb_state *mrb) /* get argc */
|
||||
{
|
||||
return mrb->c->ci->argc;
|
||||
}
|
||||
/**
|
||||
* Retrieve number of arguments from mrb_state.
|
||||
*
|
||||
* Correctly handles *splat arguments.
|
||||
*/
|
||||
MRB_API mrb_int mrb_get_argc(mrb_state *mrb);
|
||||
|
||||
MRB_API mrb_value* mrb_get_argv(mrb_state *mrb);
|
||||
|
||||
/* `strlen` for character string literals (use with caution or `strlen` instead)
|
||||
Adjacent string literals are concatenated in C/C++ in translation phase 6.
|
||||
@@ -1238,7 +1241,7 @@ MRB_API mrb_value mrb_format(mrb_state *mrb, const char *format, ...);
|
||||
/* use naive memcpy and memset instead */
|
||||
#undef memcpy
|
||||
#undef memset
|
||||
static inline void*
|
||||
static void*
|
||||
mrbmemcpy(void *dst, const void *src, size_t n)
|
||||
{
|
||||
char *d = (char*)dst;
|
||||
@@ -1249,7 +1252,7 @@ mrbmemcpy(void *dst, const void *src, size_t n)
|
||||
}
|
||||
#define memcpy(a,b,c) mrbmemcpy(a,b,c)
|
||||
|
||||
static inline void*
|
||||
static void*
|
||||
mrbmemset(void *s, int c, size_t n)
|
||||
{
|
||||
char *t = (char*)s;
|
||||
|
||||
@@ -45,9 +45,6 @@ typedef struct mrb_irep {
|
||||
struct mrb_irep_debug_info* debug_info;
|
||||
|
||||
int ilen, plen, slen, rlen, refcnt;
|
||||
|
||||
struct mrb_irep *outer; /* Refers outer scope */
|
||||
struct RClass *target_class;
|
||||
} mrb_irep;
|
||||
|
||||
#define MRB_ISEQ_NO_FREE 1
|
||||
|
||||
+32
-12
@@ -18,19 +18,20 @@ MRB_BEGIN_DECL
|
||||
struct REnv {
|
||||
MRB_OBJECT_HEADER;
|
||||
mrb_value *stack;
|
||||
ptrdiff_t cioff;
|
||||
union {
|
||||
mrb_sym mid;
|
||||
struct mrb_context *c;
|
||||
} cxt;
|
||||
struct mrb_context *cxt;
|
||||
mrb_sym mid;
|
||||
};
|
||||
|
||||
#define MRB_SET_ENV_STACK_LEN(e,len) (e)->flags = (unsigned int)(len)
|
||||
#define MRB_ENV_STACK_LEN(e) ((mrb_int)(e)->flags)
|
||||
#define MRB_ENV_UNSHARE_STACK(e) ((e)->cioff = -1)
|
||||
#define MRB_ENV_STACK_SHARED_P(e) ((e)->cioff >= 0)
|
||||
/* flags (21bits): 1(shared flag):10(cioff/bidx):10(stack_len) */
|
||||
#define MRB_ENV_SET_STACK_LEN(e,len) (e)->flags = (((e)->flags & ~0x3ff)|((unsigned int)(len) & 0x3ff))
|
||||
#define MRB_ENV_STACK_LEN(e) ((mrb_int)((e)->flags & 0x3ff))
|
||||
#define MRB_ENV_STACK_UNSHARED (1<<20)
|
||||
#define MRB_ENV_UNSHARE_STACK(e) (e)->flags |= MRB_ENV_STACK_UNSHARED
|
||||
#define MRB_ENV_STACK_SHARED_P(e) (((e)->flags & MRB_ENV_STACK_UNSHARED) == 0)
|
||||
#define MRB_ENV_BIDX(e) (((e)->flags >> 10) & 0x3ff)
|
||||
#define MRB_ENV_SET_BIDX(e,idx) (e)->flags = (((e)->flags & ~(0x3ff<<10))|((unsigned int)(idx) & 0x3ff)<<10)
|
||||
|
||||
MRB_API void mrb_env_unshare(mrb_state*, struct REnv*);
|
||||
void mrb_env_unshare(mrb_state*, struct REnv*);
|
||||
|
||||
struct RProc {
|
||||
MRB_OBJECT_HEADER;
|
||||
@@ -38,8 +39,11 @@ struct RProc {
|
||||
mrb_irep *irep;
|
||||
mrb_func_t func;
|
||||
} body;
|
||||
struct RClass *target_class;
|
||||
struct REnv *env;
|
||||
struct RProc *upper;
|
||||
union {
|
||||
struct RClass *target_class;
|
||||
struct REnv *env;
|
||||
} e;
|
||||
};
|
||||
|
||||
/* aspec access */
|
||||
@@ -57,6 +61,22 @@ struct RProc {
|
||||
#define MRB_PROC_STRICT_P(p) (((p)->flags & MRB_PROC_STRICT) != 0)
|
||||
#define MRB_PROC_ORPHAN 512
|
||||
#define MRB_PROC_ORPHAN_P(p) (((p)->flags & MRB_PROC_ORPHAN) != 0)
|
||||
#define MRB_PROC_ENVSET 1024
|
||||
#define MRB_PROC_ENV_P(p) (((p)->flags & MRB_PROC_ENVSET) != 0)
|
||||
#define MRB_PROC_ENV(p) (MRB_PROC_ENV_P(p) ? (p)->e.env : NULL)
|
||||
#define MRB_PROC_TARGET_CLASS(p) (MRB_PROC_ENV_P(p) ? (p)->e.env->c : (p)->e.target_class )
|
||||
#define MRB_PROC_SET_TARGET_CLASS(p,tc) do {\
|
||||
if (MRB_PROC_ENV_P(p)) {\
|
||||
(p)->e.env->c = (tc);\
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)(p)->e.env, (struct RBasic*)tc);\
|
||||
}\
|
||||
else {\
|
||||
(p)->e.target_class = (tc);\
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)tc);\
|
||||
}\
|
||||
} while (0)
|
||||
#define MRB_PROC_SCOPE 2048
|
||||
#define MRB_PROC_SCOPE_P(p) (((p)->flags & MRB_PROC_SCOPE) != 0)
|
||||
|
||||
#define mrb_proc_ptr(v) ((struct RProc*)(mrb_ptr(v)))
|
||||
|
||||
|
||||
@@ -68,6 +68,9 @@ struct RString {
|
||||
#define RSTR_SET_NOFREE_FLAG(s) ((s)->flags |= MRB_STR_NOFREE)
|
||||
#define RSTR_UNSET_NOFREE_FLAG(s) ((s)->flags &= ~MRB_STR_NOFREE)
|
||||
|
||||
#define RSTR_POOL_P(s) ((s)->flags & MRB_STR_POOL)
|
||||
#define RSTR_SET_POOL_FLAG(s) ((s)->flags |= MRB_STR_POOL)
|
||||
|
||||
/*
|
||||
* Returns a pointer from a Ruby string
|
||||
*/
|
||||
@@ -83,10 +86,11 @@ MRB_API mrb_int mrb_str_strlen(mrb_state*, struct RString*);
|
||||
#define MRB_STR_SHARED 1
|
||||
#define MRB_STR_FSHARED 2
|
||||
#define MRB_STR_NOFREE 4
|
||||
#define MRB_STR_NO_UTF 8
|
||||
#define MRB_STR_EMBED 16
|
||||
#define MRB_STR_EMBED_LEN_MASK 0x3e0
|
||||
#define MRB_STR_EMBED_LEN_SHIFT 5
|
||||
#define MRB_STR_POOL 8
|
||||
#define MRB_STR_NO_UTF 16
|
||||
#define MRB_STR_EMBED 32
|
||||
#define MRB_STR_EMBED_LEN_MASK 0x7c0
|
||||
#define MRB_STR_EMBED_LEN_SHIFT 6
|
||||
|
||||
void mrb_gc_free_str(mrb_state*, struct RString*);
|
||||
MRB_API void mrb_str_modify(mrb_state*, struct RString*);
|
||||
|
||||
@@ -14,6 +14,9 @@ MRuby::GemBox.new do |conf|
|
||||
# Use standard Struct class
|
||||
conf.gem :core => "mruby-struct"
|
||||
|
||||
# Use Comparable module extension
|
||||
conf.gem :core => "mruby-compar-ext"
|
||||
|
||||
# Use Enumerable module extension
|
||||
conf.gem :core => "mruby-enum-ext"
|
||||
|
||||
|
||||
@@ -2,4 +2,5 @@ MRuby::Gem::Specification.new('mruby-array-ext') do |spec|
|
||||
spec.license = 'MIT'
|
||||
spec.author = 'mruby developers'
|
||||
spec.summary = 'Array class extension'
|
||||
spec.add_test_dependency 'mruby-enumerator', core: 'mruby-enumerator'
|
||||
end
|
||||
|
||||
@@ -808,4 +808,100 @@ class Array
|
||||
n
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# ary.permutation { |p| block } -> ary
|
||||
# ary.permutation -> Enumerator
|
||||
# ary.permutation(n) { |p| block } -> ary
|
||||
# ary.permutation(n) -> Enumerator
|
||||
#
|
||||
# When invoked with a block, yield all permutations of length +n+ of the
|
||||
# elements of the array, then return the array itself.
|
||||
#
|
||||
# If +n+ is not specified, yield all permutations of all elements.
|
||||
#
|
||||
# The implementation makes no guarantees about the order in which the
|
||||
# permutations are yielded.
|
||||
#
|
||||
# If no block is given, an Enumerator is returned instead.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# a = [1, 2, 3]
|
||||
# a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
||||
# a.permutation(1).to_a #=> [[1],[2],[3]]
|
||||
# a.permutation(2).to_a #=> [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
|
||||
# a.permutation(3).to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
|
||||
# a.permutation(0).to_a #=> [[]] # one permutation of length 0
|
||||
# a.permutation(4).to_a #=> [] # no permutations of length 4
|
||||
def permutation(n=self.size, &block)
|
||||
size = self.size
|
||||
return to_enum(:permutation, n) unless block
|
||||
return if n > size
|
||||
if n == 0
|
||||
yield []
|
||||
else
|
||||
i = 0
|
||||
while i<size
|
||||
result = [self[i]]
|
||||
if n-1 > 0
|
||||
ary = self[0...i] + self[i+1..-1]
|
||||
ary.permutation(n-1) do |c|
|
||||
yield result + c
|
||||
end
|
||||
else
|
||||
yield result
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# ary.combination(n) { |c| block } -> ary
|
||||
# ary.combination(n) -> Enumerator
|
||||
#
|
||||
# When invoked with a block, yields all combinations of length +n+ of elements
|
||||
# from the array and then returns the array itself.
|
||||
#
|
||||
# The implementation makes no guarantees about the order in which the
|
||||
# combinations are yielded.
|
||||
#
|
||||
# If no block is given, an Enumerator is returned instead.
|
||||
#
|
||||
# Examples:
|
||||
#
|
||||
# a = [1, 2, 3, 4]
|
||||
# a.combination(1).to_a #=> [[1],[2],[3],[4]]
|
||||
# a.combination(2).to_a #=> [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
|
||||
# a.combination(3).to_a #=> [[1,2,3],[1,2,4],[1,3,4],[2,3,4]]
|
||||
# a.combination(4).to_a #=> [[1,2,3,4]]
|
||||
# a.combination(0).to_a #=> [[]] # one combination of length 0
|
||||
# a.combination(5).to_a #=> [] # no combinations of length 5
|
||||
|
||||
def combination(n, &block)
|
||||
size = self.size
|
||||
return to_enum(:combination, n) unless block
|
||||
return if n > size
|
||||
if n == 0
|
||||
yield []
|
||||
elsif n == 1
|
||||
i = 0
|
||||
while i<size
|
||||
yield [self[i]]
|
||||
i += 1
|
||||
end
|
||||
else
|
||||
i = 0
|
||||
while i<size
|
||||
result = [self[i]]
|
||||
self[i+1..-1].combination(n-1) do |c|
|
||||
yield result + c
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -176,14 +176,16 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
struct RArray *a = mrb_ary_ptr(self);
|
||||
mrb_int i, j, k, len, alen = ARY_LEN(a);
|
||||
mrb_value index;
|
||||
mrb_value val;
|
||||
mrb_value *ptr;
|
||||
mrb_value ary;
|
||||
|
||||
mrb_ary_modify(mrb, a);
|
||||
|
||||
if (mrb_get_args(mrb, "o|i", &index, &len) == 1) {
|
||||
if (mrb_get_argc(mrb) == 1) {
|
||||
mrb_value index;
|
||||
|
||||
mrb_get_args(mrb, "o|i", &index, &len);
|
||||
switch (mrb_type(index)) {
|
||||
case MRB_TT_RANGE:
|
||||
if (mrb_range_beg_len(mrb, index, &i, &len, alen, TRUE) == 1) {
|
||||
@@ -201,7 +203,7 @@ mrb_ary_slice_bang(mrb_state *mrb, mrb_value self)
|
||||
}
|
||||
}
|
||||
|
||||
i = mrb_fixnum(index);
|
||||
mrb_get_args(mrb, "ii", &i, &len);
|
||||
delete_pos_len:
|
||||
if (i < 0) i += alen;
|
||||
if (i < 0 || alen < i) return mrb_nil_value();
|
||||
|
||||
@@ -381,3 +381,31 @@ assert("Array#slice!") do
|
||||
assert_equal(i, [1, 2, 3])
|
||||
assert_equal(j, nil)
|
||||
end
|
||||
|
||||
assert("Array#permutation") do
|
||||
a = [1, 2, 3]
|
||||
assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],
|
||||
a.permutation.to_a)
|
||||
assert_equal([[1],[2],[3]],
|
||||
a.permutation(1).to_a)
|
||||
assert_equal([[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]],
|
||||
a.permutation(2).to_a)
|
||||
assert_equal([[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]],
|
||||
a.permutation(3).to_a)
|
||||
assert_equal([[]], a.permutation(0).to_a)
|
||||
assert_equal([], a.permutation(4).to_a)
|
||||
end
|
||||
|
||||
assert("Array#combination") do
|
||||
a = [1, 2, 3, 4]
|
||||
assert_equal([[1],[2],[3],[4]],
|
||||
a.combination(1).to_a)
|
||||
assert_equal([[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]],
|
||||
a.combination(2).to_a)
|
||||
assert_equal([[1,2,3],[1,2,4],[1,3,4],[2,3,4]],
|
||||
a.combination(3).to_a)
|
||||
assert_equal([[1,2,3,4]],
|
||||
a.combination(4).to_a)
|
||||
assert_equal([[]], a.combination(0).to_a)
|
||||
assert_equal([], a.combination(5).to_a)
|
||||
end
|
||||
|
||||
@@ -544,6 +544,13 @@ done:
|
||||
if (args.verbose) {
|
||||
mrb_codedump_all(mrb, proc);
|
||||
}
|
||||
/* adjust stack length of toplevel environment */
|
||||
if (mrb->c->cibase->env) {
|
||||
struct REnv *e = mrb->c->cibase->env;
|
||||
if (e && MRB_ENV_STACK_LEN(e) < proc->body.irep->nlocals) {
|
||||
MRB_ENV_SET_STACK_LEN(e, proc->body.irep->nlocals);
|
||||
}
|
||||
}
|
||||
/* pass a proc for evaluation */
|
||||
/* evaluate the bytecode */
|
||||
result = mrb_vm_run(mrb,
|
||||
@@ -577,6 +584,8 @@ done:
|
||||
mrb_free(mrb, history_path);
|
||||
#endif
|
||||
|
||||
if (args.rfp) fclose(args.rfp);
|
||||
mrb_free(mrb, args.argv);
|
||||
mrbc_context_free(mrb, cxt);
|
||||
mrb_close(mrb);
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
MRuby::Gem::Specification.new('mruby-compar-ext') do |spec|
|
||||
spec.license = 'MIT'
|
||||
spec.author = 'mruby developers'
|
||||
spec.summary = 'Enumerable module extension'
|
||||
end
|
||||
@@ -0,0 +1,31 @@
|
||||
module Comparable
|
||||
##
|
||||
# Returns <i>min</i> if <i>obj</i> <code><=></code> <i>min</i> is less
|
||||
# than zero, <i>max</i> if <i>obj</i> <code><=></code> <i>max</i> is
|
||||
# greater than zero and <i>obj</i> otherwise.
|
||||
#
|
||||
# 12.clamp(0, 100) #=> 12
|
||||
# 523.clamp(0, 100) #=> 100
|
||||
# -3.123.clamp(0, 100) #=> 0
|
||||
#
|
||||
# 'd'.clamp('a', 'f') #=> 'd'
|
||||
# 'z'.clamp('a', 'f') #=> 'f'
|
||||
#
|
||||
def clamp(min, max)
|
||||
if (min <=> max) > 0
|
||||
raise ArgumentError, "min argument must be smaller than max argument"
|
||||
end
|
||||
c = self <=> min
|
||||
if c == 0
|
||||
return self
|
||||
elsif c < 0
|
||||
return min
|
||||
end
|
||||
c = self <=> max
|
||||
if c > 0
|
||||
return max
|
||||
else
|
||||
return self
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -278,6 +278,19 @@ genop_peep(codegen_scope *s, mrb_code i, int val)
|
||||
}
|
||||
}
|
||||
break;
|
||||
case OP_GETUPVAR:
|
||||
if (c0 == OP_SETUPVAR) {
|
||||
if (GETARG_B(i) == GETARG_B(i0) && GETARG_C(i) == GETARG_C(i0)) {
|
||||
if (GETARG_A(i) == GETARG_A(i0)) {
|
||||
/* just skip OP_SETUPVAR */
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return genop(s, MKOP_AB(OP_MOVE, GETARG_A(i), GETARG_A(i0)));
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case OP_EPOP:
|
||||
if (c0 == OP_EPOP) {
|
||||
s->iseq[s->pc-1] = MKOP_A(OP_EPOP, GETARG_A(i0)+GETARG_A(i));
|
||||
@@ -335,6 +348,14 @@ genop_peep(codegen_scope *s, mrb_code i, int val)
|
||||
s->iseq[s->pc-1] = MKOP_ABC(OP_SUBI, GETARG_A(i), GETARG_B(i), -c);
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case OP_ARYCAT:
|
||||
case OP_ARYPUSH:
|
||||
if (c0 == OP_MOVE && GETARG_A(i0) >= s->nlocals) {
|
||||
s->iseq[s->pc-1] = MKOP_AB(c1, GETARG_A(i), GETARG_B(i0));
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
case OP_STRCAT:
|
||||
if (c0 == OP_STRING) {
|
||||
mrb_value v = s->irep->pool[GETARG_Bx(i0)];
|
||||
@@ -826,10 +847,10 @@ gen_values(codegen_scope *s, node *t, int val, int extra)
|
||||
codegen(s, t->car, VAL);
|
||||
pop(); pop();
|
||||
if (is_splat) {
|
||||
genop(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));
|
||||
genop_peep(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1), NOVAL);
|
||||
}
|
||||
else {
|
||||
genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));
|
||||
genop_peep(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1), NOVAL);
|
||||
}
|
||||
}
|
||||
t = t->cdr;
|
||||
@@ -838,10 +859,10 @@ gen_values(codegen_scope *s, node *t, int val, int extra)
|
||||
codegen(s, t->car, VAL);
|
||||
pop(); pop();
|
||||
if (nint(t->car->car) == NODE_SPLAT) {
|
||||
genop(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1));
|
||||
genop_peep(s, MKOP_AB(OP_ARYCAT, cursp(), cursp()+1), NOVAL);
|
||||
}
|
||||
else {
|
||||
genop(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1));
|
||||
genop_peep(s, MKOP_AB(OP_ARYPUSH, cursp(), cursp()+1), NOVAL);
|
||||
}
|
||||
t = t->cdr;
|
||||
}
|
||||
@@ -2146,7 +2167,7 @@ codegen(codegen_scope *s, node *tree, int val)
|
||||
while (up) {
|
||||
idx = lv_idx(up, nsym(tree));
|
||||
if (idx > 0) {
|
||||
genop(s, MKOP_ABC(OP_GETUPVAR, cursp(), idx, lv));
|
||||
genop_peep(s, MKOP_ABC(OP_GETUPVAR, cursp(), idx, lv), VAL);
|
||||
break;
|
||||
}
|
||||
lv++;
|
||||
@@ -3031,6 +3052,9 @@ mrb_generate_code(mrb_state *mrb, parser_state *p)
|
||||
mrb_irep_decref(mrb, scope->irep);
|
||||
mrb_pool_close(scope->mpool);
|
||||
proc->c = NULL;
|
||||
if (mrb->c->cibase && mrb->c->cibase->proc == proc->upper) {
|
||||
proc->upper = NULL;
|
||||
}
|
||||
mrb->jmp = prev_jmp;
|
||||
return proc;
|
||||
}
|
||||
|
||||
@@ -2579,7 +2579,7 @@ do_block : keyword_do_block
|
||||
local_nest(p);
|
||||
}
|
||||
opt_block_param
|
||||
compstmt
|
||||
bodystmt
|
||||
keyword_end
|
||||
{
|
||||
$$ = new_block(p,$3,$4);
|
||||
@@ -2669,7 +2669,7 @@ brace_block : '{'
|
||||
$<num>$ = p->lineno;
|
||||
}
|
||||
opt_block_param
|
||||
compstmt keyword_end
|
||||
bodystmt keyword_end
|
||||
{
|
||||
$$ = new_block(p,$3,$4);
|
||||
SET_LINENO($$, $<num>2);
|
||||
@@ -5731,7 +5731,7 @@ mrb_parser_set_filename(struct mrb_parser_state *p, const char *f)
|
||||
|
||||
new_table = (mrb_sym*)parser_palloc(p, sizeof(mrb_sym) * p->filename_table_length);
|
||||
if (p->filename_table) {
|
||||
memmove(new_table, p->filename_table, sizeof(mrb_sym) * p->filename_table_length);
|
||||
memmove(new_table, p->filename_table, sizeof(mrb_sym) * p->current_filename_index);
|
||||
}
|
||||
p->filename_table = new_table;
|
||||
p->filename_table[p->filename_table_length - 1] = sym;
|
||||
@@ -5833,7 +5833,7 @@ mrb_load_exec(mrb_state *mrb, struct mrb_parser_state *p, mrbc_context *c)
|
||||
c->keep_lv = TRUE;
|
||||
}
|
||||
}
|
||||
proc->target_class = target;
|
||||
MRB_PROC_SET_TARGET_CLASS(proc, target);
|
||||
if (mrb->c->ci) {
|
||||
mrb->c->ci->target_class = target;
|
||||
}
|
||||
|
||||
@@ -708,4 +708,20 @@ module Enumerable
|
||||
def nil.to_h
|
||||
{}
|
||||
end
|
||||
|
||||
def uniq(&block)
|
||||
hash = {}
|
||||
if block
|
||||
self.each do|*v|
|
||||
v = v.__svalue
|
||||
hash[block.call(v)] ||= v
|
||||
end
|
||||
else
|
||||
self.each do|*v|
|
||||
v = v.__svalue
|
||||
hash[v] ||= v
|
||||
end
|
||||
end
|
||||
hash.values
|
||||
end
|
||||
end
|
||||
|
||||
@@ -158,6 +158,21 @@ class Enumerator
|
||||
}
|
||||
end
|
||||
|
||||
def uniq(&block)
|
||||
hash = {}
|
||||
Lazy.new(self){|yielder, val|
|
||||
if block
|
||||
v = block.call(val)
|
||||
else
|
||||
v = val
|
||||
end
|
||||
unless hash.include?(v)
|
||||
yielder << val
|
||||
hash[v] = val
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
alias force to_a
|
||||
end
|
||||
end
|
||||
|
||||
@@ -33,7 +33,7 @@ assert 'Enumerator.new' do
|
||||
a, b = b, a + b
|
||||
end
|
||||
end
|
||||
assert_equal fib.take(10), [1,1,2,3,5,8,13,21,34,55]
|
||||
assert_equal [1,1,2,3,5,8,13,21,34,55], fib.take(10)
|
||||
end
|
||||
|
||||
assert 'Enumerator#initialize_copy' do
|
||||
|
||||
@@ -12,29 +12,14 @@ mrb_value mrb_obj_instance_eval(mrb_state *mrb, mrb_value self);
|
||||
static struct mrb_irep *
|
||||
get_closure_irep(mrb_state *mrb, int level)
|
||||
{
|
||||
struct mrb_context *c = mrb->c;
|
||||
struct REnv *e = c->ci[-1].proc->env;
|
||||
struct RProc *proc;
|
||||
struct RProc *proc = mrb->c->ci[-1].proc;
|
||||
|
||||
if (level == 0) {
|
||||
proc = c->ci[-1].proc;
|
||||
if (MRB_PROC_CFUNC_P(proc)) {
|
||||
return NULL;
|
||||
}
|
||||
return proc->body.irep;
|
||||
while (level--) {
|
||||
if (!proc) return NULL;
|
||||
proc = proc->upper;
|
||||
}
|
||||
|
||||
while (--level) {
|
||||
e = (struct REnv*)e->c;
|
||||
if (!e) return NULL;
|
||||
}
|
||||
|
||||
if (!e) return NULL;
|
||||
if (!MRB_ENV_STACK_SHARED_P(e)) return NULL;
|
||||
c = e->cxt.c;
|
||||
proc = c->cibase[e->cioff].proc;
|
||||
|
||||
if (!proc || MRB_PROC_CFUNC_P(proc)) {
|
||||
if (!proc) return NULL;
|
||||
if (MRB_PROC_CFUNC_P(proc)) {
|
||||
return NULL;
|
||||
}
|
||||
return proc->body.irep;
|
||||
@@ -67,7 +52,7 @@ search_variable(mrb_state *mrb, mrb_sym vsym, int bnest)
|
||||
int pos;
|
||||
|
||||
for (level = 0; (virep = get_closure_irep(mrb, level)); level++) {
|
||||
if (!virep || virep->lv == NULL) {
|
||||
if (virep->lv == NULL) {
|
||||
continue;
|
||||
}
|
||||
for (pos = 0; pos < virep->nlocals - 1; pos++) {
|
||||
@@ -130,7 +115,7 @@ patch_irep(mrb_state *mrb, mrb_irep *irep, int bnest, mrb_irep *top)
|
||||
if (GETARG_C(c) != 0) {
|
||||
break;
|
||||
}
|
||||
{
|
||||
else {
|
||||
mrb_code arg = search_variable(mrb, irep->syms[GETARG_B(c)], bnest);
|
||||
if (arg != 0) {
|
||||
/* must replace */
|
||||
@@ -204,7 +189,9 @@ create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding,
|
||||
struct mrb_parser_state *p;
|
||||
struct RProc *proc;
|
||||
struct REnv *e;
|
||||
struct mrb_context *c = mrb->c;
|
||||
mrb_callinfo *ci = &mrb->c->ci[-1]; /* callinfo of eval caller */
|
||||
struct RClass *target_class = NULL;
|
||||
int bidx;
|
||||
|
||||
if (!mrb_nil_p(binding)) {
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "Binding of eval must be nil.");
|
||||
@@ -251,19 +238,29 @@ create_proc_from_string(mrb_state *mrb, char *s, mrb_int len, mrb_value binding,
|
||||
mrbc_context_free(mrb, cxt);
|
||||
mrb_raise(mrb, E_SCRIPT_ERROR, "codegen error");
|
||||
}
|
||||
if (c->ci[-1].proc->target_class) {
|
||||
proc->target_class = c->ci[-1].proc->target_class;
|
||||
target_class = MRB_PROC_TARGET_CLASS(ci->proc);
|
||||
if (!MRB_PROC_CFUNC_P(ci->proc)) {
|
||||
if (ci->env) {
|
||||
e = ci->env;
|
||||
}
|
||||
else {
|
||||
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV,
|
||||
(struct RClass*)target_class);
|
||||
e->mid = ci->mid;
|
||||
e->stack = ci[1].stackent;
|
||||
e->cxt = mrb->c;
|
||||
MRB_ENV_SET_STACK_LEN(e, ci->proc->body.irep->nlocals);
|
||||
bidx = ci->argc;
|
||||
if (ci->argc < 0) bidx = 2;
|
||||
else bidx += 1;
|
||||
MRB_ENV_SET_BIDX(e, bidx);
|
||||
}
|
||||
proc->e.env = e;
|
||||
proc->flags |= MRB_PROC_ENVSET;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)proc, (struct RBasic*)e);
|
||||
}
|
||||
e = c->ci[-1].proc->env;
|
||||
if (!e) e = c->ci[-1].env;
|
||||
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)e);
|
||||
e->cxt.c = c;
|
||||
e->cioff = c->ci - c->cibase;
|
||||
e->stack = c->ci->stackent;
|
||||
MRB_SET_ENV_STACK_LEN(e, c->ci->proc->body.irep->nlocals);
|
||||
c->ci->target_class = proc->target_class;
|
||||
c->ci->env = 0;
|
||||
proc->env = e;
|
||||
proc->upper = ci->proc;
|
||||
mrb->c->ci->target_class = target_class;
|
||||
patch_irep(mrb, proc->body.irep, 0, proc->body.irep);
|
||||
/* mrb_codedump_all(mrb, proc); */
|
||||
|
||||
@@ -322,8 +319,7 @@ f_instance_eval(mrb_state *mrb, mrb_value self)
|
||||
mrb_get_args(mrb, "s|zi", &s, &len, &file, &line);
|
||||
cv = mrb_singleton_class(mrb, self);
|
||||
proc = create_proc_from_string(mrb, s, len, mrb_nil_value(), file, line);
|
||||
proc->target_class = mrb_class_ptr(cv);
|
||||
mrb->c->ci->env = NULL;
|
||||
MRB_PROC_SET_TARGET_CLASS(proc, mrb_class_ptr(cv));
|
||||
mrb_assert(!MRB_PROC_CFUNC_P(proc));
|
||||
return exec_irep(mrb, self, proc);
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ fiber_init(mrb_state *mrb, mrb_value self)
|
||||
|
||||
/* adjust return callinfo */
|
||||
ci = c->ci;
|
||||
ci->target_class = p->target_class;
|
||||
ci->target_class = MRB_PROC_TARGET_CLASS(p);
|
||||
ci->proc = p;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)mrb_obj_ptr(self), (struct RBasic*)p);
|
||||
ci->pc = p->body.irep->iseq;
|
||||
@@ -213,7 +213,7 @@ fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mr
|
||||
*b++ = *a++;
|
||||
}
|
||||
c->cibase->argc = (int)len;
|
||||
value = c->stack[0] = c->ci->proc->env->stack[0];
|
||||
value = c->stack[0] = MRB_PROC_ENV(c->ci->proc)->stack[0];
|
||||
}
|
||||
else {
|
||||
value = fiber_result(mrb, a, len);
|
||||
|
||||
@@ -474,4 +474,29 @@ class Hash
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
def to_proc
|
||||
->x{self[x]}
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# hsh.fetch_values(key, ...) -> array
|
||||
# hsh.fetch_values(key, ...) { |key| block } -> array
|
||||
#
|
||||
# Returns an array containing the values associated with the given keys
|
||||
# but also raises <code>KeyError</code> when one of keys can't be found.
|
||||
# Also see <code>Hash#values_at</code> and <code>Hash#fetch</code>.
|
||||
#
|
||||
# h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }
|
||||
#
|
||||
# h.fetch_values("cow", "cat") #=> ["bovine", "feline"]
|
||||
# h.fetch_values("cow", "bird") # raises KeyError
|
||||
# h.fetch_values("cow", "bird") { |k| k.upcase } #=> ["bovine", "BIRD"]
|
||||
#
|
||||
def fetch_values(*keys, &block)
|
||||
keys.map do |k|
|
||||
self.fetch(k, &block)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
module Kernel
|
||||
# call-seq:
|
||||
# obj.yield_self {|_obj|...} -> an_object
|
||||
#
|
||||
# Yields <i>obj</i> and returns the result.
|
||||
#
|
||||
# 'my string'.yield_self {|s|s.upcase} #=> "MY STRING"
|
||||
#
|
||||
def yield_self(&block)
|
||||
return to_enum :yield_self unless block
|
||||
block.call(self)
|
||||
end
|
||||
end
|
||||
@@ -224,6 +224,22 @@ mrb_f_hash(mrb_state *mrb, mrb_value self)
|
||||
return tmp;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* obj.itself -> an_object
|
||||
*
|
||||
* Returns <i>obj</i>.
|
||||
*
|
||||
* string = 'my string' #=> "my string"
|
||||
* string.itself.object_id == string.object_id #=> true
|
||||
*
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_f_itself(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
return self;
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
|
||||
{
|
||||
@@ -239,6 +255,7 @@ mrb_mruby_kernel_ext_gem_init(mrb_state *mrb)
|
||||
mrb_define_module_function(mrb, krn, "String", mrb_f_string, MRB_ARGS_REQ(1));
|
||||
mrb_define_module_function(mrb, krn, "Array", mrb_f_array, MRB_ARGS_REQ(1));
|
||||
mrb_define_module_function(mrb, krn, "Hash", mrb_f_hash, MRB_ARGS_REQ(1));
|
||||
mrb_define_module_function(mrb, krn, "itself", mrb_f_itself, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -14,4 +14,12 @@ module Integral
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
def positive?
|
||||
self > 0
|
||||
end
|
||||
|
||||
def negative?
|
||||
self < 0
|
||||
end
|
||||
end
|
||||
|
||||
@@ -144,7 +144,20 @@ class String
|
||||
def casecmp(str)
|
||||
self.downcase <=> str.to_str.downcase
|
||||
rescue NoMethodError
|
||||
raise TypeError, "no implicit conversion of #{str.class} into String"
|
||||
nil
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# str.casecmp?(other) -> true, false, or nil
|
||||
#
|
||||
# Returns true if str and other_str are equal after case folding,
|
||||
# false if they are not equal, and nil if other_str is not a string.
|
||||
|
||||
def casecmp?(str)
|
||||
c = self.casecmp(str)
|
||||
return nil if c.nil?
|
||||
return c == 0
|
||||
end
|
||||
|
||||
def partition(sep)
|
||||
|
||||
@@ -44,12 +44,13 @@ mrb_str_byteslice(mrb_state *mrb, mrb_value str)
|
||||
{
|
||||
mrb_value a1;
|
||||
mrb_int len;
|
||||
mrb_int argc;
|
||||
|
||||
argc = mrb_get_args(mrb, "o|i", &a1, &len);
|
||||
if (argc == 2) {
|
||||
if (mrb_get_argc(mrb) == 2) {
|
||||
mrb_int pos;
|
||||
mrb_get_args(mrb, "ii", &pos, &len);
|
||||
return mrb_str_substr(mrb, str, mrb_fixnum(a1), len);
|
||||
}
|
||||
mrb_get_args(mrb, "o|i", &a1, &len);
|
||||
switch (mrb_type(a1)) {
|
||||
case MRB_TT_RANGE:
|
||||
{
|
||||
@@ -652,6 +653,118 @@ mrb_str_upto(mrb_state *mrb, mrb_value beg)
|
||||
return beg;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* str.delete_prefix!(prefix) -> self or nil
|
||||
*
|
||||
* Deletes leading <code>prefix</code> from <i>str</i>, returning
|
||||
* <code>nil</code> if no change was made.
|
||||
*
|
||||
* "hello".delete_prefix!("hel") #=> "lo"
|
||||
* "hello".delete_prefix!("llo") #=> nil
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_str_del_prefix_bang(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_int plen, slen;
|
||||
char *ptr, *s;
|
||||
struct RString *str = RSTRING(self);
|
||||
|
||||
mrb_get_args(mrb, "s", &ptr, &plen);
|
||||
slen = RSTR_LEN(str);
|
||||
if (plen > slen) return mrb_nil_value();
|
||||
s = RSTR_PTR(str);
|
||||
if (memcmp(s, ptr, plen) != 0) return mrb_nil_value();
|
||||
if (!MRB_FROZEN_P(str) && (RSTR_SHARED_P(str) || RSTR_FSHARED_P(str))) {
|
||||
str->as.heap.ptr += plen;
|
||||
}
|
||||
else {
|
||||
mrb_str_modify(mrb, str);
|
||||
s = RSTR_PTR(str);
|
||||
memmove(s, s+plen, slen-plen);
|
||||
}
|
||||
RSTR_SET_LEN(str, slen-plen);
|
||||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* str.delete_prefix(prefix) -> new_str
|
||||
*
|
||||
* Returns a copy of <i>str</i> with leading <code>prefix</code> deleted.
|
||||
*
|
||||
* "hello".delete_prefix("hel") #=> "lo"
|
||||
* "hello".delete_prefix("llo") #=> "hello"
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_str_del_prefix(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_int plen, slen;
|
||||
char *ptr;
|
||||
|
||||
mrb_get_args(mrb, "s", &ptr, &plen);
|
||||
slen = RSTRING_LEN(self);
|
||||
if (plen > slen) return mrb_str_dup(mrb, self);
|
||||
if (memcmp(RSTRING_PTR(self), ptr, plen) != 0)
|
||||
return mrb_str_dup(mrb, self);
|
||||
return mrb_str_substr(mrb, self, plen, slen-plen);
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* str.delete_suffix!(suffix) -> self or nil
|
||||
*
|
||||
* Deletes trailing <code>suffix</code> from <i>str</i>, returning
|
||||
* <code>nil</code> if no change was made.
|
||||
*
|
||||
* "hello".delete_suffix!("llo") #=> "he"
|
||||
* "hello".delete_suffix!("hel") #=> nil
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_str_del_suffix_bang(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_int plen, slen;
|
||||
char *ptr, *s;
|
||||
struct RString *str = RSTRING(self);
|
||||
|
||||
mrb_get_args(mrb, "s", &ptr, &plen);
|
||||
slen = RSTR_LEN(str);
|
||||
if (plen > slen) return mrb_nil_value();
|
||||
s = RSTR_PTR(str);
|
||||
if (memcmp(s+slen-plen, ptr, plen) != 0) return mrb_nil_value();
|
||||
if (!MRB_FROZEN_P(str) && (RSTR_SHARED_P(str) || RSTR_FSHARED_P(str))) {
|
||||
/* no need to modify string */
|
||||
}
|
||||
else {
|
||||
mrb_str_modify(mrb, str);
|
||||
}
|
||||
RSTR_SET_LEN(str, slen-plen);
|
||||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
* call-seq:
|
||||
* str.delete_suffix(suffix) -> new_str
|
||||
*
|
||||
* Returns a copy of <i>str</i> with leading <code>suffix</code> deleted.
|
||||
*
|
||||
* "hello".delete_suffix("hel") #=> "lo"
|
||||
* "hello".delete_suffix("llo") #=> "hello"
|
||||
*/
|
||||
static mrb_value
|
||||
mrb_str_del_suffix(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_int plen, slen;
|
||||
char *ptr;
|
||||
|
||||
mrb_get_args(mrb, "s", &ptr, &plen);
|
||||
slen = RSTRING_LEN(self);
|
||||
if (plen > slen) return mrb_str_dup(mrb, self);
|
||||
if (memcmp(RSTRING_PTR(self)+slen-plen, ptr, plen) != 0)
|
||||
return mrb_str_dup(mrb, self);
|
||||
return mrb_str_substr(mrb, self, 0, slen-plen);
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_string_ext_gem_init(mrb_state* mrb)
|
||||
{
|
||||
@@ -675,8 +788,12 @@ mrb_mruby_string_ext_gem_init(mrb_state* mrb)
|
||||
mrb_define_method(mrb, s, "succ!", mrb_str_succ_bang, MRB_ARGS_NONE());
|
||||
mrb_alias_method(mrb, s, mrb_intern_lit(mrb, "next"), mrb_intern_lit(mrb, "succ"));
|
||||
mrb_alias_method(mrb, s, mrb_intern_lit(mrb, "next!"), mrb_intern_lit(mrb, "succ!"));
|
||||
mrb_define_method(mrb, s, "ord", mrb_str_ord, MRB_ARGS_NONE());
|
||||
mrb_define_method(mrb, s, "upto", mrb_str_upto, MRB_ARGS_ANY());
|
||||
mrb_define_method(mrb, s, "ord", mrb_str_ord, MRB_ARGS_NONE());
|
||||
mrb_define_method(mrb, s, "upto", mrb_str_upto, MRB_ARGS_ANY());
|
||||
mrb_define_method(mrb, s, "delete_prefix!", mrb_str_del_prefix_bang, MRB_ARGS_REQ(1));
|
||||
mrb_define_method(mrb, s, "delete_prefix", mrb_str_del_prefix, MRB_ARGS_REQ(1));
|
||||
mrb_define_method(mrb, s, "delete_suffix!", mrb_str_del_suffix_bang, MRB_ARGS_REQ(1));
|
||||
mrb_define_method(mrb, s, "delete_suffix", mrb_str_del_suffix, MRB_ARGS_REQ(1));
|
||||
|
||||
mrb_define_method(mrb, mrb->fixnum_class, "chr", mrb_fixnum_chr, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
# coding: utf-8
|
||||
##
|
||||
# String(Ext) Test
|
||||
|
||||
@@ -665,3 +666,17 @@ assert('String#each_codepoint(UTF-8)') do
|
||||
end
|
||||
assert_equal expect, cp
|
||||
end if UTF8STRING
|
||||
|
||||
assert('String#delete_prefix') do
|
||||
assert_equal "llo", "hello".delete_prefix("he")
|
||||
assert_equal "hello", "hello".delete_prefix("llo")
|
||||
assert_equal "llo", "hello".delete_prefix!("he")
|
||||
assert_nil "hello".delete_prefix!("llo")
|
||||
end
|
||||
|
||||
assert('String#delete_suffix') do
|
||||
assert_equal "he", "hello".delete_suffix("llo")
|
||||
assert_equal "hello", "hello".delete_suffix("he")
|
||||
assert_equal "he", "hello".delete_suffix!("llo")
|
||||
assert_nil "hello".delete_suffix!("he")
|
||||
end
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <mruby/variable.h>
|
||||
#include <mruby/hash.h>
|
||||
#include <mruby/range.h>
|
||||
#include <mruby/proc.h>
|
||||
|
||||
#define RSTRUCT_LEN(st) RARRAY_LEN(st)
|
||||
#define RSTRUCT_PTR(st) RARRAY_PTR(st)
|
||||
@@ -113,12 +114,14 @@ mrb_struct_members(mrb_state *mrb, mrb_value obj)
|
||||
return mrb_struct_s_members_m(mrb, mrb_obj_value(mrb_obj_class(mrb, obj)));
|
||||
}
|
||||
|
||||
static mrb_value struct_aref_sym(mrb_state *mrb, mrb_value obj, mrb_sym id);
|
||||
|
||||
static mrb_value
|
||||
mrb_struct_ref(mrb_state *mrb, mrb_value obj)
|
||||
{
|
||||
return struct_aref_sym(mrb, obj, mrb->c->ci->mid);
|
||||
mrb_int i = mrb_fixnum(mrb_proc_cfunc_env_get(mrb, 0));
|
||||
mrb_value *ptr = RSTRUCT_PTR(obj);
|
||||
|
||||
if (!ptr) return mrb_nil_value();
|
||||
return ptr[i];
|
||||
}
|
||||
|
||||
static mrb_sym
|
||||
@@ -140,24 +143,23 @@ mrb_id_attrset(mrb_state *mrb, mrb_sym id)
|
||||
return mid;
|
||||
}
|
||||
|
||||
static mrb_value mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val);
|
||||
|
||||
static mrb_value
|
||||
mrb_struct_set_m(mrb_state *mrb, mrb_value obj)
|
||||
{
|
||||
mrb_int i = mrb_fixnum(mrb_proc_cfunc_env_get(mrb, 0));
|
||||
mrb_value *ptr;
|
||||
mrb_value val;
|
||||
|
||||
const char *name;
|
||||
mrb_int slen;
|
||||
mrb_sym mid;
|
||||
|
||||
mrb_get_args(mrb, "o", &val);
|
||||
|
||||
/* get base id */
|
||||
name = mrb_sym2name_len(mrb, mrb->c->ci->mid, &slen);
|
||||
mid = mrb_intern(mrb, name, slen-1); /* omit last "=" */
|
||||
|
||||
return mrb_struct_aset_sym(mrb, obj, mid, val);
|
||||
mrb_struct_modify(mrb, obj);
|
||||
ptr = RSTRUCT_PTR(obj);
|
||||
if (ptr == NULL || i >= RSTRUCT_LEN(obj)) {
|
||||
mrb_ary_set(mrb, obj, i, val);
|
||||
}
|
||||
else {
|
||||
ptr[i] = val;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
static mrb_bool
|
||||
@@ -187,15 +189,18 @@ make_struct_define_accessors(mrb_state *mrb, mrb_value members, struct RClass *c
|
||||
const char *name = mrb_sym2name_len(mrb, id, NULL);
|
||||
|
||||
if (is_local_id(mrb, name) || is_const_id(mrb, name)) {
|
||||
mrb_define_method_id(mrb, c, id, mrb_struct_ref, MRB_ARGS_NONE());
|
||||
mrb_define_method_id(mrb, c, mrb_id_attrset(mrb, id), mrb_struct_set_m, MRB_ARGS_REQ(1));
|
||||
mrb_value at = mrb_fixnum_value(i);
|
||||
struct RProc *aref = mrb_proc_new_cfunc_with_env(mrb, mrb_struct_ref, 1, &at);
|
||||
struct RProc *aset = mrb_proc_new_cfunc_with_env(mrb, mrb_struct_set_m, 1, &at);
|
||||
mrb_define_method_raw(mrb, c, id, aref);
|
||||
mrb_define_method_raw(mrb, c, mrb_id_attrset(mrb, id), aset);
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass * klass)
|
||||
make_struct(mrb_state *mrb, mrb_value name, mrb_value members, struct RClass *klass)
|
||||
{
|
||||
mrb_value nstr;
|
||||
mrb_sym id;
|
||||
@@ -278,17 +283,21 @@ mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
|
||||
name = mrb_nil_value();
|
||||
mrb_get_args(mrb, "*&", &argv, &argc, &b);
|
||||
if (argc == 0) { /* special case to avoid crash */
|
||||
rest = mrb_ary_new(mrb);
|
||||
mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments");
|
||||
}
|
||||
else {
|
||||
if (argc > 0) name = argv[0];
|
||||
pargv = &argv[1];
|
||||
argcnt = argc-1;
|
||||
if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
|
||||
/* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
|
||||
name = mrb_nil_value();
|
||||
pargv = &argv[0];
|
||||
argcnt++;
|
||||
pargv = argv;
|
||||
argcnt = argc;
|
||||
if (argc > 0) {
|
||||
name = argv[0];
|
||||
if (mrb_symbol_p(name)) {
|
||||
/* 1stArgument:symbol -> name=nil rest=argv[0..n] */
|
||||
name = mrb_nil_value();
|
||||
}
|
||||
else {
|
||||
pargv++;
|
||||
argcnt--;
|
||||
}
|
||||
}
|
||||
rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
|
||||
for (i=0; i<RARRAY_LEN(rest); i++) {
|
||||
|
||||
@@ -11,13 +11,6 @@ assert('Struct.new', '15.2.18.3.1') do
|
||||
assert_equal [:m1, :m2], c.members
|
||||
end
|
||||
|
||||
# Check crash bug with Struc.new and no params.
|
||||
assert('Struct.new', '15.2.18.3.1') do
|
||||
c = Struct.new()
|
||||
assert_equal Struct, c.superclass
|
||||
assert_equal [], c.members
|
||||
end
|
||||
|
||||
assert('Struct#==', '15.2.18.4.1') do
|
||||
c = Struct.new(:m1, :m2)
|
||||
cc1 = c.new(1,2)
|
||||
@@ -192,7 +185,7 @@ assert("Struct.new generates subclass of Struct") do
|
||||
begin
|
||||
original_struct = Struct
|
||||
Struct = String
|
||||
assert_equal original_struct, original_struct.new.superclass
|
||||
assert_equal original_struct, original_struct.new(:foo).superclass
|
||||
ensure
|
||||
Struct = original_struct
|
||||
end
|
||||
|
||||
@@ -48,10 +48,23 @@ class Symbol
|
||||
def casecmp(other)
|
||||
return nil unless other.kind_of?(Symbol)
|
||||
lhs = self.to_s; lhs.upcase!
|
||||
rhs = other.to_s; rhs.upcase!
|
||||
rhs = other.to_s.upcase
|
||||
lhs <=> rhs
|
||||
end
|
||||
|
||||
##
|
||||
# call-seq:
|
||||
# sym.casecmp?(other) -> true, false, or nil
|
||||
#
|
||||
# Returns true if sym and other_sym are equal after case folding,
|
||||
# false if they are not equal, and nil if other_sym is not a string.
|
||||
|
||||
def casecmp?(sym)
|
||||
c = self.casecmp(sym)
|
||||
return nil if c.nil?
|
||||
return c == 0
|
||||
end
|
||||
|
||||
#
|
||||
# call-seq:
|
||||
# sym.empty? -> true or false
|
||||
|
||||
@@ -66,6 +66,7 @@ t_printstr(mrb_state *mrb, mrb_value obj)
|
||||
s = RSTRING_PTR(obj);
|
||||
len = RSTRING_LEN(obj);
|
||||
fwrite(s, len, 1, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
-3
@@ -5,9 +5,10 @@ class Module
|
||||
attr_writer(*names)
|
||||
end
|
||||
# 15.2.2.4.11
|
||||
def attr(name)
|
||||
attr_reader(name)
|
||||
end
|
||||
alias attr attr_reader
|
||||
#def attr(name)
|
||||
# attr_reader(name)
|
||||
#end
|
||||
|
||||
# 15.2.2.4.27
|
||||
def include(*args)
|
||||
|
||||
+6
-3
@@ -51,7 +51,7 @@ each_backtrace(mrb_state *mrb, ptrdiff_t ciidx, mrb_code *pc0, each_backtrace_fu
|
||||
pc = mrb->c->cibase[i].err;
|
||||
}
|
||||
else if (i+1 <= ciidx) {
|
||||
pc = mrb->c->cibase[i+1].pc - 1;
|
||||
pc = &mrb->c->cibase[i+1].pc[-1];
|
||||
}
|
||||
else {
|
||||
pc = pc0;
|
||||
@@ -215,11 +215,14 @@ packed_backtrace(mrb_state *mrb)
|
||||
void
|
||||
mrb_keep_backtrace(mrb_state *mrb, mrb_value exc)
|
||||
{
|
||||
mrb_sym sym = mrb_intern_lit(mrb, "backtrace");
|
||||
mrb_value backtrace;
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
int ai;
|
||||
|
||||
if (mrb_iv_defined(mrb, exc, sym)) return;
|
||||
ai = mrb_gc_arena_save(mrb);
|
||||
backtrace = packed_backtrace(mrb);
|
||||
mrb_iv_set(mrb, exc, mrb_intern_lit(mrb, "backtrace"), backtrace);
|
||||
mrb_iv_set(mrb, exc, sym, backtrace);
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
}
|
||||
|
||||
|
||||
+53
-13
@@ -436,8 +436,11 @@ mrb_define_method_raw(mrb_state *mrb, struct RClass *c, mrb_sym mid, struct RPro
|
||||
k = kh_put(mt, mrb, h, mid);
|
||||
kh_value(h, k) = p;
|
||||
if (p) {
|
||||
p->flags |= MRB_PROC_SCOPE;
|
||||
p->c = NULL;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic *)c, (struct RBasic *)p);
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)c, (struct RBasic*)p);
|
||||
MRB_PROC_SET_TARGET_CLASS(p, c);
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)c);
|
||||
}
|
||||
mc_clear_by_id(mrb, c, mid);
|
||||
}
|
||||
@@ -449,7 +452,7 @@ mrb_define_method_id(mrb_state *mrb, struct RClass *c, mrb_sym mid, mrb_func_t f
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
|
||||
p = mrb_proc_new_cfunc(mrb, func);
|
||||
p->target_class = c;
|
||||
MRB_PROC_SET_TARGET_CLASS(p, c);
|
||||
mrb_define_method_raw(mrb, c, mid, p);
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
}
|
||||
@@ -532,6 +535,35 @@ to_sym(mrb_state *mrb, mrb_value ss)
|
||||
}
|
||||
}
|
||||
|
||||
MRB_API mrb_int
|
||||
mrb_get_argc(mrb_state *mrb)
|
||||
{
|
||||
mrb_int argc = mrb->c->ci->argc;
|
||||
|
||||
if (argc < 0) {
|
||||
struct RArray *a = mrb_ary_ptr(mrb->c->stack[1]);
|
||||
|
||||
argc = ARY_LEN(a);
|
||||
}
|
||||
return argc;
|
||||
}
|
||||
|
||||
MRB_API mrb_value*
|
||||
mrb_get_argv(mrb_state *mrb)
|
||||
{
|
||||
mrb_int argc = mrb->c->ci->argc;
|
||||
mrb_value *array_argv;
|
||||
if (argc < 0) {
|
||||
struct RArray *a = mrb_ary_ptr(mrb->c->stack[1]);
|
||||
|
||||
array_argv = ARY_PTR(a);
|
||||
}
|
||||
else {
|
||||
array_argv = NULL;
|
||||
}
|
||||
return array_argv;
|
||||
}
|
||||
|
||||
/*
|
||||
retrieve arguments from mrb_state.
|
||||
|
||||
@@ -569,23 +601,14 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
|
||||
char c;
|
||||
mrb_int i = 0;
|
||||
va_list ap;
|
||||
mrb_int argc = mrb->c->ci->argc;
|
||||
mrb_int argc = mrb_get_argc(mrb);
|
||||
mrb_int arg_i = 0;
|
||||
mrb_value *array_argv;
|
||||
mrb_value *array_argv = mrb_get_argv(mrb);
|
||||
mrb_bool opt = FALSE;
|
||||
mrb_bool opt_skip = TRUE;
|
||||
mrb_bool given = TRUE;
|
||||
|
||||
va_start(ap, format);
|
||||
if (argc < 0) {
|
||||
struct RArray *a = mrb_ary_ptr(mrb->c->stack[1]);
|
||||
|
||||
argc = ARY_LEN(a);
|
||||
array_argv = ARY_PTR(a);
|
||||
}
|
||||
else {
|
||||
array_argv = NULL;
|
||||
}
|
||||
|
||||
#define ARGV \
|
||||
(array_argv ? array_argv : (mrb->c->stack + 1))
|
||||
@@ -1979,6 +2002,12 @@ mod_define_method(mrb_state *mrb, mrb_value self)
|
||||
return mrb_symbol_value(mid);
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
top_define_method(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
return mod_define_method(mrb, mrb_obj_value(mrb->object_class));
|
||||
}
|
||||
|
||||
static void
|
||||
check_cv_name_str(mrb_state *mrb, mrb_value str)
|
||||
{
|
||||
@@ -2388,6 +2417,12 @@ mrb_value mrb_obj_instance_eval(mrb_state*, mrb_value);
|
||||
/* implementation of Module.nesting */
|
||||
mrb_value mrb_mod_s_nesting(mrb_state*, mrb_value);
|
||||
|
||||
static mrb_value
|
||||
inspect_main(mrb_state *mrb, mrb_value mod)
|
||||
{
|
||||
return mrb_str_new_lit(mrb, "main");
|
||||
}
|
||||
|
||||
void
|
||||
mrb_init_class(mrb_state *mrb)
|
||||
{
|
||||
@@ -2484,4 +2519,9 @@ mrb_init_class(mrb_state *mrb)
|
||||
|
||||
mrb_undef_method(mrb, cls, "append_features");
|
||||
mrb_undef_method(mrb, cls, "extend_object");
|
||||
|
||||
mrb->top_self = (struct RObject*)mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb->object_class);
|
||||
mrb_define_singleton_method(mrb, mrb->top_self, "inspect", inspect_main, MRB_ARGS_NONE());
|
||||
mrb_define_singleton_method(mrb, mrb->top_self, "to_s", inspect_main, MRB_ARGS_NONE());
|
||||
mrb_define_singleton_method(mrb, mrb->top_self, "define_method", top_define_method, MRB_ARGS_ARG(1,1));
|
||||
}
|
||||
|
||||
+4
-4
@@ -314,22 +314,22 @@ codedump(mrb_state *mrb, mrb_irep *irep)
|
||||
GETARG_C(c));
|
||||
break;
|
||||
case OP_LT:
|
||||
printf("OP_LT\tR%d\t:%s\t%d\n", GETARG_A(c),
|
||||
printf("OP_LT\t\tR%d\t:%s\t%d\n", GETARG_A(c),
|
||||
mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
|
||||
GETARG_C(c));
|
||||
break;
|
||||
case OP_LE:
|
||||
printf("OP_LE\tR%d\t:%s\t%d\n", GETARG_A(c),
|
||||
printf("OP_LE\t\tR%d\t:%s\t%d\n", GETARG_A(c),
|
||||
mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
|
||||
GETARG_C(c));
|
||||
break;
|
||||
case OP_GT:
|
||||
printf("OP_GT\tR%d\t:%s\t%d\n", GETARG_A(c),
|
||||
printf("OP_GT\t\tR%d\t:%s\t%d\n", GETARG_A(c),
|
||||
mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
|
||||
GETARG_C(c));
|
||||
break;
|
||||
case OP_GE:
|
||||
printf("OP_GE\tR%d\t:%s\t%d\n", GETARG_A(c),
|
||||
printf("OP_GE\t\tR%d\t:%s\t%d\n", GETARG_A(c),
|
||||
mrb_sym2name(mrb, irep->syms[GETARG_B(c)]),
|
||||
GETARG_C(c));
|
||||
break;
|
||||
|
||||
@@ -200,6 +200,7 @@ exc_debug_info(mrb_state *mrb, struct RObject *exc)
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
mrb_code *pc = ci->pc;
|
||||
|
||||
if (mrb_obj_iv_defined(mrb, exc, mrb_intern_lit(mrb, "file"))) return;
|
||||
while (ci >= mrb->c->cibase) {
|
||||
mrb_code *err = ci->err;
|
||||
|
||||
|
||||
@@ -649,11 +649,8 @@ gc_mark_children(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj)
|
||||
{
|
||||
struct RProc *p = (struct RProc*)obj;
|
||||
|
||||
mrb_gc_mark(mrb, (struct RBasic*)p->env);
|
||||
mrb_gc_mark(mrb, (struct RBasic*)p->target_class);
|
||||
if (!MRB_PROC_CFUNC_P(p) && p->body.irep) {
|
||||
mrb_gc_mark(mrb, (struct RBasic*)p->body.irep->target_class);
|
||||
}
|
||||
mrb_gc_mark(mrb, (struct RBasic*)p->upper);
|
||||
mrb_gc_mark(mrb, (struct RBasic*)p->e.env);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -662,11 +659,8 @@ gc_mark_children(mrb_state *mrb, mrb_gc *gc, struct RBasic *obj)
|
||||
struct REnv *e = (struct REnv*)obj;
|
||||
mrb_int i, len;
|
||||
|
||||
if (MRB_ENV_STACK_SHARED_P(e)) {
|
||||
if (e->cxt.c->fib) {
|
||||
mrb_gc_mark(mrb, (struct RBasic*)e->cxt.c->fib);
|
||||
}
|
||||
break;
|
||||
if (MRB_ENV_STACK_SHARED_P(e) && e->cxt->fib) {
|
||||
mrb_gc_mark(mrb, (struct RBasic*)e->cxt->fib);
|
||||
}
|
||||
len = MRB_ENV_STACK_LEN(e);
|
||||
for (i=0; i<len; i++) {
|
||||
|
||||
+21
-21
@@ -136,6 +136,7 @@ mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
mrb_value *bp;
|
||||
struct RProc *p;
|
||||
|
||||
bp = ci->stackent + 1;
|
||||
ci--;
|
||||
@@ -143,24 +144,20 @@ mrb_f_block_given_p_m(mrb_state *mrb, mrb_value self)
|
||||
return mrb_false_value();
|
||||
}
|
||||
/* block_given? called within block; check upper scope */
|
||||
if (ci->proc->env) {
|
||||
struct REnv *e = ci->proc->env;
|
||||
|
||||
while (e->c) {
|
||||
e = (struct REnv*)e->c;
|
||||
}
|
||||
p = ci->proc;
|
||||
while (p) {
|
||||
if (MRB_PROC_SCOPE_P(p)) break;
|
||||
p = p->upper;
|
||||
}
|
||||
/* top-level does not have block slot (always false) */
|
||||
if (p == NULL) return mrb_false_value();
|
||||
if (MRB_PROC_ENV_P(p)) {
|
||||
struct REnv *e = MRB_PROC_ENV(p);
|
||||
/* top-level does not have block slot (always false) */
|
||||
if (e->stack == mrb->c->stbase)
|
||||
return mrb_false_value();
|
||||
if (e->stack && e->cioff < 0) {
|
||||
/* use saved block arg position */
|
||||
bp = &e->stack[-e->cioff];
|
||||
ci = 0; /* no callinfo available */
|
||||
}
|
||||
else {
|
||||
ci = e->cxt.c->cibase + e->cioff;
|
||||
bp = ci[1].stackent + 1;
|
||||
}
|
||||
/* use saved block arg position */
|
||||
bp = &e->stack[MRB_ENV_BIDX(e)];
|
||||
}
|
||||
if (ci && ci->argc > 0) {
|
||||
bp += ci->argc;
|
||||
@@ -659,7 +656,7 @@ method_entry_loop(mrb_state *mrb, struct RClass* klass, khash_t(st)* set)
|
||||
khint_t i;
|
||||
|
||||
khash_t(mt) *h = klass->mt;
|
||||
if (!h) return;
|
||||
if (!h || kh_size(h) == 0) return;
|
||||
for (i=0;i<kh_end(h);i++) {
|
||||
if (kh_exist(h, i) && kh_value(h, i)) {
|
||||
kh_put(st, mrb, set, kh_key(h, i));
|
||||
@@ -694,7 +691,7 @@ mrb_class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass* kl
|
||||
klass = klass->super;
|
||||
}
|
||||
|
||||
ary = mrb_ary_new(mrb);
|
||||
ary = mrb_ary_new_capa(mrb, kh_size(set));
|
||||
for (i=0;i<kh_end(set);i++) {
|
||||
if (kh_exist(set, i)) {
|
||||
mrb_ary_push(mrb, ary, mrb_symbol_value(kh_key(set, i)));
|
||||
@@ -1175,16 +1172,19 @@ mrb_local_variables(mrb_state *mrb, mrb_value self)
|
||||
return mrb_ary_new(mrb);
|
||||
}
|
||||
vars = mrb_hash_new(mrb);
|
||||
irep = proc->body.irep;
|
||||
while (irep) {
|
||||
while (proc) {
|
||||
if (MRB_PROC_CFUNC_P(proc)) break;
|
||||
irep = proc->body.irep;
|
||||
if (!irep->lv) break;
|
||||
for (i = 0; i + 1 < irep->nlocals; ++i) {
|
||||
if (irep->lv[i].name) {
|
||||
mrb_hash_set(mrb, vars, mrb_symbol_value(irep->lv[i].name), mrb_true_value());
|
||||
}
|
||||
}
|
||||
if (!proc->env) break;
|
||||
irep = irep->outer;
|
||||
if (!MRB_PROC_ENV_P(proc)) break;
|
||||
proc = proc->upper;
|
||||
// if (MRB_PROC_SCOPE_P(proc)) break;
|
||||
if (!proc->c) break;
|
||||
}
|
||||
|
||||
return mrb_hash_keys(mrb, vars);
|
||||
|
||||
@@ -1371,6 +1371,20 @@ num_cmp(mrb_state *mrb, mrb_value self)
|
||||
}
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
num_finite_p(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_get_args(mrb, "");
|
||||
return mrb_true_value();
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
num_infinite_p(mrb_state *mrb, mrb_value self)
|
||||
{
|
||||
mrb_get_args(mrb, "");
|
||||
return mrb_false_value();
|
||||
}
|
||||
|
||||
/* 15.2.9.3.1 */
|
||||
/*
|
||||
* call-seq:
|
||||
@@ -1406,6 +1420,8 @@ mrb_init_numeric(mrb_state *mrb)
|
||||
mrb_define_method(mrb, numeric, "/", num_div, MRB_ARGS_REQ(1)); /* 15.2.8.3.4 */
|
||||
mrb_define_method(mrb, numeric, "quo", num_div, MRB_ARGS_REQ(1)); /* 15.2.7.4.5 (x) */
|
||||
mrb_define_method(mrb, numeric, "<=>", num_cmp, MRB_ARGS_REQ(1)); /* 15.2.9.3.6 */
|
||||
mrb_define_method(mrb, numeric, "finite?", num_finite_p, MRB_ARGS_NONE());
|
||||
mrb_define_method(mrb, numeric, "infinite?",num_infinite_p, MRB_ARGS_NONE());
|
||||
|
||||
/* Integer Class */
|
||||
integer = mrb_define_class(mrb, "Integer", numeric); /* 15.2.8 */
|
||||
|
||||
+47
-25
@@ -20,15 +20,19 @@ mrb_proc_new(mrb_state *mrb, mrb_irep *irep)
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
|
||||
p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class);
|
||||
p->target_class = 0;
|
||||
if (ci) {
|
||||
if (ci->proc)
|
||||
p->target_class = ci->proc->target_class;
|
||||
if (!p->target_class)
|
||||
p->target_class = ci->target_class;
|
||||
struct RClass *tc = NULL;
|
||||
|
||||
if (ci->proc) {
|
||||
tc = MRB_PROC_TARGET_CLASS(ci->proc);
|
||||
}
|
||||
if (tc == NULL) {
|
||||
tc = ci->target_class;
|
||||
}
|
||||
p->upper = ci->proc;
|
||||
p->e.target_class = tc;
|
||||
}
|
||||
p->body.irep = irep;
|
||||
p->env = 0;
|
||||
mrb_irep_incref(mrb, irep);
|
||||
|
||||
return p;
|
||||
@@ -38,30 +42,45 @@ static struct REnv*
|
||||
env_new(mrb_state *mrb, mrb_int nlocals)
|
||||
{
|
||||
struct REnv *e;
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
int bidx;
|
||||
|
||||
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, (struct RClass*)mrb->c->ci->proc->env);
|
||||
MRB_SET_ENV_STACK_LEN(e, nlocals);
|
||||
e->cxt.c = mrb->c;
|
||||
e->cioff = mrb->c->ci - mrb->c->cibase;
|
||||
e = (struct REnv*)mrb_obj_alloc(mrb, MRB_TT_ENV, NULL);
|
||||
MRB_ENV_SET_STACK_LEN(e, nlocals);
|
||||
bidx = ci->argc;
|
||||
if (ci->argc < 0) bidx = 2;
|
||||
else bidx += 1;
|
||||
MRB_ENV_SET_BIDX(e, bidx);
|
||||
e->mid = ci->mid;
|
||||
e->stack = mrb->c->stack;
|
||||
e->cxt = mrb->c;
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
static void
|
||||
closure_setup(mrb_state *mrb, struct RProc *p, int nlocals)
|
||||
closure_setup(mrb_state *mrb, struct RProc *p)
|
||||
{
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
struct RProc *up = p->upper;
|
||||
struct REnv *e;
|
||||
|
||||
if (!mrb->c->ci->env) {
|
||||
e = env_new(mrb, nlocals);
|
||||
mrb->c->ci->env = e;
|
||||
if (ci->env) {
|
||||
e = ci->env;
|
||||
}
|
||||
else {
|
||||
e = mrb->c->ci->env;
|
||||
struct RClass *tc = MRB_PROC_TARGET_CLASS(up);
|
||||
|
||||
e = env_new(mrb, up->body.irep->nlocals);
|
||||
ci->env = e;
|
||||
if (tc) {
|
||||
e->c = tc;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)e, (struct RBasic*)tc);
|
||||
}
|
||||
}
|
||||
p->env = e;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic *)p, (struct RBasic *)p->env);
|
||||
p->e.env = e;
|
||||
p->flags |= MRB_PROC_ENVSET;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)e);
|
||||
}
|
||||
|
||||
struct RProc*
|
||||
@@ -69,7 +88,7 @@ mrb_closure_new(mrb_state *mrb, mrb_irep *irep)
|
||||
{
|
||||
struct RProc *p = mrb_proc_new(mrb, irep);
|
||||
|
||||
closure_setup(mrb, p, mrb->c->ci->proc->body.irep->nlocals);
|
||||
closure_setup(mrb, p);
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -81,7 +100,8 @@ mrb_proc_new_cfunc(mrb_state *mrb, mrb_func_t func)
|
||||
p = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, mrb->proc_class);
|
||||
p->body.func = func;
|
||||
p->flags |= MRB_PROC_CFUNC;
|
||||
p->env = 0;
|
||||
p->upper = 0;
|
||||
p->e.target_class = 0;
|
||||
|
||||
return p;
|
||||
}
|
||||
@@ -93,8 +113,9 @@ mrb_proc_new_cfunc_with_env(mrb_state *mrb, mrb_func_t func, mrb_int argc, const
|
||||
struct REnv *e;
|
||||
int i;
|
||||
|
||||
p->env = e = env_new(mrb, argc);
|
||||
mrb_field_write_barrier(mrb, (struct RBasic *)p, (struct RBasic *)p->env);
|
||||
p->e.env = e = env_new(mrb, argc);
|
||||
p->flags |= MRB_PROC_ENVSET;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)e);
|
||||
MRB_ENV_UNSHARE_STACK(e);
|
||||
e->stack = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * argc);
|
||||
if (argv) {
|
||||
@@ -120,7 +141,7 @@ MRB_API mrb_value
|
||||
mrb_proc_cfunc_env_get(mrb_state *mrb, mrb_int idx)
|
||||
{
|
||||
struct RProc *p = mrb->c->ci->proc;
|
||||
struct REnv *e = p->env;
|
||||
struct REnv *e = MRB_PROC_ENV(p);
|
||||
|
||||
if (!MRB_PROC_CFUNC_P(p)) {
|
||||
mrb_raise(mrb, E_TYPE_ERROR, "Can't get cfunc env from non-cfunc proc.");
|
||||
@@ -148,8 +169,9 @@ mrb_proc_copy(struct RProc *a, struct RProc *b)
|
||||
if (!MRB_PROC_CFUNC_P(a) && a->body.irep) {
|
||||
a->body.irep->refcnt++;
|
||||
}
|
||||
a->target_class = b->target_class;
|
||||
a->env = b->env;
|
||||
a->upper = b->upper;
|
||||
a->e.env = b->e.env;
|
||||
/* a->e.target_class = a->e.target_class; */
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
@@ -169,7 +191,7 @@ mrb_proc_s_new(mrb_state *mrb, mrb_value proc_class)
|
||||
proc = mrb_obj_value(p);
|
||||
mrb_funcall_with_block(mrb, proc, mrb_intern_lit(mrb, "initialize"), 0, NULL, proc);
|
||||
if (!MRB_PROC_STRICT_P(p) &&
|
||||
mrb->c->ci > mrb->c->cibase && p->env == mrb->c->ci[-1].env) {
|
||||
mrb->c->ci > mrb->c->cibase && MRB_PROC_ENV(p) == mrb->c->ci[-1].env) {
|
||||
p->flags |= MRB_PROC_ORPHAN;
|
||||
}
|
||||
return proc;
|
||||
|
||||
+2
-20
@@ -11,6 +11,7 @@
|
||||
#include <mruby/variable.h>
|
||||
#include <mruby/debug.h>
|
||||
#include <mruby/string.h>
|
||||
#include <mruby/class.h>
|
||||
|
||||
void mrb_init_core(mrb_state*);
|
||||
void mrb_init_mrbgems(mrb_state*);
|
||||
@@ -18,12 +19,6 @@ void mrb_init_mrbgems(mrb_state*);
|
||||
void mrb_gc_init(mrb_state*, mrb_gc *gc);
|
||||
void mrb_gc_destroy(mrb_state*, mrb_gc *gc);
|
||||
|
||||
static mrb_value
|
||||
inspect_main(mrb_state *mrb, mrb_value mod)
|
||||
{
|
||||
return mrb_str_new_lit(mrb, "main");
|
||||
}
|
||||
|
||||
MRB_API mrb_state*
|
||||
mrb_open_core(mrb_allocf f, void *ud)
|
||||
{
|
||||
@@ -145,11 +140,6 @@ mrb_irep_cutref(mrb_state *mrb, mrb_irep *irep)
|
||||
irep->reps[i] = NULL;
|
||||
if (tmp) mrb_irep_decref(mrb, tmp);
|
||||
}
|
||||
if (irep->outer) {
|
||||
tmp = irep->outer;
|
||||
irep->outer = NULL;
|
||||
if (tmp) mrb_irep_decref(mrb, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@@ -176,10 +166,6 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
|
||||
if (irep->reps[i])
|
||||
mrb_irep_decref(mrb, irep->reps[i]);
|
||||
}
|
||||
if (irep->outer) {
|
||||
if (irep->outer)
|
||||
mrb_irep_decref(mrb, irep->outer);
|
||||
}
|
||||
mrb_free(mrb, irep->reps);
|
||||
mrb_free(mrb, irep->lv);
|
||||
if (irep->own_filename) {
|
||||
@@ -237,6 +223,7 @@ mrb_str_pool(mrb_state *mrb, mrb_value str)
|
||||
ns->as.heap.ptr[len] = '\0';
|
||||
}
|
||||
}
|
||||
RSTR_SET_POOL_FLAG(ns);
|
||||
MRB_SET_FROZEN_FLAG(ns);
|
||||
return mrb_obj_value(ns);
|
||||
}
|
||||
@@ -294,11 +281,6 @@ mrb_add_irep(mrb_state *mrb)
|
||||
MRB_API mrb_value
|
||||
mrb_top_self(mrb_state *mrb)
|
||||
{
|
||||
if (!mrb->top_self) {
|
||||
mrb->top_self = (struct RObject*)mrb_obj_alloc(mrb, MRB_TT_OBJECT, mrb->object_class);
|
||||
mrb_define_singleton_method(mrb, mrb->top_self, "inspect", inspect_main, MRB_ARGS_NONE());
|
||||
mrb_define_singleton_method(mrb, mrb->top_self, "to_s", inspect_main, MRB_ARGS_NONE());
|
||||
}
|
||||
return mrb_obj_value(mrb->top_self);
|
||||
}
|
||||
|
||||
|
||||
+61
-89
@@ -59,7 +59,7 @@ str_new(mrb_state *mrb, const char *p, size_t len)
|
||||
return str_new_static(mrb, p, len);
|
||||
}
|
||||
s = mrb_obj_alloc_string(mrb);
|
||||
if (len < RSTRING_EMBED_LEN_MAX) {
|
||||
if (len <= RSTRING_EMBED_LEN_MAX) {
|
||||
RSTR_SET_EMBED_FLAG(s);
|
||||
RSTR_SET_EMBED_LEN(s, len);
|
||||
if (p) {
|
||||
@@ -343,48 +343,59 @@ mrb_memsearch(const void *x0, mrb_int m, const void *y0, mrb_int n)
|
||||
return mrb_memsearch_qs((const unsigned char *)x0, m, (const unsigned char *)y0, n);
|
||||
}
|
||||
|
||||
static mrb_bool
|
||||
str_make_shared(mrb_state *mrb, struct RString *s)
|
||||
static void
|
||||
str_make_shared(mrb_state *mrb, struct RString *orig, struct RString *s)
|
||||
{
|
||||
if (!RSTR_SHARED_P(s)) {
|
||||
if (MRB_FROZEN_P(s) || RSTR_FSHARED_P(s)) {
|
||||
return FALSE;
|
||||
mrb_shared_string *shared;
|
||||
mrb_int len = RSTR_LEN(orig);
|
||||
|
||||
mrb_assert(!RSTR_EMBED_P(orig));
|
||||
if (RSTR_SHARED_P(orig)) {
|
||||
shared = orig->as.heap.aux.shared;
|
||||
shared->refcnt++;
|
||||
s->as.heap.ptr = orig->as.heap.ptr;
|
||||
s->as.heap.len = len;
|
||||
s->as.heap.aux.shared = shared;
|
||||
RSTR_SET_SHARED_FLAG(s);
|
||||
RSTR_UNSET_EMBED_FLAG(s);
|
||||
}
|
||||
else if (RSTR_FSHARED_P(orig)) {
|
||||
struct RString *fs;
|
||||
|
||||
fs = orig->as.heap.aux.fshared;
|
||||
s->as.heap.ptr = orig->as.heap.ptr;
|
||||
s->as.heap.len = len;
|
||||
s->as.heap.aux.fshared = fs;
|
||||
RSTR_SET_FSHARED_FLAG(s);
|
||||
RSTR_UNSET_EMBED_FLAG(s);
|
||||
}
|
||||
else if (MRB_FROZEN_P(orig) && !RSTR_POOL_P(orig)) {
|
||||
s->as.heap.ptr = orig->as.heap.ptr;
|
||||
s->as.heap.len = len;
|
||||
s->as.heap.aux.fshared = orig;
|
||||
RSTR_SET_FSHARED_FLAG(s);
|
||||
RSTR_UNSET_EMBED_FLAG(s);
|
||||
}
|
||||
else {
|
||||
shared = (mrb_shared_string *)mrb_malloc(mrb, sizeof(mrb_shared_string));
|
||||
shared->refcnt = 2;
|
||||
shared->nofree = !!RSTR_NOFREE_P(orig);
|
||||
if (!shared->nofree && orig->as.heap.aux.capa > orig->as.heap.len) {
|
||||
shared->ptr = (char *)mrb_realloc(mrb, orig->as.heap.ptr, len+1);
|
||||
orig->as.heap.ptr = shared->ptr;
|
||||
}
|
||||
else {
|
||||
mrb_shared_string *shared = (mrb_shared_string *)mrb_malloc(mrb, sizeof(mrb_shared_string));
|
||||
|
||||
shared->refcnt = 1;
|
||||
if (RSTR_EMBED_P(s)) {
|
||||
const mrb_int len = RSTR_EMBED_LEN(s);
|
||||
char *const tmp = (char *)mrb_malloc(mrb, len+1);
|
||||
memcpy(tmp, s->as.ary, len);
|
||||
tmp[len] = '\0';
|
||||
RSTR_UNSET_EMBED_FLAG(s);
|
||||
s->as.heap.ptr = tmp;
|
||||
s->as.heap.len = len;
|
||||
shared->nofree = FALSE;
|
||||
shared->ptr = s->as.heap.ptr;
|
||||
}
|
||||
else if (RSTR_NOFREE_P(s)) {
|
||||
shared->nofree = TRUE;
|
||||
shared->ptr = s->as.heap.ptr;
|
||||
RSTR_UNSET_NOFREE_FLAG(s);
|
||||
}
|
||||
else {
|
||||
shared->nofree = FALSE;
|
||||
if (s->as.heap.aux.capa > s->as.heap.len) {
|
||||
s->as.heap.ptr = shared->ptr = (char *)mrb_realloc(mrb, s->as.heap.ptr, s->as.heap.len+1);
|
||||
}
|
||||
else {
|
||||
shared->ptr = s->as.heap.ptr;
|
||||
}
|
||||
}
|
||||
shared->len = s->as.heap.len;
|
||||
s->as.heap.aux.shared = shared;
|
||||
RSTR_SET_SHARED_FLAG(s);
|
||||
shared->ptr = orig->as.heap.ptr;
|
||||
}
|
||||
orig->as.heap.aux.shared = shared;
|
||||
RSTR_SET_SHARED_FLAG(orig);
|
||||
shared->len = len;
|
||||
s->as.heap.aux.shared = shared;
|
||||
s->as.heap.ptr = shared->ptr;
|
||||
s->as.heap.len = len;
|
||||
RSTR_SET_SHARED_FLAG(s);
|
||||
RSTR_UNSET_EMBED_FLAG(s);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
@@ -393,32 +404,15 @@ byte_subseq(mrb_state *mrb, mrb_value str, mrb_int beg, mrb_int len)
|
||||
struct RString *orig, *s;
|
||||
|
||||
orig = mrb_str_ptr(str);
|
||||
if (RSTR_EMBED_P(orig) || RSTR_LEN(orig) == 0) {
|
||||
s = str_new(mrb, orig->as.ary+beg, len);
|
||||
}
|
||||
else if (str_make_shared(mrb, orig)) {
|
||||
mrb_shared_string *shared = orig->as.heap.aux.shared;
|
||||
|
||||
s = mrb_obj_alloc_string(mrb);
|
||||
s->as.heap.ptr = orig->as.heap.ptr + beg;
|
||||
s->as.heap.len = len;
|
||||
s->as.heap.aux.shared = shared;
|
||||
RSTR_SET_SHARED_FLAG(s);
|
||||
shared->refcnt++;
|
||||
if (RSTR_EMBED_P(orig) || RSTR_LEN(orig) == 0 || len <= RSTRING_EMBED_LEN_MAX) {
|
||||
s = str_new(mrb, RSTR_PTR(orig)+beg, len);
|
||||
}
|
||||
else {
|
||||
s = mrb_obj_alloc_string(mrb);
|
||||
s->as.heap.ptr = orig->as.heap.ptr + beg;
|
||||
str_make_shared(mrb, orig, s);
|
||||
s->as.heap.ptr += beg;
|
||||
s->as.heap.len = len;
|
||||
if (MRB_FROZEN_P(orig)) {
|
||||
s->as.heap.aux.fshared = orig;
|
||||
}
|
||||
else {
|
||||
s->as.heap.aux.fshared = orig->as.heap.aux.fshared;
|
||||
}
|
||||
RSTR_SET_FSHARED_FLAG(s);
|
||||
}
|
||||
|
||||
return mrb_obj_value(s);
|
||||
}
|
||||
#ifdef MRB_UTF8_STRING
|
||||
@@ -525,38 +519,15 @@ str_replace(mrb_state *mrb, struct RString *s1, struct RString *s2)
|
||||
|
||||
RSTR_UNSET_FSHARED_FLAG(s1);
|
||||
RSTR_UNSET_NOFREE_FLAG(s1);
|
||||
RSTR_UNSET_EMBED_FLAG(s1);
|
||||
|
||||
if (MRB_FROZEN_P(s2)) {
|
||||
RSTR_SET_FSHARED_FLAG(s1);
|
||||
s1->as.heap.ptr = RSTR_PTR(s2);
|
||||
s1->as.heap.len = len;
|
||||
s1->as.heap.aux.fshared = s2;
|
||||
}
|
||||
else if (RSTR_FSHARED_P(s2)) {
|
||||
RSTR_SET_FSHARED_FLAG(s1);
|
||||
s1->as.heap.ptr = s2->as.heap.ptr;
|
||||
s1->as.heap.len = len;
|
||||
s1->as.heap.aux.fshared = s2->as.heap.aux.fshared;
|
||||
}
|
||||
else if (RSTR_SHARED_P(s2)) {
|
||||
L_SHARE:
|
||||
RSTR_SET_SHARED_FLAG(s1);
|
||||
s1->as.heap.ptr = s2->as.heap.ptr;
|
||||
s1->as.heap.len = len;
|
||||
s1->as.heap.aux.shared = s2->as.heap.aux.shared;
|
||||
s1->as.heap.aux.shared->refcnt++;
|
||||
if (len <= RSTRING_EMBED_LEN_MAX) {
|
||||
RSTR_UNSET_SHARED_FLAG(s1);
|
||||
RSTR_UNSET_FSHARED_FLAG(s1);
|
||||
RSTR_SET_EMBED_FLAG(s1);
|
||||
memcpy(s1->as.ary, RSTR_PTR(s2), len);
|
||||
RSTR_SET_EMBED_LEN(s1, len);
|
||||
}
|
||||
else {
|
||||
if (len <= RSTRING_EMBED_LEN_MAX) {
|
||||
RSTR_SET_EMBED_FLAG(s1);
|
||||
memcpy(s1->as.ary, RSTR_PTR(s2), len);
|
||||
RSTR_SET_EMBED_LEN(s1, len);
|
||||
}
|
||||
else {
|
||||
str_make_shared(mrb, s2);
|
||||
goto L_SHARE;
|
||||
}
|
||||
str_make_shared(mrb, s2, s1);
|
||||
}
|
||||
|
||||
return mrb_obj_value(s1);
|
||||
@@ -712,6 +683,7 @@ mrb_str_modify(mrb_state *mrb, struct RString *s)
|
||||
char *p = s->as.heap.ptr;
|
||||
mrb_int len = s->as.heap.len;
|
||||
|
||||
RSTR_UNSET_FSHARED_FLAG(s);
|
||||
RSTR_UNSET_NOFREE_FLAG(s);
|
||||
RSTR_UNSET_FSHARED_FLAG(s);
|
||||
if (len < RSTRING_EMBED_LEN_MAX) {
|
||||
|
||||
+20
-24
@@ -632,19 +632,18 @@ mrb_cv_defined(mrb_state *mrb, mrb_value mod, mrb_sym sym)
|
||||
mrb_value
|
||||
mrb_vm_cv_get(mrb_state *mrb, mrb_sym sym)
|
||||
{
|
||||
struct RClass *c = mrb->c->ci->proc->target_class;
|
||||
|
||||
if (!c) c = mrb->c->ci->target_class;
|
||||
struct RClass *c;
|
||||
|
||||
c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
|
||||
return mrb_mod_cv_get(mrb, c, sym);
|
||||
}
|
||||
|
||||
void
|
||||
mrb_vm_cv_set(mrb_state *mrb, mrb_sym sym, mrb_value v)
|
||||
{
|
||||
struct RClass *c = mrb->c->ci->proc->target_class;
|
||||
struct RClass *c;
|
||||
|
||||
if (!c) c = mrb->c->ci->target_class;
|
||||
c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
|
||||
mrb_mod_cv_set(mrb, c, sym, v);
|
||||
}
|
||||
|
||||
@@ -663,17 +662,18 @@ mod_const_check(mrb_state *mrb, mrb_value mod)
|
||||
}
|
||||
|
||||
static mrb_value
|
||||
const_get(mrb_state *mrb, struct RClass *base, mrb_sym sym)
|
||||
const_get(mrb_state *mrb, struct RClass *base, mrb_sym sym, mrb_bool top)
|
||||
{
|
||||
struct RClass *c = base;
|
||||
mrb_value v;
|
||||
iv_tbl *t;
|
||||
mrb_bool retry = FALSE;
|
||||
mrb_value name;
|
||||
struct RClass *oclass = mrb->object_class;
|
||||
|
||||
L_RETRY:
|
||||
while (c) {
|
||||
if (c->iv) {
|
||||
if (c->iv && (top || c != oclass || base == oclass)) {
|
||||
t = c->iv;
|
||||
if (iv_get(mrb, t, sym, &v))
|
||||
return v;
|
||||
@@ -693,20 +693,18 @@ MRB_API mrb_value
|
||||
mrb_const_get(mrb_state *mrb, mrb_value mod, mrb_sym sym)
|
||||
{
|
||||
mod_const_check(mrb, mod);
|
||||
return const_get(mrb, mrb_class_ptr(mod), sym);
|
||||
return const_get(mrb, mrb_class_ptr(mod), sym, FALSE);
|
||||
}
|
||||
|
||||
mrb_value
|
||||
mrb_vm_const_get(mrb_state *mrb, mrb_sym sym)
|
||||
{
|
||||
struct RClass *c = mrb->c->ci->proc->target_class;
|
||||
struct RClass *c;
|
||||
struct RClass *c2;
|
||||
mrb_value v;
|
||||
mrb_irep *irep;
|
||||
|
||||
if (!c) c = mrb->c->ci->target_class;
|
||||
mrb_assert(c != NULL);
|
||||
struct RProc *proc;
|
||||
|
||||
c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
|
||||
if (c->iv && iv_get(mrb, c->iv, sym, &v)) {
|
||||
return v;
|
||||
}
|
||||
@@ -719,17 +717,15 @@ mrb_vm_const_get(mrb_state *mrb, mrb_sym sym)
|
||||
}
|
||||
if (c2->tt == MRB_TT_CLASS || c2->tt == MRB_TT_MODULE) c = c2;
|
||||
mrb_assert(!MRB_PROC_CFUNC_P(mrb->c->ci->proc));
|
||||
irep = mrb->c->ci->proc->body.irep;
|
||||
while (irep) {
|
||||
if (irep->target_class) {
|
||||
c2 = irep->target_class;
|
||||
if (c2->iv && iv_get(mrb, c2->iv, sym, &v)) {
|
||||
return v;
|
||||
}
|
||||
proc = mrb->c->ci->proc;
|
||||
while (proc) {
|
||||
c2 = MRB_PROC_TARGET_CLASS(proc);
|
||||
if (c2 && c2->iv && iv_get(mrb, c2->iv, sym, &v)) {
|
||||
return v;
|
||||
}
|
||||
irep = irep->outer;
|
||||
proc = proc->upper;
|
||||
}
|
||||
return const_get(mrb, c, sym);
|
||||
return const_get(mrb, c, sym, TRUE);
|
||||
}
|
||||
|
||||
MRB_API void
|
||||
@@ -745,9 +741,9 @@ mrb_const_set(mrb_state *mrb, mrb_value mod, mrb_sym sym, mrb_value v)
|
||||
void
|
||||
mrb_vm_const_set(mrb_state *mrb, mrb_sym sym, mrb_value v)
|
||||
{
|
||||
struct RClass *c = mrb->c->ci->proc->target_class;
|
||||
struct RClass *c;
|
||||
|
||||
if (!c) c = mrb->c->ci->target_class;
|
||||
c = MRB_PROC_TARGET_CLASS(mrb->c->ci->proc);
|
||||
mrb_obj_iv_set(mrb, (struct RObject*)c, sym, v);
|
||||
}
|
||||
|
||||
|
||||
@@ -211,38 +211,38 @@ stack_extend(mrb_state *mrb, int room)
|
||||
static inline struct REnv*
|
||||
uvenv(mrb_state *mrb, int up)
|
||||
{
|
||||
struct REnv *e = mrb->c->ci->proc->env;
|
||||
struct RProc *proc = mrb->c->ci->proc;
|
||||
struct REnv *e;
|
||||
|
||||
while (up--) {
|
||||
if (!e) return NULL;
|
||||
e = (struct REnv*)e->c;
|
||||
proc = proc->upper;
|
||||
if (!proc) return NULL;
|
||||
}
|
||||
return e;
|
||||
e = MRB_PROC_ENV(proc);
|
||||
if (e) return e; /* proc has enclosed env */
|
||||
else {
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
mrb_callinfo *cb = mrb->c->cibase;
|
||||
|
||||
while (cb <= ci) {
|
||||
if (ci->proc == proc) {
|
||||
return ci->env;
|
||||
}
|
||||
ci--;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline mrb_bool
|
||||
is_strict(mrb_state *mrb, struct REnv *e)
|
||||
static inline struct RProc*
|
||||
top_proc(mrb_state *mrb, struct RProc *proc)
|
||||
{
|
||||
ptrdiff_t cioff = e->cioff;
|
||||
|
||||
if (MRB_ENV_STACK_SHARED_P(e) && e->cxt.c->cibase[cioff].proc &&
|
||||
MRB_PROC_STRICT_P(e->cxt.c->cibase[cioff].proc)) {
|
||||
return TRUE;
|
||||
while (proc->upper) {
|
||||
if (MRB_PROC_SCOPE_P(proc) || MRB_PROC_STRICT_P(proc))
|
||||
return proc;
|
||||
proc = proc->upper;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static inline struct REnv*
|
||||
top_env(mrb_state *mrb, struct RProc *proc)
|
||||
{
|
||||
struct REnv *e = proc->env;
|
||||
|
||||
if (is_strict(mrb, e)) return e;
|
||||
while (e->c) {
|
||||
e = (struct REnv*)e->c;
|
||||
if (is_strict(mrb, e)) return e;
|
||||
}
|
||||
return e;
|
||||
return proc;
|
||||
}
|
||||
|
||||
#define CI_ACC_SKIP -1
|
||||
@@ -273,25 +273,17 @@ cipush(mrb_state *mrb)
|
||||
return ci;
|
||||
}
|
||||
|
||||
MRB_API void
|
||||
void
|
||||
mrb_env_unshare(mrb_state *mrb, struct REnv *e)
|
||||
{
|
||||
if (e == NULL) return;
|
||||
else {
|
||||
size_t len = (size_t)MRB_ENV_STACK_LEN(e);
|
||||
ptrdiff_t cioff = e->cioff;
|
||||
mrb_value *p;
|
||||
|
||||
if (!MRB_ENV_STACK_SHARED_P(e)) return;
|
||||
if (e->cxt.c != mrb->c) return;
|
||||
if (e->cioff == 0 && e->cxt.c == mrb->root_c) return;
|
||||
if (e->cxt != mrb->c) return;
|
||||
MRB_ENV_UNSHARE_STACK(e);
|
||||
if (!e->c) {
|
||||
/* save block argument position (negated) */
|
||||
e->cioff = -e->cxt.c->cibase[cioff].argc-1;
|
||||
if (e->cioff == 0) e->cioff = -2; /* blkarg position for vararg (1:args, 2:blk) */
|
||||
}
|
||||
e->cxt.mid = e->cxt.c->cibase[cioff].mid;
|
||||
p = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value)*len);
|
||||
if (len > 0) {
|
||||
stack_copy(p, e->stack, len);
|
||||
@@ -317,20 +309,22 @@ static void
|
||||
ecall(mrb_state *mrb, int i)
|
||||
{
|
||||
struct RProc *p;
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
mrb_value *self = mrb->c->stack;
|
||||
struct mrb_context *c = mrb->c;
|
||||
mrb_callinfo *ci = c->ci;
|
||||
struct RObject *exc;
|
||||
struct REnv *env;
|
||||
ptrdiff_t cioff;
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
|
||||
if (i<0) return;
|
||||
if (ci - mrb->c->cibase > MRB_FUNCALL_DEPTH_MAX) {
|
||||
if (ci - c->cibase > MRB_FUNCALL_DEPTH_MAX) {
|
||||
mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
|
||||
}
|
||||
p = mrb->c->ensure[i];
|
||||
p = c->ensure[i];
|
||||
if (!p) return;
|
||||
mrb->c->ensure[i] = NULL;
|
||||
cioff = ci - mrb->c->cibase;
|
||||
mrb_assert(!MRB_PROC_CFUNC_P(p));
|
||||
c->ensure[i] = NULL;
|
||||
cioff = ci - c->cibase;
|
||||
ci = cipush(mrb);
|
||||
ci->stackent = mrb->c->stack;
|
||||
ci->mid = ci[-1].mid;
|
||||
@@ -338,14 +332,17 @@ ecall(mrb_state *mrb, int i)
|
||||
ci->argc = 0;
|
||||
ci->proc = p;
|
||||
ci->nregs = p->body.irep->nregs;
|
||||
ci->target_class = p->target_class;
|
||||
mrb->c->stack = mrb->c->stack + ci[-1].nregs;
|
||||
ci->target_class = MRB_PROC_TARGET_CLASS(p);
|
||||
env = MRB_PROC_ENV(p);
|
||||
mrb_assert(env);
|
||||
c->stack += p->body.irep->nregs;
|
||||
exc = mrb->exc; mrb->exc = 0;
|
||||
if (exc) {
|
||||
mrb_gc_protect(mrb, mrb_obj_value(exc));
|
||||
}
|
||||
mrb_run(mrb, p, *self);
|
||||
mrb->c->ci = mrb->c->cibase + cioff;
|
||||
mrb_run(mrb, p, env->stack[0]);
|
||||
mrb->c = c;
|
||||
c->ci = c->cibase + cioff;
|
||||
if (!mrb->exc) mrb->exc = exc;
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
}
|
||||
@@ -497,7 +494,7 @@ mrb_exec_irep(mrb_state *mrb, mrb_value self, struct RProc *p)
|
||||
|
||||
mrb->c->stack[0] = self;
|
||||
ci->proc = p;
|
||||
ci->target_class = p->target_class;
|
||||
ci->target_class = MRB_PROC_TARGET_CLASS(p);
|
||||
if (MRB_PROC_CFUNC_P(p)) {
|
||||
return p->body.func(mrb, self);
|
||||
}
|
||||
@@ -734,7 +731,7 @@ mrb_yield_argv(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value *argv)
|
||||
{
|
||||
struct RProc *p = mrb_proc_ptr(b);
|
||||
|
||||
return mrb_yield_with_class(mrb, b, argc, argv, p->env->stack[0], p->target_class);
|
||||
return mrb_yield_with_class(mrb, b, argc, argv, MRB_PROC_ENV(p)->stack[0], MRB_PROC_TARGET_CLASS(p));
|
||||
}
|
||||
|
||||
MRB_API mrb_value
|
||||
@@ -742,7 +739,7 @@ mrb_yield(mrb_state *mrb, mrb_value b, mrb_value arg)
|
||||
{
|
||||
struct RProc *p = mrb_proc_ptr(b);
|
||||
|
||||
return mrb_yield_with_class(mrb, b, 1, &arg, p->env->stack[0], p->target_class);
|
||||
return mrb_yield_with_class(mrb, b, 1, &arg, MRB_PROC_ENV(p)->stack[0], MRB_PROC_TARGET_CLASS(p));
|
||||
}
|
||||
|
||||
mrb_value
|
||||
@@ -772,23 +769,23 @@ mrb_value
|
||||
mrb_mod_s_nesting(mrb_state *mrb, mrb_value mod)
|
||||
{
|
||||
struct RProc *proc;
|
||||
mrb_irep *irep;
|
||||
mrb_value ary;
|
||||
struct RClass *c;
|
||||
struct RClass *c = NULL;
|
||||
|
||||
mrb_get_args(mrb, "");
|
||||
ary = mrb_ary_new(mrb);
|
||||
proc = mrb->c->ci[-1].proc; /* callee proc */
|
||||
c = proc->target_class;
|
||||
mrb_ary_push(mrb, ary, mrb_obj_value(c));
|
||||
mrb_assert(!MRB_PROC_CFUNC_P(proc));
|
||||
irep = proc->body.irep;
|
||||
while (irep) {
|
||||
if (irep->target_class && irep->target_class != c) {
|
||||
c = irep->target_class;
|
||||
mrb_ary_push(mrb, ary, mrb_obj_value(c));
|
||||
while (proc) {
|
||||
if (MRB_PROC_SCOPE_P(proc)) {
|
||||
struct RClass *c2 = MRB_PROC_TARGET_CLASS(proc);
|
||||
|
||||
if (c2 != c) {
|
||||
c = c2;
|
||||
mrb_ary_push(mrb, ary, mrb_obj_value(c));
|
||||
}
|
||||
}
|
||||
irep = irep->outer;
|
||||
proc = proc->upper;
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
@@ -854,18 +851,6 @@ argnum_error(mrb_state *mrb, mrb_int num)
|
||||
mrb_exc_set(mrb, exc);
|
||||
}
|
||||
|
||||
void
|
||||
irep_uplink(mrb_state *mrb, mrb_irep *outer, mrb_irep *irep)
|
||||
{
|
||||
if (irep->outer != outer) {
|
||||
if (irep->outer) {
|
||||
mrb_irep_decref(mrb, irep->outer);
|
||||
}
|
||||
irep->outer = outer;
|
||||
mrb_irep_incref(mrb, outer);
|
||||
}
|
||||
}
|
||||
|
||||
#define ERR_PC_SET(mrb, pc) mrb->c->ci->err = pc;
|
||||
#define ERR_PC_CLR(mrb) mrb->c->ci->err = 0;
|
||||
#ifdef MRB_ENABLE_DEBUG_HOOK
|
||||
@@ -1323,9 +1308,10 @@ RETRY_TRY_BLOCK:
|
||||
CASE(OP_EPOP) {
|
||||
/* A A.times{ensure_pop().call} */
|
||||
int a = GETARG_A(i);
|
||||
int n, epos = mrb->c->ci->epos;
|
||||
mrb_callinfo *ci;
|
||||
mrb_callinfo *ci = mrb->c->ci;
|
||||
int n, epos = ci->epos;
|
||||
mrb_value self = regs[0];
|
||||
struct RClass *target_class = ci->target_class;
|
||||
|
||||
if (mrb->c->eidx == epos) {
|
||||
NEXT;
|
||||
@@ -1343,7 +1329,7 @@ RETRY_TRY_BLOCK:
|
||||
ci->proc = proc;
|
||||
ci->stackent = mrb->c->stack;
|
||||
ci->nregs = irep->nregs;
|
||||
ci->target_class = proc->target_class;
|
||||
ci->target_class = target_class;
|
||||
ci->pc = pc;
|
||||
ci->acc = ci[-1].nregs;
|
||||
mrb->c->stack += ci->acc;
|
||||
@@ -1445,7 +1431,7 @@ RETRY_TRY_BLOCK:
|
||||
if (GET_OPCODE(i) == OP_SENDB) {
|
||||
if (mrb_type(blk) == MRB_TT_PROC) {
|
||||
struct RProc *p = mrb_proc_ptr(blk);
|
||||
if (p && !MRB_PROC_STRICT_P(p) && p->env == ci[-1].env) {
|
||||
if (p && !MRB_PROC_STRICT_P(p) && MRB_PROC_ENV(p) == ci[-1].env) {
|
||||
p->flags |= MRB_PROC_ORPHAN;
|
||||
}
|
||||
}
|
||||
@@ -1497,20 +1483,16 @@ RETRY_TRY_BLOCK:
|
||||
|
||||
/* replace callinfo */
|
||||
ci = mrb->c->ci;
|
||||
ci->target_class = m->target_class;
|
||||
ci->target_class = MRB_PROC_TARGET_CLASS(m);
|
||||
ci->proc = m;
|
||||
if (m->env) {
|
||||
if (MRB_PROC_ENV_P(m)) {
|
||||
mrb_sym mid;
|
||||
struct REnv *e = MRB_PROC_ENV(m);
|
||||
|
||||
if (MRB_ENV_STACK_SHARED_P(m->env)) {
|
||||
mid = m->env->cxt.c->cibase[m->env->cioff].mid;
|
||||
}
|
||||
else {
|
||||
mid = m->env->cxt.mid;
|
||||
}
|
||||
mid = e->mid;
|
||||
if (mid) ci->mid = mid;
|
||||
if (!m->env->stack) {
|
||||
m->env->stack = mrb->c->stack;
|
||||
if (!e->stack) {
|
||||
e->stack = mrb->c->stack;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1551,8 +1533,8 @@ RETRY_TRY_BLOCK:
|
||||
else if (ci->argc+2 < irep->nregs) {
|
||||
stack_clear(regs+ci->argc+2, irep->nregs-ci->argc-2);
|
||||
}
|
||||
if (m->env) {
|
||||
regs[0] = m->env->stack[0];
|
||||
if (MRB_PROC_ENV_P(m)) {
|
||||
regs[0] = MRB_PROC_ENV(m)->stack[0];
|
||||
}
|
||||
pc = irep->iseq;
|
||||
JUMP;
|
||||
@@ -1837,6 +1819,12 @@ RETRY_TRY_BLOCK:
|
||||
/* A B return R(A) (B=normal,in-block return/break) */
|
||||
mrb_callinfo *ci;
|
||||
|
||||
#define ecall_adjust() do {\
|
||||
ptrdiff_t cioff = ci - mrb->c->cibase;\
|
||||
ecall(mrb, --mrb->c->eidx);\
|
||||
ci = mrb->c->cibase + cioff;\
|
||||
} while (0)
|
||||
|
||||
ci = mrb->c->ci;
|
||||
if (ci->mid) {
|
||||
mrb_value blk;
|
||||
@@ -1851,7 +1839,7 @@ RETRY_TRY_BLOCK:
|
||||
struct RProc *p = mrb_proc_ptr(blk);
|
||||
|
||||
if (!MRB_PROC_STRICT_P(p) &&
|
||||
ci > mrb->c->cibase && p->env == ci[-1].env) {
|
||||
ci > mrb->c->cibase && MRB_PROC_ENV(p) == ci[-1].env) {
|
||||
p->flags |= MRB_PROC_ORPHAN;
|
||||
}
|
||||
}
|
||||
@@ -1901,8 +1889,7 @@ RETRY_TRY_BLOCK:
|
||||
if (ci[0].ridx == ci[-1].ridx) {
|
||||
mrb_value *org_stbase = mrb->c->stbase;
|
||||
while (mrb->c->eidx > ci->epos) {
|
||||
ecall(mrb, --mrb->c->eidx);
|
||||
ci = mrb->c->ci;
|
||||
ecall_adjust();
|
||||
if (org_stbase != mrb->c->stbase) {
|
||||
stk = mrb->c->stack;
|
||||
}
|
||||
@@ -1915,7 +1902,7 @@ RETRY_TRY_BLOCK:
|
||||
irep = proc->body.irep;
|
||||
pool = irep->pool;
|
||||
syms = irep->syms;
|
||||
if (ci != ci0) {
|
||||
if (ci < ci0) {
|
||||
mrb->c->stack = ci[1].stackent;
|
||||
}
|
||||
stack_extend(mrb, irep->nregs);
|
||||
@@ -1924,37 +1911,37 @@ RETRY_TRY_BLOCK:
|
||||
else {
|
||||
int acc;
|
||||
mrb_value v;
|
||||
struct RProc *dst;
|
||||
|
||||
ci = mrb->c->ci;
|
||||
v = regs[GETARG_A(i)];
|
||||
mrb_gc_protect(mrb, v);
|
||||
switch (GETARG_B(i)) {
|
||||
case OP_R_RETURN:
|
||||
/* Fall through to OP_R_NORMAL otherwise */
|
||||
if (ci->acc >=0 && proc->env && !MRB_PROC_STRICT_P(proc)) {
|
||||
struct REnv *e = top_env(mrb, proc);
|
||||
mrb_callinfo *ce;
|
||||
if (ci->acc >=0 && MRB_PROC_ENV_P(proc) && !MRB_PROC_STRICT_P(proc)) {
|
||||
mrb_callinfo *cibase = mrb->c->cibase;
|
||||
dst = top_proc(mrb, proc);
|
||||
|
||||
if (!MRB_ENV_STACK_SHARED_P(e) || e->cxt.c != mrb->c) {
|
||||
localjump_error(mrb, LOCALJUMP_ERROR_RETURN);
|
||||
goto L_RAISE;
|
||||
if (MRB_PROC_ENV_P(dst)) {
|
||||
struct REnv *e = MRB_PROC_ENV(dst);
|
||||
|
||||
if (!MRB_ENV_STACK_SHARED_P(e) || e->cxt != mrb->c) {
|
||||
localjump_error(mrb, LOCALJUMP_ERROR_RETURN);
|
||||
goto L_RAISE;
|
||||
}
|
||||
}
|
||||
|
||||
ce = mrb->c->cibase + e->cioff;
|
||||
while (ci > ce) {
|
||||
mrb_env_unshare(mrb, ci->env);
|
||||
while (cibase <= ci && ci->proc != dst) {
|
||||
if (ci->acc < 0) {
|
||||
localjump_error(mrb, LOCALJUMP_ERROR_RETURN);
|
||||
goto L_RAISE;
|
||||
}
|
||||
ci--;
|
||||
}
|
||||
mrb_env_unshare(mrb, ci->env);
|
||||
if (ce == mrb->c->cibase) {
|
||||
if (ci <= cibase) {
|
||||
localjump_error(mrb, LOCALJUMP_ERROR_RETURN);
|
||||
goto L_RAISE;
|
||||
}
|
||||
mrb->c->stack = mrb->c->ci->stackent;
|
||||
mrb->c->ci = ce;
|
||||
break;
|
||||
}
|
||||
case OP_R_NORMAL:
|
||||
@@ -1970,14 +1957,14 @@ RETRY_TRY_BLOCK:
|
||||
goto L_RAISE;
|
||||
}
|
||||
while (mrb->c->eidx > 0) {
|
||||
ecall(mrb, --mrb->c->eidx);
|
||||
ecall_adjust();
|
||||
}
|
||||
/* automatic yield at the end */
|
||||
mrb->c->status = MRB_FIBER_TERMINATED;
|
||||
mrb->c = mrb->c->prev;
|
||||
mrb->c->status = MRB_FIBER_RUNNING;
|
||||
ci = mrb->c->ci;
|
||||
}
|
||||
ci = mrb->c->ci;
|
||||
break;
|
||||
case OP_R_BREAK:
|
||||
if (MRB_PROC_STRICT_P(proc)) goto NORMAL_RETURN;
|
||||
@@ -1990,23 +1977,23 @@ RETRY_TRY_BLOCK:
|
||||
mrb_exc_set(mrb, exc);
|
||||
goto L_RAISE;
|
||||
}
|
||||
if (!proc->env || !MRB_ENV_STACK_SHARED_P(proc->env)) {
|
||||
if (!MRB_PROC_ENV_P(proc) || !MRB_ENV_STACK_SHARED_P(MRB_PROC_ENV(proc))) {
|
||||
goto L_BREAK_ERROR;
|
||||
}
|
||||
if (proc->env->cxt.c != mrb->c) {
|
||||
if (MRB_PROC_ENV(proc)->cxt != mrb->c) {
|
||||
goto L_BREAK_ERROR;
|
||||
}
|
||||
while (mrb->c->eidx > mrb->c->ci->epos) {
|
||||
ecall(mrb, --mrb->c->eidx);
|
||||
ecall_adjust();
|
||||
}
|
||||
/* break from fiber block */
|
||||
if (mrb->c->ci == mrb->c->cibase && mrb->c->ci->pc) {
|
||||
if (ci == mrb->c->cibase && ci->pc) {
|
||||
struct mrb_context *c = mrb->c;
|
||||
|
||||
mrb->c = c->prev;
|
||||
c->prev = NULL;
|
||||
ci = mrb->c->ci;
|
||||
}
|
||||
ci = mrb->c->ci;
|
||||
if (ci->acc < 0) {
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
mrb->c->vmexec = FALSE;
|
||||
@@ -2022,31 +2009,32 @@ RETRY_TRY_BLOCK:
|
||||
ci = mrb->c->ci;
|
||||
}
|
||||
mrb->c->stack = ci->stackent;
|
||||
mrb->c->ci = mrb->c->cibase + proc->env->cioff + 1;
|
||||
while (ci > mrb->c->ci) {
|
||||
mrb_env_unshare(mrb, ci->env);
|
||||
proc = proc->upper;
|
||||
while (mrb->c->cibase < ci && ci[-1].proc != proc) {
|
||||
if (ci[-1].acc == CI_ACC_SKIP) {
|
||||
mrb->c->ci = ci;
|
||||
goto L_BREAK_ERROR;
|
||||
}
|
||||
ci--;
|
||||
}
|
||||
mrb_env_unshare(mrb, ci->env);
|
||||
break;
|
||||
default:
|
||||
/* cannot happen */
|
||||
break;
|
||||
}
|
||||
while (mrb->c->eidx > mrb->c->ci->epos) {
|
||||
ecall(mrb, --mrb->c->eidx);
|
||||
while (mrb->c->eidx > ci->epos) {
|
||||
ecall_adjust();
|
||||
}
|
||||
if (mrb->c->vmexec && !mrb->c->ci->target_class) {
|
||||
if (mrb->c->vmexec && !ci->target_class) {
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
mrb->c->vmexec = FALSE;
|
||||
mrb->jmp = prev_jmp;
|
||||
return v;
|
||||
}
|
||||
ci = mrb->c->ci;
|
||||
while (ci < mrb->c->ci) {
|
||||
mrb_env_unshare(mrb, mrb->c->ci->env);
|
||||
mrb->c->ci--;
|
||||
}
|
||||
acc = ci->acc;
|
||||
mrb->c->stack = ci->stackent;
|
||||
cipop(mrb);
|
||||
@@ -2056,6 +2044,7 @@ RETRY_TRY_BLOCK:
|
||||
return v;
|
||||
}
|
||||
pc = ci->pc;
|
||||
ci = mrb->c->ci;
|
||||
DEBUG(fprintf(stderr, "from :%s\n", mrb_sym2name(mrb, ci->mid)));
|
||||
proc = mrb->c->ci->proc;
|
||||
irep = proc->body.irep;
|
||||
@@ -2156,8 +2145,7 @@ RETRY_TRY_BLOCK:
|
||||
if (lv == 0) stack = regs + 1;
|
||||
else {
|
||||
struct REnv *e = uvenv(mrb, lv-1);
|
||||
if (!e || e->cioff == 0 ||
|
||||
(!MRB_ENV_STACK_SHARED_P(e) && e->cxt.mid == 0) ||
|
||||
if (!e || (!MRB_ENV_STACK_SHARED_P(e) && e->mid == 0) ||
|
||||
MRB_ENV_STACK_LEN(e) <= m1+r+m2+1) {
|
||||
localjump_error(mrb, LOCALJUMP_ERROR_YIELD);
|
||||
goto L_RAISE;
|
||||
@@ -2359,75 +2347,51 @@ RETRY_TRY_BLOCK:
|
||||
CASE(OP_DIV) {
|
||||
/* A B C R(A) := R(A)/R(A+1) (Syms[B]=:/,C=1)*/
|
||||
int a = GETARG_A(i);
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
double x, y, f;
|
||||
#endif
|
||||
|
||||
/* need to check if op is overridden */
|
||||
switch (TYPES2(mrb_type(regs[a]),mrb_type(regs[a+1]))) {
|
||||
case TYPES2(MRB_TT_FIXNUM,MRB_TT_FIXNUM):
|
||||
#ifdef MRB_WITHOUT_FLOAT
|
||||
{
|
||||
mrb_int x = mrb_fixnum(regs[a]);
|
||||
mrb_int y = mrb_fixnum(regs[a+1]);
|
||||
#ifdef MRB_WITHOUT_FLOAT
|
||||
SET_INT_VALUE(regs[a], y ? x / y : 0);
|
||||
#else
|
||||
double f;
|
||||
if (y == 0) {
|
||||
if (x > 0) f = INFINITY;
|
||||
else if (x < 0) f = -INFINITY;
|
||||
else /* if (x == 0) */ f = NAN;
|
||||
}
|
||||
else {
|
||||
f = (mrb_float)x / (mrb_float)y;
|
||||
}
|
||||
SET_FLOAT_VALUE(mrb, regs[a], f);
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
#else
|
||||
x = (mrb_float)mrb_fixnum(regs[a]);
|
||||
y = (mrb_float)mrb_fixnum(regs[a+1]);
|
||||
break;
|
||||
case TYPES2(MRB_TT_FIXNUM,MRB_TT_FLOAT):
|
||||
{
|
||||
mrb_int x = mrb_fixnum(regs[a]);
|
||||
mrb_float y = mrb_float(regs[a+1]);
|
||||
SET_FLOAT_VALUE(mrb, regs[a], (mrb_float)x / y);
|
||||
}
|
||||
x = (mrb_float)mrb_fixnum(regs[a]);
|
||||
y = mrb_float(regs[a+1]);
|
||||
break;
|
||||
case TYPES2(MRB_TT_FLOAT,MRB_TT_FIXNUM):
|
||||
#ifdef MRB_WORD_BOXING
|
||||
{
|
||||
mrb_float x = mrb_float(regs[a]);
|
||||
mrb_int y = mrb_fixnum(regs[a+1]);
|
||||
double f;
|
||||
if (y == 0) {
|
||||
f = INFINITY;
|
||||
}
|
||||
else {
|
||||
f = x / y;
|
||||
}
|
||||
SET_FLOAT_VALUE(mrb, regs[a], f);
|
||||
}
|
||||
#else
|
||||
OP_MATH_BODY(/,mrb_float,mrb_fixnum);
|
||||
#endif
|
||||
x = mrb_float(regs[a]);
|
||||
y = (mrb_float)mrb_fixnum(regs[a+1]);
|
||||
break;
|
||||
case TYPES2(MRB_TT_FLOAT,MRB_TT_FLOAT):
|
||||
#ifdef MRB_WORD_BOXING
|
||||
{
|
||||
mrb_float x = mrb_float(regs[a]);
|
||||
mrb_float y = mrb_float(regs[a+1]);
|
||||
SET_FLOAT_VALUE(mrb, regs[a], x / y);
|
||||
}
|
||||
#else
|
||||
OP_MATH_BODY(/,mrb_float,mrb_float);
|
||||
#endif
|
||||
x = mrb_float(regs[a]);
|
||||
y = mrb_float(regs[a+1]);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
goto L_SEND;
|
||||
}
|
||||
#ifdef MRB_NAN_BOXING
|
||||
if (isnan(mrb_float(regs[a]))) {
|
||||
mrb_value v = mrb_float_value(mrb, mrb_float(regs[a]));
|
||||
regs[a] = v;
|
||||
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
if (y == 0) {
|
||||
if (x > 0) f = INFINITY;
|
||||
else if (x < 0) f = -INFINITY;
|
||||
else /* if (x == 0) */ f = NAN;
|
||||
}
|
||||
else {
|
||||
f = x / y;
|
||||
}
|
||||
SET_FLOAT_VALUE(mrb, regs[a], f);
|
||||
#endif
|
||||
NEXT;
|
||||
}
|
||||
@@ -2738,7 +2702,6 @@ RETRY_TRY_BLOCK:
|
||||
int c = GETARG_c(i);
|
||||
mrb_irep *nirep = irep->reps[b];
|
||||
|
||||
irep_uplink(mrb, irep, nirep);
|
||||
if (c & OP_L_CAPTURE) {
|
||||
p = mrb_closure_new(mrb, nirep);
|
||||
}
|
||||
@@ -2767,9 +2730,7 @@ RETRY_TRY_BLOCK:
|
||||
base = regs[a];
|
||||
super = regs[a+1];
|
||||
if (mrb_nil_p(base)) {
|
||||
baseclass = mrb->c->ci->proc->target_class;
|
||||
if (!baseclass) baseclass = mrb->c->ci->target_class;
|
||||
|
||||
baseclass = mrb->c->ci->target_class;
|
||||
base = mrb_obj_value(baseclass);
|
||||
}
|
||||
c = mrb_vm_define_class(mrb, base, super, id);
|
||||
@@ -2787,9 +2748,7 @@ RETRY_TRY_BLOCK:
|
||||
|
||||
base = regs[a];
|
||||
if (mrb_nil_p(base)) {
|
||||
baseclass = mrb->c->ci->proc->target_class;
|
||||
if (!baseclass) baseclass = mrb->c->ci->target_class;
|
||||
|
||||
baseclass = mrb->c->ci->target_class;
|
||||
base = mrb_obj_value(baseclass);
|
||||
}
|
||||
c = mrb_vm_define_module(mrb, base, id);
|
||||
@@ -2807,13 +2766,14 @@ RETRY_TRY_BLOCK:
|
||||
struct RProc *p;
|
||||
mrb_irep *nirep = irep->reps[bx];
|
||||
|
||||
irep_uplink(mrb, irep, nirep);
|
||||
nirep->target_class = mrb_class_ptr(recv);
|
||||
/* prepare closure */
|
||||
p = mrb_closure_new(mrb, nirep);
|
||||
p = mrb_proc_new(mrb, nirep);
|
||||
p->c = NULL;
|
||||
mrb_field_write_barrier(mrb, (struct RBasic*)p, (struct RBasic*)proc);
|
||||
MRB_PROC_SET_TARGET_CLASS(p, mrb_class_ptr(recv));
|
||||
p->flags |= MRB_PROC_SCOPE;
|
||||
|
||||
/* prepare stack */
|
||||
/* prepare call stack */
|
||||
ci = cipush(mrb);
|
||||
ci->pc = pc + 1;
|
||||
ci->acc = a;
|
||||
@@ -2825,8 +2785,7 @@ RETRY_TRY_BLOCK:
|
||||
/* prepare stack */
|
||||
mrb->c->stack += a;
|
||||
|
||||
/* setup closure */
|
||||
p->target_class = ci->target_class;
|
||||
/* setup block to call */
|
||||
ci->proc = p;
|
||||
|
||||
irep = p->body.irep;
|
||||
@@ -2957,7 +2916,6 @@ mrb_top_run(mrb_state *mrb, struct RProc *proc, mrb_value self, unsigned int sta
|
||||
return mrb_vm_run(mrb, proc, self, stack_keep);
|
||||
}
|
||||
if (mrb->c->ci == mrb->c->cibase) {
|
||||
mrb->c->ci->env = NULL;
|
||||
return mrb_vm_run(mrb, proc, self, stack_keep);
|
||||
}
|
||||
ci = cipush(mrb);
|
||||
|
||||
+6
-4
@@ -34,11 +34,13 @@ MRuby.each_target do
|
||||
f.puts %Q[#include <mruby.h>]
|
||||
f.puts %Q[]
|
||||
f.write gem_func_decls
|
||||
unless gem_final_calls.empty?
|
||||
f.puts %Q[]
|
||||
f.puts %Q[static void]
|
||||
f.puts %Q[mrb_final_mrbgems(mrb_state *mrb) {]
|
||||
f.write gem_final_calls
|
||||
f.puts %Q[}]
|
||||
f.puts %Q[static void]
|
||||
f.puts %Q[mrb_final_mrbgems(mrb_state *mrb) {]
|
||||
f.write gem_final_calls
|
||||
f.puts %Q[}]
|
||||
end
|
||||
f.puts %Q[]
|
||||
f.puts %Q[void]
|
||||
f.puts %Q[mrb_init_mrbgems(mrb_state *mrb) {]
|
||||
|
||||
+2
-11
@@ -52,11 +52,6 @@ assert('Kernel.lambda', '15.3.1.2.6') do
|
||||
assert_equal Proc, m.class
|
||||
end
|
||||
|
||||
# Not implemented at the moment
|
||||
#assert('Kernel.local_variables', '15.3.1.2.7') do
|
||||
# Kernel.local_variables.class == Array
|
||||
#end
|
||||
|
||||
assert('Kernel.loop', '15.3.1.2.8') do
|
||||
i = 0
|
||||
|
||||
@@ -334,11 +329,6 @@ assert('Kernel#lambda', '15.3.1.3.27') do
|
||||
assert_equal Proc, m.class
|
||||
end
|
||||
|
||||
# Not implemented yet
|
||||
#assert('Kernel#local_variables', '15.3.1.3.28') do
|
||||
# local_variables.class == Array
|
||||
#end
|
||||
|
||||
assert('Kernel#loop', '15.3.1.3.29') do
|
||||
i = 0
|
||||
|
||||
@@ -571,7 +561,8 @@ assert('Kernel.local_variables', '15.3.1.2.7') do
|
||||
|
||||
assert_equal [:a, :b, :c, :vars], Proc.new { |a, b|
|
||||
c = 2
|
||||
Kernel.local_variables.sort
|
||||
# Kernel#local_variables: 15.3.1.3.28
|
||||
local_variables.sort
|
||||
}.call(-1, -2)
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user