enclave 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (739) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +182 -0
  3. data/Rakefile +30 -0
  4. data/ext/enclave/enclave.c +390 -0
  5. data/ext/enclave/extconf.rb +33 -0
  6. data/ext/enclave/mruby/Makefile +63 -0
  7. data/ext/enclave/mruby/Rakefile +102 -0
  8. data/ext/enclave/mruby/benchmark/bm_ao_render.rb +309 -0
  9. data/ext/enclave/mruby/benchmark/bm_app_lc_fizzbuzz.rb +51 -0
  10. data/ext/enclave/mruby/benchmark/bm_fib.rb +6 -0
  11. data/ext/enclave/mruby/benchmark/bm_mandel_term.rb +34 -0
  12. data/ext/enclave/mruby/benchmark/bm_so_lists.rb +49 -0
  13. data/ext/enclave/mruby/benchmark/bm_so_mandelbrot.rb +65 -0
  14. data/ext/enclave/mruby/benchmark/vm_dispatch_bench.c +197 -0
  15. data/ext/enclave/mruby/benchmark/vm_optimization_bench.rb +513 -0
  16. data/ext/enclave/mruby/build/host/include/mrbconf.h +230 -0
  17. data/ext/enclave/mruby/build/host/include/mruby/array.h +303 -0
  18. data/ext/enclave/mruby/build/host/include/mruby/boxing_nan.h +169 -0
  19. data/ext/enclave/mruby/build/host/include/mruby/boxing_no.h +59 -0
  20. data/ext/enclave/mruby/build/host/include/mruby/boxing_word.h +248 -0
  21. data/ext/enclave/mruby/build/host/include/mruby/class.h +157 -0
  22. data/ext/enclave/mruby/build/host/include/mruby/common.h +127 -0
  23. data/ext/enclave/mruby/build/host/include/mruby/compile.h +187 -0
  24. data/ext/enclave/mruby/build/host/include/mruby/data.h +76 -0
  25. data/ext/enclave/mruby/build/host/include/mruby/debug.h +75 -0
  26. data/ext/enclave/mruby/build/host/include/mruby/dump.h +159 -0
  27. data/ext/enclave/mruby/build/host/include/mruby/endian.h +44 -0
  28. data/ext/enclave/mruby/build/host/include/mruby/error.h +168 -0
  29. data/ext/enclave/mruby/build/host/include/mruby/gc.h +77 -0
  30. data/ext/enclave/mruby/build/host/include/mruby/hash.h +234 -0
  31. data/ext/enclave/mruby/build/host/include/mruby/internal.h +278 -0
  32. data/ext/enclave/mruby/build/host/include/mruby/irep.h +142 -0
  33. data/ext/enclave/mruby/build/host/include/mruby/istruct.h +50 -0
  34. data/ext/enclave/mruby/build/host/include/mruby/khash.h +455 -0
  35. data/ext/enclave/mruby/build/host/include/mruby/mempool.h +19 -0
  36. data/ext/enclave/mruby/build/host/include/mruby/numeric.h +174 -0
  37. data/ext/enclave/mruby/build/host/include/mruby/object.h +44 -0
  38. data/ext/enclave/mruby/build/host/include/mruby/opcode.h +73 -0
  39. data/ext/enclave/mruby/build/host/include/mruby/ops.h +133 -0
  40. data/ext/enclave/mruby/build/host/include/mruby/presym/id.h +895 -0
  41. data/ext/enclave/mruby/build/host/include/mruby/presym/scanning.h +82 -0
  42. data/ext/enclave/mruby/build/host/include/mruby/presym/table.h +1787 -0
  43. data/ext/enclave/mruby/build/host/include/mruby/presym.h +65 -0
  44. data/ext/enclave/mruby/build/host/include/mruby/proc.h +184 -0
  45. data/ext/enclave/mruby/build/host/include/mruby/range.h +77 -0
  46. data/ext/enclave/mruby/build/host/include/mruby/re.h +16 -0
  47. data/ext/enclave/mruby/build/host/include/mruby/string.h +428 -0
  48. data/ext/enclave/mruby/build/host/include/mruby/throw.h +81 -0
  49. data/ext/enclave/mruby/build/host/include/mruby/value.h +471 -0
  50. data/ext/enclave/mruby/build/host/include/mruby/variable.h +108 -0
  51. data/ext/enclave/mruby/build/host/include/mruby/version.h +143 -0
  52. data/ext/enclave/mruby/build/host/include/mruby.h +1632 -0
  53. data/ext/enclave/mruby/build/host/mrbc/include/mruby/presym/id.h +281 -0
  54. data/ext/enclave/mruby/build/host/mrbc/include/mruby/presym/table.h +559 -0
  55. data/ext/enclave/mruby/build/host/mrbgems/gem_init.c +164 -0
  56. data/ext/enclave/mruby/build/host/mrbgems/mruby-array-ext/gem_init.c +650 -0
  57. data/ext/enclave/mruby/build/host/mrbgems/mruby-binding/gem_init.c +21 -0
  58. data/ext/enclave/mruby/build/host/mrbgems/mruby-catch/gem_init.c +86 -0
  59. data/ext/enclave/mruby/build/host/mrbgems/mruby-class-ext/gem_init.c +21 -0
  60. data/ext/enclave/mruby/build/host/mrbgems/mruby-compar-ext/gem_init.c +99 -0
  61. data/ext/enclave/mruby/build/host/mrbgems/mruby-complex/gem_init.c +362 -0
  62. data/ext/enclave/mruby/build/host/mrbgems/mruby-data/gem_init.c +21 -0
  63. data/ext/enclave/mruby/build/host/mrbgems/mruby-enum-chain/gem_init.c +229 -0
  64. data/ext/enclave/mruby/build/host/mrbgems/mruby-enum-ext/gem_init.c +1420 -0
  65. data/ext/enclave/mruby/build/host/mrbgems/mruby-enum-lazy/gem_init.c +602 -0
  66. data/ext/enclave/mruby/build/host/mrbgems/mruby-enumerator/gem_init.c +822 -0
  67. data/ext/enclave/mruby/build/host/mrbgems/mruby-eval/gem_init.c +21 -0
  68. data/ext/enclave/mruby/build/host/mrbgems/mruby-fiber/gem_init.c +21 -0
  69. data/ext/enclave/mruby/build/host/mrbgems/mruby-hash-ext/gem_init.c +591 -0
  70. data/ext/enclave/mruby/build/host/mrbgems/mruby-kernel-ext/gem_init.c +21 -0
  71. data/ext/enclave/mruby/build/host/mrbgems/mruby-math/gem_init.c +21 -0
  72. data/ext/enclave/mruby/build/host/mrbgems/mruby-metaprog/gem_init.c +21 -0
  73. data/ext/enclave/mruby/build/host/mrbgems/mruby-method/gem_init.c +153 -0
  74. data/ext/enclave/mruby/build/host/mrbgems/mruby-numeric-ext/gem_init.c +211 -0
  75. data/ext/enclave/mruby/build/host/mrbgems/mruby-object-ext/gem_init.c +94 -0
  76. data/ext/enclave/mruby/build/host/mrbgems/mruby-objectspace/gem_init.c +21 -0
  77. data/ext/enclave/mruby/build/host/mrbgems/mruby-pack/gem_init.c +21 -0
  78. data/ext/enclave/mruby/build/host/mrbgems/mruby-proc-binding/gem_init.c +21 -0
  79. data/ext/enclave/mruby/build/host/mrbgems/mruby-proc-ext/gem_init.c +237 -0
  80. data/ext/enclave/mruby/build/host/mrbgems/mruby-random/gem_init.c +21 -0
  81. data/ext/enclave/mruby/build/host/mrbgems/mruby-range-ext/gem_init.c +205 -0
  82. data/ext/enclave/mruby/build/host/mrbgems/mruby-rational/gem_init.c +147 -0
  83. data/ext/enclave/mruby/build/host/mrbgems/mruby-set/gem_init.c +487 -0
  84. data/ext/enclave/mruby/build/host/mrbgems/mruby-sprintf/gem_init.c +83 -0
  85. data/ext/enclave/mruby/build/host/mrbgems/mruby-string-ext/gem_init.c +220 -0
  86. data/ext/enclave/mruby/build/host/mrbgems/mruby-struct/gem_init.c +175 -0
  87. data/ext/enclave/mruby/build/host/mrbgems/mruby-symbol-ext/gem_init.c +153 -0
  88. data/ext/enclave/mruby/build/host/mrbgems/mruby-time/gem_init.c +21 -0
  89. data/ext/enclave/mruby/build/host/mrbgems/mruby-toplevel-ext/gem_init.c +78 -0
  90. data/ext/enclave/mruby/build/host/mrblib/mrblib.c +1758 -0
  91. data/ext/enclave/mruby/build_config/ArduinoDue.rb +72 -0
  92. data/ext/enclave/mruby/build_config/IntelEdison.rb +69 -0
  93. data/ext/enclave/mruby/build_config/IntelGalileo.rb +88 -0
  94. data/ext/enclave/mruby/build_config/RX630.rb +63 -0
  95. data/ext/enclave/mruby/build_config/android_arm64_v8a.rb +11 -0
  96. data/ext/enclave/mruby/build_config/android_armeabi_v7a_neon_hard.rb +13 -0
  97. data/ext/enclave/mruby/build_config/bench.rb +11 -0
  98. data/ext/enclave/mruby/build_config/boxing.rb +19 -0
  99. data/ext/enclave/mruby/build_config/chipKITMax32.rb +68 -0
  100. data/ext/enclave/mruby/build_config/ci/gcc-clang.rb +40 -0
  101. data/ext/enclave/mruby/build_config/ci/msvc.rb +20 -0
  102. data/ext/enclave/mruby/build_config/clang-asan.rb +11 -0
  103. data/ext/enclave/mruby/build_config/cosmopolitan.rb +86 -0
  104. data/ext/enclave/mruby/build_config/cross-32bit.rb +14 -0
  105. data/ext/enclave/mruby/build_config/cross-mingw-winetest.rb +90 -0
  106. data/ext/enclave/mruby/build_config/cross-mingw.rb +14 -0
  107. data/ext/enclave/mruby/build_config/default.rb +83 -0
  108. data/ext/enclave/mruby/build_config/dreamcast_shelf.rb +81 -0
  109. data/ext/enclave/mruby/build_config/emscripten-cxx.rb +12 -0
  110. data/ext/enclave/mruby/build_config/emscripten.rb +10 -0
  111. data/ext/enclave/mruby/build_config/gameboyadvance.rb +72 -0
  112. data/ext/enclave/mruby/build_config/helpers/wine_runner.rb +71 -0
  113. data/ext/enclave/mruby/build_config/host-cxx.rb +12 -0
  114. data/ext/enclave/mruby/build_config/host-debug.rb +20 -0
  115. data/ext/enclave/mruby/build_config/host-f32.rb +14 -0
  116. data/ext/enclave/mruby/build_config/host-gprof.rb +14 -0
  117. data/ext/enclave/mruby/build_config/host-m32.rb +15 -0
  118. data/ext/enclave/mruby/build_config/host-nofloat.rb +22 -0
  119. data/ext/enclave/mruby/build_config/host-shared.rb +36 -0
  120. data/ext/enclave/mruby/build_config/i586-pc-msdosdjgpp.rb +76 -0
  121. data/ext/enclave/mruby/build_config/luckfox_pico.rb +106 -0
  122. data/ext/enclave/mruby/build_config/milkv_duo.rb +106 -0
  123. data/ext/enclave/mruby/build_config/minimal.rb +4 -0
  124. data/ext/enclave/mruby/build_config/mrbc.rb +10 -0
  125. data/ext/enclave/mruby/build_config/nintendo_switch.rb +73 -0
  126. data/ext/enclave/mruby/build_config/nintendo_wii.rb +95 -0
  127. data/ext/enclave/mruby/build_config/no-float.rb +17 -0
  128. data/ext/enclave/mruby/build_config/playstationportable.rb +78 -0
  129. data/ext/enclave/mruby/build_config/serenity.rb +26 -0
  130. data/ext/enclave/mruby/build_config.rb +9 -0
  131. data/ext/enclave/mruby/examples/mrbgems/c_and_ruby_extension_example/mrblib/example.rb +5 -0
  132. data/ext/enclave/mruby/examples/mrbgems/c_and_ruby_extension_example/src/example.c +23 -0
  133. data/ext/enclave/mruby/examples/mrbgems/c_and_ruby_extension_example/test/example.rb +7 -0
  134. data/ext/enclave/mruby/examples/mrbgems/c_extension_example/src/example.c +23 -0
  135. data/ext/enclave/mruby/examples/mrbgems/c_extension_example/test/example.c +7 -0
  136. data/ext/enclave/mruby/examples/mrbgems/c_extension_example/test/example.rb +3 -0
  137. data/ext/enclave/mruby/examples/mrbgems/cdata_extension_example/src/example.c +77 -0
  138. data/ext/enclave/mruby/examples/mrbgems/cdata_extension_example/test/example.c +7 -0
  139. data/ext/enclave/mruby/examples/mrbgems/mruby-YOUR-bigint/core/bigint.c +70 -0
  140. data/ext/enclave/mruby/examples/mrbgems/ruby_extension_example/mrblib/example.rb +5 -0
  141. data/ext/enclave/mruby/examples/mrbgems/ruby_extension_example/test/example.rb +3 -0
  142. data/ext/enclave/mruby/include/mrbconf.h +230 -0
  143. data/ext/enclave/mruby/include/mruby/array.h +303 -0
  144. data/ext/enclave/mruby/include/mruby/boxing_nan.h +169 -0
  145. data/ext/enclave/mruby/include/mruby/boxing_no.h +59 -0
  146. data/ext/enclave/mruby/include/mruby/boxing_word.h +248 -0
  147. data/ext/enclave/mruby/include/mruby/class.h +157 -0
  148. data/ext/enclave/mruby/include/mruby/common.h +127 -0
  149. data/ext/enclave/mruby/include/mruby/compile.h +187 -0
  150. data/ext/enclave/mruby/include/mruby/data.h +76 -0
  151. data/ext/enclave/mruby/include/mruby/debug.h +75 -0
  152. data/ext/enclave/mruby/include/mruby/dump.h +159 -0
  153. data/ext/enclave/mruby/include/mruby/endian.h +44 -0
  154. data/ext/enclave/mruby/include/mruby/error.h +168 -0
  155. data/ext/enclave/mruby/include/mruby/gc.h +77 -0
  156. data/ext/enclave/mruby/include/mruby/hash.h +234 -0
  157. data/ext/enclave/mruby/include/mruby/internal.h +278 -0
  158. data/ext/enclave/mruby/include/mruby/irep.h +142 -0
  159. data/ext/enclave/mruby/include/mruby/istruct.h +50 -0
  160. data/ext/enclave/mruby/include/mruby/khash.h +455 -0
  161. data/ext/enclave/mruby/include/mruby/mempool.h +19 -0
  162. data/ext/enclave/mruby/include/mruby/numeric.h +174 -0
  163. data/ext/enclave/mruby/include/mruby/object.h +44 -0
  164. data/ext/enclave/mruby/include/mruby/opcode.h +73 -0
  165. data/ext/enclave/mruby/include/mruby/ops.h +133 -0
  166. data/ext/enclave/mruby/include/mruby/presym/scanning.h +82 -0
  167. data/ext/enclave/mruby/include/mruby/presym.h +65 -0
  168. data/ext/enclave/mruby/include/mruby/proc.h +184 -0
  169. data/ext/enclave/mruby/include/mruby/range.h +77 -0
  170. data/ext/enclave/mruby/include/mruby/re.h +16 -0
  171. data/ext/enclave/mruby/include/mruby/string.h +428 -0
  172. data/ext/enclave/mruby/include/mruby/throw.h +81 -0
  173. data/ext/enclave/mruby/include/mruby/value.h +471 -0
  174. data/ext/enclave/mruby/include/mruby/variable.h +108 -0
  175. data/ext/enclave/mruby/include/mruby/version.h +143 -0
  176. data/ext/enclave/mruby/include/mruby.h +1632 -0
  177. data/ext/enclave/mruby/lib/mruby/amalgam.rb +568 -0
  178. data/ext/enclave/mruby/lib/mruby/build/command.rb +383 -0
  179. data/ext/enclave/mruby/lib/mruby/build/load_gems.rb +383 -0
  180. data/ext/enclave/mruby/lib/mruby/build.rb +616 -0
  181. data/ext/enclave/mruby/lib/mruby/core_ext.rb +61 -0
  182. data/ext/enclave/mruby/lib/mruby/doc.rb +51 -0
  183. data/ext/enclave/mruby/lib/mruby/gem.rb +585 -0
  184. data/ext/enclave/mruby/lib/mruby/lockfile.rb +81 -0
  185. data/ext/enclave/mruby/lib/mruby/presym.rb +167 -0
  186. data/ext/enclave/mruby/lib/mruby/source.rb +32 -0
  187. data/ext/enclave/mruby/mrbgems/default-no-fpu.gembox +3 -0
  188. data/ext/enclave/mruby/mrbgems/default-no-stdio.gembox +4 -0
  189. data/ext/enclave/mruby/mrbgems/default.gembox +25 -0
  190. data/ext/enclave/mruby/mrbgems/full-core.gembox +6 -0
  191. data/ext/enclave/mruby/mrbgems/hal-posix-dir/mrbgem.rake +7 -0
  192. data/ext/enclave/mruby/mrbgems/hal-posix-dir/src/dir_hal.c +193 -0
  193. data/ext/enclave/mruby/mrbgems/hal-posix-io/mrbgem.rake +8 -0
  194. data/ext/enclave/mruby/mrbgems/hal-posix-io/src/io_hal.c +602 -0
  195. data/ext/enclave/mruby/mrbgems/hal-posix-socket/mrbgem.rake +8 -0
  196. data/ext/enclave/mruby/mrbgems/hal-posix-socket/src/socket_hal.c +158 -0
  197. data/ext/enclave/mruby/mrbgems/hal-posix-task/README.md +102 -0
  198. data/ext/enclave/mruby/mrbgems/hal-posix-task/mrbgem.rake +8 -0
  199. data/ext/enclave/mruby/mrbgems/hal-posix-task/src/task_hal.c +252 -0
  200. data/ext/enclave/mruby/mrbgems/hal-win-dir/mrbgem.rake +7 -0
  201. data/ext/enclave/mruby/mrbgems/hal-win-dir/src/dir_hal.c +249 -0
  202. data/ext/enclave/mruby/mrbgems/hal-win-io/mrbgem.rake +11 -0
  203. data/ext/enclave/mruby/mrbgems/hal-win-io/src/io_hal.c +646 -0
  204. data/ext/enclave/mruby/mrbgems/hal-win-socket/mrbgem.rake +12 -0
  205. data/ext/enclave/mruby/mrbgems/hal-win-socket/src/socket_hal.c +177 -0
  206. data/ext/enclave/mruby/mrbgems/hal-win-task/README.md +109 -0
  207. data/ext/enclave/mruby/mrbgems/hal-win-task/mrbgem.rake +11 -0
  208. data/ext/enclave/mruby/mrbgems/hal-win-task/src/task_hal.c +187 -0
  209. data/ext/enclave/mruby/mrbgems/math.gembox +12 -0
  210. data/ext/enclave/mruby/mrbgems/metaprog.gembox +21 -0
  211. data/ext/enclave/mruby/mrbgems/mruby-array-ext/README.md +87 -0
  212. data/ext/enclave/mruby/mrbgems/mruby-array-ext/mrbgem.rake +5 -0
  213. data/ext/enclave/mruby/mrbgems/mruby-array-ext/mrblib/array.rb +784 -0
  214. data/ext/enclave/mruby/mrbgems/mruby-array-ext/src/array.c +1583 -0
  215. data/ext/enclave/mruby/mrbgems/mruby-array-ext/test/array.rb +849 -0
  216. data/ext/enclave/mruby/mrbgems/mruby-benchmark/README.md +257 -0
  217. data/ext/enclave/mruby/mrbgems/mruby-benchmark/mrbgem.rake +10 -0
  218. data/ext/enclave/mruby/mrbgems/mruby-benchmark/mrblib/benchmark.rb +131 -0
  219. data/ext/enclave/mruby/mrbgems/mruby-benchmark/test/benchmark.rb +244 -0
  220. data/ext/enclave/mruby/mrbgems/mruby-bigint/README-fgmp.md +154 -0
  221. data/ext/enclave/mruby/mrbgems/mruby-bigint/README.md +44 -0
  222. data/ext/enclave/mruby/mrbgems/mruby-bigint/core/bigint.c +6335 -0
  223. data/ext/enclave/mruby/mrbgems/mruby-bigint/core/bigint.h +117 -0
  224. data/ext/enclave/mruby/mrbgems/mruby-bigint/mrbgem.rake +10 -0
  225. data/ext/enclave/mruby/mrbgems/mruby-bigint/test/bigint.rb +157 -0
  226. data/ext/enclave/mruby/mrbgems/mruby-bin-config/README.md +46 -0
  227. data/ext/enclave/mruby/mrbgems/mruby-bin-config/mrbgem.rake +49 -0
  228. data/ext/enclave/mruby/mrbgems/mruby-bin-config/mruby-config +46 -0
  229. data/ext/enclave/mruby/mrbgems/mruby-bin-config/mruby-config.bat +92 -0
  230. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/README.md +63 -0
  231. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/bintest/mrdb.rb +283 -0
  232. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/bintest/print.rb +703 -0
  233. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/mrbgem.rake +10 -0
  234. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.c +523 -0
  235. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/apibreak.h +26 -0
  236. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/apilist.c +238 -0
  237. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/apilist.h +14 -0
  238. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/apiprint.c +88 -0
  239. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/apiprint.h +13 -0
  240. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/apistring.c +34 -0
  241. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/apistring.h +14 -0
  242. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/cmdbreak.c +436 -0
  243. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/cmdmisc.c +509 -0
  244. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/cmdprint.c +71 -0
  245. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/cmdrun.c +64 -0
  246. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/mrdb.c +768 -0
  247. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/mrdb.h +159 -0
  248. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/mrdbconf.h +24 -0
  249. data/ext/enclave/mruby/mrbgems/mruby-bin-debugger/tools/mrdb/mrdberror.h +19 -0
  250. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/README.md +94 -0
  251. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/bintest/mirb.rb +58 -0
  252. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/mrbgem.rake +7 -0
  253. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c +808 -0
  254. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_buffer.c +1035 -0
  255. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_buffer.h +185 -0
  256. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_completion.c +797 -0
  257. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_completion.h +132 -0
  258. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_editor.c +1118 -0
  259. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_editor.h +153 -0
  260. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_highlight.c +516 -0
  261. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_highlight.h +95 -0
  262. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_history.c +185 -0
  263. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_history.h +76 -0
  264. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_term.c +491 -0
  265. data/ext/enclave/mruby/mrbgems/mruby-bin-mirb/tools/mirb/mirb_term.h +137 -0
  266. data/ext/enclave/mruby/mrbgems/mruby-bin-mrbc/README.md +58 -0
  267. data/ext/enclave/mruby/mrbgems/mruby-bin-mrbc/bintest/mrbc.rb +30 -0
  268. data/ext/enclave/mruby/mrbgems/mruby-bin-mrbc/mrbgem.rake +15 -0
  269. data/ext/enclave/mruby/mrbgems/mruby-bin-mrbc/tools/mrbc/mrbc.c +362 -0
  270. data/ext/enclave/mruby/mrbgems/mruby-bin-mrbc/tools/mrbc/stub.c +104 -0
  271. data/ext/enclave/mruby/mrbgems/mruby-bin-mruby/README.md +47 -0
  272. data/ext/enclave/mruby/mrbgems/mruby-bin-mruby/bintest/mruby.rb +178 -0
  273. data/ext/enclave/mruby/mrbgems/mruby-bin-mruby/mrbgem.rake +7 -0
  274. data/ext/enclave/mruby/mrbgems/mruby-bin-mruby/tools/mruby/mruby.c +380 -0
  275. data/ext/enclave/mruby/mrbgems/mruby-bin-strip/README.md +38 -0
  276. data/ext/enclave/mruby/mrbgems/mruby-bin-strip/bintest/mruby_strip.rb +73 -0
  277. data/ext/enclave/mruby/mrbgems/mruby-bin-strip/mrbgem.rake +7 -0
  278. data/ext/enclave/mruby/mrbgems/mruby-bin-strip/tools/mruby-strip/mruby_strip.c +139 -0
  279. data/ext/enclave/mruby/mrbgems/mruby-binding/README.md +256 -0
  280. data/ext/enclave/mruby/mrbgems/mruby-binding/mrbgem.rake +7 -0
  281. data/ext/enclave/mruby/mrbgems/mruby-binding/src/binding.c +518 -0
  282. data/ext/enclave/mruby/mrbgems/mruby-binding/test/binding.c +14 -0
  283. data/ext/enclave/mruby/mrbgems/mruby-binding/test/binding.rb +64 -0
  284. data/ext/enclave/mruby/mrbgems/mruby-catch/README.md +94 -0
  285. data/ext/enclave/mruby/mrbgems/mruby-catch/mrbgem.rake +5 -0
  286. data/ext/enclave/mruby/mrbgems/mruby-catch/mrblib/catch.rb +29 -0
  287. data/ext/enclave/mruby/mrbgems/mruby-catch/src/catch.c +149 -0
  288. data/ext/enclave/mruby/mrbgems/mruby-catch/test/catch.rb +86 -0
  289. data/ext/enclave/mruby/mrbgems/mruby-class-ext/README.md +81 -0
  290. data/ext/enclave/mruby/mrbgems/mruby-class-ext/mrbgem.rake +5 -0
  291. data/ext/enclave/mruby/mrbgems/mruby-class-ext/src/class.c +377 -0
  292. data/ext/enclave/mruby/mrbgems/mruby-class-ext/test/class.rb +46 -0
  293. data/ext/enclave/mruby/mrbgems/mruby-class-ext/test/module.rb +109 -0
  294. data/ext/enclave/mruby/mrbgems/mruby-cmath/README.md +45 -0
  295. data/ext/enclave/mruby/mrbgems/mruby-cmath/mrbgem.rake +8 -0
  296. data/ext/enclave/mruby/mrbgems/mruby-cmath/src/cmath.c +426 -0
  297. data/ext/enclave/mruby/mrbgems/mruby-cmath/test/cmath.rb +41 -0
  298. data/ext/enclave/mruby/mrbgems/mruby-compar-ext/README.md +65 -0
  299. data/ext/enclave/mruby/mrbgems/mruby-compar-ext/mrbgem.rake +5 -0
  300. data/ext/enclave/mruby/mrbgems/mruby-compar-ext/mrblib/compar.rb +75 -0
  301. data/ext/enclave/mruby/mrbgems/mruby-compar-ext/test/compar.rb +21 -0
  302. data/ext/enclave/mruby/mrbgems/mruby-compiler/README.md +28 -0
  303. data/ext/enclave/mruby/mrbgems/mruby-compiler/core/codegen.c +7316 -0
  304. data/ext/enclave/mruby/mrbgems/mruby-compiler/core/keywords +52 -0
  305. data/ext/enclave/mruby/mrbgems/mruby-compiler/core/lex.def +207 -0
  306. data/ext/enclave/mruby/mrbgems/mruby-compiler/core/node.h +784 -0
  307. data/ext/enclave/mruby/mrbgems/mruby-compiler/core/parse.y +8977 -0
  308. data/ext/enclave/mruby/mrbgems/mruby-compiler/core/y.tab.c +16136 -0
  309. data/ext/enclave/mruby/mrbgems/mruby-compiler/mrbgem.rake +42 -0
  310. data/ext/enclave/mruby/mrbgems/mruby-complex/README.md +55 -0
  311. data/ext/enclave/mruby/mrbgems/mruby-complex/mrbgem.rake +7 -0
  312. data/ext/enclave/mruby/mrbgems/mruby-complex/mrblib/complex.rb +259 -0
  313. data/ext/enclave/mruby/mrbgems/mruby-complex/src/complex.c +597 -0
  314. data/ext/enclave/mruby/mrbgems/mruby-complex/test/complex.rb +174 -0
  315. data/ext/enclave/mruby/mrbgems/mruby-data/README.md +126 -0
  316. data/ext/enclave/mruby/mrbgems/mruby-data/mrbgem.rake +5 -0
  317. data/ext/enclave/mruby/mrbgems/mruby-data/src/data.c +550 -0
  318. data/ext/enclave/mruby/mrbgems/mruby-data/test/data.rb +76 -0
  319. data/ext/enclave/mruby/mrbgems/mruby-dir/README.md +53 -0
  320. data/ext/enclave/mruby/mrbgems/mruby-dir/include/dir_hal.h +79 -0
  321. data/ext/enclave/mruby/mrbgems/mruby-dir/mrbgem.rake +35 -0
  322. data/ext/enclave/mruby/mrbgems/mruby-dir/mrblib/dir.rb +124 -0
  323. data/ext/enclave/mruby/mrbgems/mruby-dir/src/dir.c +493 -0
  324. data/ext/enclave/mruby/mrbgems/mruby-dir/test/dir.rb +138 -0
  325. data/ext/enclave/mruby/mrbgems/mruby-dir/test/dirtest.c +125 -0
  326. data/ext/enclave/mruby/mrbgems/mruby-encoding/README.md +96 -0
  327. data/ext/enclave/mruby/mrbgems/mruby-encoding/mrbgem.rake +8 -0
  328. data/ext/enclave/mruby/mrbgems/mruby-encoding/src/encoding.c +128 -0
  329. data/ext/enclave/mruby/mrbgems/mruby-encoding/test/numeric.rb +27 -0
  330. data/ext/enclave/mruby/mrbgems/mruby-encoding/test/string.rb +30 -0
  331. data/ext/enclave/mruby/mrbgems/mruby-enum-chain/README.md +80 -0
  332. data/ext/enclave/mruby/mrbgems/mruby-enum-chain/mrbgem.rake +6 -0
  333. data/ext/enclave/mruby/mrbgems/mruby-enum-chain/mrblib/chain.rb +149 -0
  334. data/ext/enclave/mruby/mrbgems/mruby-enum-chain/test/enum_chain.rb +108 -0
  335. data/ext/enclave/mruby/mrbgems/mruby-enum-ext/README.md +487 -0
  336. data/ext/enclave/mruby/mrbgems/mruby-enum-ext/mrbgem.rake +5 -0
  337. data/ext/enclave/mruby/mrbgems/mruby-enum-ext/mrblib/enum.rb +938 -0
  338. data/ext/enclave/mruby/mrbgems/mruby-enum-ext/test/enum.rb +223 -0
  339. data/ext/enclave/mruby/mrbgems/mruby-enum-lazy/README.md +91 -0
  340. data/ext/enclave/mruby/mrbgems/mruby-enum-lazy/mrbgem.rake +7 -0
  341. data/ext/enclave/mruby/mrbgems/mruby-enum-lazy/mrblib/lazy.rb +363 -0
  342. data/ext/enclave/mruby/mrbgems/mruby-enum-lazy/test/lazy.rb +59 -0
  343. data/ext/enclave/mruby/mrbgems/mruby-enumerator/README.md +89 -0
  344. data/ext/enclave/mruby/mrbgems/mruby-enumerator/mrbgem.rake +6 -0
  345. data/ext/enclave/mruby/mrbgems/mruby-enumerator/mrblib/enumerator.rb +861 -0
  346. data/ext/enclave/mruby/mrbgems/mruby-enumerator/test/enumerator.rb +664 -0
  347. data/ext/enclave/mruby/mrbgems/mruby-errno/README.md +83 -0
  348. data/ext/enclave/mruby/mrbgems/mruby-errno/mrbgem.rake +5 -0
  349. data/ext/enclave/mruby/mrbgems/mruby-errno/mrblib/errno.rb +50 -0
  350. data/ext/enclave/mruby/mrbgems/mruby-errno/src/errno.c +335 -0
  351. data/ext/enclave/mruby/mrbgems/mruby-errno/src/gen.rb +18 -0
  352. data/ext/enclave/mruby/mrbgems/mruby-errno/src/known_errors.def +156 -0
  353. data/ext/enclave/mruby/mrbgems/mruby-errno/src/known_errors_def.cstub +780 -0
  354. data/ext/enclave/mruby/mrbgems/mruby-errno/test/errno.rb +57 -0
  355. data/ext/enclave/mruby/mrbgems/mruby-error/README.md +103 -0
  356. data/ext/enclave/mruby/mrbgems/mruby-error/mrbgem.rake +5 -0
  357. data/ext/enclave/mruby/mrbgems/mruby-error/src/exception.c +143 -0
  358. data/ext/enclave/mruby/mrbgems/mruby-error/test/exception.c +60 -0
  359. data/ext/enclave/mruby/mrbgems/mruby-error/test/exception.rb +55 -0
  360. data/ext/enclave/mruby/mrbgems/mruby-eval/README.md +26 -0
  361. data/ext/enclave/mruby/mrbgems/mruby-eval/mrbgem.rake +10 -0
  362. data/ext/enclave/mruby/mrbgems/mruby-eval/src/eval.c +437 -0
  363. data/ext/enclave/mruby/mrbgems/mruby-eval/test/binding.rb +81 -0
  364. data/ext/enclave/mruby/mrbgems/mruby-eval/test/eval.rb +185 -0
  365. data/ext/enclave/mruby/mrbgems/mruby-exit/mrbgem.rake +5 -0
  366. data/ext/enclave/mruby/mrbgems/mruby-exit/src/mruby_exit.c +83 -0
  367. data/ext/enclave/mruby/mrbgems/mruby-fiber/mrbgem.rake +5 -0
  368. data/ext/enclave/mruby/mrbgems/mruby-fiber/src/fiber.c +575 -0
  369. data/ext/enclave/mruby/mrbgems/mruby-fiber/test/fiber.rb +210 -0
  370. data/ext/enclave/mruby/mrbgems/mruby-fiber/test/fiber2.rb +155 -0
  371. data/ext/enclave/mruby/mrbgems/mruby-fiber/test/fibertest.c +87 -0
  372. data/ext/enclave/mruby/mrbgems/mruby-hash-ext/README.md +392 -0
  373. data/ext/enclave/mruby/mrbgems/mruby-hash-ext/mrbgem.rake +6 -0
  374. data/ext/enclave/mruby/mrbgems/mruby-hash-ext/mrblib/hash.rb +408 -0
  375. data/ext/enclave/mruby/mrbgems/mruby-hash-ext/src/hash_ext.c +392 -0
  376. data/ext/enclave/mruby/mrbgems/mruby-hash-ext/test/hash.rb +317 -0
  377. data/ext/enclave/mruby/mrbgems/mruby-io/README.md +198 -0
  378. data/ext/enclave/mruby/mrbgems/mruby-io/include/io_hal.h +451 -0
  379. data/ext/enclave/mruby/mrbgems/mruby-io/include/mruby/io.h +76 -0
  380. data/ext/enclave/mruby/mrbgems/mruby-io/mrbgem.rake +34 -0
  381. data/ext/enclave/mruby/mrbgems/mruby-io/mrblib/file.rb +109 -0
  382. data/ext/enclave/mruby/mrbgems/mruby-io/mrblib/file_constants.rb +24 -0
  383. data/ext/enclave/mruby/mrbgems/mruby-io/mrblib/io.rb +303 -0
  384. data/ext/enclave/mruby/mrbgems/mruby-io/mrblib/kernel.rb +175 -0
  385. data/ext/enclave/mruby/mrbgems/mruby-io/src/file.c +1192 -0
  386. data/ext/enclave/mruby/mrbgems/mruby-io/src/file_test.c +403 -0
  387. data/ext/enclave/mruby/mrbgems/mruby-io/src/io.c +2260 -0
  388. data/ext/enclave/mruby/mrbgems/mruby-io/src/mruby_io_gem.c +21 -0
  389. data/ext/enclave/mruby/mrbgems/mruby-io/test/file.rb +415 -0
  390. data/ext/enclave/mruby/mrbgems/mruby-io/test/file_test.rb +112 -0
  391. data/ext/enclave/mruby/mrbgems/mruby-io/test/io.rb +668 -0
  392. data/ext/enclave/mruby/mrbgems/mruby-io/test/mruby_io_test.c +293 -0
  393. data/ext/enclave/mruby/mrbgems/mruby-kernel-ext/README.md +165 -0
  394. data/ext/enclave/mruby/mrbgems/mruby-kernel-ext/mrbgem.rake +5 -0
  395. data/ext/enclave/mruby/mrbgems/mruby-kernel-ext/src/kernel.c +316 -0
  396. data/ext/enclave/mruby/mrbgems/mruby-kernel-ext/test/kernel.rb +134 -0
  397. data/ext/enclave/mruby/mrbgems/mruby-math/README.md +77 -0
  398. data/ext/enclave/mruby/mrbgems/mruby-math/mrbgem.rake +5 -0
  399. data/ext/enclave/mruby/mrbgems/mruby-math/src/math.c +753 -0
  400. data/ext/enclave/mruby/mrbgems/mruby-math/test/math.rb +201 -0
  401. data/ext/enclave/mruby/mrbgems/mruby-metaprog/README.md +123 -0
  402. data/ext/enclave/mruby/mrbgems/mruby-metaprog/mrbgem.rake +5 -0
  403. data/ext/enclave/mruby/mrbgems/mruby-metaprog/src/metaprog.c +739 -0
  404. data/ext/enclave/mruby/mrbgems/mruby-metaprog/test/metaprog.rb +465 -0
  405. data/ext/enclave/mruby/mrbgems/mruby-method/README.md +66 -0
  406. data/ext/enclave/mruby/mrbgems/mruby-method/mrbgem.rake +7 -0
  407. data/ext/enclave/mruby/mrbgems/mruby-method/mrblib/method.rb +73 -0
  408. data/ext/enclave/mruby/mrbgems/mruby-method/src/method.c +890 -0
  409. data/ext/enclave/mruby/mrbgems/mruby-method/test/method.rb +514 -0
  410. data/ext/enclave/mruby/mrbgems/mruby-numeric-ext/README.md +78 -0
  411. data/ext/enclave/mruby/mrbgems/mruby-numeric-ext/mrbgem.rake +5 -0
  412. data/ext/enclave/mruby/mrbgems/mruby-numeric-ext/mrblib/numeric_ext.rb +125 -0
  413. data/ext/enclave/mruby/mrbgems/mruby-numeric-ext/src/numeric_ext.c +527 -0
  414. data/ext/enclave/mruby/mrbgems/mruby-numeric-ext/test/numeric.rb +137 -0
  415. data/ext/enclave/mruby/mrbgems/mruby-object-ext/README.md +144 -0
  416. data/ext/enclave/mruby/mrbgems/mruby-object-ext/mrbgem.rake +5 -0
  417. data/ext/enclave/mruby/mrbgems/mruby-object-ext/mrblib/object.rb +33 -0
  418. data/ext/enclave/mruby/mrbgems/mruby-object-ext/src/object.c +128 -0
  419. data/ext/enclave/mruby/mrbgems/mruby-object-ext/test/nil.rb +16 -0
  420. data/ext/enclave/mruby/mrbgems/mruby-object-ext/test/object.rb +67 -0
  421. data/ext/enclave/mruby/mrbgems/mruby-object-ext/test/object_ext.c +17 -0
  422. data/ext/enclave/mruby/mrbgems/mruby-objectspace/README.md +59 -0
  423. data/ext/enclave/mruby/mrbgems/mruby-objectspace/mrbgem.rake +5 -0
  424. data/ext/enclave/mruby/mrbgems/mruby-objectspace/src/mruby_objectspace.c +188 -0
  425. data/ext/enclave/mruby/mrbgems/mruby-objectspace/test/objectspace.rb +60 -0
  426. data/ext/enclave/mruby/mrbgems/mruby-os-memsize/README.md +75 -0
  427. data/ext/enclave/mruby/mrbgems/mruby-os-memsize/mrbgem.rake +10 -0
  428. data/ext/enclave/mruby/mrbgems/mruby-os-memsize/src/memsize.c +271 -0
  429. data/ext/enclave/mruby/mrbgems/mruby-os-memsize/test/memsize.rb +63 -0
  430. data/ext/enclave/mruby/mrbgems/mruby-pack/README.md +140 -0
  431. data/ext/enclave/mruby/mrbgems/mruby-pack/mrbgem.rake +5 -0
  432. data/ext/enclave/mruby/mrbgems/mruby-pack/src/pack.c +2129 -0
  433. data/ext/enclave/mruby/mrbgems/mruby-pack/test/pack.rb +202 -0
  434. data/ext/enclave/mruby/mrbgems/mruby-proc-binding/README.md +140 -0
  435. data/ext/enclave/mruby/mrbgems/mruby-proc-binding/mrbgem.rake +10 -0
  436. data/ext/enclave/mruby/mrbgems/mruby-proc-binding/src/proc_binding.c +76 -0
  437. data/ext/enclave/mruby/mrbgems/mruby-proc-binding/test/proc_binding.c +14 -0
  438. data/ext/enclave/mruby/mrbgems/mruby-proc-binding/test/proc_binding.rb +22 -0
  439. data/ext/enclave/mruby/mrbgems/mruby-proc-ext/README.md +107 -0
  440. data/ext/enclave/mruby/mrbgems/mruby-proc-ext/mrbgem.rake +5 -0
  441. data/ext/enclave/mruby/mrbgems/mruby-proc-ext/mrblib/proc.rb +130 -0
  442. data/ext/enclave/mruby/mrbgems/mruby-proc-ext/src/proc.c +267 -0
  443. data/ext/enclave/mruby/mrbgems/mruby-proc-ext/test/proc.c +62 -0
  444. data/ext/enclave/mruby/mrbgems/mruby-proc-ext/test/proc.rb +130 -0
  445. data/ext/enclave/mruby/mrbgems/mruby-random/README.md +272 -0
  446. data/ext/enclave/mruby/mrbgems/mruby-random/mrbgem.rake +5 -0
  447. data/ext/enclave/mruby/mrbgems/mruby-random/src/random.c +608 -0
  448. data/ext/enclave/mruby/mrbgems/mruby-random/test/random.rb +151 -0
  449. data/ext/enclave/mruby/mrbgems/mruby-range-ext/README.md +93 -0
  450. data/ext/enclave/mruby/mrbgems/mruby-range-ext/mrbgem.rake +5 -0
  451. data/ext/enclave/mruby/mrbgems/mruby-range-ext/mrblib/range.rb +155 -0
  452. data/ext/enclave/mruby/mrbgems/mruby-range-ext/src/range.c +236 -0
  453. data/ext/enclave/mruby/mrbgems/mruby-range-ext/test/range.rb +211 -0
  454. data/ext/enclave/mruby/mrbgems/mruby-rational/README.md +61 -0
  455. data/ext/enclave/mruby/mrbgems/mruby-rational/mrbgem.rake +7 -0
  456. data/ext/enclave/mruby/mrbgems/mruby-rational/mrblib/rational.rb +66 -0
  457. data/ext/enclave/mruby/mrbgems/mruby-rational/src/rational.c +1269 -0
  458. data/ext/enclave/mruby/mrbgems/mruby-rational/test/rational.rb +316 -0
  459. data/ext/enclave/mruby/mrbgems/mruby-set/LICENSE +24 -0
  460. data/ext/enclave/mruby/mrbgems/mruby-set/README.md +260 -0
  461. data/ext/enclave/mruby/mrbgems/mruby-set/mrbgem.rake +9 -0
  462. data/ext/enclave/mruby/mrbgems/mruby-set/mrblib/set.rb +325 -0
  463. data/ext/enclave/mruby/mrbgems/mruby-set/mruby-set.gem +6 -0
  464. data/ext/enclave/mruby/mrbgems/mruby-set/src/set.c +1535 -0
  465. data/ext/enclave/mruby/mrbgems/mruby-set/test/set.rb +764 -0
  466. data/ext/enclave/mruby/mrbgems/mruby-sleep/README.md +29 -0
  467. data/ext/enclave/mruby/mrbgems/mruby-sleep/example/sleep.rb +2 -0
  468. data/ext/enclave/mruby/mrbgems/mruby-sleep/mrbgem.rake +5 -0
  469. data/ext/enclave/mruby/mrbgems/mruby-sleep/src/sleep.c +187 -0
  470. data/ext/enclave/mruby/mrbgems/mruby-sleep/test/sleep_test.rb +29 -0
  471. data/ext/enclave/mruby/mrbgems/mruby-socket/README.md +56 -0
  472. data/ext/enclave/mruby/mrbgems/mruby-socket/include/socket_hal.h +83 -0
  473. data/ext/enclave/mruby/mrbgems/mruby-socket/mrbgem.rake +37 -0
  474. data/ext/enclave/mruby/mrbgems/mruby-socket/mrblib/socket.rb +1131 -0
  475. data/ext/enclave/mruby/mrbgems/mruby-socket/src/const.cstub +480 -0
  476. data/ext/enclave/mruby/mrbgems/mruby-socket/src/const.def +172 -0
  477. data/ext/enclave/mruby/mrbgems/mruby-socket/src/gen.rb +17 -0
  478. data/ext/enclave/mruby/mrbgems/mruby-socket/src/socket.c +1374 -0
  479. data/ext/enclave/mruby/mrbgems/mruby-socket/test/addrinfo.rb +91 -0
  480. data/ext/enclave/mruby/mrbgems/mruby-socket/test/basicsocket.rb +17 -0
  481. data/ext/enclave/mruby/mrbgems/mruby-socket/test/ipsocket.rb +44 -0
  482. data/ext/enclave/mruby/mrbgems/mruby-socket/test/socket.rb +38 -0
  483. data/ext/enclave/mruby/mrbgems/mruby-socket/test/sockettest.c +84 -0
  484. data/ext/enclave/mruby/mrbgems/mruby-socket/test/tcpsocket.rb +4 -0
  485. data/ext/enclave/mruby/mrbgems/mruby-socket/test/udpsocket.rb +16 -0
  486. data/ext/enclave/mruby/mrbgems/mruby-socket/test/unix.rb +130 -0
  487. data/ext/enclave/mruby/mrbgems/mruby-sprintf/README.md +235 -0
  488. data/ext/enclave/mruby/mrbgems/mruby-sprintf/mrbgem.rake +5 -0
  489. data/ext/enclave/mruby/mrbgems/mruby-sprintf/mrblib/string.rb +24 -0
  490. data/ext/enclave/mruby/mrbgems/mruby-sprintf/src/sprintf.c +1195 -0
  491. data/ext/enclave/mruby/mrbgems/mruby-sprintf/test/sprintf.rb +92 -0
  492. data/ext/enclave/mruby/mrbgems/mruby-strftime/README.md +140 -0
  493. data/ext/enclave/mruby/mrbgems/mruby-strftime/mrbgem.rake +7 -0
  494. data/ext/enclave/mruby/mrbgems/mruby-strftime/src/strftime.c +119 -0
  495. data/ext/enclave/mruby/mrbgems/mruby-strftime/test/strftime.rb +152 -0
  496. data/ext/enclave/mruby/mrbgems/mruby-string-ext/README.md +886 -0
  497. data/ext/enclave/mruby/mrbgems/mruby-string-ext/mrbgem.rake +5 -0
  498. data/ext/enclave/mruby/mrbgems/mruby-string-ext/mrblib/string.rb +166 -0
  499. data/ext/enclave/mruby/mrbgems/mruby-string-ext/src/string.c +2290 -0
  500. data/ext/enclave/mruby/mrbgems/mruby-string-ext/test/numeric.rb +27 -0
  501. data/ext/enclave/mruby/mrbgems/mruby-string-ext/test/range.rb +26 -0
  502. data/ext/enclave/mruby/mrbgems/mruby-string-ext/test/string.rb +765 -0
  503. data/ext/enclave/mruby/mrbgems/mruby-struct/README.md +105 -0
  504. data/ext/enclave/mruby/mrbgems/mruby-struct/mrbgem.rake +5 -0
  505. data/ext/enclave/mruby/mrbgems/mruby-struct/mrblib/struct.rb +69 -0
  506. data/ext/enclave/mruby/mrbgems/mruby-struct/src/struct.c +812 -0
  507. data/ext/enclave/mruby/mrbgems/mruby-struct/test/struct.rb +303 -0
  508. data/ext/enclave/mruby/mrbgems/mruby-symbol-ext/README.md +50 -0
  509. data/ext/enclave/mruby/mrbgems/mruby-symbol-ext/mrbgem.rake +5 -0
  510. data/ext/enclave/mruby/mrbgems/mruby-symbol-ext/mrblib/symbol.rb +72 -0
  511. data/ext/enclave/mruby/mrbgems/mruby-symbol-ext/src/symbol.c +79 -0
  512. data/ext/enclave/mruby/mrbgems/mruby-symbol-ext/test/symbol.rb +55 -0
  513. data/ext/enclave/mruby/mrbgems/mruby-task/README.md +770 -0
  514. data/ext/enclave/mruby/mrbgems/mruby-task/examples/inspection.rb +65 -0
  515. data/ext/enclave/mruby/mrbgems/mruby-task/examples/priority.rb +41 -0
  516. data/ext/enclave/mruby/mrbgems/mruby-task/examples/producer_consumer.rb +58 -0
  517. data/ext/enclave/mruby/mrbgems/mruby-task/examples/simple.rb +27 -0
  518. data/ext/enclave/mruby/mrbgems/mruby-task/examples/statistics.rb +59 -0
  519. data/ext/enclave/mruby/mrbgems/mruby-task/examples/suspend_resume.rb +47 -0
  520. data/ext/enclave/mruby/mrbgems/mruby-task/examples/task_pass.rb +21 -0
  521. data/ext/enclave/mruby/mrbgems/mruby-task/include/task.h +143 -0
  522. data/ext/enclave/mruby/mrbgems/mruby-task/include/task_hal.h +162 -0
  523. data/ext/enclave/mruby/mrbgems/mruby-task/mrbgem.rake +36 -0
  524. data/ext/enclave/mruby/mrbgems/mruby-task/src/task.c +1573 -0
  525. data/ext/enclave/mruby/mrbgems/mruby-task/test/task.rb +185 -0
  526. data/ext/enclave/mruby/mrbgems/mruby-test/README.md +7 -0
  527. data/ext/enclave/mruby/mrbgems/mruby-test/driver.c +315 -0
  528. data/ext/enclave/mruby/mrbgems/mruby-test/mrbgem.rake +173 -0
  529. data/ext/enclave/mruby/mrbgems/mruby-test/vformat.c +151 -0
  530. data/ext/enclave/mruby/mrbgems/mruby-test-inline-struct/mrbgem.rake +5 -0
  531. data/ext/enclave/mruby/mrbgems/mruby-test-inline-struct/test/inline.c +92 -0
  532. data/ext/enclave/mruby/mrbgems/mruby-test-inline-struct/test/inline.rb +118 -0
  533. data/ext/enclave/mruby/mrbgems/mruby-time/README.md +102 -0
  534. data/ext/enclave/mruby/mrbgems/mruby-time/include/mruby/time.h +27 -0
  535. data/ext/enclave/mruby/mrbgems/mruby-time/mrbgem.rake +5 -0
  536. data/ext/enclave/mruby/mrbgems/mruby-time/src/time.c +1739 -0
  537. data/ext/enclave/mruby/mrbgems/mruby-time/test/time.rb +313 -0
  538. data/ext/enclave/mruby/mrbgems/mruby-toplevel-ext/README.md +32 -0
  539. data/ext/enclave/mruby/mrbgems/mruby-toplevel-ext/mrbgem.rake +5 -0
  540. data/ext/enclave/mruby/mrbgems/mruby-toplevel-ext/mrblib/toplevel.rb +24 -0
  541. data/ext/enclave/mruby/mrbgems/mruby-toplevel-ext/test/toplevel.rb +23 -0
  542. data/ext/enclave/mruby/mrbgems/stdlib-ext.gembox +21 -0
  543. data/ext/enclave/mruby/mrbgems/stdlib-io.gembox +15 -0
  544. data/ext/enclave/mruby/mrbgems/stdlib.gembox +63 -0
  545. data/ext/enclave/mruby/mrblib/10error.rb +23 -0
  546. data/ext/enclave/mruby/mrblib/array.rb +102 -0
  547. data/ext/enclave/mruby/mrblib/compar.rb +102 -0
  548. data/ext/enclave/mruby/mrblib/enum.rb +358 -0
  549. data/ext/enclave/mruby/mrblib/hash.rb +276 -0
  550. data/ext/enclave/mruby/mrblib/kernel.rb +45 -0
  551. data/ext/enclave/mruby/mrblib/numeric.rb +160 -0
  552. data/ext/enclave/mruby/mrblib/range.rb +98 -0
  553. data/ext/enclave/mruby/mrblib/string.rb +173 -0
  554. data/ext/enclave/mruby/mrblib/symbol.rb +8 -0
  555. data/ext/enclave/mruby/oss-fuzz/mruby_fuzzer.c +18 -0
  556. data/ext/enclave/mruby/oss-fuzz/proto_to_ruby.h +55 -0
  557. data/ext/enclave/mruby/src/allocf.c +37 -0
  558. data/ext/enclave/mruby/src/array.c +2302 -0
  559. data/ext/enclave/mruby/src/backtrace.c +294 -0
  560. data/ext/enclave/mruby/src/cdump.c +498 -0
  561. data/ext/enclave/mruby/src/class.c +4394 -0
  562. data/ext/enclave/mruby/src/codedump.c +693 -0
  563. data/ext/enclave/mruby/src/debug.c +290 -0
  564. data/ext/enclave/mruby/src/dump.c +998 -0
  565. data/ext/enclave/mruby/src/enum.c +30 -0
  566. data/ext/enclave/mruby/src/error.c +937 -0
  567. data/ext/enclave/mruby/src/etc.c +421 -0
  568. data/ext/enclave/mruby/src/fmt_fp.c +372 -0
  569. data/ext/enclave/mruby/src/gc.c +1610 -0
  570. data/ext/enclave/mruby/src/hash.c +2355 -0
  571. data/ext/enclave/mruby/src/init.c +50 -0
  572. data/ext/enclave/mruby/src/kernel.c +718 -0
  573. data/ext/enclave/mruby/src/load.c +760 -0
  574. data/ext/enclave/mruby/src/mempool.c +225 -0
  575. data/ext/enclave/mruby/src/numeric.c +2420 -0
  576. data/ext/enclave/mruby/src/numops.c +112 -0
  577. data/ext/enclave/mruby/src/object.c +883 -0
  578. data/ext/enclave/mruby/src/print.c +136 -0
  579. data/ext/enclave/mruby/src/proc.c +572 -0
  580. data/ext/enclave/mruby/src/range.c +597 -0
  581. data/ext/enclave/mruby/src/readfloat.c +228 -0
  582. data/ext/enclave/mruby/src/readint.c +27 -0
  583. data/ext/enclave/mruby/src/readnum.c +43 -0
  584. data/ext/enclave/mruby/src/state.c +247 -0
  585. data/ext/enclave/mruby/src/string.c +3577 -0
  586. data/ext/enclave/mruby/src/symbol.c +1023 -0
  587. data/ext/enclave/mruby/src/value_array.h +28 -0
  588. data/ext/enclave/mruby/src/variable.c +1475 -0
  589. data/ext/enclave/mruby/src/version.c +17 -0
  590. data/ext/enclave/mruby/src/vm.c +3696 -0
  591. data/ext/enclave/mruby/tasks/amalgam.rake +34 -0
  592. data/ext/enclave/mruby/tasks/benchmark.rake +93 -0
  593. data/ext/enclave/mruby/tasks/bin.rake +23 -0
  594. data/ext/enclave/mruby/tasks/core.rake +12 -0
  595. data/ext/enclave/mruby/tasks/doc.rake +118 -0
  596. data/ext/enclave/mruby/tasks/install.rake +40 -0
  597. data/ext/enclave/mruby/tasks/libmruby.rake +90 -0
  598. data/ext/enclave/mruby/tasks/mrbgems.rake +152 -0
  599. data/ext/enclave/mruby/tasks/mrblib.rake +29 -0
  600. data/ext/enclave/mruby/tasks/presym.rake +57 -0
  601. data/ext/enclave/mruby/tasks/test.rake +84 -0
  602. data/ext/enclave/mruby/tasks/toolchains/android.rake +228 -0
  603. data/ext/enclave/mruby/tasks/toolchains/clang.rake +8 -0
  604. data/ext/enclave/mruby/tasks/toolchains/emscripten.rake +57 -0
  605. data/ext/enclave/mruby/tasks/toolchains/gcc.rake +74 -0
  606. data/ext/enclave/mruby/tasks/toolchains/openwrt.rake +32 -0
  607. data/ext/enclave/mruby/tasks/toolchains/visualcpp.rake +48 -0
  608. data/ext/enclave/mruby/test/assert.rb +404 -0
  609. data/ext/enclave/mruby/test/bintest.rb +55 -0
  610. data/ext/enclave/mruby/test/t/argumenterror.rb +37 -0
  611. data/ext/enclave/mruby/test/t/array.rb +478 -0
  612. data/ext/enclave/mruby/test/t/basicobject.rb +10 -0
  613. data/ext/enclave/mruby/test/t/bs_block.rb +621 -0
  614. data/ext/enclave/mruby/test/t/bs_literal.rb +38 -0
  615. data/ext/enclave/mruby/test/t/class.rb +505 -0
  616. data/ext/enclave/mruby/test/t/codegen.rb +196 -0
  617. data/ext/enclave/mruby/test/t/comparable.rb +79 -0
  618. data/ext/enclave/mruby/test/t/ensure.rb +36 -0
  619. data/ext/enclave/mruby/test/t/enumerable.rb +134 -0
  620. data/ext/enclave/mruby/test/t/exception.rb +425 -0
  621. data/ext/enclave/mruby/test/t/false.rb +31 -0
  622. data/ext/enclave/mruby/test/t/float.rb +296 -0
  623. data/ext/enclave/mruby/test/t/gc.rb +45 -0
  624. data/ext/enclave/mruby/test/t/hash.rb +976 -0
  625. data/ext/enclave/mruby/test/t/indexerror.rb +6 -0
  626. data/ext/enclave/mruby/test/t/integer.rb +247 -0
  627. data/ext/enclave/mruby/test/t/iterations.rb +61 -0
  628. data/ext/enclave/mruby/test/t/kernel.rb +462 -0
  629. data/ext/enclave/mruby/test/t/lang.rb +74 -0
  630. data/ext/enclave/mruby/test/t/literals.rb +386 -0
  631. data/ext/enclave/mruby/test/t/localjumperror.rb +13 -0
  632. data/ext/enclave/mruby/test/t/methods.rb +138 -0
  633. data/ext/enclave/mruby/test/t/module.rb +931 -0
  634. data/ext/enclave/mruby/test/t/nameerror.rb +28 -0
  635. data/ext/enclave/mruby/test/t/nil.rb +39 -0
  636. data/ext/enclave/mruby/test/t/nomethoderror.rb +22 -0
  637. data/ext/enclave/mruby/test/t/numeric.rb +114 -0
  638. data/ext/enclave/mruby/test/t/object.rb +10 -0
  639. data/ext/enclave/mruby/test/t/proc.rb +198 -0
  640. data/ext/enclave/mruby/test/t/range.rb +194 -0
  641. data/ext/enclave/mruby/test/t/rangeerror.rb +6 -0
  642. data/ext/enclave/mruby/test/t/regexperror.rb +4 -0
  643. data/ext/enclave/mruby/test/t/runtimeerror.rb +6 -0
  644. data/ext/enclave/mruby/test/t/standarderror.rb +6 -0
  645. data/ext/enclave/mruby/test/t/string.rb +981 -0
  646. data/ext/enclave/mruby/test/t/superclass.rb +46 -0
  647. data/ext/enclave/mruby/test/t/symbol.rb +30 -0
  648. data/ext/enclave/mruby/test/t/syntax.rb +1296 -0
  649. data/ext/enclave/mruby/test/t/true.rb +31 -0
  650. data/ext/enclave/mruby/test/t/typeerror.rb +6 -0
  651. data/ext/enclave/mruby/test/t/unicode.rb +39 -0
  652. data/ext/enclave/mruby/test/t/vformat.rb +57 -0
  653. data/ext/enclave/mruby/tools/lrama/LEGAL.md +12 -0
  654. data/ext/enclave/mruby/tools/lrama/MIT +21 -0
  655. data/ext/enclave/mruby/tools/lrama/NEWS.md +696 -0
  656. data/ext/enclave/mruby/tools/lrama/exe/lrama +7 -0
  657. data/ext/enclave/mruby/tools/lrama/lib/lrama/bitmap.rb +34 -0
  658. data/ext/enclave/mruby/tools/lrama/lib/lrama/command.rb +68 -0
  659. data/ext/enclave/mruby/tools/lrama/lib/lrama/context.rb +499 -0
  660. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples/derivation.rb +66 -0
  661. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples/example.rb +129 -0
  662. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples/path.rb +29 -0
  663. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples/production_path.rb +19 -0
  664. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples/start_path.rb +23 -0
  665. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples/state_item.rb +8 -0
  666. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples/transition_path.rb +19 -0
  667. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples/triple.rb +23 -0
  668. data/ext/enclave/mruby/tools/lrama/lib/lrama/counterexamples.rb +298 -0
  669. data/ext/enclave/mruby/tools/lrama/lib/lrama/diagnostics.rb +36 -0
  670. data/ext/enclave/mruby/tools/lrama/lib/lrama/digraph.rb +83 -0
  671. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/auxiliary.rb +9 -0
  672. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/binding.rb +67 -0
  673. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/code/destructor_code.rb +42 -0
  674. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/code/initial_action_code.rb +36 -0
  675. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/code/no_reference_code.rb +30 -0
  676. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/code/printer_code.rb +42 -0
  677. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/code/rule_action.rb +92 -0
  678. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/code.rb +53 -0
  679. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/counter.rb +17 -0
  680. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/destructor.rb +11 -0
  681. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/error_token.rb +11 -0
  682. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/parameterizing_rule/resolver.rb +62 -0
  683. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/parameterizing_rule/rhs.rb +40 -0
  684. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/parameterizing_rule/rule.rb +24 -0
  685. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/parameterizing_rule.rb +5 -0
  686. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/percent_code.rb +14 -0
  687. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/precedence.rb +13 -0
  688. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/printer.rb +11 -0
  689. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/reference.rb +16 -0
  690. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/rule.rb +75 -0
  691. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/rule_builder.rb +255 -0
  692. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/stdlib.y +122 -0
  693. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/symbol.rb +105 -0
  694. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/symbols/resolver.rb +301 -0
  695. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/symbols.rb +3 -0
  696. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/type.rb +20 -0
  697. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar/union.rb +12 -0
  698. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar.rb +407 -0
  699. data/ext/enclave/mruby/tools/lrama/lib/lrama/grammar_validator.rb +37 -0
  700. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer/grammar_file.rb +40 -0
  701. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer/location.rb +115 -0
  702. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer/token/char.rb +11 -0
  703. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer/token/ident.rb +11 -0
  704. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer/token/instantiate_rule.rb +30 -0
  705. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer/token/tag.rb +16 -0
  706. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer/token/user_code.rb +83 -0
  707. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer/token.rb +70 -0
  708. data/ext/enclave/mruby/tools/lrama/lib/lrama/lexer.rb +191 -0
  709. data/ext/enclave/mruby/tools/lrama/lib/lrama/logger.rb +21 -0
  710. data/ext/enclave/mruby/tools/lrama/lib/lrama/option_parser.rb +169 -0
  711. data/ext/enclave/mruby/tools/lrama/lib/lrama/options.rb +28 -0
  712. data/ext/enclave/mruby/tools/lrama/lib/lrama/output.rb +459 -0
  713. data/ext/enclave/mruby/tools/lrama/lib/lrama/parser.rb +2144 -0
  714. data/ext/enclave/mruby/tools/lrama/lib/lrama/report/duration.rb +27 -0
  715. data/ext/enclave/mruby/tools/lrama/lib/lrama/report/profile.rb +16 -0
  716. data/ext/enclave/mruby/tools/lrama/lib/lrama/report.rb +4 -0
  717. data/ext/enclave/mruby/tools/lrama/lib/lrama/state/reduce.rb +37 -0
  718. data/ext/enclave/mruby/tools/lrama/lib/lrama/state/reduce_reduce_conflict.rb +11 -0
  719. data/ext/enclave/mruby/tools/lrama/lib/lrama/state/resolved_conflict.rb +31 -0
  720. data/ext/enclave/mruby/tools/lrama/lib/lrama/state/shift.rb +15 -0
  721. data/ext/enclave/mruby/tools/lrama/lib/lrama/state/shift_reduce_conflict.rb +11 -0
  722. data/ext/enclave/mruby/tools/lrama/lib/lrama/state.rb +433 -0
  723. data/ext/enclave/mruby/tools/lrama/lib/lrama/states/item.rb +91 -0
  724. data/ext/enclave/mruby/tools/lrama/lib/lrama/states.rb +595 -0
  725. data/ext/enclave/mruby/tools/lrama/lib/lrama/states_reporter.rb +362 -0
  726. data/ext/enclave/mruby/tools/lrama/lib/lrama/trace_reporter.rb +45 -0
  727. data/ext/enclave/mruby/tools/lrama/lib/lrama/version.rb +5 -0
  728. data/ext/enclave/mruby/tools/lrama/lib/lrama.rb +22 -0
  729. data/ext/enclave/mruby/tools/lrama/template/bison/_yacc.h +71 -0
  730. data/ext/enclave/mruby/tools/lrama/template/bison/yacc.c +2068 -0
  731. data/ext/enclave/mruby/tools/lrama/template/bison/yacc.h +40 -0
  732. data/ext/enclave/sandbox_build_config.rb +15 -0
  733. data/ext/enclave/sandbox_core.c +722 -0
  734. data/ext/enclave/sandbox_core.h +87 -0
  735. data/lib/enclave/result.rb +29 -0
  736. data/lib/enclave/tool.rb +38 -0
  737. data/lib/enclave/version.rb +3 -0
  738. data/lib/enclave.rb +73 -0
  739. metadata +819 -0
@@ -0,0 +1,2420 @@
1
+ /*
2
+ ** numeric.c - Numeric, Integer, Float class
3
+ **
4
+ ** See Copyright Notice in mruby.h
5
+ */
6
+
7
+ #include <mruby.h>
8
+ #include <mruby/array.h>
9
+ #include <mruby/numeric.h>
10
+ #include <mruby/string.h>
11
+ #include <mruby/class.h>
12
+ #include <mruby/internal.h>
13
+ #include <mruby/presym.h>
14
+ #include <string.h>
15
+
16
+ #ifndef MRB_NO_FLOAT
17
+ #ifdef MRB_USE_FLOAT32
18
+ #define trunc(f) truncf(f)
19
+ #define fmod(x,y) fmodf(x,y)
20
+ #else
21
+ #endif
22
+ #endif
23
+
24
+ /**
25
+ * This function is called to raise a RangeError when an integer operation
26
+ * results in an overflow. It's marked mrb_noreturn as it always raises an
27
+ * exception and does not return.
28
+ *
29
+ * @param mrb The mruby state.
30
+ * @param reason A string describing the operation that caused the overflow
31
+ * (e.g., "addition", "multiplication").
32
+ */
33
+ mrb_noreturn void
34
+ mrb_int_overflow(mrb_state *mrb, const char *reason)
35
+ {
36
+ mrb_raisef(mrb, E_RANGE_ERROR, "integer overflow in %s", reason);
37
+ }
38
+
39
+ /**
40
+ * This function is called to raise a ZeroDivisionError. It's marked
41
+ * mrb_noreturn as it always raises an exception and does not return.
42
+ *
43
+ * @param mrb The mruby state.
44
+ */
45
+ mrb_noreturn void
46
+ mrb_int_zerodiv(mrb_state *mrb)
47
+ {
48
+ mrb_raise(mrb, E_ZERODIV_ERROR, "divided by 0");
49
+ }
50
+
51
+ static mrb_noreturn void
52
+ mrb_int_noconv(mrb_state *mrb, mrb_value y)
53
+ {
54
+ mrb_raisef(mrb, E_TYPE_ERROR, "can't convert %Y into Integer", y);
55
+ }
56
+
57
+ /**
58
+ * Calculates x raised to the power of y, where x is an integer.
59
+ * y can be an integer or float. The result type can be Integer,
60
+ * Float, or BigInt depending on the inputs and intermediate calculations.
61
+ *
62
+ * @param mrb The mruby state.
63
+ * @param x The base (must be an integer type, possibly BigInt).
64
+ * @param y The exponent (can be Integer or Float).
65
+ * @return An mrb_value representing the result of the exponentiation.
66
+ * This can be an Integer, Float, or BigInt.
67
+ * Handles potential overflows by promoting to BigInt if MRB_USE_BIGINT is defined,
68
+ * or by raising RangeError if not.
69
+ * Handles negative exponents by returning a Float if MRB_NO_FLOAT is not defined,
70
+ * or raising RangeError if it is.
71
+ */
72
+ mrb_value
73
+ mrb_int_pow(mrb_state *mrb, mrb_value x, mrb_value y)
74
+ {
75
+ #ifdef MRB_USE_BIGINT
76
+ if (mrb_bigint_p(x)) {
77
+ #ifndef MRB_NO_FLOAT
78
+ if (mrb_float_p(y)) {
79
+ return mrb_float_value(mrb, pow(mrb_bint_as_float(mrb, x), mrb_float(y)));
80
+ }
81
+ #endif
82
+ return mrb_bint_pow(mrb, x, y);
83
+ }
84
+ #endif
85
+ mrb_int base = mrb_integer(x);
86
+ mrb_int result = 1;
87
+ mrb_int exp;
88
+
89
+ #ifndef MRB_NO_FLOAT
90
+ if (mrb_float_p(y)) {
91
+ return mrb_float_value(mrb, pow((double)base, mrb_float(y)));
92
+ }
93
+ else if (mrb_integer_p(y)) {
94
+ exp = mrb_integer(y);
95
+ }
96
+ else
97
+ #endif
98
+ {
99
+ exp = mrb_as_int(mrb, y);
100
+ }
101
+ if (exp < 0) {
102
+ #ifndef MRB_NO_FLOAT
103
+ return mrb_float_value(mrb, pow((double)base, (double)exp));
104
+ #else
105
+ mrb_int_overflow(mrb, "negative power");
106
+ #endif
107
+ }
108
+ for (;;) {
109
+ if (exp & 1) {
110
+ if (mrb_int_mul_overflow(result, base, &result)) {
111
+ #ifdef MRB_USE_BIGINT
112
+ return mrb_bint_pow(mrb, mrb_bint_new_int(mrb, mrb_integer(x)), y);
113
+ #else
114
+ mrb_int_overflow(mrb, "power");
115
+ #endif
116
+ }
117
+ }
118
+ exp >>= 1;
119
+ if (exp == 0) break;
120
+ if (mrb_int_mul_overflow(base, base, &base)) {
121
+ #ifdef MRB_USE_BIGINT
122
+ return mrb_bint_pow(mrb, mrb_bint_new_int(mrb, mrb_integer(x)), y);
123
+ #else
124
+ mrb_int_overflow(mrb, "power");
125
+ #endif
126
+ }
127
+ }
128
+ return mrb_int_value(mrb, result);
129
+ }
130
+
131
+ /*
132
+ * call-seq:
133
+ *
134
+ * num ** other -> num
135
+ *
136
+ * Raises `num` the `other` power.
137
+ *
138
+ * 2.0**3 #=> 8.0
139
+ */
140
+ static mrb_value
141
+ int_pow(mrb_state *mrb, mrb_value x)
142
+ {
143
+ return mrb_int_pow(mrb, x, mrb_get_arg1(mrb));
144
+ }
145
+
146
+ /**
147
+ * Performs integer division of x by y. This function implements specific
148
+ * rounding behavior for negative numbers to match Ruby's / operator for
149
+ * integers (floor division).
150
+ *
151
+ * @param x The dividend.
152
+ * @param y The divisor.
153
+ * @return The result of the integer division (mrb_int).
154
+ * Note: This function does not handle division by zero; the caller is
155
+ * expected to check for this.
156
+ */
157
+ mrb_int
158
+ mrb_div_int(mrb_int x, mrb_int y)
159
+ {
160
+ mrb_int div = x / y;
161
+
162
+ if ((x ^ y) < 0 && x != div * y) {
163
+ div -= 1;
164
+ }
165
+ return div;
166
+ }
167
+
168
+ /**
169
+ * Performs integer division of x by y and returns the result as an mrb_value.
170
+ * It uses mrb_div_int for the division logic.
171
+ *
172
+ * @param mrb The mruby state.
173
+ * @param x The dividend.
174
+ * @param y The divisor.
175
+ * @return An mrb_value (integer) representing the result of the division.
176
+ * @raise ZeroDivisionError if y is 0.
177
+ * @raise RangeError for overflow conditions (specifically MRB_INT_MIN / -1).
178
+ */
179
+ mrb_value
180
+ mrb_div_int_value(mrb_state *mrb, mrb_int x, mrb_int y)
181
+ {
182
+ if (y == 0) {
183
+ mrb_int_zerodiv(mrb);
184
+ }
185
+ else if (x == MRB_INT_MIN && y == -1) {
186
+ #ifdef MRB_USE_BIGINT
187
+ return mrb_bint_mul_ii(mrb, x, y);
188
+ #else
189
+ mrb_int_overflow(mrb, "division");
190
+ #endif
191
+ }
192
+ return mrb_int_value(mrb, mrb_div_int(x, y));
193
+ }
194
+
195
+ /* 15.2.8.3.6 */
196
+ /*
197
+ * call-seq:
198
+ * int / num -> num
199
+ *
200
+ * Performs division: the class of the resulting object depends on
201
+ * the class of `num` and on the magnitude of the
202
+ * result.
203
+ */
204
+ static mrb_value
205
+ int_div(mrb_state *mrb, mrb_value x)
206
+ {
207
+ mrb_value y = mrb_get_arg1(mrb);
208
+ #ifdef MRB_USE_BIGINT
209
+ if (mrb_bigint_p(x)) {
210
+ if (mrb_bigint_p(y) || mrb_integer_p(y)) {
211
+ return mrb_bint_div(mrb, x, y);
212
+ }
213
+ } else
214
+ #endif
215
+ if (mrb_integer_p(y)) {
216
+ return mrb_div_int_value(mrb, mrb_integer(x), mrb_integer(y));
217
+ }
218
+ switch (mrb_type(y)) {
219
+ #ifdef MRB_USE_BIGINT
220
+ case MRB_TT_INTEGER:
221
+ case MRB_TT_BIGINT:
222
+ return mrb_bint_div(mrb, mrb_as_bint(mrb, x), y);
223
+ #endif
224
+ #ifdef MRB_USE_RATIONAL
225
+ case MRB_TT_RATIONAL:
226
+ return mrb_rational_div(mrb, mrb_as_rational(mrb, x), y);
227
+ #endif
228
+ #ifdef MRB_USE_COMPLEX
229
+ case MRB_TT_COMPLEX:
230
+ x = mrb_complex_new(mrb, mrb_as_float(mrb, x), 0);
231
+ return mrb_complex_div(mrb, x, y);
232
+ #endif
233
+ #ifndef MRB_NO_FLOAT
234
+ case MRB_TT_FLOAT:
235
+ return mrb_float_value(mrb, mrb_div_float(mrb_as_float(mrb, x), mrb_as_float(mrb, y)));
236
+ #endif
237
+ default:
238
+ mrb_int_noconv(mrb, y);
239
+ }
240
+ }
241
+
242
+ /* 15.2.9.3.19(x) */
243
+ /*
244
+ * call-seq:
245
+ * num.quo(numeric) -> real
246
+ *
247
+ * Returns most exact division.
248
+ */
249
+
250
+ /*
251
+ * call-seq:
252
+ * int.div(other) -> int
253
+ *
254
+ * Performs division: resulting integer.
255
+ */
256
+ static mrb_value
257
+ int_idiv(mrb_state *mrb, mrb_value x)
258
+ {
259
+ #ifdef MRB_USE_BIGINT
260
+ if (mrb_bigint_p(x)) {
261
+ return mrb_bint_div(mrb, x, mrb_get_arg1(mrb));
262
+ }
263
+ #endif
264
+ mrb_int y = mrb_as_int(mrb, mrb_get_arg1(mrb));
265
+ return mrb_div_int_value(mrb, mrb_integer(x), y);
266
+ }
267
+
268
+ #ifndef MRB_NO_FLOAT
269
+ static mrb_value
270
+ int_fdiv(mrb_state *mrb, mrb_value x)
271
+ {
272
+ mrb_float y = mrb_as_float(mrb, mrb_get_arg1(mrb));
273
+
274
+ if (y == 0) {
275
+ mrb_int_zerodiv(mrb);
276
+ }
277
+ #ifdef MRB_USE_BIGINT
278
+ if (mrb_bigint_p(x)) {
279
+ return mrb_float_value(mrb, mrb_bint_as_float(mrb, x) / y);
280
+ }
281
+ #endif
282
+ return mrb_float_value(mrb, mrb_integer(x) / y);
283
+ }
284
+ #endif
285
+
286
+ static mrb_value
287
+ int_quo(mrb_state *mrb, mrb_value x)
288
+ {
289
+ #ifndef MRB_USE_RATIONAL
290
+
291
+ #ifdef MRB_NO_FLOAT
292
+ return int_idiv(mrb, x);
293
+ #else
294
+ return int_fdiv(mrb, x);
295
+ #endif
296
+
297
+ #else
298
+ mrb_int a = mrb_integer(x);
299
+ mrb_value y = mrb_get_arg1(mrb);
300
+ if (mrb_integer_p(y) && mrb_class_defined_id(mrb, MRB_SYM(Rational))) {
301
+ return mrb_rational_new(mrb, a, mrb_integer(y));
302
+ }
303
+ switch (mrb_type(y)) {
304
+ case MRB_TT_RATIONAL:
305
+ x = mrb_rational_new(mrb, a, 1);
306
+ return mrb_rational_div(mrb, x, y);
307
+ default:
308
+ #ifndef MRB_NO_FLOAT
309
+ return mrb_float_value(mrb, mrb_div_float((mrb_float)a, mrb_as_float(mrb, y)));
310
+ #else
311
+ mrb_int_noconv(mrb, y);
312
+ break;
313
+ #endif
314
+ }
315
+ #endif
316
+ }
317
+
318
+ static mrb_value
319
+ coerce_step_counter(mrb_state *mrb, mrb_value self)
320
+ {
321
+ mrb->c->ci->mid = 0;
322
+ #ifndef MRB_NO_FLOAT
323
+ mrb_value step = mrb_get_arg1(mrb);
324
+ if (mrb_float_p(step)) {
325
+ return mrb_ensure_float_type(mrb, self);
326
+ }
327
+ #endif
328
+ return self;
329
+ }
330
+
331
+ #ifndef MRB_NO_FLOAT
332
+ /********************************************************************
333
+ *
334
+ * Document-class: Float
335
+ *
336
+ * `Float` objects represent inexact real numbers using
337
+ * the native architecture's double-precision floating-point
338
+ * representation.
339
+ */
340
+
341
+ static mrb_value
342
+ flo_pow(mrb_state *mrb, mrb_value x)
343
+ {
344
+ mrb_value y = mrb_get_arg1(mrb);
345
+ mrb_float d = pow(mrb_as_float(mrb, x), mrb_as_float(mrb, y));
346
+ return mrb_float_value(mrb, d);
347
+ }
348
+
349
+ static mrb_value
350
+ flo_idiv(mrb_state *mrb, mrb_value xv)
351
+ {
352
+ mrb_float x = mrb_float(xv);
353
+ mrb_check_num_exact(mrb, x);
354
+ mrb_int y = mrb_as_int(mrb, mrb_get_arg1(mrb));
355
+ return mrb_div_int_value(mrb, (mrb_int)x, y);
356
+ }
357
+
358
+ mrb_float
359
+ mrb_div_float(mrb_float x, mrb_float y)
360
+ {
361
+ if (y != 0.0) {
362
+ return x / y;
363
+ }
364
+ else if (x == 0.0) {
365
+ return NAN;
366
+ }
367
+ else {
368
+ return x * (signbit(y) ? -1.0 : 1.0) * INFINITY;
369
+ }
370
+ }
371
+
372
+ /* 15.2.9.3.6 */
373
+ /*
374
+ * call-seq:
375
+ * float / num -> float
376
+ *
377
+ * Returns a new Float which is the result of dividing float by num.
378
+ */
379
+ static mrb_value
380
+ flo_div(mrb_state *mrb, mrb_value x)
381
+ {
382
+ mrb_value y = mrb_get_arg1(mrb);
383
+ mrb_float a = mrb_float(x);
384
+
385
+ switch(mrb_type(y)) {
386
+ #ifdef MRB_USE_COMPLEX
387
+ case MRB_TT_COMPLEX:
388
+ return mrb_complex_div(mrb, mrb_complex_new(mrb, a, 0), y);
389
+ #endif
390
+ case MRB_TT_FLOAT:
391
+ a = mrb_div_float(a, mrb_float(y));
392
+ return mrb_float_value(mrb, a);
393
+ default:
394
+ a = mrb_div_float(a, mrb_as_float(mrb, y));
395
+ return mrb_float_value(mrb, a);
396
+ }
397
+ return mrb_float_value(mrb, a);
398
+ }
399
+
400
+ static mrb_value
401
+ num_fdiv(mrb_state *mrb, mrb_value x)
402
+ {
403
+ return flo_div(mrb, mrb_ensure_float_type(mrb, x));
404
+ }
405
+
406
+ /**
407
+ * Converts an mrb_value float to a new mrb_value string.
408
+ * It handles formatting to ensure the string representation includes a
409
+ * decimal point and fractional part (e.g., ".0" is appended if not present).
410
+ *
411
+ * @param mrb The mruby state.
412
+ * @param flo The float mrb_value to convert.
413
+ * @param fmt This argument is noted as no longer used and can be NULL.
414
+ * The function uses a default format.
415
+ * @return A new mrb_value string representing the float.
416
+ */
417
+ /* the argument `fmt` is no longer used; you can pass `NULL` */
418
+ mrb_value
419
+ mrb_float_to_str(mrb_state *mrb, mrb_value flo, const char *fmt)
420
+ {
421
+ char buf[25];
422
+ #ifdef MRB_USE_FLOAT32
423
+ const int prec = 7;
424
+ #else
425
+ const int prec = 15;
426
+ #endif
427
+
428
+ mrb_format_float(mrb_float(flo), buf, sizeof(buf), 'g', prec, '\0');
429
+ for (char *p = buf; *p; p++) {
430
+ if (*p == '.') goto exit;
431
+ if (*p == 'e') {
432
+ memmove(p+2, p, strlen(p)+1);
433
+ p[0] = '.';
434
+ p[1] = '0';
435
+ goto exit;
436
+ }
437
+ }
438
+ strcat(buf, ".0");
439
+ exit:
440
+ return mrb_str_new_cstr(mrb, buf);
441
+ }
442
+
443
+ /* 15.2.9.3.16(x) */
444
+ /*
445
+ * call-seq:
446
+ * flt.to_s -> string
447
+ * flt.inspect -> string
448
+ *
449
+ * Returns a string containing a representation of self. As well as a
450
+ * fixed or exponential form of the number, the call may return
451
+ * "`NaN`", "`Infinity`", and
452
+ * "`-Infinity`".
453
+ *
454
+ * 3.0.to_s #=> 3.0
455
+ * 3.25.to_s #=> 3.25
456
+ */
457
+
458
+ static mrb_value
459
+ flo_to_s(mrb_state *mrb, mrb_value flt)
460
+ {
461
+ mrb_float f = mrb_float(flt);
462
+ mrb_value str;
463
+
464
+ if (isinf(f)) {
465
+ str = f < 0 ? mrb_str_new_lit(mrb, "-Infinity")
466
+ : mrb_str_new_lit(mrb, "Infinity");
467
+ }
468
+ else if (isnan(f)) {
469
+ str = mrb_str_new_lit(mrb, "NaN");
470
+ }
471
+ else {
472
+ str = mrb_float_to_str(mrb, flt, NULL);
473
+ }
474
+
475
+ RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
476
+ return str;
477
+ }
478
+
479
+ /* 15.2.9.3.3 */
480
+ /*
481
+ * call-seq:
482
+ * float + other -> float
483
+ *
484
+ * Returns a new float which is the sum of `float`
485
+ * and `other`.
486
+ */
487
+ static mrb_value
488
+ flo_add(mrb_state *mrb, mrb_value x)
489
+ {
490
+ mrb_value y = mrb_get_arg1(mrb);
491
+ mrb_float a = mrb_float(x);
492
+
493
+ switch (mrb_type(y)) {
494
+ case MRB_TT_FLOAT:
495
+ return mrb_float_value(mrb, a + mrb_float(y));
496
+ #if defined(MRB_USE_COMPLEX)
497
+ case MRB_TT_COMPLEX:
498
+ return mrb_complex_add(mrb, y, x);
499
+ #endif
500
+ default:
501
+ return mrb_float_value(mrb, a + mrb_as_float(mrb, y));
502
+ }
503
+ }
504
+
505
+ /* 15.2.9.3.4 */
506
+ /*
507
+ * call-seq:
508
+ * float - other -> float
509
+ *
510
+ * Returns a new float which is the difference of `float`
511
+ * and `other`.
512
+ */
513
+
514
+ static mrb_value
515
+ flo_sub(mrb_state *mrb, mrb_value x)
516
+ {
517
+ mrb_value y = mrb_get_arg1(mrb);
518
+ mrb_float a = mrb_float(x);
519
+
520
+ switch (mrb_type(y)) {
521
+ case MRB_TT_FLOAT:
522
+ return mrb_float_value(mrb, a - mrb_float(y));
523
+ #if defined(MRB_USE_COMPLEX)
524
+ case MRB_TT_COMPLEX:
525
+ return mrb_complex_sub(mrb, mrb_complex_new(mrb, a, 0), y);
526
+ #endif
527
+ default:
528
+ return mrb_float_value(mrb, a - mrb_as_float(mrb, y));
529
+ }
530
+ }
531
+
532
+ /* 15.2.9.3.5 */
533
+ /*
534
+ * call-seq:
535
+ * float * other -> float
536
+ *
537
+ * Returns a new float which is the product of `float`
538
+ * and `other`.
539
+ */
540
+
541
+ static mrb_value
542
+ flo_mul(mrb_state *mrb, mrb_value x)
543
+ {
544
+ mrb_value y = mrb_get_arg1(mrb);
545
+ mrb_float a = mrb_float(x);
546
+
547
+ switch (mrb_type(y)) {
548
+ case MRB_TT_FLOAT:
549
+ return mrb_float_value(mrb, a * mrb_float(y));
550
+ #if defined(MRB_USE_COMPLEX)
551
+ case MRB_TT_COMPLEX:
552
+ return mrb_complex_mul(mrb, y, x);
553
+ #endif
554
+ default:
555
+ return mrb_float_value(mrb, a * mrb_as_float(mrb, y));
556
+ }
557
+ }
558
+
559
+ static void
560
+ flodivmod(mrb_state *mrb, double x, double y, mrb_float *divp, mrb_float *modp)
561
+ {
562
+ double div, mod;
563
+
564
+ if (isnan(y)) {
565
+ /* y is NaN so all results are NaN */
566
+ div = mod = y;
567
+ goto exit;
568
+ }
569
+ if (y == 0.0) {
570
+ mrb_int_zerodiv(mrb);
571
+ }
572
+ if (isinf(y) && !isinf(x)) {
573
+ mod = x;
574
+ }
575
+ else {
576
+ mod = fmod(x, y);
577
+ }
578
+ if (isinf(x) && !isinf(y)) {
579
+ div = x;
580
+ }
581
+ else {
582
+ div = (x - mod) / y;
583
+ if (modp && divp) div = round(div);
584
+ }
585
+ if (div == 0) div = 0.0;
586
+ if (mod == 0) mod = 0.0;
587
+ if (y*mod < 0) {
588
+ mod += y;
589
+ div -= 1.0;
590
+ }
591
+ exit:
592
+ if (modp) *modp = mod;
593
+ if (divp) *divp = div;
594
+ }
595
+
596
+ /* 15.2.9.3.5 */
597
+ /*
598
+ * call-seq:
599
+ * flt % other -> float
600
+ * flt.modulo(other) -> float
601
+ *
602
+ * Return the modulo after division of `flt` by `other`.
603
+ *
604
+ * 6543.21.modulo(137) #=> 104.21
605
+ * 6543.21.modulo(137.24) #=> 92.9299999999996
606
+ */
607
+
608
+ static mrb_value
609
+ flo_mod(mrb_state *mrb, mrb_value x)
610
+ {
611
+ mrb_value y = mrb_get_arg1(mrb);
612
+ mrb_float mod;
613
+
614
+ flodivmod(mrb, mrb_float(x), mrb_as_float(mrb, y), NULL, &mod);
615
+ return mrb_float_value(mrb, mod);
616
+ }
617
+ #endif
618
+
619
+ /* 15.2.8.3.16 */
620
+ /*
621
+ * call-seq:
622
+ * num.eql?(numeric) -> true or false
623
+ *
624
+ * Returns `true` if `num` and `numeric` are the
625
+ * same type and have equal values.
626
+ *
627
+ * 1 == 1.0 #=> true
628
+ * 1.eql?(1.0) #=> false
629
+ * (1.0).eql?(1.0) #=> true
630
+ */
631
+ static mrb_value
632
+ num_eql(mrb_state *mrb, mrb_value x)
633
+ {
634
+ mrb_value y = mrb_get_arg1(mrb);
635
+
636
+ #ifdef MRB_USE_BIGINT
637
+ if (mrb_bigint_p(x)) {
638
+ return mrb_bool_value(mrb_bint_cmp(mrb, x, y) == 0);
639
+ }
640
+ #endif
641
+ #ifndef MRB_NO_FLOAT
642
+ if (mrb_float_p(x)) {
643
+ if (!mrb_float_p(y)) return mrb_false_value();
644
+ return mrb_bool_value(mrb_float(x) == mrb_float(y));
645
+ }
646
+ #endif
647
+ if (mrb_integer_p(x)) {
648
+ if (!mrb_integer_p(y)) return mrb_false_value();
649
+ return mrb_bool_value(mrb_integer(x) == mrb_integer(y));
650
+ }
651
+ return mrb_bool_value(mrb_equal(mrb, x, y));
652
+ }
653
+
654
+ #ifndef MRB_NO_FLOAT
655
+ /* 15.2.9.3.7 */
656
+ /*
657
+ * call-seq:
658
+ * flt == obj -> true or false
659
+ *
660
+ * Returns `true` only if *obj* has the same value
661
+ * as *flt*. Contrast this with `Float#eql?`, which
662
+ * requires *obj* to be a `Float`.
663
+ *
664
+ * 1.0 == 1 #=> true
665
+ *
666
+ */
667
+
668
+ static mrb_value
669
+ flo_eq(mrb_state *mrb, mrb_value x)
670
+ {
671
+ mrb_value y = mrb_get_arg1(mrb);
672
+
673
+ switch (mrb_type(y)) {
674
+ case MRB_TT_INTEGER:
675
+ return mrb_bool_value(mrb_float(x) == (mrb_float)mrb_integer(y));
676
+ case MRB_TT_FLOAT:
677
+ return mrb_bool_value(mrb_float(x) == mrb_float(y));
678
+ #ifdef MRB_USE_RATIONAL
679
+ case MRB_TT_RATIONAL:
680
+ return mrb_bool_value(mrb_float(x) == mrb_as_float(mrb, y));
681
+ #endif
682
+ #ifdef MRB_USE_COMPLEX
683
+ case MRB_TT_COMPLEX:
684
+ return mrb_bool_value(mrb_equal(mrb, y, x));
685
+ #endif
686
+ default:
687
+ return mrb_false_value();
688
+ }
689
+ }
690
+
691
+ /* 15.2.9.3.13 */
692
+ /*
693
+ * Document-method: Float#to_f
694
+ *
695
+ * call-seq:
696
+ * flt.to_f -> self
697
+ *
698
+ * As `flt` is already a float, returns `self`.
699
+ */
700
+
701
+ /* 15.2.9.3.11 */
702
+ /*
703
+ * call-seq:
704
+ * flt.infinite? -> nil, -1, +1
705
+ *
706
+ * Returns `nil`, -1, or +1 depending on whether *flt*
707
+ * is finite, -infinity, or +infinity.
708
+ *
709
+ * (0.0).infinite? #=> nil
710
+ * (-1.0/0.0).infinite? #=> -1
711
+ * (+1.0/0.0).infinite? #=> 1
712
+ */
713
+
714
+ static mrb_value
715
+ flo_infinite_p(mrb_state *mrb, mrb_value num)
716
+ {
717
+ mrb_float value = mrb_float(num);
718
+
719
+ if (isinf(value)) {
720
+ return mrb_fixnum_value(value < 0 ? -1 : 1);
721
+ }
722
+ return mrb_nil_value();
723
+ }
724
+
725
+ /* 15.2.9.3.9 */
726
+ /*
727
+ * call-seq:
728
+ * flt.finite? -> true or false
729
+ *
730
+ * Returns `true` if *flt* is a valid IEEE floating
731
+ * point number (it is not infinite, and `nan?` is
732
+ * `false`).
733
+ *
734
+ */
735
+
736
+ static mrb_value
737
+ flo_finite_p(mrb_state *mrb, mrb_value num)
738
+ {
739
+ return mrb_bool_value(isfinite(mrb_float(num)));
740
+ }
741
+
742
+ /*
743
+ * Document-class: FloatDomainError
744
+ *
745
+ * Raised when attempting to convert special float values
746
+ * (in particular infinite or NaN)
747
+ * to numerical classes which don't support them.
748
+ *
749
+ * Float::INFINITY.to_i
750
+ *
751
+ * <em>raises the exception:</em>
752
+ *
753
+ * FloatDomainError: Infinity
754
+ */
755
+ /* ------------------------------------------------------------------------*/
756
+ /**
757
+ * Checks if a mrb_float value is Infinity or NaN. If it is, this function
758
+ * raises a FloatDomainError. This is used to prevent conversions of these
759
+ * special float values to exact number types like Integer.
760
+ *
761
+ * @param mrb The mruby state.
762
+ * @param num The float value to check.
763
+ * It does not return a value (void function) but will raise an exception
764
+ * if the number is not exact.
765
+ */
766
+ void
767
+ mrb_check_num_exact(mrb_state *mrb, mrb_float num)
768
+ {
769
+ if (isinf(num)) {
770
+ mrb_raise(mrb, E_FLOATDOMAIN_ERROR, num < 0 ? "-Infinity" : "Infinity");
771
+ }
772
+ if (isnan(num)) {
773
+ mrb_raise(mrb, E_FLOATDOMAIN_ERROR, "NaN");
774
+ }
775
+ }
776
+
777
+ static mrb_value
778
+ flo_rounding_int(mrb_state *mrb, mrb_float f)
779
+ {
780
+ if (!FIXABLE_FLOAT(f)) {
781
+ #ifdef MRB_USE_BIGINT
782
+ return mrb_bint_new_float(mrb, f);
783
+ #else
784
+ mrb_int_overflow(mrb, "rounding");
785
+ #endif
786
+ }
787
+ return mrb_int_value(mrb, (mrb_int)f);
788
+ }
789
+
790
+ static mrb_value
791
+ flo_rounding(mrb_state *mrb, mrb_value num, double (*func)(double))
792
+ {
793
+ mrb_float f = mrb_float(num);
794
+ mrb_int ndigits = 0;
795
+ #ifdef MRB_USE_FLOAT32
796
+ const int fprec = 7;
797
+ #else
798
+ const int fprec = 15;
799
+ #endif
800
+
801
+ mrb_get_args(mrb, "|i", &ndigits);
802
+ if (f == 0.0) {
803
+ return ndigits > 0 ? mrb_float_value(mrb, f) : mrb_fixnum_value(0);
804
+ }
805
+ if (ndigits > 0) {
806
+ if (ndigits > fprec) return num;
807
+ mrb_float d = pow(10, (double)ndigits);
808
+ f = func(f * d) / d;
809
+ mrb_check_num_exact(mrb, f);
810
+ return mrb_float_value(mrb, f);
811
+ }
812
+ if (ndigits < 0) {
813
+ mrb_float d = pow(10, -(double)ndigits);
814
+ f = func(f / d) * d;
815
+ }
816
+ else { /* ndigits == 0 */
817
+ f = func(f);
818
+ }
819
+ mrb_check_num_exact(mrb, f);
820
+ return flo_rounding_int(mrb, f);
821
+ }
822
+
823
+ /* 15.2.9.3.10 */
824
+ /*
825
+ * call-seq:
826
+ * float.floor([ndigits]) -> integer or float
827
+ *
828
+ * Returns the largest number less than or equal to `float` with
829
+ * a precision of `ndigits` decimal digits (default: 0).
830
+ *
831
+ * When the precision is negative, the returned value is an integer
832
+ * with at least `ndigits.abs` trailing zeros.
833
+ *
834
+ * Returns a floating-point number when `ndigits` is positive,
835
+ * otherwise returns an integer.
836
+ *
837
+ * 1.2.floor #=> 1
838
+ * 2.0.floor #=> 2
839
+ * (-1.2).floor #=> -2
840
+ * (-2.0).floor #=> -2
841
+ *
842
+ * 1.234567.floor(2) #=> 1.23
843
+ * 1.234567.floor(3) #=> 1.234
844
+ * 1.234567.floor(4) #=> 1.2345
845
+ * 1.234567.floor(5) #=> 1.23456
846
+ *
847
+ * 34567.89.floor(-5) #=> 0
848
+ * 34567.89.floor(-4) #=> 30000
849
+ * 34567.89.floor(-3) #=> 34000
850
+ * 34567.89.floor(-2) #=> 34500
851
+ * 34567.89.floor(-1) #=> 34560
852
+ * 34567.89.floor(0) #=> 34567
853
+ * 34567.89.floor(1) #=> 34567.8
854
+ * 34567.89.floor(2) #=> 34567.89
855
+ * 34567.89.floor(3) #=> 34567.89
856
+ *
857
+ * Note that the limited precision of floating-point arithmetic
858
+ * might lead to surprising results:
859
+ *
860
+ * (0.3 / 0.1).floor #=> 2 (!)
861
+ */
862
+ static mrb_value
863
+ flo_floor(mrb_state *mrb, mrb_value num)
864
+ {
865
+ return flo_rounding(mrb, num, floor);
866
+ }
867
+
868
+ /* 15.2.9.3.8 */
869
+ /*
870
+ * call-seq:
871
+ * float.ceil([ndigits]) -> integer or float
872
+ *
873
+ * Returns the smallest number greater than or equal to `float` with
874
+ * a precision of `ndigits` decimal digits (default: 0).
875
+ *
876
+ * When the precision is negative, the returned value is an integer
877
+ * with at least `ndigits.abs` trailing zeros.
878
+ *
879
+ * Returns a floating-point number when `ndigits` is positive,
880
+ * otherwise returns an integer.
881
+ *
882
+ * 1.2.ceil #=> 2
883
+ * 2.0.ceil #=> 2
884
+ * (-1.2).ceil #=> -1
885
+ * (-2.0).ceil #=> -2
886
+ *
887
+ * 1.234567.ceil(2) #=> 1.24
888
+ * 1.234567.ceil(3) #=> 1.235
889
+ * 1.234567.ceil(4) #=> 1.2346
890
+ * 1.234567.ceil(5) #=> 1.23457
891
+ *
892
+ * 34567.89.ceil(-5) #=> 100000
893
+ * 34567.89.ceil(-4) #=> 40000
894
+ * 34567.89.ceil(-3) #=> 35000
895
+ * 34567.89.ceil(-2) #=> 34600
896
+ * 34567.89.ceil(-1) #=> 34570
897
+ * 34567.89.ceil(0) #=> 34568
898
+ * 34567.89.ceil(1) #=> 34567.9
899
+ * 34567.89.ceil(2) #=> 34567.89
900
+ * 34567.89.ceil(3) #=> 34567.89
901
+ *
902
+ * Note that the limited precision of floating-point arithmetic
903
+ * might lead to surprising results:
904
+ *
905
+ * (2.1 / 0.7).ceil #=> 4 (!)
906
+ */
907
+
908
+ static mrb_value
909
+ flo_ceil(mrb_state *mrb, mrb_value num)
910
+ {
911
+ return flo_rounding(mrb, num, ceil);
912
+ }
913
+
914
+ /* 15.2.9.3.12 */
915
+ /*
916
+ * call-seq:
917
+ * flt.round([ndigits]) -> integer or float
918
+ *
919
+ * Rounds *flt* to a given precision in decimal digits (default 0 digits).
920
+ * Precision may be negative. Returns a floating-point number when ndigits
921
+ * is more than zero.
922
+ *
923
+ * 1.4.round #=> 1
924
+ * 1.5.round #=> 2
925
+ * 1.6.round #=> 2
926
+ * (-1.5).round #=> -2
927
+ *
928
+ * 1.234567.round(2) #=> 1.23
929
+ * 1.234567.round(3) #=> 1.235
930
+ * 1.234567.round(4) #=> 1.2346
931
+ * 1.234567.round(5) #=> 1.23457
932
+ *
933
+ * 34567.89.round(-5) #=> 0
934
+ * 34567.89.round(-4) #=> 30000
935
+ * 34567.89.round(-3) #=> 35000
936
+ * 34567.89.round(-2) #=> 34600
937
+ * 34567.89.round(-1) #=> 34570
938
+ * 34567.89.round(0) #=> 34568
939
+ * 34567.89.round(1) #=> 34567.9
940
+ * 34567.89.round(2) #=> 34567.89
941
+ * 34567.89.round(3) #=> 34567.89
942
+ *
943
+ */
944
+
945
+ static mrb_value
946
+ flo_round(mrb_state *mrb, mrb_value num)
947
+ {
948
+ double number, f;
949
+ mrb_int ndigits = 0;
950
+
951
+ mrb_get_args(mrb, "|i", &ndigits);
952
+ number = mrb_float(num);
953
+
954
+ if (0 < ndigits && (isinf(number) || isnan(number))) {
955
+ return num;
956
+ }
957
+ mrb_check_num_exact(mrb, number);
958
+
959
+ f = 1.0;
960
+ if (ndigits < -DBL_DIG-2) return mrb_fixnum_value(0);
961
+
962
+ mrb_int i = ndigits >= 0 ? ndigits : -ndigits;
963
+ if (ndigits > DBL_DIG+2) return num;
964
+ while (--i >= 0)
965
+ f = f*10.0;
966
+
967
+ if (isinf(f)) {
968
+ if (ndigits < 0) number = 0;
969
+ }
970
+ else {
971
+ double d;
972
+
973
+ if (ndigits < 0) number /= f;
974
+ else number *= f;
975
+
976
+ /* home-made inline implementation of round(3) */
977
+ if (number > 0.0) {
978
+ d = floor(number);
979
+ number = d + (number - d >= 0.5);
980
+ }
981
+ else if (number < 0.0) {
982
+ d = ceil(number);
983
+ number = d - (d - number >= 0.5);
984
+ }
985
+
986
+ if (ndigits < 0) number *= f;
987
+ else number /= f;
988
+ }
989
+
990
+ if (ndigits > 0) {
991
+ if (!isfinite(number)) return num;
992
+ return mrb_float_value(mrb, number);
993
+ }
994
+ if (!FIXABLE_FLOAT(number))
995
+ return mrb_float_value(mrb, number);
996
+ return mrb_int_value(mrb, (mrb_int)number);
997
+ }
998
+
999
+ /* 15.2.9.3.14 */
1000
+ static mrb_value
1001
+ flo_to_i(mrb_state *mrb, mrb_value num)
1002
+ {
1003
+ mrb_float f = mrb_float(num);
1004
+
1005
+ mrb_check_num_exact(mrb, f);
1006
+ if (!FIXABLE_FLOAT(f)) {
1007
+ #ifdef MRB_USE_BIGINT
1008
+ return mrb_bint_new_float(mrb, f);
1009
+ #else
1010
+ mrb_int_overflow(mrb, "to_f");
1011
+ #endif
1012
+ }
1013
+ if (f > 0.0) f = floor(f);
1014
+ if (f < 0.0) f = ceil(f);
1015
+
1016
+ return mrb_int_value(mrb, (mrb_int)f);
1017
+ }
1018
+
1019
+ /* 15.2.9.3.15 */
1020
+ /*
1021
+ * call-seq:
1022
+ * flt.to_i -> integer
1023
+ * flt.truncate -> integer
1024
+ *
1025
+ * Returns *flt* truncated to an `Integer`.
1026
+ */
1027
+
1028
+ static mrb_value
1029
+ flo_truncate(mrb_state *mrb, mrb_value num)
1030
+ {
1031
+ if (signbit(mrb_float(num))) return flo_ceil(mrb, num);
1032
+ return flo_floor(mrb, num);
1033
+ }
1034
+
1035
+ static mrb_value
1036
+ flo_nan_p(mrb_state *mrb, mrb_value num)
1037
+ {
1038
+ return mrb_bool_value(isnan(mrb_float(num)));
1039
+ }
1040
+
1041
+ static mrb_value
1042
+ flo_abs(mrb_state *mrb, mrb_value num)
1043
+ {
1044
+ mrb_float f = mrb_float(num);
1045
+
1046
+ if (signbit(f)) return mrb_float_value(mrb, -f);
1047
+ return num;
1048
+ }
1049
+ #endif
1050
+
1051
+ /*
1052
+ * Document-class: Integer
1053
+ *
1054
+ * `Integer` is hold whole numbers.
1055
+ *
1056
+ */
1057
+
1058
+ /* 15.2.9.3.24 */
1059
+ /*
1060
+ * Document-method: Integer#to_i
1061
+ * Document-method: Integer#to_int
1062
+ *
1063
+ * call-seq:
1064
+ * int.to_i -> integer
1065
+ * int.to_int -> integer
1066
+ *
1067
+ * As *int* is already an `Integer`, all these
1068
+ * methods simply return the receiver.
1069
+ */
1070
+
1071
+ /**
1072
+ * Multiplies two mrb_values, x and y, where x is expected to be an integer.
1073
+ * y can be an integer, BigInt, Rational, Complex, or Float. The function
1074
+ * handles type promotion and dispatches to appropriate handlers
1075
+ * (e.g., mrb_bint_mul for BigInts).
1076
+ *
1077
+ * @param mrb The mruby state.
1078
+ * @param x The first operand (integer).
1079
+ * @param y The second operand (can be various numeric types).
1080
+ * @return An mrb_value representing the product. The type of the result
1081
+ * depends on the types of the inputs and the magnitude of the result
1082
+ * (e.g., could be Integer, BigInt, Float, Rational, Complex).
1083
+ * Handles potential integer overflows by promoting to BigInt if MRB_USE_BIGINT
1084
+ * is defined, or raising RangeError otherwise.
1085
+ * If y is not a recognized numeric type, it raises E_TYPE_ERROR.
1086
+ */
1087
+ mrb_value
1088
+ mrb_int_mul(mrb_state *mrb, mrb_value x, mrb_value y)
1089
+ {
1090
+ mrb_int a = mrb_integer(x);
1091
+
1092
+ if (mrb_integer_p(y)) {
1093
+ mrb_int b, c;
1094
+
1095
+ if (a == 0) return x;
1096
+ if (a == 1) return y;
1097
+ b = mrb_integer(y);
1098
+ if (b == 0) return y;
1099
+ if (b == 1) return x;
1100
+ if (mrb_int_mul_overflow(a, b, &c)) {
1101
+ #ifdef MRB_USE_BIGINT
1102
+ x = mrb_bint_new_int(mrb, a);
1103
+ return mrb_bint_mul(mrb, x, y);
1104
+ #else
1105
+ mrb_int_overflow(mrb, "multiplication");
1106
+ #endif
1107
+ }
1108
+ return mrb_int_value(mrb, c);
1109
+ }
1110
+ switch (mrb_type(y)) {
1111
+ #ifdef MRB_USE_BIGINT
1112
+ case MRB_TT_BIGINT:
1113
+ if (a == 0) return x;
1114
+ if (a == 1) return y;
1115
+ return mrb_bint_mul(mrb, y, x);
1116
+ #endif
1117
+ #ifdef MRB_USE_RATIONAL
1118
+ case MRB_TT_RATIONAL:
1119
+ if (a == 0) return x;
1120
+ if (a == 1) return y;
1121
+ return mrb_rational_mul(mrb, y, x);
1122
+ #endif
1123
+ #ifdef MRB_USE_COMPLEX
1124
+ case MRB_TT_COMPLEX:
1125
+ if (a == 0) return x;
1126
+ if (a == 1) return y;
1127
+ return mrb_complex_mul(mrb, y, x);
1128
+ #endif
1129
+ #ifndef MRB_NO_FLOAT
1130
+ case MRB_TT_FLOAT:
1131
+ return mrb_float_value(mrb, (mrb_float)a * mrb_as_float(mrb, y));
1132
+ #endif
1133
+ default:
1134
+ mrb_int_noconv(mrb, y);
1135
+ }
1136
+ }
1137
+
1138
+ /* 15.2.8.3.5 */
1139
+ /*
1140
+ * call-seq:
1141
+ * int * numeric -> numeric_result
1142
+ *
1143
+ * Performs multiplication: the class of the resulting object depends on
1144
+ * the class of `numeric` and on the magnitude of the
1145
+ * result.
1146
+ */
1147
+
1148
+ static mrb_value
1149
+ int_mul(mrb_state *mrb, mrb_value x)
1150
+ {
1151
+ mrb_value y = mrb_get_arg1(mrb);
1152
+
1153
+ #ifdef MRB_USE_BIGINT
1154
+ if (mrb_bigint_p(x)) {
1155
+ return mrb_bint_mul(mrb, x, y);
1156
+ }
1157
+ #endif
1158
+ return mrb_int_mul(mrb, x, y);
1159
+ }
1160
+
1161
+ static void
1162
+ intdivmod(mrb_state *mrb, mrb_int x, mrb_int y, mrb_int *divp, mrb_int *modp)
1163
+ {
1164
+ if (y == 0) {
1165
+ mrb_int_zerodiv(mrb);
1166
+ }
1167
+ else if (x == MRB_INT_MIN && y == -1) {
1168
+ mrb_int_overflow(mrb, "division");
1169
+ }
1170
+ else {
1171
+ mrb_int div = x / y;
1172
+ mrb_int mod = x - div * y;
1173
+
1174
+ if ((x ^ y) < 0 && x != div * y) {
1175
+ mod += y;
1176
+ div -= 1;
1177
+ }
1178
+ if (divp) *divp = div;
1179
+ if (modp) *modp = mod;
1180
+ }
1181
+ }
1182
+
1183
+ /* 15.2.8.3.7 */
1184
+ /*
1185
+ * call-seq:
1186
+ * int % num -> num
1187
+ *
1188
+ * Returns `int` modulo `other`.
1189
+ * See `numeric.divmod` for more information.
1190
+ */
1191
+
1192
+ static mrb_value
1193
+ int_mod(mrb_state *mrb, mrb_value x)
1194
+ {
1195
+ mrb_value y = mrb_get_arg1(mrb);
1196
+ mrb_int a, b;
1197
+
1198
+ #ifdef MRB_USE_BIGINT
1199
+ if (mrb_bigint_p(x)) {
1200
+ return mrb_bint_mod(mrb, x, y);
1201
+ }
1202
+ if (mrb_bigint_p(y)) {
1203
+ return mrb_bint_mod(mrb, mrb_as_bint(mrb, x), y);
1204
+ }
1205
+ #endif
1206
+ a = mrb_integer(x);
1207
+ if (a == 0) return x;
1208
+ if (mrb_integer_p(y)) {
1209
+ b = mrb_integer(y);
1210
+ if (b == 0) mrb_int_zerodiv(mrb);
1211
+ if (a == MRB_INT_MIN && b == -1) return mrb_fixnum_value(0);
1212
+ mrb_int mod = a % b;
1213
+ if ((a < 0) != (b < 0) && mod != 0) {
1214
+ mod += b;
1215
+ }
1216
+ return mrb_int_value(mrb, mod);
1217
+ }
1218
+ #ifdef MRB_NO_FLOAT
1219
+ mrb_raise(mrb, E_TYPE_ERROR, "non integer modulo");
1220
+ #else
1221
+ mrb_float mod;
1222
+
1223
+ flodivmod(mrb, (mrb_float)a, mrb_as_float(mrb, y), NULL, &mod);
1224
+ return mrb_float_value(mrb, mod);
1225
+ #endif
1226
+ }
1227
+
1228
+ #ifndef MRB_NO_FLOAT
1229
+ static mrb_value flo_divmod(mrb_state *mrb, mrb_value x);
1230
+ #endif
1231
+
1232
+ /*
1233
+ * call-seq:
1234
+ * int.divmod(numeric) -> array
1235
+ *
1236
+ * See `Numeric#divmod`.
1237
+ */
1238
+ static mrb_value
1239
+ int_divmod(mrb_state *mrb, mrb_value x)
1240
+ {
1241
+ mrb_value y = mrb_get_arg1(mrb);
1242
+
1243
+ #ifdef MRB_USE_BIGINT
1244
+ if (mrb_bigint_p(x)) {
1245
+ #ifndef MRB_NO_FLOAT
1246
+ if (mrb_float_p(y)) {
1247
+ mrb_float f = mrb_bint_as_float(mrb, x);
1248
+ return flo_divmod(mrb, mrb_float_value(mrb, f));
1249
+ }
1250
+ #endif
1251
+ return mrb_bint_divmod(mrb, x, y);
1252
+ }
1253
+ if (mrb_bigint_p(y)) {
1254
+ return mrb_bint_divmod(mrb, mrb_as_bint(mrb, x), y);
1255
+ }
1256
+ #endif
1257
+ if (mrb_integer_p(y)) {
1258
+ mrb_int div, mod;
1259
+
1260
+ intdivmod(mrb, mrb_integer(x), mrb_integer(y), &div, &mod);
1261
+ return mrb_assoc_new(mrb, mrb_int_value(mrb, div), mrb_int_value(mrb, mod));
1262
+ }
1263
+ #ifdef MRB_NO_FLOAT
1264
+ mrb_raise(mrb, E_TYPE_ERROR, "non integer divmod");
1265
+ #else
1266
+ return flo_divmod(mrb, mrb_ensure_float_type(mrb, x));
1267
+ #endif
1268
+ }
1269
+
1270
+ #ifndef MRB_NO_FLOAT
1271
+ static mrb_value
1272
+ flo_divmod(mrb_state *mrb, mrb_value x)
1273
+ {
1274
+ mrb_value y = mrb_get_arg1(mrb);
1275
+ mrb_float div, mod;
1276
+ mrb_value a, b;
1277
+
1278
+ flodivmod(mrb, mrb_float(x), mrb_as_float(mrb, y), &div, &mod);
1279
+ if (!FIXABLE_FLOAT(div))
1280
+ a = mrb_float_value(mrb, div);
1281
+ else
1282
+ a = mrb_int_value(mrb, (mrb_int)div);
1283
+ b = mrb_float_value(mrb, mod);
1284
+ return mrb_assoc_new(mrb, a, b);
1285
+ }
1286
+ #endif
1287
+
1288
+ /* 15.2.8.3.2 */
1289
+ /*
1290
+ * call-seq:
1291
+ * int == other -> true or false
1292
+ *
1293
+ * Return `true` if `int` equals `other`
1294
+ * numerically.
1295
+ *
1296
+ * 1 == 2 #=> false
1297
+ * 1 == 1.0 #=> true
1298
+ */
1299
+
1300
+ static mrb_value
1301
+ int_equal(mrb_state *mrb, mrb_value x)
1302
+ {
1303
+ mrb_value y = mrb_get_arg1(mrb);
1304
+
1305
+ switch (mrb_type(y)) {
1306
+ case MRB_TT_INTEGER:
1307
+ return mrb_bool_value(mrb_integer(x) == mrb_integer(y));
1308
+ #ifndef MRB_NO_FLOAT
1309
+ case MRB_TT_FLOAT:
1310
+ return mrb_bool_value((mrb_float)mrb_integer(x) == mrb_float(y));
1311
+ #endif
1312
+ #ifdef MRB_USE_BIGINT
1313
+ case MRB_TT_BIGINT:
1314
+ return mrb_bool_value(mrb_bint_cmp(mrb, y, x) == 0);
1315
+ #endif
1316
+ #ifdef MRB_USE_RATIONAL
1317
+ case MRB_TT_RATIONAL:
1318
+ return mrb_bool_value(mrb_equal(mrb, y, x));
1319
+ #endif
1320
+ #ifdef MRB_USE_COMPLEX
1321
+ case MRB_TT_COMPLEX:
1322
+ return mrb_bool_value(mrb_equal(mrb, y, x));
1323
+ #endif
1324
+ default:
1325
+ return mrb_false_value();
1326
+ }
1327
+ }
1328
+
1329
+ /* 15.2.8.3.8 */
1330
+ /*
1331
+ * call-seq:
1332
+ * ~int -> integer
1333
+ *
1334
+ * One's complement: returns a number where each bit is flipped.
1335
+ * ex.0---00001 (1)-> 1---11110 (-2)
1336
+ * ex.0---00010 (2)-> 1---11101 (-3)
1337
+ * ex.0---00100 (4)-> 1---11011 (-5)
1338
+ */
1339
+
1340
+ static mrb_value
1341
+ int_rev(mrb_state *mrb, mrb_value num)
1342
+ {
1343
+ #ifdef MRB_USE_BIGINT
1344
+ if (mrb_bigint_p(num)) {
1345
+ return mrb_bint_rev(mrb, num);
1346
+ }
1347
+ #endif
1348
+ mrb_int val = mrb_integer(num);
1349
+ return mrb_int_value(mrb, ~val);
1350
+ }
1351
+
1352
+ #define bit_op(x,y,op1,op2) do {\
1353
+ return mrb_int_value(mrb, (mrb_integer(x) op2 mrb_integer(y)));\
1354
+ } while(0)
1355
+
1356
+ /* 15.2.8.3.9 */
1357
+ /*
1358
+ * call-seq:
1359
+ * int & integer -> integer_result
1360
+ *
1361
+ * Bitwise AND.
1362
+ */
1363
+
1364
+ static mrb_value
1365
+ int_and(mrb_state *mrb, mrb_value x)
1366
+ {
1367
+ mrb_value y = mrb_get_arg1(mrb);
1368
+
1369
+ #ifdef MRB_USE_BIGINT
1370
+ if (mrb_bigint_p(x)) {
1371
+ return mrb_bint_and(mrb, x, y);
1372
+ }
1373
+ if (mrb_bigint_p(y)) {
1374
+ return mrb_bint_and(mrb, mrb_as_bint(mrb, x), y);
1375
+ }
1376
+ #endif
1377
+ bit_op(x, y, and, &);
1378
+ }
1379
+
1380
+ /* 15.2.8.3.10 */
1381
+ /*
1382
+ * call-seq:
1383
+ * int | integer -> integer_result
1384
+ *
1385
+ * Bitwise OR.
1386
+ */
1387
+
1388
+ static mrb_value
1389
+ int_or(mrb_state *mrb, mrb_value x)
1390
+ {
1391
+ mrb_value y = mrb_get_arg1(mrb);
1392
+
1393
+ #ifdef MRB_USE_BIGINT
1394
+ if (mrb_bigint_p(x)) {
1395
+ return mrb_bint_or(mrb, x, y);
1396
+ }
1397
+ if (mrb_bigint_p(y)) {
1398
+ return mrb_bint_or(mrb, mrb_as_bint(mrb, x), y);
1399
+ }
1400
+ #endif
1401
+ bit_op(x, y, or, |);
1402
+ }
1403
+
1404
+ /* 15.2.8.3.11 */
1405
+ /*
1406
+ * call-seq:
1407
+ * int ^ integer -> integer_result
1408
+ *
1409
+ * Bitwise EXCLUSIVE OR.
1410
+ */
1411
+
1412
+ static mrb_value
1413
+ int_xor(mrb_state *mrb, mrb_value x)
1414
+ {
1415
+ mrb_value y = mrb_get_arg1(mrb);
1416
+
1417
+ #ifdef MRB_USE_BIGINT
1418
+ if (mrb_bigint_p(x)) {
1419
+ return mrb_bint_xor(mrb, x, y);
1420
+ }
1421
+ if (mrb_bigint_p(y)) {
1422
+ return mrb_bint_xor(mrb, mrb_as_bint(mrb, x), y);
1423
+ }
1424
+ #endif
1425
+ bit_op(x, y, xor, ^);
1426
+ }
1427
+
1428
+ #define NUMERIC_SHIFT_WIDTH_MAX (MRB_INT_BIT-1)
1429
+
1430
+ /**
1431
+ * Performs a bitwise shift operation (left or right) on an mrb_int value
1432
+ * (val) by width positions.
1433
+ *
1434
+ * @param mrb The mruby state (though not directly used in the function
1435
+ * logic, it's often part of MRB_API signatures).
1436
+ * @param val The integer value to be shifted.
1437
+ * @param width The number of positions to shift. Positive for left shift,
1438
+ * negative for right shift.
1439
+ * @param num A pointer to an mrb_int where the result of the shift will be
1440
+ * stored.
1441
+ * @return An mrb_bool indicating whether the shift was successful.
1442
+ * - TRUE if the shift was performed without overflow.
1443
+ * - FALSE if the shift would result in an overflow (e.g., shifting
1444
+ * a large positive number too far left, or a negative number
1445
+ * too far left).
1446
+ * Special handling for right shifts of negative numbers (arithmetic shift)
1447
+ * and large shift widths.
1448
+ */
1449
+ mrb_bool
1450
+ mrb_num_shift(mrb_state *mrb, mrb_int val, mrb_int width, mrb_int *num)
1451
+ {
1452
+ if (width < 0) { /* rshift */
1453
+ if (width == MRB_INT_MIN || -width >= NUMERIC_SHIFT_WIDTH_MAX) {
1454
+ if (val < 0) {
1455
+ *num = -1;
1456
+ }
1457
+ else {
1458
+ *num = 0;
1459
+ }
1460
+ }
1461
+ else {
1462
+ *num = val >> -width;
1463
+ }
1464
+ }
1465
+ else if (val > 0) {
1466
+ if ((width > NUMERIC_SHIFT_WIDTH_MAX) ||
1467
+ (val > (MRB_INT_MAX >> width))) {
1468
+ return FALSE;
1469
+ }
1470
+ *num = val << width;
1471
+ }
1472
+ else {
1473
+ if ((width > NUMERIC_SHIFT_WIDTH_MAX) ||
1474
+ (val < (MRB_INT_MIN >> width))) {
1475
+ return FALSE;
1476
+ }
1477
+ if (width == NUMERIC_SHIFT_WIDTH_MAX)
1478
+ *num = MRB_INT_MIN;
1479
+ else
1480
+ *num = val * ((mrb_int)1 << width);
1481
+ }
1482
+ return TRUE;
1483
+ }
1484
+
1485
+ /* 15.2.8.3.12 */
1486
+ /*
1487
+ * call-seq:
1488
+ * int << count -> integer or float
1489
+ *
1490
+ * Shifts _int_ left _count_ positions (right if _count_ is negative).
1491
+ */
1492
+
1493
+ static mrb_value
1494
+ int_lshift(mrb_state *mrb, mrb_value x)
1495
+ {
1496
+ mrb_int width, val;
1497
+
1498
+ width = mrb_as_int(mrb, mrb_get_arg1(mrb));
1499
+ if (width == 0) {
1500
+ return x;
1501
+ }
1502
+ if (width == MRB_INT_MIN) mrb_int_overflow(mrb, "bit shift");
1503
+ #ifdef MRB_USE_BIGINT
1504
+ if (mrb_bigint_p(x)) {
1505
+ return mrb_bint_lshift(mrb, x, width);
1506
+ }
1507
+ #endif
1508
+ val = mrb_integer(x);
1509
+ if (val == 0) return x;
1510
+ if (!mrb_num_shift(mrb, val, width, &val)) {
1511
+ #ifdef MRB_USE_BIGINT
1512
+ return mrb_bint_lshift(mrb, mrb_bint_new_int(mrb, val), width);
1513
+ #else
1514
+ mrb_int_overflow(mrb, "bit shift");
1515
+ #endif
1516
+ }
1517
+ return mrb_int_value(mrb, val);
1518
+ }
1519
+
1520
+ /* 15.2.8.3.13 */
1521
+ /*
1522
+ * call-seq:
1523
+ * int >> count -> integer or float
1524
+ *
1525
+ * Shifts _int_ right _count_ positions (left if _count_ is negative).
1526
+ */
1527
+
1528
+ static mrb_value
1529
+ int_rshift(mrb_state *mrb, mrb_value x)
1530
+ {
1531
+ mrb_int width, val;
1532
+
1533
+ width = mrb_as_int(mrb, mrb_get_arg1(mrb));
1534
+ if (width == 0) {
1535
+ return x;
1536
+ }
1537
+ if (width == MRB_INT_MIN) mrb_int_overflow(mrb, "bit shift");
1538
+ #ifdef MRB_USE_BIGINT
1539
+ if (mrb_bigint_p(x)) {
1540
+ return mrb_bint_rshift(mrb, x, width);
1541
+ }
1542
+ #endif
1543
+ val = mrb_integer(x);
1544
+ if (val == 0) return x;
1545
+ if (!mrb_num_shift(mrb, val, -width, &val)) {
1546
+ #ifdef MRB_USE_BIGINT
1547
+ return mrb_bint_rshift(mrb, mrb_bint_new_int(mrb, val), width);
1548
+ #else
1549
+ mrb_int_overflow(mrb, "bit shift");
1550
+ #endif
1551
+ }
1552
+ return mrb_int_value(mrb, val);
1553
+ }
1554
+
1555
+ static mrb_value
1556
+ prepare_int_rounding(mrb_state *mrb, mrb_value x)
1557
+ {
1558
+ mrb_int nd = 0;
1559
+ size_t bytes;
1560
+
1561
+ mrb_get_args(mrb, "|i", &nd);
1562
+ if (nd >= 0) {
1563
+ return mrb_nil_value();
1564
+ }
1565
+ #ifdef MRB_USE_BIGINT
1566
+ if (mrb_bigint_p(x)) {
1567
+ bytes = mrb_bint_memsize(x);
1568
+ }
1569
+ else
1570
+ #endif
1571
+ bytes = sizeof(mrb_int);
1572
+ if (-0.415241 * nd - 0.125 > bytes) {
1573
+ return mrb_undef_value();
1574
+ }
1575
+ return mrb_int_pow(mrb, mrb_fixnum_value(10), mrb_fixnum_value(-nd));
1576
+ }
1577
+
1578
+ /* 15.2.8.3.14 Integer#ceil */
1579
+ /*
1580
+ * call-seq:
1581
+ * int.ceil -> int
1582
+ * int.ceil(ndigits) -> int
1583
+ *
1584
+ * Returns self.
1585
+ *
1586
+ * When the precision (ndigits) is negative, the returned value is an integer
1587
+ * with at least `ndigits.abs` trailing zeros.
1588
+ */
1589
+ static mrb_value
1590
+ int_ceil(mrb_state *mrb, mrb_value x)
1591
+ {
1592
+ mrb_value f = prepare_int_rounding(mrb, x);
1593
+ if (mrb_undef_p(f)) return mrb_fixnum_value(0);
1594
+ if (mrb_nil_p(f)) return x;
1595
+ #ifdef MRB_USE_BIGINT
1596
+ if (mrb_bigint_p(x)) {
1597
+ x = mrb_bint_add_n(mrb, x, f);
1598
+ return mrb_bint_sub(mrb, x, mrb_bint_mod(mrb, x, f));
1599
+ }
1600
+ #endif
1601
+ mrb_int a = mrb_integer(x);
1602
+ mrb_int b = mrb_integer(f);
1603
+ mrb_int c = a % b;
1604
+ int neg = a < 0;
1605
+ a -= c;
1606
+ if (!neg) {
1607
+ if (mrb_int_add_overflow(a, b, &c)) {
1608
+ #ifdef MRB_USE_BIGINT
1609
+ x = mrb_bint_new_int(mrb, a);
1610
+ return mrb_bint_add(mrb, x, f);
1611
+ #else
1612
+ mrb_int_overflow(mrb, "ceil");
1613
+ #endif
1614
+ }
1615
+ a = c;
1616
+ }
1617
+ return mrb_int_value(mrb, a);
1618
+ }
1619
+
1620
+ /* 15.2.8.3.17 Integer#floor */
1621
+ /*
1622
+ * call-seq:
1623
+ * int.floor -> int
1624
+ * int.floor(ndigits) -> int
1625
+ *
1626
+ * Returns self.
1627
+ *
1628
+ * When the precision (ndigits) is negative, the returned value is an integer
1629
+ * with at least `ndigits.abs` trailing zeros.
1630
+ */
1631
+ static mrb_value
1632
+ int_floor(mrb_state *mrb, mrb_value x)
1633
+ {
1634
+ mrb_value f = prepare_int_rounding(mrb, x);
1635
+ if (mrb_undef_p(f)) return mrb_fixnum_value(0);
1636
+ if (mrb_nil_p(f)) return x;
1637
+ #ifdef MRB_USE_BIGINT
1638
+ if (mrb_bigint_p(x)) {
1639
+ return mrb_bint_sub(mrb, x, mrb_bint_mod(mrb, x, f));
1640
+ }
1641
+ #endif
1642
+ mrb_int a = mrb_integer(x);
1643
+ mrb_int b = mrb_integer(f);
1644
+ mrb_int c = a % b;
1645
+ int neg = a < 0;
1646
+ a -= c;
1647
+ if (neg) {
1648
+ if (mrb_int_sub_overflow(a, b, &c)) {
1649
+ #ifdef MRB_USE_BIGINT
1650
+ x = mrb_bint_new_int(mrb, a);
1651
+ return mrb_bint_sub(mrb, x, f);
1652
+ #else
1653
+ mrb_int_overflow(mrb, "floor");
1654
+ #endif
1655
+ }
1656
+ a = c;
1657
+ }
1658
+ return mrb_int_value(mrb, a);
1659
+ }
1660
+
1661
+ /* 15.2.8.3.20 Integer#round */
1662
+ /*
1663
+ * call-seq:
1664
+ * int.round -> int
1665
+ * int.round(ndigits) -> int
1666
+ *
1667
+ * Returns self.
1668
+ *
1669
+ * When the precision (ndigits) is negative, the returned value is an integer
1670
+ * with at least `ndigits.abs` trailing zeros.
1671
+ */
1672
+ static mrb_value
1673
+ int_round(mrb_state *mrb, mrb_value x)
1674
+ {
1675
+ mrb_value f = prepare_int_rounding(mrb, x);
1676
+ if (mrb_undef_p(f)) return mrb_fixnum_value(0);
1677
+ if (mrb_nil_p(f)) return x;
1678
+ #ifdef MRB_USE_BIGINT
1679
+ if (mrb_bigint_p(x)) {
1680
+ mrb_value r = mrb_bint_mod(mrb, x, f);
1681
+ mrb_value n = mrb_bint_sub(mrb, x, r);
1682
+ mrb_value h = mrb_bigint_p(f) ? mrb_bint_rshift(mrb, f, 1) : mrb_int_value(mrb, mrb_integer(f)>>1);
1683
+ mrb_int cmp = mrb_bigint_p(r) ? mrb_bint_cmp(mrb, r, h) : (mrb_bigint_p(h) ? -mrb_bint_cmp(mrb, h, r) : (mrb_integer(r)-mrb_integer(h)));
1684
+ if ((cmp > 0) || (cmp == 0 && mrb_bint_cmp(mrb, x, mrb_fixnum_value(0)) > 0)) {
1685
+ n = mrb_as_bint(mrb, n);
1686
+ n = mrb_bint_add(mrb, n, f);
1687
+ }
1688
+ return n;
1689
+ }
1690
+ #endif
1691
+ mrb_int a = mrb_integer(x);
1692
+ mrb_int b = mrb_integer(f);
1693
+ mrb_int c = a % b;
1694
+ a -= c;
1695
+ if (c < 0) {
1696
+ c = -c;
1697
+ if (b/2 < c) {
1698
+ if (mrb_int_sub_overflow(a, b, &c)) {
1699
+ #ifdef MRB_USE_BIGINT
1700
+ x = mrb_bint_new_int(mrb, a);
1701
+ return mrb_bint_sub(mrb, x, f);
1702
+ #else
1703
+ mrb_int_overflow(mrb, "round");
1704
+ #endif
1705
+ }
1706
+ }
1707
+ a = c;
1708
+ }
1709
+ else {
1710
+ if (b/2 < c) {
1711
+ if (mrb_int_add_overflow(a, b, &c)) {
1712
+ #ifdef MRB_USE_BIGINT
1713
+ x = mrb_bint_new_int(mrb, a);
1714
+ return mrb_bint_add(mrb, x, f);
1715
+ #else
1716
+ mrb_int_overflow(mrb, "round");
1717
+ #endif
1718
+ }
1719
+ }
1720
+ a = c;
1721
+ }
1722
+ return mrb_int_value(mrb, a);
1723
+ }
1724
+
1725
+ /* 15.2.8.3.26 Integer#truncate */
1726
+ /*
1727
+ * call-seq:
1728
+ * int.truncate -> int
1729
+ * int.truncate(ndigits) -> int
1730
+ *
1731
+ * Returns self.
1732
+ *
1733
+ * When the precision (ndigits) is negative, the returned value is an integer
1734
+ * with at least `ndigits.abs` trailing zeros.
1735
+ */
1736
+ static mrb_value
1737
+ int_truncate(mrb_state *mrb, mrb_value x)
1738
+ {
1739
+ mrb_value f = prepare_int_rounding(mrb, x);
1740
+ if (mrb_undef_p(f)) return mrb_fixnum_value(0);
1741
+ if (mrb_nil_p(f)) return x;
1742
+ #ifdef MRB_USE_BIGINT
1743
+ if (mrb_bigint_p(x)) {
1744
+ mrb_value m = mrb_bint_mod(mrb, x, f);
1745
+ x = mrb_bint_sub_n(mrb, x, m);
1746
+ if (mrb_bint_cmp(mrb, x, mrb_fixnum_value(0)) < 0) {
1747
+ return mrb_bint_add(mrb, x, f);
1748
+ }
1749
+ return x;
1750
+ }
1751
+ #endif
1752
+ mrb_int a = mrb_integer(x);
1753
+ mrb_int b = mrb_integer(f);
1754
+ return mrb_int_value(mrb, a - (a % b));
1755
+ }
1756
+
1757
+ /* 15.2.8.3.23 */
1758
+ /*
1759
+ * call-seq:
1760
+ * int.to_f -> float
1761
+ *
1762
+ * Converts *int* to a `Float`.
1763
+ *
1764
+ */
1765
+
1766
+ #ifndef MRB_NO_FLOAT
1767
+ static mrb_value
1768
+ int_to_f(mrb_state *mrb, mrb_value num)
1769
+ {
1770
+ #ifdef MRB_USE_BIGINT
1771
+ if (mrb_bigint_p(num)) {
1772
+ return mrb_float_value(mrb, mrb_bint_as_float(mrb, num));
1773
+ }
1774
+ #endif
1775
+ return mrb_float_value(mrb, (mrb_float)mrb_integer(num));
1776
+ }
1777
+
1778
+ /**
1779
+ * Converts an mrb_value float to an mrb_value integer.
1780
+ *
1781
+ * @param mrb The mruby state.
1782
+ * @param x The float mrb_value to convert.
1783
+ * @return An mrb_value integer if the conversion is successful.
1784
+ * @raise E_TYPE_ERROR if the input is not a float.
1785
+ * @raise E_RANGE_ERROR if the float is Infinity or NaN.
1786
+ */
1787
+ MRB_API mrb_value
1788
+ mrb_float_to_integer(mrb_state *mrb, mrb_value x)
1789
+ {
1790
+ if (!mrb_float_p(x)) {
1791
+ mrb_raise(mrb, E_TYPE_ERROR, "non float value");
1792
+ }
1793
+ mrb_float f = mrb_float(x);
1794
+ if (isinf(f) || isnan(f)) {
1795
+ mrb_raisef(mrb, E_RANGE_ERROR, "float %f out of range", f);
1796
+ }
1797
+ return flo_to_i(mrb, x);
1798
+ }
1799
+ #endif
1800
+
1801
+ /**
1802
+ * Adds two mrb_values, x and y, where x is expected to be an integer.
1803
+ * y can be an integer, BigInt, Rational, Complex, or Float. The function
1804
+ * handles type promotion and dispatches to appropriate handlers.
1805
+ *
1806
+ * @param mrb The mruby state.
1807
+ * @param x The first operand (integer).
1808
+ * @param y The second operand (can be various numeric types).
1809
+ * @return An mrb_value representing the sum. The type of the result depends
1810
+ * on the types of the inputs and the magnitude of the result.
1811
+ * Handles potential integer overflows by promoting to BigInt if MRB_USE_BIGINT
1812
+ * is defined, or raising RangeError otherwise.
1813
+ * If y is not a recognized numeric type and MRB_NO_FLOAT is defined, it
1814
+ * raises E_TYPE_ERROR. If MRB_NO_FLOAT is not defined, it attempts to
1815
+ * convert y to a float.
1816
+ */
1817
+ mrb_value
1818
+ mrb_int_add(mrb_state *mrb, mrb_value x, mrb_value y)
1819
+ {
1820
+ mrb_int a = mrb_integer(x);
1821
+
1822
+ if (mrb_integer_p(y)) {
1823
+ mrb_int b, c;
1824
+
1825
+ if (a == 0) return y;
1826
+ b = mrb_integer(y);
1827
+ if (b == 0) return x;
1828
+ if (mrb_int_add_overflow(a, b, &c)) {
1829
+ #ifdef MRB_USE_BIGINT
1830
+ x = mrb_bint_new_int(mrb, a);
1831
+ return mrb_bint_add(mrb, x, y);
1832
+ #else
1833
+ mrb_int_overflow(mrb, "addition");
1834
+ #endif
1835
+ }
1836
+ return mrb_int_value(mrb, c);
1837
+ }
1838
+ switch (mrb_type(y)) {
1839
+ #ifdef MRB_USE_BIGINT
1840
+ case MRB_TT_BIGINT:
1841
+ return mrb_bint_add(mrb, y, x);
1842
+ #endif
1843
+ #ifdef MRB_USE_RATIONAL
1844
+ case MRB_TT_RATIONAL:
1845
+ return mrb_rational_add(mrb, y, x);
1846
+ #endif
1847
+ #ifdef MRB_USE_COMPLEX
1848
+ case MRB_TT_COMPLEX:
1849
+ return mrb_complex_add(mrb, y, x);
1850
+ #endif
1851
+ default:
1852
+ #ifdef MRB_NO_FLOAT
1853
+ mrb_raise(mrb, E_TYPE_ERROR, "non integer addition");
1854
+ #else
1855
+ return mrb_float_value(mrb, (mrb_float)a + mrb_as_float(mrb, y));
1856
+ #endif
1857
+ }
1858
+ }
1859
+
1860
+ /* 15.2.8.3.3 */
1861
+ /*
1862
+ * call-seq:
1863
+ * int + numeric -> numeric_result
1864
+ *
1865
+ * Performs addition: the class of the resulting object depends on
1866
+ * the class of `numeric` and on the magnitude of the
1867
+ * result.
1868
+ */
1869
+ static mrb_value
1870
+ int_add(mrb_state *mrb, mrb_value self)
1871
+ {
1872
+ mrb_value other = mrb_get_arg1(mrb);
1873
+
1874
+ #ifdef MRB_USE_BIGINT
1875
+ if (mrb_bigint_p(self)) {
1876
+ return mrb_bint_add(mrb, self, other);
1877
+ }
1878
+ #endif
1879
+ return mrb_int_add(mrb, self, other);
1880
+ }
1881
+
1882
+ /**
1883
+ * Subtracts mrb_value y from mrb_value x, where x is expected to be an
1884
+ * integer. y can be an integer, BigInt, Rational, Complex, or Float.
1885
+ * The function handles type promotion and dispatches to appropriate handlers.
1886
+ *
1887
+ * @param mrb The mruby state.
1888
+ * @param x The minuend (integer).
1889
+ * @param y The subtrahend (can be various numeric types).
1890
+ * @return An mrb_value representing the difference. The type of the result
1891
+ * depends on the types of the inputs and the magnitude of the result.
1892
+ * Handles potential integer overflows by promoting to BigInt if MRB_USE_BIGINT
1893
+ * is defined, or raising RangeError otherwise.
1894
+ * If y is not a recognized numeric type and MRB_NO_FLOAT is defined, it
1895
+ * raises E_TYPE_ERROR. If MRB_NO_FLOAT is not defined, it attempts to
1896
+ * convert y to a float.
1897
+ */
1898
+ mrb_value
1899
+ mrb_int_sub(mrb_state *mrb, mrb_value x, mrb_value y)
1900
+ {
1901
+ mrb_int a = mrb_integer(x);
1902
+
1903
+ if (mrb_integer_p(y)) {
1904
+ mrb_int b, c;
1905
+
1906
+ b = mrb_integer(y);
1907
+ if (mrb_int_sub_overflow(a, b, &c)) {
1908
+ #ifdef MRB_USE_BIGINT
1909
+ x = mrb_bint_new_int(mrb, a);
1910
+ return mrb_bint_sub(mrb, x, y);
1911
+ #else
1912
+ mrb_int_overflow(mrb, "subtraction");
1913
+ #endif
1914
+ }
1915
+ return mrb_int_value(mrb, c);
1916
+ }
1917
+ switch (mrb_type(y)) {
1918
+ #ifdef MRB_USE_BIGINT
1919
+ case MRB_TT_BIGINT:
1920
+ return mrb_bint_sub(mrb, mrb_bint_new_int(mrb, a), y);
1921
+ #endif
1922
+ #ifdef MRB_USE_RATIONAL
1923
+ case MRB_TT_RATIONAL:
1924
+ return mrb_rational_sub(mrb, mrb_rational_new(mrb, a, 1), y);
1925
+ #endif
1926
+ #ifdef MRB_USE_COMPLEX
1927
+ case MRB_TT_COMPLEX:
1928
+ return mrb_complex_sub(mrb, mrb_complex_new(mrb, (mrb_float)a, 0), y);
1929
+ #endif
1930
+ default:
1931
+ #ifdef MRB_NO_FLOAT
1932
+ mrb_raise(mrb, E_TYPE_ERROR, "non integer subtraction");
1933
+ #else
1934
+ return mrb_float_value(mrb, (mrb_float)a - mrb_as_float(mrb, y));
1935
+ #endif
1936
+ }
1937
+ }
1938
+
1939
+ /* 15.2.8.3.4 */
1940
+ /*
1941
+ * call-seq:
1942
+ * int - numeric -> numeric
1943
+ *
1944
+ * Performs subtraction: the class of the resulting object depends on
1945
+ * the class of `numeric` and on the magnitude of the
1946
+ * result.
1947
+ */
1948
+ static mrb_value
1949
+ int_sub(mrb_state *mrb, mrb_value self)
1950
+ {
1951
+ mrb_value other = mrb_get_arg1(mrb);
1952
+
1953
+ #ifdef MRB_USE_BIGINT
1954
+ if (mrb_bigint_p(self)) {
1955
+ return mrb_bint_sub(mrb, self, other);
1956
+ }
1957
+ #endif
1958
+ return mrb_int_sub(mrb, self, other);
1959
+ }
1960
+
1961
+ /**
1962
+ * Converts an mrb_int to a C-style string.
1963
+ *
1964
+ * @param buf The buffer to write the string to.
1965
+ * @param len The size of the buffer.
1966
+ * @param n The integer to convert.
1967
+ * @param base The radix for conversion (2-36).
1968
+ * @return A pointer to the beginning of the string in the buffer,
1969
+ * or NULL if an error occurs (e.g., invalid base, buffer too small).
1970
+ */
1971
+ MRB_API char*
1972
+ mrb_int_to_cstr(char *buf, size_t len, mrb_int n, mrb_int base)
1973
+ {
1974
+ char *bufend = buf + len;
1975
+ char *b = bufend-1;
1976
+
1977
+ if (base < 2 || 36 < base) return NULL;
1978
+ if (len < 2) return NULL;
1979
+
1980
+ if (n == 0) {
1981
+ buf[0] = '0';
1982
+ buf[1] = '\0';
1983
+ return buf;
1984
+ }
1985
+
1986
+ *b = '\0';
1987
+ if (n < 0) {
1988
+ do {
1989
+ if (b-- == buf) return NULL;
1990
+ *b = mrb_digitmap[-(n % base)];
1991
+ } while (n /= base);
1992
+ if (b-- == buf) return NULL;
1993
+ *b = '-';
1994
+ }
1995
+ else {
1996
+ do {
1997
+ if (b-- == buf) return NULL;
1998
+ *b = mrb_digitmap[(int)(n % base)];
1999
+ } while (n /= base);
2000
+ }
2001
+ return b;
2002
+ }
2003
+
2004
+ /**
2005
+ * Converts an mrb_value representing an integer to a new mrb_value string.
2006
+ *
2007
+ * @param mrb The mruby state.
2008
+ * @param x The integer mrb_value to convert.
2009
+ * @param base The radix for conversion (2-36).
2010
+ * @return A new mrb_value string representing the integer,
2011
+ * or raises an E_ARGUMENT_ERROR if the base is invalid.
2012
+ */
2013
+ MRB_API mrb_value
2014
+ mrb_integer_to_str(mrb_state *mrb, mrb_value x, mrb_int base)
2015
+ {
2016
+ char buf[MRB_INT_BIT+1];
2017
+
2018
+ if (base < 2 || 36 < base) {
2019
+ mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid radix %i", base);
2020
+ }
2021
+ #ifdef MRB_USE_BIGINT
2022
+ if (mrb_bigint_p(x)) {
2023
+ return mrb_bint_to_s(mrb, x, base);
2024
+ }
2025
+ #endif
2026
+ mrb_int val = mrb_integer(x);
2027
+ const char *p = mrb_int_to_cstr(buf, sizeof(buf), val, base);
2028
+ mrb_assert(p != NULL);
2029
+ mrb_value str = mrb_str_new_cstr(mrb, p);
2030
+ RSTR_SET_ASCII_FLAG(mrb_str_ptr(str));
2031
+ return str;
2032
+ }
2033
+
2034
+ /* 15.2.8.3.25 */
2035
+ /*
2036
+ * call-seq:
2037
+ * int.to_s(base=10) -> string
2038
+ *
2039
+ * Returns a string containing the representation of *int* radix
2040
+ * *base* (between 2 and 36).
2041
+ *
2042
+ * 12345.to_s #=> "12345"
2043
+ * 12345.to_s(2) #=> "11000000111001"
2044
+ * 12345.to_s(8) #=> "30071"
2045
+ * 12345.to_s(10) #=> "12345"
2046
+ * 12345.to_s(16) #=> "3039"
2047
+ * 12345.to_s(36) #=> "9ix"
2048
+ *
2049
+ */
2050
+ static mrb_value
2051
+ int_to_s(mrb_state *mrb, mrb_value self)
2052
+ {
2053
+ mrb_int base;
2054
+
2055
+ if (mrb_get_argc(mrb) > 0) {
2056
+ base = mrb_integer(mrb_get_arg1(mrb));
2057
+ }
2058
+ else {
2059
+ base = 10;
2060
+ }
2061
+ return mrb_integer_to_str(mrb, self, base);
2062
+ }
2063
+
2064
+ /* compare two numbers: (1:0:-1; -2 for error) */
2065
+ static mrb_int
2066
+ cmpnum(mrb_state *mrb, mrb_value v1, mrb_value v2)
2067
+ {
2068
+ #ifdef MRB_NO_FLOAT /* integer version */
2069
+
2070
+ if (!mrb_fixnum_p(v2)) {
2071
+ if (!mrb_obj_is_kind_of(mrb, v2, mrb_class_get_id(mrb, MRB_SYM(Numeric)))) {
2072
+ return -2;
2073
+ }
2074
+ v1 = mrb_funcall_argv(mrb, v2, MRB_OPSYM(cmp), 1, &v1);
2075
+ if (mrb_integer_p(v1)) {
2076
+ return -mrb_integer(v1);
2077
+ }
2078
+ return -2;
2079
+ }
2080
+ mrb_int x = mrb_as_int(mrb, v1);
2081
+ mrb_int y = mrb_integer(v2);
2082
+
2083
+ #else /* float version */
2084
+
2085
+ mrb_float x, y;
2086
+
2087
+ if (mrb_fixnum_p(v1)) {
2088
+ if (mrb_fixnum_p(v2)) {
2089
+ mrb_int x = mrb_integer(v1);
2090
+ mrb_int y = mrb_integer(v2);
2091
+
2092
+ if (x > y) return 1;
2093
+ else if (x < y) return -1;
2094
+ return 0;
2095
+ }
2096
+ #ifdef MRB_USE_BIGINT
2097
+ if (mrb_bigint_p(v2)) {
2098
+ return -mrb_bint_cmp(mrb, v2, v1);
2099
+ }
2100
+ #endif
2101
+ x = (mrb_float)mrb_integer(v1);
2102
+ }
2103
+ #ifdef MRB_USE_BIGINT
2104
+ else if (mrb_bigint_p(v1)) {
2105
+ if (mrb_integer_p(v2) || mrb_bigint_p(v2)) {
2106
+ return mrb_bint_cmp(mrb, v1, v2);
2107
+ }
2108
+ x = mrb_as_float(mrb, v1);
2109
+ }
2110
+ #endif
2111
+ else {
2112
+ x = mrb_as_float(mrb, v1);
2113
+ }
2114
+
2115
+ switch (mrb_type(v2)) {
2116
+ #ifdef MRB_USE_RATIONAL
2117
+ case MRB_TT_RATIONAL:
2118
+ #endif
2119
+ #ifdef MRB_USE_BIGINT
2120
+ case MRB_TT_BIGINT:
2121
+ #endif
2122
+ case MRB_TT_INTEGER:
2123
+ if (mrb_fixnum_p(v2)) {
2124
+ y = (mrb_float)mrb_integer(v2);
2125
+ break;
2126
+ }
2127
+ /* fall through */
2128
+ case MRB_TT_FLOAT:
2129
+ y = mrb_as_float(mrb, v2);
2130
+ break;
2131
+ default:
2132
+ if (!mrb_obj_is_kind_of(mrb, v2, mrb_class_get_id(mrb, MRB_SYM(Numeric)))) {
2133
+ return -2;
2134
+ }
2135
+ /* fall through */
2136
+ #ifdef MRB_USE_COMPLEX
2137
+ case MRB_TT_COMPLEX:
2138
+ #endif
2139
+ v1 = mrb_funcall_argv(mrb, v2, MRB_OPSYM(cmp), 1, &v1);
2140
+ if (mrb_fixnum_p(v1)) {
2141
+ return -mrb_integer(v1);
2142
+ }
2143
+ return -2;
2144
+ }
2145
+ #endif
2146
+ if (x > y)
2147
+ return 1;
2148
+ else if (x < y)
2149
+ return -1;
2150
+ return 0;
2151
+ }
2152
+
2153
+ static mrb_value
2154
+ int_hash(mrb_state *mrb, mrb_value self)
2155
+ {
2156
+ #ifdef MRB_USE_BIGINT
2157
+ if (mrb_bigint_p(self)) {
2158
+ return mrb_bint_hash(mrb, self);
2159
+ }
2160
+ #endif
2161
+ mrb_int n = mrb_integer(self);
2162
+ return mrb_int_value(mrb, mrb_byte_hash((uint8_t*)&n, sizeof(n)));
2163
+ }
2164
+
2165
+ /* 15.2.8.3.1 */
2166
+ /* 15.2.9.3.1 */
2167
+ /*
2168
+ * call-seq:
2169
+ * self.f <=> other.f => -1, 0, +1, or nil
2170
+ * < => -1
2171
+ * = => 0
2172
+ * > => +1
2173
+ * Comparison---Returns -1, 0, or +1 depending on whether *int* is
2174
+ * less than, equal to, or greater than *numeric*. This is the
2175
+ * basis for the tests in `Comparable`. When the operands are
2176
+ * not comparable, it returns nil instead of raising an exception.
2177
+ */
2178
+ static mrb_value
2179
+ num_cmp(mrb_state *mrb, mrb_value self)
2180
+ {
2181
+ mrb_value other = mrb_get_arg1(mrb);
2182
+ mrb_int n = cmpnum(mrb, self, other);
2183
+
2184
+ if (n == -2) return mrb_nil_value();
2185
+ return mrb_fixnum_value(n);
2186
+ }
2187
+
2188
+ static mrb_noreturn void
2189
+ cmperr(mrb_state *mrb, mrb_value v1, mrb_value v2)
2190
+ {
2191
+ mrb_raisef(mrb, E_ARGUMENT_ERROR, "comparison of %t with %t failed", v1, v2);
2192
+ }
2193
+
2194
+ static mrb_value
2195
+ num_lt(mrb_state *mrb, mrb_value self)
2196
+ {
2197
+ mrb_value other = mrb_get_arg1(mrb);
2198
+ mrb_int n = cmpnum(mrb, self, other);
2199
+
2200
+ if (n == -2) cmperr(mrb, self, other);
2201
+ if (n < 0) return mrb_true_value();
2202
+ return mrb_false_value();
2203
+ }
2204
+
2205
+ static mrb_value
2206
+ num_le(mrb_state *mrb, mrb_value self)
2207
+ {
2208
+ mrb_value other = mrb_get_arg1(mrb);
2209
+ mrb_int n = cmpnum(mrb, self, other);
2210
+
2211
+ if (n == -2) cmperr(mrb, self, other);
2212
+ if (n <= 0) return mrb_true_value();
2213
+ return mrb_false_value();
2214
+ }
2215
+
2216
+ static mrb_value
2217
+ num_gt(mrb_state *mrb, mrb_value self)
2218
+ {
2219
+ mrb_value other = mrb_get_arg1(mrb);
2220
+ mrb_int n = cmpnum(mrb, self, other);
2221
+
2222
+ if (n == -2) cmperr(mrb, self, other);
2223
+ if (n > 0) return mrb_true_value();
2224
+ return mrb_false_value();
2225
+ }
2226
+
2227
+ static mrb_value
2228
+ num_ge(mrb_state *mrb, mrb_value self)
2229
+ {
2230
+ mrb_value other = mrb_get_arg1(mrb);
2231
+ mrb_int n = cmpnum(mrb, self, other);
2232
+
2233
+ if (n == -2) cmperr(mrb, self, other);
2234
+ if (n >= 0) return mrb_true_value();
2235
+ return mrb_false_value();
2236
+ }
2237
+
2238
+ /**
2239
+ * Compares two mrb_value objects (obj1 and obj2).
2240
+ *
2241
+ * @param mrb The mruby state.
2242
+ * @param obj1 The first object.
2243
+ * @param obj2 The second object.
2244
+ * @return An mrb_int indicating the comparison result:
2245
+ * - 0 if obj1 is equal to obj2.
2246
+ * - 1 if obj1 is greater than obj2.
2247
+ * - -1 if obj1 is less than obj2.
2248
+ * - -2 if the objects are not comparable (error).
2249
+ * It handles comparisons for integers, floats, bigints, and strings directly.
2250
+ * For other types, it attempts to call the <=> (spaceship) operator on obj1
2251
+ * with obj2 as an argument.
2252
+ */
2253
+ MRB_API mrb_int
2254
+ mrb_cmp(mrb_state *mrb, mrb_value obj1, mrb_value obj2)
2255
+ {
2256
+ mrb_value v;
2257
+
2258
+ if (mrb_fixnum_p(obj1) || mrb_float_p(obj1)) {
2259
+ return cmpnum(mrb, obj1, obj2);
2260
+ }
2261
+ switch (mrb_type(obj1)) {
2262
+ case MRB_TT_INTEGER:
2263
+ case MRB_TT_FLOAT:
2264
+ case MRB_TT_BIGINT:
2265
+ return cmpnum(mrb, obj1, obj2);
2266
+ case MRB_TT_STRING:
2267
+ if (!mrb_string_p(obj2))
2268
+ return -2;
2269
+ return mrb_str_cmp(mrb, obj1, obj2);
2270
+ default:
2271
+ v = mrb_funcall_argv(mrb, obj1, MRB_OPSYM(cmp), 1, &obj2);
2272
+ if (mrb_nil_p(v) || !mrb_integer_p(v))
2273
+ return -2;
2274
+ return mrb_integer(v);
2275
+ }
2276
+ }
2277
+
2278
+ static mrb_value
2279
+ num_finite_p(mrb_state *mrb, mrb_value self)
2280
+ {
2281
+ return mrb_true_value();
2282
+ }
2283
+
2284
+ static mrb_value
2285
+ num_infinite_p(mrb_state *mrb, mrb_value self)
2286
+ {
2287
+ return mrb_false_value();
2288
+ }
2289
+
2290
+ #ifndef MRB_NO_FLOAT
2291
+ static mrb_value
2292
+ flo_hash(mrb_state *mrb, mrb_value flo)
2293
+ {
2294
+ mrb_float f = mrb_float(flo);
2295
+ /* normalize -0.0 to 0.0 */
2296
+ if (f == 0) f = 0.0;
2297
+ return mrb_int_value(mrb, (mrb_int)mrb_byte_hash((uint8_t*)&f, sizeof(f)));
2298
+ }
2299
+ #endif
2300
+
2301
+ /* ------------------------------------------------------------------------*/
2302
+ static const mrb_mt_entry numeric_rom_entries[] = {
2303
+ MRB_MT_ENTRY(num_finite_p, MRB_SYM_Q(finite), MRB_ARGS_NONE()),
2304
+ MRB_MT_ENTRY(num_infinite_p, MRB_SYM_Q(infinite), MRB_ARGS_NONE()),
2305
+ MRB_MT_ENTRY(num_eql, MRB_SYM_Q(eql), MRB_ARGS_REQ(1)), /* 15.2.8.3.16 */
2306
+ #ifndef MRB_NO_FLOAT
2307
+ MRB_MT_ENTRY(num_fdiv, MRB_SYM(fdiv), MRB_ARGS_REQ(1)),
2308
+ #endif
2309
+ };
2310
+
2311
+ static const mrb_mt_entry integer_rom_entries[] = {
2312
+ MRB_MT_ENTRY(int_pow, MRB_OPSYM(pow), MRB_ARGS_REQ(1)),
2313
+ MRB_MT_ENTRY(num_cmp, MRB_OPSYM(cmp), MRB_ARGS_REQ(1)), /* 15.2.8.3.1 */
2314
+ MRB_MT_ENTRY(num_lt, MRB_OPSYM(lt), MRB_ARGS_REQ(1)),
2315
+ MRB_MT_ENTRY(num_le, MRB_OPSYM(le), MRB_ARGS_REQ(1)),
2316
+ MRB_MT_ENTRY(num_gt, MRB_OPSYM(gt), MRB_ARGS_REQ(1)),
2317
+ MRB_MT_ENTRY(num_ge, MRB_OPSYM(ge), MRB_ARGS_REQ(1)),
2318
+ MRB_MT_ENTRY(mrb_obj_itself, MRB_SYM(to_i), MRB_ARGS_NONE()), /* 15.2.8.3.24 */
2319
+ MRB_MT_ENTRY(mrb_obj_itself, MRB_SYM(to_int), MRB_ARGS_NONE()),
2320
+ MRB_MT_ENTRY(int_add, MRB_OPSYM(add), MRB_ARGS_REQ(1)), /* 15.2.8.3.1 */
2321
+ MRB_MT_ENTRY(int_sub, MRB_OPSYM(sub), MRB_ARGS_REQ(1)), /* 15.2.8.3.2 */
2322
+ MRB_MT_ENTRY(int_mul, MRB_OPSYM(mul), MRB_ARGS_REQ(1)), /* 15.2.8.3.3 */
2323
+ MRB_MT_ENTRY(int_mod, MRB_OPSYM(mod), MRB_ARGS_REQ(1)), /* 15.2.8.3.5 */
2324
+ MRB_MT_ENTRY(int_div, MRB_OPSYM(div), MRB_ARGS_REQ(1)), /* 15.2.8.3.6 */
2325
+ MRB_MT_ENTRY(int_quo, MRB_SYM(quo), MRB_ARGS_REQ(1)), /* 15.2.7.4.5(x) */
2326
+ MRB_MT_ENTRY(int_idiv, MRB_SYM(div), MRB_ARGS_REQ(1)),
2327
+ MRB_MT_ENTRY(int_equal, MRB_OPSYM(eq), MRB_ARGS_REQ(1)), /* 15.2.8.3.7 */
2328
+ MRB_MT_ENTRY(int_rev, MRB_OPSYM(neg), MRB_ARGS_NONE()), /* 15.2.8.3.8 */
2329
+ MRB_MT_ENTRY(int_and, MRB_OPSYM(and), MRB_ARGS_REQ(1)), /* 15.2.8.3.9 */
2330
+ MRB_MT_ENTRY(int_or, MRB_OPSYM(or), MRB_ARGS_REQ(1)), /* 15.2.8.3.10 */
2331
+ MRB_MT_ENTRY(int_xor, MRB_OPSYM(xor), MRB_ARGS_REQ(1)), /* 15.2.8.3.11 */
2332
+ MRB_MT_ENTRY(int_lshift, MRB_OPSYM(lshift), MRB_ARGS_REQ(1)), /* 15.2.8.3.12 */
2333
+ MRB_MT_ENTRY(int_rshift, MRB_OPSYM(rshift), MRB_ARGS_REQ(1)), /* 15.2.8.3.13 */
2334
+ MRB_MT_ENTRY(int_ceil, MRB_SYM(ceil), MRB_ARGS_OPT(1)), /* 15.2.8.3.14 */
2335
+ MRB_MT_ENTRY(int_floor, MRB_SYM(floor), MRB_ARGS_OPT(1)), /* 15.2.8.3.17 */
2336
+ MRB_MT_ENTRY(int_round, MRB_SYM(round), MRB_ARGS_OPT(1)), /* 15.2.8.3.20 */
2337
+ MRB_MT_ENTRY(int_truncate, MRB_SYM(truncate), MRB_ARGS_OPT(1)), /* 15.2.8.3.26 */
2338
+ MRB_MT_ENTRY(int_hash, MRB_SYM(hash), MRB_ARGS_NONE()), /* 15.2.8.3.18 */
2339
+ MRB_MT_ENTRY(int_to_s, MRB_SYM(to_s), MRB_ARGS_OPT(1)), /* 15.2.8.3.25 */
2340
+ MRB_MT_ENTRY(int_to_s, MRB_SYM(inspect), MRB_ARGS_OPT(1)),
2341
+ MRB_MT_ENTRY(int_divmod, MRB_SYM(divmod), MRB_ARGS_REQ(1)), /* 15.2.8.3.30(x) */
2342
+ MRB_MT_ENTRY(coerce_step_counter, MRB_SYM(__coerce_step_counter), MRB_ARGS_REQ(1)),
2343
+ #ifndef MRB_NO_FLOAT
2344
+ MRB_MT_ENTRY(int_fdiv, MRB_SYM(fdiv), MRB_ARGS_REQ(1)),
2345
+ MRB_MT_ENTRY(int_to_f, MRB_SYM(to_f), MRB_ARGS_NONE()), /* 15.2.8.3.23 */
2346
+ #endif
2347
+ };
2348
+
2349
+ #ifndef MRB_NO_FLOAT
2350
+ static const mrb_mt_entry float_rom_entries[] = {
2351
+ MRB_MT_ENTRY(flo_pow, MRB_OPSYM(pow), MRB_ARGS_REQ(1)),
2352
+ MRB_MT_ENTRY(flo_div, MRB_OPSYM(div), MRB_ARGS_REQ(1)), /* 15.2.9.3.6 */
2353
+ MRB_MT_ENTRY(flo_div, MRB_SYM(quo), MRB_ARGS_REQ(1)), /* 15.2.7.4.5(x) */
2354
+ MRB_MT_ENTRY(flo_div, MRB_SYM(fdiv), MRB_ARGS_REQ(1)),
2355
+ MRB_MT_ENTRY(flo_idiv, MRB_SYM(div), MRB_ARGS_REQ(1)),
2356
+ MRB_MT_ENTRY(flo_add, MRB_OPSYM(add), MRB_ARGS_REQ(1)), /* 15.2.9.3.3 */
2357
+ MRB_MT_ENTRY(flo_sub, MRB_OPSYM(sub), MRB_ARGS_REQ(1)), /* 15.2.9.3.4 */
2358
+ MRB_MT_ENTRY(flo_mul, MRB_OPSYM(mul), MRB_ARGS_REQ(1)), /* 15.2.9.3.5 */
2359
+ MRB_MT_ENTRY(flo_mod, MRB_OPSYM(mod), MRB_ARGS_REQ(1)), /* 15.2.9.3.7 */
2360
+ MRB_MT_ENTRY(num_cmp, MRB_OPSYM(cmp), MRB_ARGS_REQ(1)), /* 15.2.8.3.1 */
2361
+ MRB_MT_ENTRY(num_lt, MRB_OPSYM(lt), MRB_ARGS_REQ(1)),
2362
+ MRB_MT_ENTRY(num_le, MRB_OPSYM(le), MRB_ARGS_REQ(1)),
2363
+ MRB_MT_ENTRY(num_gt, MRB_OPSYM(gt), MRB_ARGS_REQ(1)),
2364
+ MRB_MT_ENTRY(num_ge, MRB_OPSYM(ge), MRB_ARGS_REQ(1)),
2365
+ MRB_MT_ENTRY(flo_eq, MRB_OPSYM(eq), MRB_ARGS_REQ(1)), /* 15.2.9.3.2 */
2366
+ MRB_MT_ENTRY(flo_ceil, MRB_SYM(ceil), MRB_ARGS_OPT(1)), /* 15.2.9.3.8 */
2367
+ MRB_MT_ENTRY(flo_finite_p, MRB_SYM_Q(finite), MRB_ARGS_NONE()), /* 15.2.9.3.9 */
2368
+ MRB_MT_ENTRY(flo_floor, MRB_SYM(floor), MRB_ARGS_OPT(1)), /* 15.2.9.3.10 */
2369
+ MRB_MT_ENTRY(flo_infinite_p, MRB_SYM_Q(infinite), MRB_ARGS_NONE()), /* 15.2.9.3.11 */
2370
+ MRB_MT_ENTRY(flo_round, MRB_SYM(round), MRB_ARGS_OPT(1)), /* 15.2.9.3.12 */
2371
+ MRB_MT_ENTRY(mrb_obj_itself, MRB_SYM(to_f), MRB_ARGS_NONE()), /* 15.2.9.3.13 */
2372
+ MRB_MT_ENTRY(flo_to_i, MRB_SYM(to_i), MRB_ARGS_NONE()), /* 15.2.9.3.14 */
2373
+ MRB_MT_ENTRY(flo_truncate, MRB_SYM(truncate), MRB_ARGS_OPT(1)), /* 15.2.9.3.15 */
2374
+ MRB_MT_ENTRY(flo_divmod, MRB_SYM(divmod), MRB_ARGS_REQ(1)),
2375
+ MRB_MT_ENTRY(flo_to_s, MRB_SYM(to_s), MRB_ARGS_NONE()), /* 15.2.9.3.16(x) */
2376
+ MRB_MT_ENTRY(flo_to_s, MRB_SYM(inspect), MRB_ARGS_NONE()),
2377
+ MRB_MT_ENTRY(flo_nan_p, MRB_SYM_Q(nan), MRB_ARGS_NONE()),
2378
+ MRB_MT_ENTRY(flo_abs, MRB_SYM(abs), MRB_ARGS_NONE()), /* 15.2.7.4.3 */
2379
+ MRB_MT_ENTRY(flo_hash, MRB_SYM(hash), MRB_ARGS_NONE()),
2380
+ };
2381
+ #endif /* !MRB_NO_FLOAT */
2382
+
2383
+ void
2384
+ mrb_init_numeric(mrb_state *mrb)
2385
+ {
2386
+ struct RClass *numeric, *integer;
2387
+ #ifndef MRB_NO_FLOAT
2388
+ struct RClass *fl;
2389
+ #endif
2390
+
2391
+ /* Numeric Class */
2392
+ numeric = mrb_define_class_id(mrb, MRB_SYM(Numeric), mrb->object_class); /* 15.2.7 */
2393
+ MRB_MT_INIT_ROM(mrb, numeric, numeric_rom_entries);
2394
+
2395
+ /* Integer Class */
2396
+ mrb->integer_class = integer = mrb_define_class_id(mrb, MRB_SYM(Integer), numeric); /* 15.2.8 */
2397
+ MRB_SET_INSTANCE_TT(integer, MRB_TT_INTEGER);
2398
+ MRB_UNDEF_ALLOCATOR(integer);
2399
+ mrb_undef_class_method_id(mrb, integer, MRB_SYM(new));
2400
+ MRB_MT_INIT_ROM(mrb, integer, integer_rom_entries);
2401
+
2402
+ /* Fixnum Class for compatibility */
2403
+ mrb_define_const_id(mrb, mrb->object_class, MRB_SYM(Fixnum), mrb_obj_value(integer));
2404
+
2405
+ #ifndef MRB_NO_FLOAT
2406
+ /* Float Class */
2407
+ mrb->float_class = fl = mrb_define_class_id(mrb, MRB_SYM(Float), numeric); /* 15.2.9 */
2408
+ MRB_SET_INSTANCE_TT(fl, MRB_TT_FLOAT);
2409
+ MRB_UNDEF_ALLOCATOR(fl);
2410
+ mrb_undef_class_method(mrb, fl, "new");
2411
+ MRB_MT_INIT_ROM(mrb, fl, float_rom_entries);
2412
+
2413
+ #ifdef INFINITY
2414
+ mrb_define_const_id(mrb, fl, MRB_SYM(INFINITY), mrb_float_value(mrb, INFINITY));
2415
+ #endif
2416
+ #ifdef NAN
2417
+ mrb_define_const_id(mrb, fl, MRB_SYM(NAN), mrb_float_value(mrb, NAN));
2418
+ #endif
2419
+ #endif
2420
+ }