diff options
Diffstat (limited to 'mrbgems/mruby-sleep')
| -rw-r--r-- | mrbgems/mruby-sleep/.gitignore | 4 | ||||
| -rw-r--r-- | mrbgems/mruby-sleep/.travis.yml | 29 | ||||
| -rw-r--r-- | mrbgems/mruby-sleep/.travis_build_config.rb | 6 | ||||
| -rw-r--r-- | mrbgems/mruby-sleep/README.md | 27 | ||||
| -rw-r--r-- | mrbgems/mruby-sleep/Rakefile | 29 | ||||
| -rw-r--r-- | mrbgems/mruby-sleep/example/sleep.rb | 3 | ||||
| -rw-r--r-- | mrbgems/mruby-sleep/mrbgem.rake | 5 | ||||
| -rw-r--r-- | mrbgems/mruby-sleep/src/mrb_sleep.c | 135 | ||||
| -rw-r--r-- | mrbgems/mruby-sleep/test/sleep_test.rb | 29 |
9 files changed, 267 insertions, 0 deletions
diff --git a/mrbgems/mruby-sleep/.gitignore b/mrbgems/mruby-sleep/.gitignore new file mode 100644 index 000000000..b9f9e71df --- /dev/null +++ b/mrbgems/mruby-sleep/.gitignore @@ -0,0 +1,4 @@ +/mruby + +*.so +*.a
\ No newline at end of file diff --git a/mrbgems/mruby-sleep/.travis.yml b/mrbgems/mruby-sleep/.travis.yml new file mode 100644 index 000000000..ad6b007b6 --- /dev/null +++ b/mrbgems/mruby-sleep/.travis.yml @@ -0,0 +1,29 @@ +dist: trusty + +language: c +compiler: + - gcc + - clang +env: + - MRUBY_VERSION=1.2.0 + - MRUBY_VERSION=master +matrix: + allow_failures: + - env: MRUBY_VERSION=master +branches: + only: + - master +addons: + apt: + packages: + - rake + - bison + - git + - gperf + # - aclocal + # - automake + # - autoconf + # - autotools-dev + +script: + - rake test
\ No newline at end of file diff --git a/mrbgems/mruby-sleep/.travis_build_config.rb b/mrbgems/mruby-sleep/.travis_build_config.rb new file mode 100644 index 000000000..b32e38a9d --- /dev/null +++ b/mrbgems/mruby-sleep/.travis_build_config.rb @@ -0,0 +1,6 @@ +MRuby::Build.new do |conf| + toolchain :gcc + conf.gembox 'default' + conf.gem '../mruby-sleep' + conf.enable_test +end diff --git a/mrbgems/mruby-sleep/README.md b/mrbgems/mruby-sleep/README.md new file mode 100644 index 000000000..7707cd040 --- /dev/null +++ b/mrbgems/mruby-sleep/README.md @@ -0,0 +1,27 @@ +# Sleep Module for mruby +mruby sleep module + +## install by mrbgems + - add conf.gem line to `build_config.rb` +```ruby +MRuby::Build.new do |conf| + + # ... (snip) ... + + conf.gem :core => 'mruby-sleep' +end +``` + +## example + +```ruby +sleep(10) +usleep(10000) +``` + +# License +under the MIT License: + +* http://www.opensource.org/licenses/mit-license.php + + diff --git a/mrbgems/mruby-sleep/Rakefile b/mrbgems/mruby-sleep/Rakefile new file mode 100644 index 000000000..5e3c46173 --- /dev/null +++ b/mrbgems/mruby-sleep/Rakefile @@ -0,0 +1,29 @@ +MRUBY_CONFIG=File.expand_path(ENV["MRUBY_CONFIG"] || ".travis_build_config.rb") +MRUBY_VERSION=ENV["MRUBY_VERSION"] || "1.2.0" + +file :mruby do + cmd = "git clone --depth=1 git://github.com/mruby/mruby.git" + if MRUBY_VERSION != 'master' + cmd << " && cd mruby" + cmd << " && git fetch --tags && git checkout $(git rev-parse #{MRUBY_VERSION})" + end + sh cmd +end + +desc "compile binary" +task :compile => :mruby do + sh "cd mruby && MRUBY_CONFIG=#{MRUBY_CONFIG} rake all" +end + +desc "test" +task :test => :mruby do + sh "cd mruby && MRUBY_CONFIG=#{MRUBY_CONFIG} rake all test" +end + +desc "cleanup" +task :clean do + exit 0 unless File.directory?('mruby') + sh "cd mruby && rake deep_clean" +end + +task :default => :compile diff --git a/mrbgems/mruby-sleep/example/sleep.rb b/mrbgems/mruby-sleep/example/sleep.rb new file mode 100644 index 000000000..e5acea3b2 --- /dev/null +++ b/mrbgems/mruby-sleep/example/sleep.rb @@ -0,0 +1,3 @@ +sleep(10) +usleep(10000) + diff --git a/mrbgems/mruby-sleep/mrbgem.rake b/mrbgems/mruby-sleep/mrbgem.rake new file mode 100644 index 000000000..8827b3580 --- /dev/null +++ b/mrbgems/mruby-sleep/mrbgem.rake @@ -0,0 +1,5 @@ +MRuby::Gem::Specification.new('mruby-sleep') do |spec| + spec.license = 'MIT' + spec.authors = 'MATSUMOTO Ryosuke' + spec.version = '0.0.1' +end diff --git a/mrbgems/mruby-sleep/src/mrb_sleep.c b/mrbgems/mruby-sleep/src/mrb_sleep.c new file mode 100644 index 000000000..3f8ef90cf --- /dev/null +++ b/mrbgems/mruby-sleep/src/mrb_sleep.c @@ -0,0 +1,135 @@ +/* +** mrb_sleep - sleep methods for mruby +** +** Copyright (c) mod_mruby developers 2012- +** +** Permission is hereby granted, free of charge, to any person obtaining +** a copy of this software and associated documentation files (the +** "Software"), to deal in the Software without restriction, including +** without limitation the rights to use, copy, modify, merge, publish, +** distribute, sublicense, and/or sell copies of the Software, and to +** permit persons to whom the Software is furnished to do so, subject to +** the following conditions: +** +** The above copyright notice and this permission notice shall be +** included in all copies or substantial portions of the Software. +** +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +** +** [ MIT license: http://www.opensource.org/licenses/mit-license.php ] +*/ + +#include <time.h> +#ifdef _WIN32 + #include <windows.h> + #define sleep(x) Sleep(x * 1000) + #define usleep(x) Sleep((DWORD)((x)<1000) ? 1 : ((x)/1000)) +#else + #include <unistd.h> + #include <sys/time.h> +#endif + +#include "mruby.h" + +/* not implemented forever sleep (called without an argument)*/ +static mrb_value +mrb_f_sleep(mrb_state *mrb, mrb_value self) +{ + time_t beg = time(0); + time_t end; +#ifndef MRB_WITHOUT_FLOAT + mrb_float sec; + + mrb_get_args(mrb, "f", &sec); + if (sec >= 0) { + usleep(sec * 1000000); + } + else { + mrb_raise(mrb, E_ARGUMENT_ERROR, "time interval must not be negative"); + } +#else + mrb_int sec; + + mrb_get_args(mrb, "i", &sec); + if (sec >= 0) { + sleep(sec); + } else { + mrb_raise(mrb, E_ARGUMENT_ERROR, "time interval must not be negative"); + } +#endif + end = time(0) - beg; + + return mrb_fixnum_value(end); +} + +/* mruby special; needed for mruby without float numbers */ +static mrb_value +mrb_f_usleep(mrb_state *mrb, mrb_value self) +{ + mrb_int usec; +#ifdef _WIN32 + FILETIME st_ft,ed_ft; + unsigned __int64 st_time = 0; + unsigned __int64 ed_time = 0; +#else + struct timeval st_tm,ed_tm; +#endif + time_t slp_tm; + +#ifdef _WIN32 + GetSystemTimeAsFileTime(&st_ft); +#else + gettimeofday(&st_tm, NULL); +#endif + + /* not implemented forever sleep (called without an argument)*/ + mrb_get_args(mrb, "i", &usec); + + if (usec >= 0) { + usleep(usec); + } else { + mrb_raise(mrb, E_ARGUMENT_ERROR, "time interval must not be negative integer"); + } + +#ifdef _WIN32 + GetSystemTimeAsFileTime(&ed_ft); + + st_time |= st_ft.dwHighDateTime; + st_time <<=32; + st_time |= st_ft.dwLowDateTime; + ed_time |= ed_ft.dwHighDateTime; + ed_time <<=32; + ed_time |= ed_ft.dwLowDateTime; + + slp_tm = (ed_time - st_time) / 10; +#else + gettimeofday(&ed_tm, NULL); + + if (st_tm.tv_usec > ed_tm.tv_usec) { + slp_tm = 1000000 + ed_tm.tv_usec - st_tm.tv_usec; + } + else { + slp_tm = ed_tm.tv_usec - st_tm.tv_usec; + } +#endif + + return mrb_fixnum_value(slp_tm); +} + +void +mrb_mruby_sleep_gem_init(mrb_state *mrb) +{ + mrb_define_method(mrb, mrb->kernel_module, "sleep", mrb_f_sleep, MRB_ARGS_REQ(1)); + mrb_define_method(mrb, mrb->kernel_module, "usleep", mrb_f_usleep, MRB_ARGS_REQ(1)); +} + +void +mrb_mruby_sleep_gem_final(mrb_state *mrb) +{ +} diff --git a/mrbgems/mruby-sleep/test/sleep_test.rb b/mrbgems/mruby-sleep/test/sleep_test.rb new file mode 100644 index 000000000..f05b7a30b --- /dev/null +++ b/mrbgems/mruby-sleep/test/sleep_test.rb @@ -0,0 +1,29 @@ +assert("sleep works") do + assert_nothing_raised { sleep(1) } + assert_nothing_raised { sleep(0) } +end + +assert("sleep would accept non-negative float value") do + skip unless Object.const_defined?(:Float) + assert_nothing_raised { sleep(0.01) } + assert_nothing_raised { sleep(0.0) } + assert_nothing_raised { sleep(-0.0) } +end + +assert("sleep would not accept negative integer value") do + assert_raise(ArgumentError) { sleep(-1) } +end + +assert("sleep would not accept negative float value") do + skip unless Object.const_defined?(:Float) + assert_raise(ArgumentError) { sleep(-0.1) } +end + +assert("usleep works") do + assert_nothing_raised { usleep(100) } + assert_nothing_raised { usleep(0) } +end + +assert("usleep would not accept negative value") do + assert_raise(ArgumentError) { usleep(-100) } +end |
