summaryrefslogtreecommitdiffhomepage
path: root/ext
diff options
context:
space:
mode:
authorTom Black <[email protected]>2017-04-26 22:16:19 -0400
committerTom Black <[email protected]>2017-04-28 18:08:26 -0400
commit8d39db97fd82e99015d31fa38436fea6c1530a87 (patch)
treef5303da94a6986cc6d72371d125755eb3e282ad9 /ext
parent10d9acbe39a6e649e8752c3d06d1157d43b6e73a (diff)
downloadruby2d-8d39db97fd82e99015d31fa38436fea6c1530a87.tar.gz
ruby2d-8d39db97fd82e99015d31fa38436fea6c1530a87.zip
Add `File.exists?` to MRuby
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!
Diffstat (limited to 'ext')
-rw-r--r--ext/ruby2d/ruby2d.c20
1 files changed, 20 insertions, 0 deletions
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
@@ -148,6 +148,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