Navigation Menu

Skip to content

Commit

Permalink
mrb: define Kernel#p
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Jun 8, 2014
1 parent 72a2782 commit f53a728
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/ctx_impl_mrb.c
Expand Up @@ -20,6 +20,7 @@
#include "ctx_impl.h"

#include "mrb.h"
#include "mrb/mrb_kernel.h"
#include "mrb/mrb_ctx.h"
#include "mrb/mrb_bulk.h"
#include "mrb/mrb_obj.h"
Expand All @@ -40,6 +41,7 @@ grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx)
mrb->ud = ctx;
ctx->impl->mrb.module = mrb_define_module(mrb, "Groonga");

grn_mrb_kernel_init(ctx);
grn_mrb_ctx_init(ctx);
grn_mrb_bulk_init(ctx);
grn_mrb_obj_init(ctx);
Expand Down
50 changes: 50 additions & 0 deletions lib/mrb/mrb_kernel.c
@@ -0,0 +1,50 @@
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2014 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 "../ctx_impl.h"

#ifdef GRN_WITH_MRUBY
#include <mruby.h>

#include "../mrb.h"
#include "mrb_kernel.h"

static mrb_value
kernel_print(mrb_state *mrb, mrb_value self)
{
char *content;
int content_length;

mrb_get_args(mrb, "s", &content, &content_length);
printf("%.*s", content_length, content);

return mrb_nil_value();
}

void
grn_mrb_kernel_init(grn_ctx *ctx)
{
grn_mrb_data *data = &(ctx->impl->mrb);
mrb_state *mrb = data->state;

mrb_define_method(mrb, mrb->kernel_module,
"print", kernel_print, MRB_ARGS_REQ(1));

grn_mrb_load(ctx, "kernel.rb");
}
#endif
34 changes: 34 additions & 0 deletions lib/mrb/mrb_kernel.h
@@ -0,0 +1,34 @@
/* -*- c-basic-offset: 2 -*- */
/*
Copyright(C) 2014 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
*/

#ifndef GRN_MRB_KERNEL_H
#define GRN_MRB_KERNEL_H

#include "../ctx.h"

#ifdef __cplusplus
extern "C" {
#endif

void grn_mrb_kernel_init(grn_ctx *ctx);

#ifdef __cplusplus
}
#endif

#endif /* GRN_MRB_KERNEL_H */
25 changes: 25 additions & 0 deletions lib/mrb/scripts/kernel.rb
@@ -0,0 +1,25 @@
module Kernel
def puts(*arguments)
arguments.each do |argument|
argument = argument.to_s unless argument.is_a?(String)
print(argument)
print("\n") unless argument[argument.size] == "\n"
end
nil
end

def p(*arguments)
return nil if arguments.empty?

if arguments.size == 1
argument = arguments.first
puts(argument.inspect)
argument
else
arguments.each do |argument|
puts(argument.inspect)
end
arguments
end
end
end
1 change: 1 addition & 0 deletions lib/mrb/scripts/sources.am
Expand Up @@ -2,4 +2,5 @@ RUBY_SCRIPT_FILES = \
eval_context.rb \
expression.rb \
index_info.rb \
kernel.rb \
scan_info.rb
2 changes: 2 additions & 0 deletions lib/mrb/sources.am
Expand Up @@ -15,6 +15,8 @@ libgrnmrb_la_SOURCES = \
mrb_fixed_size_column.h \
mrb_index_column.c \
mrb_index_column.h \
mrb_kernel.c \
mrb_kernel.h \
mrb_obj.c \
mrb_obj.h \
mrb_procedure.c \
Expand Down

0 comments on commit f53a728

Please sign in to comment.