From 8d39db97fd82e99015d31fa38436fea6c1530a87 Mon Sep 17 00:00:00 2001 From: Tom Black Date: Wed, 26 Apr 2017 22:16:19 -0400 Subject: Add `File.exists?` to MRuby MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This also enables file checking to the `Image` class for MRI and MRuby (on the web, a “failed to load resource” already appears). Also, all exceptions will now be printed in MRuby, yay! --- ext/ruby2d/ruby2d.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'ext') diff --git a/ext/ruby2d/ruby2d.c b/ext/ruby2d/ruby2d.c index 6c3dc4e..d91e0dc 100644 --- a/ext/ruby2d/ruby2d.c +++ b/ext/ruby2d/ruby2d.c @@ -147,6 +147,18 @@ static void free_window() { } +/* + * File#exists? for MRuby + */ +#if MRUBY +static R_VAL file_exists(mrb_state* mrb, R_VAL self) { + mrb_value path; + mrb_get_args(mrb, "o", &path); + return S2D_FileExists(RSTRING_PTR(path)) ? R_TRUE : R_FALSE; +} +#endif + + /* * Ruby2D::Image#init * Initialize image structure data @@ -793,6 +805,11 @@ int main(void) { // Load the Ruby 2D library mrb_load_irep(mrb, ruby2d_lib); + + // Add missing MRuby classes, methods + R_CLASS file_class = mrb_define_class(mrb, "File", mrb->object_class); + mrb_define_class_method(mrb, file_class, "exists?", file_exists, r_args_req(1)); + #else /* * Ruby C extension init @@ -867,6 +884,9 @@ void Init_ruby2d() { // Load the Ruby 2D app mrb_load_irep(mrb, ruby2d_app); + // If an exception, print error + if (mrb->exc) mrb_print_error(mrb); + // Close the MRuby environment mrb_close(mrb); #endif -- cgit v1.2.3