[Groonga-commit] groonga/groonga at 2e2e361 [master] plugin ruby: re-implement by Ruby

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Mar 30 14:29:13 JST 2016


Kouhei Sutou	2016-03-30 14:29:13 +0900 (Wed, 30 Mar 2016)

  New Revision: 2e2e3612b547be8e2e0d25c619fbab0ffcc9d882
  https://github.com/groonga/groonga/commit/2e2e3612b547be8e2e0d25c619fbab0ffcc9d882

  Message:
    plugin ruby: re-implement by Ruby
    
    ruby/load is removed. Because it's useless and (maybe) nobody uses it.

  Added files:
    plugins/ruby/eval.rb
    plugins/ruby/sources.am
  Removed files:
    lib/mrb/scripts/test/empty.rb
    plugins/ruby/eval.c
    plugins/ruby/eval_sources.am
    plugins/ruby/load.c
    plugins/ruby/load_sources.am
    plugins/ruby/ruby_plugin.h
    test/command/suite/ruby/load/existent.expected
    test/command/suite/ruby/load/existent.test
    test/command/suite/ruby/load/nonexistent.expected
    test/command/suite/ruby/load/nonexistent.test
  Modified files:
    lib/ctx_impl_mrb.c
    lib/grn_mrb.h
    lib/mrb/scripts/eval_context.rb
    lib/mrb/sources.am
    plugins/ruby/CMakeLists.txt
    plugins/ruby/Makefile.am
    test/command/suite/ruby/eval/symbol.expected

  Modified: lib/ctx_impl_mrb.c (+2 -0)
===================================================================
--- lib/ctx_impl_mrb.c    2016-03-30 10:04:38 +0900 (dc7a925)
+++ lib/ctx_impl_mrb.c    2016-03-30 14:29:13 +0900 (d8c6d04)
@@ -66,6 +66,7 @@
 # include "mrb/mrb_content_type.h"
 # include "mrb/mrb_writer.h"
 # include "mrb/mrb_config.h"
+# include "mrb/mrb_eval_context.h"
 
 # include <mruby/array.h>
 # include <mruby/string.h>
@@ -179,6 +180,7 @@ mrb_groonga_init(mrb_state *mrb, mrb_value self)
   grn_mrb_content_type_init(ctx);
   grn_mrb_writer_init(ctx);
   grn_mrb_config_init(ctx);
+  grn_mrb_eval_context_init(ctx);
 
   grn_mrb_load(ctx, "initialize/post.rb");
 

  Modified: lib/grn_mrb.h (+0 -2)
===================================================================
--- lib/grn_mrb.h    2016-03-30 10:04:38 +0900 (f7bba8c)
+++ lib/grn_mrb.h    2016-03-30 14:29:13 +0900 (832aa94)
@@ -32,9 +32,7 @@ extern "C" {
 void grn_mrb_init_from_env(void);
 
 #ifdef GRN_WITH_MRUBY
-GRN_API mrb_value grn_mrb_eval(grn_ctx *ctx, const char *script, int script_length);
 GRN_API mrb_value grn_mrb_load(grn_ctx *ctx, const char *path);
-GRN_API grn_rc grn_mrb_to_grn(grn_ctx *ctx, mrb_value mrb_object, grn_obj *grn_object);
 GRN_API const char *grn_mrb_get_system_ruby_scripts_dir(grn_ctx *ctx);
 #endif
 

  Modified: lib/mrb/scripts/eval_context.rb (+5 -0)
===================================================================
--- lib/mrb/scripts/eval_context.rb    2016-03-30 10:04:38 +0900 (546c130)
+++ lib/mrb/scripts/eval_context.rb    2016-03-30 14:29:13 +0900 (05c7ee8)
@@ -1,5 +1,10 @@
 module Groonga
   class EvalContext
+    def eval(script)
+      proc = compile(script)
+      instance_eval(&proc)
+    end
+
     def method_missing(id, *args, &block)
       return super unless args.empty?
       return super if block_given?

  Deleted: lib/mrb/scripts/test/empty.rb (+0 -1) 100644
===================================================================
--- lib/mrb/scripts/test/empty.rb    2016-03-30 10:04:38 +0900 (123002d)
+++ /dev/null
@@ -1 +0,0 @@
-# This file is just for test.

  Modified: lib/mrb/sources.am (+2 -0)
===================================================================
--- lib/mrb/sources.am    2016-03-30 10:04:38 +0900 (997b0b6)
+++ lib/mrb/sources.am    2016-03-30 14:29:13 +0900 (b30e834)
@@ -29,6 +29,8 @@ libgrnmrb_la_SOURCES =				\
 	mrb_double_array_trie.h			\
 	mrb_error.c				\
 	mrb_error.h				\
+	mrb_eval_context.c			\
+	mrb_eval_context.h			\
 	mrb_expr.c				\
 	mrb_expr.h				\
 	mrb_fixed_size_column.c			\

  Modified: plugins/ruby/CMakeLists.txt (+7 -43)
===================================================================
--- plugins/ruby/CMakeLists.txt    2016-03-30 10:04:38 +0900 (63a7993)
+++ plugins/ruby/CMakeLists.txt    2016-03-30 14:29:13 +0900 (7d34be1)
@@ -1,4 +1,4 @@
-# Copyright(C) 2013 Brazil
+# Copyright(C) 2013-2016 Brazil
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -13,48 +13,12 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
-include_directories(
-  ${CMAKE_CURRENT_SOURCE_DIR}/../../lib
-  ${MRUBY_INCLUDE_DIRS})
+if(NOT GRN_EMBED)
+  if(GRN_WITH_MRUBY)
+    set(GRN_RELATIVE_RUBY_PLUGINS_DIR "${GRN_RELATIVE_PLUGINS_DIR}/ruby")
 
-if(GRN_WITH_MRUBY)
-  set(GRN_RELATIVE_RUBY_PLUGINS_DIR "${GRN_RELATIVE_PLUGINS_DIR}/ruby")
-
-  read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/eval_sources.am RUBY_EVAL_SOURCES)
-  set_source_files_properties(${RUBY_EVAL_SOURCES}
-    PROPERTIES
-    COMPILE_FLAGS "${GRN_C_COMPILE_FLAGS}")
-  if(GRN_EMBED)
-    add_library(ruby_eval STATIC ${RUBY_EVAL_SOURCES})
-    set_target_properties(
-      ruby_eval
-      PROPERTIES
-      POSITION_INDEPENDENT_CODE ON)
-  else()
-    add_library(ruby_eval MODULE ${RUBY_EVAL_SOURCES})
-    set_target_properties(ruby_eval PROPERTIES
-      PREFIX ""
-      OUTPUT_NAME "eval")
-    install(TARGETS ruby_eval DESTINATION "${GRN_RELATIVE_RUBY_PLUGINS_DIR}")
-  endif()
-  target_link_libraries(ruby_eval libgroonga)
-
-  read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/load_sources.am RUBY_LOAD_SOURCES)
-  set_source_files_properties(${RUBY_LOAD_SOURCES}
-    PROPERTIES
-    COMPILE_FLAGS "${GRN_C_COMPILE_FLAGS}")
-  if(GRN_EMBED)
-    add_library(ruby_load STATIC ${RUBY_LOAD_SOURCES})
-    set_target_properties(
-      ruby_load
-      PROPERTIES
-      POSITION_INDEPENDENT_CODE ON)
-  else()
-    add_library(ruby_load MODULE ${RUBY_LOAD_SOURCES})
-    set_target_properties(ruby_load PROPERTIES
-      PREFIX ""
-      OUTPUT_NAME "load")
-    install(TARGETS ruby_load DESTINATION "${GRN_RELATIVE_RUBY_PLUGINS_DIR}")
+    read_file_list(${CMAKE_CURRENT_SOURCE_DIR}/sources.am RUBY_SCRIPTS)
+    install(FILES ${RUBY_SCRIPTS}
+      DESTINATION "${GRN_RELATIVE_RUBY_PLUGINS_DIR}")
   endif()
-  target_link_libraries(ruby_load libgroonga)
 endif()

  Modified: plugins/ruby/Makefile.am (+3 -23)
===================================================================
--- plugins/ruby/Makefile.am    2016-03-30 10:04:38 +0900 (381fb47)
+++ plugins/ruby/Makefile.am    2016-03-30 14:29:13 +0900 (a494972)
@@ -1,29 +1,9 @@
 EXTRA_DIST =					\
 	CMakeLists.txt
 
-AM_CFLAGS =					\
-	$(MESSAGE_PACK_CFLAGS)			\
-	$(MRUBY_CFLAGS)
-
-AM_CPPFLAGS =			\
-	-I$(top_builddir)	\
-	-I$(top_srcdir)/include	\
-	-I$(top_srcdir)/lib
-
-AM_LDFLAGS =					\
-	-avoid-version				\
-	-module					\
-	-no-undefined
-
-LIBS =						\
-	$(top_builddir)/lib/libgroonga.la	\
-	$(MESSAGE_PACK_LIBS)
-
 if WITH_MRUBY
-ruby_plugins_LTLIBRARIES =			\
-	eval.la					\
-	load.la
+dist_ruby_plugins_DATA =			\
+	$(ruby_scripts)
 endif
 
-include eval_sources.am
-include load_sources.am
+include sources.am

  Deleted: plugins/ruby/eval.c (+0 -68) 100644
===================================================================
--- plugins/ruby/eval.c    2016-03-30 10:04:38 +0900 (bacd901)
+++ /dev/null
@@ -1,68 +0,0 @@
-/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
-/*
-  Copyright(C) 2013 Brazil
-
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Lesser General Public
-  License version 2.1 as published by the Free Software Foundation.
-
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public
-  License along with this library; if not, write to the Free Software
-  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-*/
-
-#ifdef GRN_EMBEDDED
-#  define GRN_PLUGIN_FUNCTION_TAG ruby_eval
-#endif
-
-#include "ruby_plugin.h"
-
-static grn_obj *
-command_ruby_eval(grn_ctx *ctx, int nargs, grn_obj **args,
-                  grn_user_data *user_data)
-{
-  mrb_state *mrb = ctx->impl->mrb.state;
-  grn_obj *script;
-  mrb_value result;
-
-  script = VAR(0);
-  switch (script->header.domain) {
-  case GRN_DB_SHORT_TEXT :
-  case GRN_DB_TEXT :
-  case GRN_DB_LONG_TEXT :
-    break;
-  default :
-    {
-      grn_obj inspected;
-      GRN_TEXT_INIT(&inspected, 0);
-      grn_inspect(ctx, &inspected, script);
-      ERR(GRN_INVALID_ARGUMENT, "script must be a string: <%.*s>",
-          (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected));
-      GRN_OBJ_FIN(ctx, &inspected);
-      return NULL;
-    }
-    break;
-  }
-
-  mrb->exc = NULL;
-  result = grn_mrb_eval(ctx, GRN_TEXT_VALUE(script), GRN_TEXT_LEN(script));
-  output_result(ctx, result);
-
-  return NULL;
-}
-
-grn_rc
-GRN_PLUGIN_REGISTER(grn_ctx *ctx)
-{
-  grn_expr_var vars[1];
-
-  grn_plugin_expr_var_init(ctx, &vars[0], "script", -1);
-  grn_plugin_command_create(ctx, "ruby_eval", -1, command_ruby_eval, 1, vars);
-
-  return ctx->rc;
-}

  Added: plugins/ruby/eval.rb (+36 -0) 100644
===================================================================
--- /dev/null
+++ plugins/ruby/eval.rb    2016-03-30 14:29:13 +0900 (c746484)
@@ -0,0 +1,36 @@
+module Groonga
+  module Ruby
+    class EvalCommand < Command
+      register("ruby_eval",
+               [
+                 "script",
+               ])
+
+      def run_body(input)
+        script = input[:script]
+        unless script.is_a?(String)
+          message = "script must be a string: <#{script.inspect}>"
+          raise Groonga::InvalidArgument, message
+        end
+
+        eval_context = EvalContext.new
+        begin
+          result = eval_context.eval(script)
+        rescue => error
+          writer.map("result", 1) do
+            writer.write("exception")
+            writer.map("exception", 1) do
+              writer.write("message")
+              writer.write(error.message)
+            end
+          end
+        else
+          writer.map("result", 1) do
+            writer.write("value")
+            writer.write(result)
+          end
+        end
+      end
+    end
+  end
+end

  Deleted: plugins/ruby/eval_sources.am (+0 -3) 100644
===================================================================
--- plugins/ruby/eval_sources.am    2016-03-30 10:04:38 +0900 (08543e4)
+++ /dev/null
@@ -1,3 +0,0 @@
-eval_la_SOURCES =				\
-	ruby_plugin.h				\
-	eval.c

  Deleted: plugins/ruby/load.c (+0 -67) 100644
===================================================================
--- plugins/ruby/load.c    2016-03-30 10:04:38 +0900 (4478823)
+++ /dev/null
@@ -1,67 +0,0 @@
-/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
-/*
-  Copyright(C) 2013 Brazil
-
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Lesser General Public
-  License version 2.1 as published by the Free Software Foundation.
-
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public
-  License along with this library; if not, write to the Free Software
-  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-*/
-
-#ifdef GRN_EMBEDDED
-#  define GRN_PLUGIN_FUNCTION_TAG ruby_load
-#endif
-
-#include "ruby_plugin.h"
-
-static grn_obj *
-command_ruby_load(grn_ctx *ctx, int nargs, grn_obj **args,
-                  grn_user_data *user_data)
-{
-  grn_obj *path;
-  mrb_value result;
-
-  path = VAR(0);
-  switch (path->header.domain) {
-  case GRN_DB_SHORT_TEXT :
-  case GRN_DB_TEXT :
-  case GRN_DB_LONG_TEXT :
-    break;
-  default :
-    {
-      grn_obj inspected;
-      GRN_TEXT_INIT(&inspected, 0);
-      grn_inspect(ctx, &inspected, path);
-      ERR(GRN_INVALID_ARGUMENT, "path must be a string: <%.*s>",
-          (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected));
-      GRN_OBJ_FIN(ctx, &inspected);
-      return NULL;
-    }
-    break;
-  }
-
-  GRN_TEXT_PUTC(ctx, path, '\0');
-  result = grn_mrb_load(ctx, GRN_TEXT_VALUE(path));
-  output_result(ctx, result);
-
-  return NULL;
-}
-
-grn_rc
-GRN_PLUGIN_REGISTER(grn_ctx *ctx)
-{
-  grn_expr_var vars[1];
-
-  grn_plugin_expr_var_init(ctx, &vars[0], "path", -1);
-  grn_plugin_command_create(ctx, "ruby_load", -1, command_ruby_load, 1, vars);
-
-  return ctx->rc;
-}

  Deleted: plugins/ruby/load_sources.am (+0 -3) 100644
===================================================================
--- plugins/ruby/load_sources.am    2016-03-30 10:04:38 +0900 (d1cce25)
+++ /dev/null
@@ -1,3 +0,0 @@
-load_la_SOURCES =				\
-	ruby_plugin.h				\
-	load.c

  Deleted: plugins/ruby/ruby_plugin.h (+0 -76) 100644
===================================================================
--- plugins/ruby/ruby_plugin.h    2016-03-30 10:04:38 +0900 (57cab28)
+++ /dev/null
@@ -1,76 +0,0 @@
-/* -*- c-basic-offset: 2; indent-tabs-mode: nil -*- */
-/*
-  Copyright(C) 2013 Brazil
-
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Lesser General Public
-  License version 2.1 as published by the Free Software Foundation.
-
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public
-  License along with this library; if not, write to the Free Software
-  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-*/
-
-#include <grn_mrb.h>
-#include <grn_output.h>
-#include <grn_db.h>
-#include <grn_ctx_impl.h>
-#include <grn_util.h>
-
-#include <groonga/plugin.h>
-
-#include <mruby.h>
-
-#define VAR GRN_PROC_GET_VAR_BY_OFFSET
-
-static void
-output_result(grn_ctx *ctx, mrb_value result)
-{
-  mrb_state *mrb = ctx->impl->mrb.state;
-
-  GRN_OUTPUT_MAP_OPEN("result", 1);
-  if (mrb->exc) {
-    mrb_value mrb_message;
-    grn_obj grn_message;
-    GRN_OUTPUT_CSTR("exception");
-    GRN_OUTPUT_MAP_OPEN("exception", 1);
-    GRN_OUTPUT_CSTR("message");
-    mrb_message = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "message", 0);
-    GRN_VOID_INIT(&grn_message);
-    if (grn_mrb_to_grn(ctx, mrb_message, &grn_message) == GRN_SUCCESS) {
-      GRN_OUTPUT_OBJ(&grn_message, NULL);
-    } else {
-      GRN_OUTPUT_CSTR("unsupported message type");
-    }
-    grn_obj_unlink(ctx, &grn_message);
-    GRN_OUTPUT_MAP_CLOSE();
-  } else {
-    grn_obj grn_result;
-    GRN_OUTPUT_CSTR("value");
-    GRN_VOID_INIT(&grn_result);
-    if (grn_mrb_to_grn(ctx, result, &grn_result) == GRN_SUCCESS) {
-      GRN_OUTPUT_OBJ(&grn_result, NULL);
-    } else {
-      GRN_OUTPUT_CSTR("unsupported return value");
-    }
-    grn_obj_unlink(ctx, &grn_result);
-  }
-  GRN_OUTPUT_MAP_CLOSE();
-}
-
-grn_rc
-GRN_PLUGIN_INIT(grn_ctx *ctx)
-{
-  return GRN_SUCCESS;
-}
-
-grn_rc
-GRN_PLUGIN_FIN(grn_ctx *ctx)
-{
-  return GRN_SUCCESS;
-}

  Added: plugins/ruby/sources.am (+2 -0) 100644
===================================================================
--- /dev/null
+++ plugins/ruby/sources.am    2016-03-30 14:29:13 +0900 (f893829)
@@ -0,0 +1,2 @@
+ruby_scripts =					\
+	eval.rb

  Modified: test/command/suite/ruby/eval/symbol.expected (+1 -1)
===================================================================
--- test/command/suite/ruby/eval/symbol.expected    2016-03-30 10:04:38 +0900 (c7c53e4)
+++ test/command/suite/ruby/eval/symbol.expected    2016-03-30 14:29:13 +0900 (fb16bf5)
@@ -1,4 +1,4 @@
 register ruby/eval
 [[0,0.0,0.0],true]
 ruby_eval ":symbol"
-[[0,0.0,0.0],{"value":":symbol"}]
+[[0,0.0,0.0],{"value":"symbol"}]

  Deleted: test/command/suite/ruby/load/existent.expected (+0 -4) 100644
===================================================================
--- test/command/suite/ruby/load/existent.expected    2016-03-30 10:04:38 +0900 (93e0bbd)
+++ /dev/null
@@ -1,4 +0,0 @@
-register ruby/load
-[[0,0.0,0.0],true]
-ruby_load "test/empty.rb"
-[[0,0.0,0.0],{"value":null}]

  Deleted: test/command/suite/ruby/load/existent.test (+0 -5) 100644
===================================================================
--- test/command/suite/ruby/load/existent.test    2016-03-30 10:04:38 +0900 (a816d13)
+++ /dev/null
@@ -1,5 +0,0 @@
-#@on-error omit
-register ruby/load
-#@on-error default
-
-ruby_load "test/empty.rb"

  Deleted: test/command/suite/ruby/load/nonexistent.expected (+0 -19) 100644
===================================================================
--- test/command/suite/ruby/load/nonexistent.expected    2016-03-30 10:04:38 +0900 (76b9d52)
+++ /dev/null
@@ -1,19 +0,0 @@
-register ruby/load
-[[0,0.0,0.0],true]
-ruby_load "nonexistent.rb"
-[
-  [
-    [
-      -3,
-      0.0,
-      0.0
-    ],
-    "system call error: No such file or directory: fopen: failed to open mruby script file: <PATH>"
-  ],
-  {
-    "exception": {
-      "message": "system call error: No such file or directory: fopen: failed to open mruby script file: <PATH>"
-    }
-  }
-]
-#|e| system call error: No such file or directory: fopen: failed to open mruby script file: <PATH>

  Deleted: test/command/suite/ruby/load/nonexistent.test (+0 -5) 100644
===================================================================
--- test/command/suite/ruby/load/nonexistent.test    2016-03-30 10:04:38 +0900 (5bc4bf0)
+++ /dev/null
@@ -1,5 +0,0 @@
-#@on-error omit
-register ruby/load
-#@on-error default
-
-ruby_load "nonexistent.rb"
-------------- next part --------------
HTML����������������������������...
Descargar 



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