Permalink
Browse files

init commit and still have bugs

  • Loading branch information...
1 parent a1826dc commit dc4a58ea70dfb26b9f58cb8b52c3cc8feb04350a @masuidrive masuidrive committed Jul 21, 2012
View
@@ -1,13 +1,21 @@
# https://help.github.com/articles/ignoring-files
+# 3rd party #
+#############
+vendors
+
# Compiled source #
###################
+tmp
*.com
*.class
*.dll
*.exe
*.o
*.so
+*.d
+*.dSYM
+*.a
# Packages #
############
View
@@ -1,2 +1,2 @@
Original Authors "MobiRuby developers" are:
- Yuichiro MASUI <masui@masuidrive.jp>
+ Yuichiro MASUI <masui@masuidrive.jp>
View
@@ -1,4 +1,4 @@
-Copyright (c) 2012 MobiRuby developers
+Copyright (c) 2012- MobiRuby developers
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
View
@@ -0,0 +1,81 @@
+# makefile discription.
+# basic build file for mruby
+
+# compiler, linker (gcc), archiver, parser generator
+export CC = gcc
+export LL = gcc
+export AR = ar
+
+ifeq ($(strip $(COMPILE_MODE)),)
+ # default compile option
+ COMPILE_MODE = debug
+endif
+
+ifeq ($(COMPILE_MODE),debug)
+ CFLAGS = -g -O3
+else ifeq ($(COMPILE_MODE),release)
+ CFLAGS = -O3
+else ifeq ($(COMPILE_MODE),small)
+ CFLAGS = -Os
+endif
+
+LIBFFI_CFLAGS = $(shell pkg-config ./vendors/lib/pkgconfig/libffi.pc --cflags)
+
+ALL_CFLAGS = -Wall -Werror-implicit-function-declaration -std=c99 $(CFLAGS) $(LIBFFI_CFLAGS)
+ifeq ($(OS),Windows_NT)
+ MAKE_FLAGS = --no-print-directory CC=$(CC) LL=$(LL) ALL_CFLAGS='$(ALL_CFLAGS)'
+else
+ MAKE_FLAGS = --no-print-directory CC='$(CC)' LL='$(LL)' ALL_CFLAGS='$(ALL_CFLAGS)'
+endif
+
+##############################
+# internal variables
+
+export MSG_BEGIN = @for line in
+export MSG_END = ; do echo "$$line"; done
+
+export CP := cp
+export RM_F := rm -f
+export CAT := cat
+
+
+##############################
+# generic build targets, rules
+
+.PHONY : all
+all : vendors/lib/libffi.a vendors/lib/libmruby.a
+ @$(MAKE) -C src $(MAKE_FLAGS)
+
+# mruby test
+.PHONY : test
+test : all
+ @$(MAKE) -C test $(MAKE_FLAGS) run
+
+# clean up
+.PHONY : clean
+clean :
+ @$(MAKE) clean -C src $(MAKE_FLAGS)
+ @$(MAKE) clean -C test $(MAKE_FLAGS)
+
+
+##################
+# libffi.a
+tmp/libffi:
+ mkdir -p tmp/libffi
+ cd tmp && git clone https://github.com/atgreen/libffi.git
+
+vendors/lib/libffi.a: tmp/libffi
+ cd tmp/libffi && ./configure --prefix=`pwd`/../../vendors && make install
+
+
+##################
+# libmruby.a
+tmp/mruby:
+ mkdir -p tmp/mruby
+ cd tmp; git clone https://github.com/mruby/mruby.git
+
+vendors/lib/libmruby.a: tmp/mruby
+ cd tmp/mruby && make
+ cp -r tmp/mruby/include vendors/
+ cp -r tmp/mruby/lib vendors/
+ cp -r tmp/mruby/bin vendors/
View
@@ -0,0 +1,61 @@
+/*
+** cfunc-mruby - Interface to C functions on mruby
+**
+** Copyright (c) MobiRuby developers 2012-
+**
+** Permission is hereby granted, free of charge, to any person obtaining
+** a copy of this software and associated documentation files (the
+** "Software"), to deal in the Software without restriction, including
+** without limitation the rights to use, copy, modify, merge, publish,
+** distribute, sublicense, and/or sell copies of the Software, and to
+** permit persons to whom the Software is furnished to do so, subject to
+** the following conditions:
+**
+** The above copyright notice and this permission notice shall be
+** included in all copies or substantial portions of the Software.
+**
+** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+**
+** [ MIT license: http://www.opensource.org/licenses/mit-license.php ]
+*/
+
+#ifndef cfunc_h
+#define cfunc_h
+
+#include "mruby.h"
+
+struct mrb_state_ud {
+ struct RClass *cfunc_module;
+
+ struct RClass *cfunc_type_class;
+ struct RClass *cfunc_void_class;
+ struct RClass *cfunc_uint8_class;
+ struct RClass *cfunc_sint8_class;
+ struct RClass *cfunc_uint16_class;
+ struct RClass *cfunc_sint16_class;
+ struct RClass *cfunc_uint32_class;
+ struct RClass *cfunc_sint32_class;
+ struct RClass *cfunc_uint64_class;
+ struct RClass *cfunc_sint64_class;
+ struct RClass *cfunc_float_class;
+ struct RClass *cfunc_double_class;
+ struct RClass *cfunc_pointer_class;
+ struct RClass *cfunc_struct_class;
+ struct RClass *cfunc_closure_class;
+};
+
+void init_cfunc_module(mrb_state *mrb);
+
+static inline struct mrb_state_ud*
+mrb_ud(mrb_state* mrb)
+{
+ return (struct mrb_state_ud *)(mrb->ud);
+}
+
+#endif
View
No changes.
View
@@ -0,0 +1,80 @@
+# makefile discription.
+# basic build file for cfunc library
+
+# project-specific macros
+# extension of the executable-file is modifiable(.exe .out ...)
+BASEDIR = .
+TARGET := ../lib/libmruby-cfunc.a
+MRBCSRC := $(patsubst %.rb,%.c,$(wildcard $(BASEDIR)/mrb/*.rb))
+LIBR := $(BASEDIR)/../vendors/lib/libmruby.a \
+ $(BASEDIR)/../lib/libmruby-cfunc.a
+
+EXCEPT1 := $(MRBCSRC)
+
+OBJ1 := $(patsubst %.c,%.o,$(filter-out $(EXCEPT1),$(wildcard $(BASEDIR)/*.c)))
+OBJMRB := $(patsubst %.c,%.o,$(MRBCSRC))
+OBJS := $(OBJ1) $(OBJ2) $(OBJ3)
+
+MRBC := ../vendors/bin/mrbc
+
+
+# libraries, includes
+INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include -I$(BASEDIR)/../vendors/include
+
+# libffi
+LIBFFI_CFLAGS = $(shell pkg-config $(BASEDIR)/../vendors/lib/pkgconfig/libffi.pc --cflags)
+LIBFFI_LIBS = $(shell pkg-config $(BASEDIR)/../vendors/lib/pkgconfig/libffi.pc --libs)
+
+ifeq ($(strip $(COMPILE_MODE)),)
+ # default compile option
+ COMPILE_MODE = debug
+endif
+
+ifeq ($(COMPILE_MODE),debug)
+ CFLAGS = -g -O3
+else ifeq ($(COMPILE_MODE),release)
+ CFLAGS = -O3
+else ifeq ($(COMPILE_MODE),small)
+ CFLAGS = -Os
+endif
+
+ALL_CFLAGS = -Wall -Werror-implicit-function-declaration -std=c99 $(CFLAGS) $(LIBFFI_CFLAGS)
+
+
+##############################
+# internal variables
+
+export CP := cp
+export RM_F := rm -f
+export CAT := cat
+
+
+##############################
+# generic build targets, rules
+
+.PHONY : all
+all : $(TARGET)
+
+# executable constructed using linker from object files
+$(TARGET) : $(OBJS) $(OBJMRB)
+ $(AR) r $@ $(OBJS) $(OBJMRB)
+
+-include $(OBJS:.o=.d) $(OBJMRB:.o=.d)
+
+# mrby complie
+$(OBJMRB) : %.o : %.rb
+ $(MRBC) -Cinit_$(*F) $<
+ $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(basename $<).c -o $@
+
+# objects compiled from source
+$(OBJS) : %.o : %.c
+ echo $(LIBFFI_CFLAGS)
+ $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $< -o $@
+
+# clean up
+.PHONY : clean #cleandep
+clean :
+ @echo "make: removing targets, objects and depend files of `pwd`"
+ -$(RM_F) $(TARGET) $(OBJS) $(OBJMRB) $(MRB)
+ -$(RM_F) $(OBJS:.o=.d) $(OBJY:.o=.d)
+ -$(RM_F) $(patsubst %.c,%.o,$(EXCEPT1)) $(patsubst %.c,%.d,$(EXCEPT1))
View
@@ -0,0 +1,34 @@
+//
+// CFunc initializer method in C
+//
+// See Copyright Notice in cfunc.h
+//
+
+#include "cfunc.h"
+
+#include "cfunc_call.h"
+#include "cfunc_pointer.h"
+#include "cfunc_struct.h"
+#include "cfunc_closure.h"
+#include "cfunc_type.h"
+
+void
+init_cfunc_rb(mrb_state *mrb);
+
+
+void init_cfunc_module(mrb_state *mrb)
+{
+ if(mrb->ud == NULL) {
+ mrb->ud = malloc(sizeof(struct mrb_state_ud));
+ }
+
+ struct RClass *module = mrb_define_module(mrb, "CFunc");
+
+ init_cfunc_type(mrb, module);
+ init_cfunc_pointer(mrb, module);
+ init_cfunc_struct(mrb, module);
+ init_cfunc_closure(mrb, module);
+ init_cfunc_call(mrb, module);
+
+ init_cfunc_rb(mrb);
+}
View
@@ -0,0 +1,75 @@
+//
+// CFunc::Cass method
+//
+// See Copyright Notice in cfunc.h
+//
+
+#include "cfunc_call.h"
+#include "cfunc.h"
+#include "cfunc_pointer.h"
+#include "cfunc_type.h"
+
+#include "mruby/string.h"
+#include "mruby/class.h"
+
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include "ffi.h"
+
+
+static mrb_value
+cfunc_call(mrb_state *mrb, mrb_value self)
+{
+ int margc;
+ mrb_value mresult_type, mname, *margs;
+ mrb_get_args(mrb, "oo*", &mresult_type, &mname, &margs, &margc);
+
+ void *dlh = dlopen(NULL, RTLD_LAZY);
+ void *fp = dlsym(dlh, RSTRING_PTR(mname));
+
+ ffi_type **args = malloc(sizeof(ffi_type*) * margc);
+ void **values = malloc(sizeof(void*) * margc);
+ mrb_sym to_pointer = mrb_intern(mrb, "to_pointer");
+
+ for(int i = 0; i < margc; ++i) {
+ if(!mrb_respond_to(mrb, margs[i], to_pointer)) {
+ // Todo: should free some malloc-ed pointers
+ mrb_raise(mrb, E_TYPE_ERROR, "ignore argument type");
+ }
+ args[i] = mrb_value_to_mrb_ffi_type(mrb, margs[i])->ffi_type_value;
+ values[i] = mobi_pointer_ptr(mrb_funcall(mrb, margs[i], "to_pointer", 0));
+ }
+
+ ffi_type *result_type = rclass_to_mrb_ffi_type(mrb, mrb_class_ptr(mresult_type))->ffi_type_value;
+ if (result_type == NULL) {
+ mrb_raise(mrb, E_ARGUMENT_ERROR, "ignore return type");
+ }
+
+ mrb_value mresult = mrb_nil_value();
+ ffi_cif cif;
+ if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, margc, result_type, args) == FFI_OK) {
+ void *result = result_type->size ? malloc(result_type->size) : NULL;
+ ffi_call(&cif, fp, result, values);
+
+ if(result) {
+ mrb_value result_ptr = cfunc_pointer_new_with_pointer(mrb, result, 1);
+ mresult = mrb_funcall(mrb, mresult_type, "refer", 1, result_ptr);
+ }
+ }
+ else {
+ // todo
+ mrb_raise(mrb, E_NAME_ERROR, "can't find C function");
+ }
+
+cfunc_call_exit:
+ free(values);
+ free(args);
+ return mresult;
+}
+
+
+void init_cfunc_call(mrb_state *mrb, struct RClass* module)
+{
+ mrb_define_class_method(mrb, module, "call", cfunc_call, ARGS_ANY());
+}
View
@@ -0,0 +1,14 @@
+//
+// CFunc::Cass method
+//
+// See Copyright Notice in cfunc.h
+//
+
+#ifndef cfunc_call_h
+#define cfunc_call_h
+
+#include "cfunc.h"
+
+void init_cfunc_call(mrb_state *mrb, struct RClass* module);
+
+#endif
Oops, something went wrong.

0 comments on commit dc4a58e

Please sign in to comment.