[Groonga-commit] groonga/groonga at 58648df [master] mrb: implement require

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Dec 23 00:47:49 JST 2014


Kouhei Sutou	2014-12-23 00:47:49 +0900 (Tue, 23 Dec 2014)

  New Revision: 58648dfa4d47062f8ac65622ec204e1ed10c30a6
  https://github.com/groonga/groonga/commit/58648dfa4d47062f8ac65622ec204e1ed10c30a6

  Message:
    mrb: implement require

  Added files:
    lib/mrb/scripts/require.rb
  Modified files:
    lib/ctx_impl_mrb.c
    lib/mrb/scripts/sources.am

  Modified: lib/ctx_impl_mrb.c (+17 -0)
===================================================================
--- lib/ctx_impl_mrb.c    2014-12-23 00:19:53 +0900 (7c5dd97)
+++ lib/ctx_impl_mrb.c    2014-12-23 00:47:49 +0900 (274bd01)
@@ -40,6 +40,11 @@
 #include "mrb/mrb_procedure.h"
 
 #ifdef GRN_WITH_MRUBY
+# include <mruby/array.h>
+# include <mruby/variable.h>
+#endif
+
+#ifdef GRN_WITH_MRUBY
 static mrb_value
 mrb_kernel_load(mrb_state *mrb, mrb_value self)
 {
@@ -68,6 +73,18 @@ grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx)
                     "load", mrb_kernel_load, MRB_ARGS_REQ(1));
 
   grn_mrb_load(ctx, "backtrace_entry.rb");
+  {
+    mrb_value load_path;
+    const char *system_ruby_scripts_dir;
+
+    load_path = mrb_ary_new(mrb);
+    system_ruby_scripts_dir = grn_mrb_get_system_ruby_scripts_dir(ctx);
+    mrb_ary_push(mrb, load_path,
+                 mrb_str_new_cstr(mrb, system_ruby_scripts_dir));
+    mrb_gv_set(mrb, mrb_intern_cstr(mrb, "$LOAD_PATH"), load_path);
+  }
+
+  grn_mrb_load(ctx, "require.rb");
 
   grn_mrb_error_init(ctx);
   grn_mrb_id_init(ctx);

  Added: lib/mrb/scripts/require.rb (+68 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/require.rb    2014-12-23 00:47:49 +0900 (a951aed)
@@ -0,0 +1,68 @@
+$" = []
+
+class ScriptLoader
+  @@loading_paths = {}
+
+  def initialize(path)
+    @base_path = path
+  end
+
+  def load_once
+    if absolete_path?(@base_path)
+      loaded = load_once_path(@base_path)
+      if loaded.nil?
+        raise LoadError, error_message
+      else
+        loaded
+      end
+    else
+      $LOAD_PATH.each do |load_path|
+        unless absolete_path?(load_path)
+          load_path = File.expand_path(load_path)
+        end
+        loaded = load_once_path(File.join(load_path, @base_path))
+        return loaded unless loaded.nil?
+      end
+      raise LoadError, error_message
+    end
+  end
+
+  private
+  def error_message
+    "cannot load such file -- #{@base_path}"
+  end
+
+  def absolete_path?(path)
+    path.start_with?("/")
+  end
+
+  def load_once_path(path)
+    loaded = load_once_absolete_path(path)
+    return loaded unless loaded.nil?
+
+    return nil unless File.extname(path).empty?
+
+    load_once_absolete_path("#{path}.rb")
+  end
+
+  def load_once_absolete_path(path)
+    return false if $".include?(path)
+    return false if @@loading_paths.key?(path)
+
+    return nil unless File.exist?(path)
+
+    @@loading_paths[path] = true
+    load(path)
+    $" << path
+    @@loading_paths.delete(path)
+
+    true
+  end
+end
+
+module Kernel
+  def require(path)
+    loader = ScriptLoader.new(path)
+    loader.load_once
+  end
+end

  Modified: lib/mrb/scripts/sources.am (+1 -0)
===================================================================
--- lib/mrb/scripts/sources.am    2014-12-23 00:19:53 +0900 (93ade16)
+++ lib/mrb/scripts/sources.am    2014-12-23 00:47:49 +0900 (1fca7b5)
@@ -8,6 +8,7 @@ RUBY_SCRIPT_FILES =				\
 	index_info.rb				\
 	logger.rb				\
 	logger/level.rb				\
+	require.rb				\
 	scan_info.rb				\
 	scan_info_builder.rb			\
 	scan_info_data.rb
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index