diff options
| -rw-r--r-- | .github/workflows/build.yml | 16 | ||||
| -rw-r--r-- | .github/workflows/codeql-analysis.yml | 2 | ||||
| -rw-r--r-- | .github/workflows/lint.yml | 4 | ||||
| -rw-r--r-- | .github/workflows/super-linter.yml | 2 | ||||
| -rw-r--r-- | include/mruby/boxing_nan.h | 2 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/mrblib/array.rb | 47 | ||||
| -rw-r--r-- | mrbgems/mruby-array-ext/test/array.rb | 22 | ||||
| -rw-r--r-- | src/class.c | 10 | ||||
| -rw-r--r-- | src/object.c | 7 |
9 files changed, 94 insertions, 18 deletions
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 00141695a..07a24d994 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -10,7 +10,7 @@ jobs: MRUBY_CONFIG: ci/gcc-clang CC: gcc steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - name: Ruby version run: ruby -v - name: Compiler version @@ -25,7 +25,7 @@ jobs: MRUBY_CONFIG: ci/gcc-clang CC: clang steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - name: Ruby version run: ruby -v - name: Compiler version @@ -40,7 +40,7 @@ jobs: MRUBY_CONFIG: ci/gcc-clang CC: gcc steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - name: Ruby version run: ruby -v - name: Compiler version @@ -55,7 +55,7 @@ jobs: MRUBY_CONFIG: ci/gcc-clang CC: clang steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - name: Ruby version run: ruby -v - name: Compiler version @@ -70,7 +70,7 @@ jobs: MRUBY_CONFIG: ci/gcc-clang CC: clang steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - name: Ruby version run: ruby -v - name: Compiler version @@ -85,7 +85,7 @@ jobs: MRUBY_CONFIG: ci/gcc-clang CC: gcc steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - name: Ruby version run: ruby -v - name: Compiler version @@ -105,7 +105,7 @@ jobs: package-dir: C:\cygwin-package cache-version: v1 steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - uses: actions/[email protected] with: path: ${{ env.package-dir }} @@ -145,7 +145,7 @@ jobs: env: MRUBY_CONFIG: ci/msvc steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - name: Ruby version run: ruby -v - name: Build and test diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 28bcb3ae5..184ca54c3 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -13,7 +13,7 @@ jobs: steps: - name: Checkout repository - uses: actions/[email protected] + uses: actions/[email protected] with: # We must fetch at least the immediate parents so that if this is # a pull request then we can checkout the head. diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6c08915c4..51c50faf9 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Check Out - uses: actions/[email protected] + uses: actions/[email protected] - name: Install run: wget -O - -q https://git.io/misspell | sh -s -- -b . - name: Misspell @@ -17,7 +17,7 @@ jobs: name: Run pre-commit runs-on: ubuntu-latest steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - name: Check merge conflict run: | python -m pip install --upgrade pip diff --git a/.github/workflows/super-linter.yml b/.github/workflows/super-linter.yml index 043cac9de..df453bdce 100644 --- a/.github/workflows/super-linter.yml +++ b/.github/workflows/super-linter.yml @@ -12,7 +12,7 @@ jobs: name: Lint Code Base runs-on: ubuntu-latest steps: - - uses: actions/[email protected] + - uses: actions/[email protected] - uses: github/[email protected] env: ERROR_ON_MISSING_EXEC_BIT: true diff --git a/include/mruby/boxing_nan.h b/include/mruby/boxing_nan.h index 7f7478420..a28a3e56d 100644 --- a/include/mruby/boxing_nan.h +++ b/include/mruby/boxing_nan.h @@ -92,7 +92,7 @@ mrb_type(mrb_value o) case MRB_NANBOX_TT_SYMBOL: return MRB_TT_SYMBOL; case MRB_NANBOX_TT_MISC: - return (enum mrb_vtype)(o.u >> 8) & 0x1f; + return (enum mrb_vtype)((o.u >> 8) & 0x1f); default: /* never happen */ return MRB_TT_FLOAT; diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb index 7520b932f..f7576cbf7 100644 --- a/mrbgems/mruby-array-ext/mrblib/array.rb +++ b/mrbgems/mruby-array-ext/mrblib/array.rb @@ -896,4 +896,51 @@ class Array alias append push alias prepend unshift alias filter! select! + + ## + # call-seq: + # ary.product(*arys) -> array + # ary.product(*arys) { |item| ... } -> self + def product(*arys, &block) + size = arys.size + i = size + while i > 0 + i -= 1 + unless arys[i].kind_of?(Array) + raise TypeError, "no implicit conversion into Array" + end + end + + i = size + total = self.size + total *= arys[i -= 1].size while i > 0 + + if block + result = self + list = ->(*, e) { block.call e } + class << list; alias []= call; end + else + result = [nil] * total + list = result + end + + i = 0 + while i < total + group = [nil] * (size + 1) + j = size + n = i + while j > 0 + j -= 1 + a = arys[j] + b = a.size + group[j + 1] = a[n % b] + n /= b + end + group[0] = self[n] + list[i] = group + i += 1 + end + + result + end end diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb index 3f73ad8b9..879980c7e 100644 --- a/mrbgems/mruby-array-ext/test/array.rb +++ b/mrbgems/mruby-array-ext/test/array.rb @@ -421,3 +421,25 @@ assert('Array#transpose') do assert_raise(TypeError) { [1].transpose } assert_raise(IndexError) { [[1], [2,3,4]].transpose } end + +assert "Array#product" do + assert_equal [[1], [2], [3]], [1, 2, 3].product + assert_equal [], [1, 2, 3].product([]) + assert_equal [], [1, 2, 3].product([4, 5, 6], []) + + expect = [[1, 5, 8], [1, 5, 9], [1, 6, 8], [1, 6, 9], [1, 7, 8], [1, 7, 9], + [2, 5, 8], [2, 5, 9], [2, 6, 8], [2, 6, 9], [2, 7, 8], [2, 7, 9], + [3, 5, 8], [3, 5, 9], [3, 6, 8], [3, 6, 9], [3, 7, 8], [3, 7, 9], + [4, 5, 8], [4, 5, 9], [4, 6, 8], [4, 6, 9], [4, 7, 8], [4, 7, 9]] + assert_equal expect, [1, 2, 3, 4].product([5, 6, 7], [8, 9]) + + expect = [[1, 4, 7], [1, 4, 8], [1, 4, 9], [1, 5, 7], [1, 5, 8], [1, 5, 9], [1, 6, 7], [1, 6, 8], [1, 6, 9], + [2, 4, 7], [2, 4, 8], [2, 4, 9], [2, 5, 7], [2, 5, 8], [2, 5, 9], [2, 6, 7], [2, 6, 8], [2, 6, 9], + [3, 4, 7], [3, 4, 8], [3, 4, 9], [3, 5, 7], [3, 5, 8], [3, 5, 9], [3, 6, 7], [3, 6, 8], [3, 6, 9]] + + assert_equal expect, [1, 2, 3].product([4, 5, 6], [7, 8, 9]) + base = [1, 2, 3] + x = [] + assert_equal base, base.product([4, 5, 6], [7, 8, 9]) { |e| x << e } + assert_equal expect, x +end diff --git a/src/class.c b/src/class.c index 1403ed48e..97a37c54a 100644 --- a/src/class.c +++ b/src/class.c @@ -2840,17 +2840,17 @@ static const mrb_code new_iseq[] = { OP_LOADSELF, 4, // OP_LOADSELF R4 OP_SEND, 4, 0, 0, // OP_SEND R4 :allocate n=0 OP_MOVE, 0, 4, // OP_MOVE R0 R4 - OP_MOVE, 5, 1, // OP_MOVE R5 R1 (*) - OP_MOVE, 6, 2, // OP_MOVE R6 R2 (**) - OP_MOVE, 7, 3, // OP_MOVE R7 R3 - OP_SENDB, 4, 1, 255, // OP_SENDB R4 :initialize n=*|nk=* + OP_MOVE, 4, 3, // OP_MOVE R4 R3 (&) + OP_MOVE, 3, 2, // OP_MOVE R3 R2 (**) + OP_MOVE, 2, 1, // OP_MOVE R2 R1 (*) + OP_SSENDB, 1, 1, 255, // OP_SSENDB R1 :initialize n=*|nk=* OP_RETURN, 0 // OP_RETURN R0 }; MRB_PRESYM_DEFINE_VAR_AND_INITER(new_syms, 2, MRB_SYM(allocate), MRB_SYM(initialize)) static const mrb_irep new_irep = { - 3, 6, 0, MRB_IREP_STATIC, + 4, 5, 0, MRB_IREP_STATIC, new_iseq, NULL, new_syms, NULL, NULL, NULL, sizeof(new_iseq), 0, 2, 0, 0, }; diff --git a/src/object.c b/src/object.c index b3c9973bf..8eb6b6b5e 100644 --- a/src/object.c +++ b/src/object.c @@ -14,12 +14,18 @@ MRB_API mrb_bool mrb_obj_eq(mrb_state *mrb, mrb_value v1, mrb_value v2) { +#if defined(MRB_NAN_BOXING) + return v1.u == v2.u; +#elif defined(MRB_NAN_BOXING) + return v1.w == v2.w; +#else /* MRB_NO_BOXING */ if (mrb_type(v1) != mrb_type(v2)) return FALSE; switch (mrb_type(v1)) { case MRB_TT_TRUE: return TRUE; case MRB_TT_FALSE: + return (mrb_fixnum(v1) == mrb_fixnum(v2)); case MRB_TT_INTEGER: return (mrb_integer(v1) == mrb_integer(v2)); case MRB_TT_SYMBOL: @@ -33,6 +39,7 @@ mrb_obj_eq(mrb_state *mrb, mrb_value v1, mrb_value v2) default: return (mrb_ptr(v1) == mrb_ptr(v2)); } +#endif } MRB_API mrb_bool |
