Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mruby/mruby
Browse files Browse the repository at this point in the history
  • Loading branch information
pandax381 committed Nov 3, 2017
2 parents b70d69d + e7fe6ee commit 625f9f6
Show file tree
Hide file tree
Showing 48 changed files with 1,008 additions and 546 deletions.
17 changes: 14 additions & 3 deletions appveyor.yml
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion appveyor_config.rb
Expand Up @@ -5,7 +5,7 @@
# 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
Expand Down
23 changes: 11 additions & 12 deletions build_config.rb
Expand Up @@ -124,17 +124,17 @@
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|
Expand All @@ -148,5 +148,4 @@
# conf.gem 'examples/mrbgems/c_and_ruby_extension_example'
#
# conf.test_runner.command = 'env'
#
# end
17 changes: 10 additions & 7 deletions include/mruby.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions include/mruby/irep.h
Expand Up @@ -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
Expand Down
44 changes: 32 additions & 12 deletions include/mruby/proc.h
Expand Up @@ -18,28 +18,32 @@ 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;
union {
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 */
Expand All @@ -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)))

Expand Down
12 changes: 8 additions & 4 deletions include/mruby/string.h
Expand Up @@ -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
*/
Expand All @@ -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*);
Expand Down
3 changes: 3 additions & 0 deletions mrbgems/default.gembox
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions mrbgems/mruby-array-ext/mrbgem.rake
Expand Up @@ -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
96 changes: 96 additions & 0 deletions mrbgems/mruby-array-ext/mrblib/array.rb
Expand Up @@ -808,4 +808,100 @@ def dig(idx,*args)
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
8 changes: 5 additions & 3 deletions mrbgems/mruby-array-ext/src/array.c
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions mrbgems/mruby-array-ext/test/array.rb
Expand Up @@ -381,3 +381,31 @@ def to_ary
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

0 comments on commit 625f9f6

Please sign in to comment.