Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Following mrbgems

  • Loading branch information...
commit a0ea4b89774cb128766c21ae1a24460fa16b019f 1 parent 3c425e8
@masuidrive masuidrive authored
Showing with 353 additions and 677 deletions.
  1. +0 −1  .gitignore
  2. +33 −86 Makefile
  3. +13 −24 include/cocoa.h
  4. +18 −0 mrbgem.rake
  5. 0  {src/mrb → mrblib}/.gitignore
  6. 0  {src/mrb → mrblib}/block.rb
  7. +1 −1  {src/mrb → mrblib}/cocoa.rb
  8. 0  {src/mrb → mrblib}/object.rb
  9. 0  {src/mrb → mrblib}/object_ivar.rb
  10. +1 −2  {src/mrb → mrblib}/object_method.rb
  11. +1 −1  {src/mrb → mrblib}/object_property.rb
  12. 0  {src/mrb → mrblib}/protocol.rb
  13. +3 −2 src/cocoa_block.m
  14. +1 −1  src/cocoa_bridgesupport.h
  15. +19 −16 src/cocoa_bridgesupport.m
  16. +4 −3 src/cocoa_obj_hook.m
  17. +48 −28 src/cocoa_object.m
  18. +12 −11 src/cocoa_type.m
  19. +10 −31 src/init_cocoa.m
  20. +0 −101 test/Makefile
  21. +1 −0  test/bridgesupport_struct.rb
  22. +2 −0  test/const.rb
  23. +112 −0 test/define.m
  24. +50 −124 test/main.m
  25. 0  test/{t → }/nsstring.rb
  26. +0 −42 test/{t → }/object_call_method.rb
  27. 0  test/{t → }/object_declare_ivar.rb
  28. 0  test/{t → }/object_declare_property.rb
  29. +0 −39 test/{t → }/object_inherit.rb
  30. +0 −29 test/{t → }/object_use_ivar.rb
  31. +24 −0 test/object_use_property.rb
  32. 0  test/{t → }/protocol.rb
  33. +0 −1  test/t/.gitignore
  34. +0 −23 test/t/bridgesupport_struct.rb
  35. +0 −28 test/t/const.rb
  36. +0 −83 test/t/object_use_property.rb
View
1  .gitignore
@@ -3,7 +3,6 @@
# 3rd party #
#############
vendors
-include/ffi
# Compiled source #
###################
View
119 Makefile
@@ -1,108 +1,55 @@
-# makefile discription.
-# basic build file for mruby
+GEM := mruby-cocoa
-# compiler, linker (gcc), archiver, parser generator
-CC = clang
-LL = clang
-LIBFFI_VERSION = 3.0.11
+-include $(MAKEFILE_4_GEM)
-ifeq ($(strip $(COMPILE_MODE)),)
- # default compile option
- COMPILE_MODE = debug
-endif
+MRUBY_CFLAGS =
+MRUBY_LDFLAGS =
+MRUBY_LIBS = -framework Foundation
-ifeq ($(COMPILE_MODE),debug)
- CFLAGS = -g -O3
-else ifeq ($(COMPILE_MODE),release)
- CFLAGS = -O3
-else ifeq ($(COMPILE_MODE),small)
- CFLAGS = -Os
-endif
-CFLAGS += -pthread
+GEM_C_FILES := $(wildcard $(SRC_DIR)/*.c)
+GEM_M_FILES := $(wildcard $(SRC_DIR)/*.m)
+GEM_OBJECTS := $(patsubst %.c, %.o, $(GEM_C_FILES)) $(patsubst %.m, %.o, $(GEM_M_FILES))
-BASEDIR = $(shell pwd)
-INCLUDES = -I$(BASEDIR)/include -I$(BASEDIR)/vendors/include
+GEM_RB_FILES := $(wildcard $(MRB_DIR)/*.rb)
-MRUBY_CFLAGS = -I$(BASEDIR)/vendors/include
-MRUBY_LIBS = -L$(BASEDIR)/vendors/lib -lmruby
+GEM_TEST_C_FILES := $(wildcard $(TEST_DIR)/*.c)
+GEM_TEST_M_FILES := $(wildcard $(TEST_DIR)/*.m)
+GEM_TEST_OBJECTS := $(patsubst %.c, %.o, $(GEM_TEST_C_FILES)) $(patsubst %.m, %.o, $(GEM_TEST_M_FILES)) gem_test.o
-LIBFFI_CFLAGS = -I$(BASEDIR)/vendors/lib/libffi-$(LIBFFI_VERSION)/include/
-LIBFFI_LIBS = $(BASEDIR)/vendors/lib/libffi.a
+gem-all : $(GEM_OBJECTS) gem-c-and-rb-files
-CFUNC_CFLAGS = -I$(BASEDIR)/vendors/include
-CFUNC_LIBS = -L$(BASEDIR)/vendors/lib -lmruby-cfunc
+gem-clean : gem-clean-c-and-rb-files
-ALL_CFLAGS = $(CFLAGS) $(INCLUDES) $(MRUBY_CFLAGS)
-MAKE_FLAGS = --no-print-directory CC='$(CC)' LL='$(LL)' CFLAGS='$(ALL_CFLAGS)' \
- LIBFFI_CFLAGS='$(LIBFFI_CFLAGS)' LIBFFI_LIBS='$(LIBFFI_LIBS)' \
- CFUNC_CFLAGS='$(CFUNC_CFLAGS)' CFUNC_LIBS='$(CFUNC_LIBS)' \
- MRUBY_CFLAGS='$(MRUBY_CFLAGS)' MRUBY_LIBS='$(MRUBY_LIBS)'
+gem-test : $(GEM_TEST_OBJECTS) gem_test.o
+ $(AR) rs gem_test.a $^
-##############################
-# internal variables
-
-export CP := cp
-export RM_F := rm -f
-export CAT := cat
-
-
-##############################
-# generic build targets, rules
-
-# mruby test
.PHONY : test
-test : all
- @$(MAKE) -C test $(MAKE_FLAGS) run
-
-.PHONY : all
-all : vendors/lib/libffi.a vendors/lib/libmruby.a vendors/lib/libmruby-cfunc.a
- @$(MAKE) -C src $(MAKE_FLAGS)
-
-# clean up
-.PHONY : clean
-clean :
- @$(MAKE) clean -C src $(MAKE_FLAGS)
- @$(MAKE) clean -C test $(MAKE_FLAGS)
+test: tmp/mruby tmp/mruby-cfunc
+ rm -Rf `pwd`/tmp/mruby-cfunc
+ ln -s `pwd` `pwd`/tmp/mruby-cfunc
+ echo `pwd`/tmp/mruby-cfunc > tmp/mruby/mrbgems/GEMS.active
+ cd tmp/mruby; ENABLE_GEMS=true ./minirake clean
+ cd tmp/mruby; ENABLE_GEMS=true ./minirake test
+tmp/mruby:
+ mkdir -p tmp/mruby
+ cd tmp; git clone https://github.com/mruby/mruby.git
+ sed -i -e 's/\/\/\#define MRB_INT64/\#define MRB_INT64/' tmp/mruby/include/mrbconf.h
-##################
-# libmruby-cfunc.a
tmp/mruby-cfunc:
- mkdir -p tmp
cd tmp && git clone https://github.com/mobiruby/mruby-cfunc.git
-
-vendors/lib/libmruby-cfunc.a: tmp/mruby-cfunc vendors/lib/libffi.a vendors/lib/libmruby.a
- cd tmp/mruby-cfunc/src && make MRBC="$(BASEDIR)/vendors/bin/mrbc" \
- CFLAGS="$(CFLAGS)" \
- TARGET="$(BASEDIR)/vendors/lib/libmruby-cfunc.a" \
- MRUBY_CFLAGS="$(MRUBY_CFLAGS)" \
- MRUBY_LIBS="$(MRUBY_LIBS)" \
- LIBFFI_CFLAGS="$(LIBFFI_CFLAGS)" \
- LIBFFI_LIBS="$(LIBFFI_LIBS)"
- cp -r tmp/mruby-cfunc/include/* vendors/include/
-
-
-##################
-# libffi.a
tmp/libffi:
- mkdir -p tmp
+ mkdir -p tmp/
cd tmp && git clone https://github.com/atgreen/libffi.git
-vendors/lib/libffi.a: tmp/libffi
+lib/libffi.a: tmp/libffi
+ mkdir -p vendors
cd tmp/libffi && ./configure --prefix=`pwd`/../../vendors && make clean install CFLAGS="$(CFLAGS)"
+ cp -r vendors/lib/libffi-3.0.11/include/* include/
+ cp vendors/lib/libffi.a lib/
-##################
-# libmruby.a
-tmp/mruby:
- mkdir -p tmp/mruby
- cd tmp; git clone https://github.com/mruby/mruby.git
- sed -i -e 's/\/\/\#define MRB_INT64/\#define MRB_INT64/' tmp/mruby/include/mrbconf.h
-
-vendors/lib/libmruby.a: tmp/mruby
- cd tmp/mruby && make clean && make all CFLAGS="$(CFLAGS)"
- cp -r tmp/mruby/include vendors/
- cp -r tmp/mruby/lib vendors/
- cp -r tmp/mruby/bin vendors/
+%.o : %.m
+ $(CC) -c $(CFLAGS) -std=c99 $(CPPFLAGS) $(GEM_INCLUDE_LIST) $< -o $@
View
37 include/cocoa.h
@@ -76,7 +76,6 @@ struct cocoa_state {
extern mrb_state **cocoa_mrb_states;
extern int cocoa_vm_count;
-void init_cocoa_module(mrb_state *mrb);
void close_cocoa_module(mrb_state *mrb);
void
@@ -86,32 +85,22 @@ load_cocoa_bridgesupport(mrb_state *mrb,
struct BridgeSupportEnumTable *enum_table);
-/* offset of cocoa_state in mrb->ud */
-extern size_t cocoa_state_offset;
-
-/* service function for setting cocoa_state_offset */
-#define cocoa_offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-
-/*
-example:
-
-struct mrb_state_ud {
- struct cocoa_state cocoa_state;
-};
-
-cocoa_state_offset = cfunc_offsetof(struct mrb_state_ud, cocoa_state);
-
-mrb_state *mrb = mrb_open();
-mrb->ud = malloc(sizeof(struct mrb_state_ud));
-
-init_cocoa_module(mrb);
-*/
+static inline struct cocoa_state *
+cocoa_state(mrb_state *mrb, struct RClass* klass)
+{
+ if(klass == NULL) {
+ klass = mrb_object(mrb_vm_const_get(mrb, mrb_intern(mrb, "Cocoa")));
+ }
+ mrb_value state = mrb_mod_cv_get(mrb, klass, mrb_intern(mrb, "cocoa_state"));
+ return (struct cocoa_state *)mrb_voidp(state);
+}
-static inline struct cocoa_state *
-cocoa_state(mrb_state* mrb)
+static inline void
+set_cocoa_state(mrb_state *mrb, struct RClass* klass, struct cocoa_state *state)
{
- return (struct cocoa_state *)(mrb->ud + cocoa_state_offset);
+ mrb_value mstate = mrb_voidp_value(state);
+ mrb_mod_cv_set(mrb, klass, mrb_intern(mrb, "cocoa_state"), mstate);
}
View
18 mrbgem.rake
@@ -0,0 +1,18 @@
+MRuby::Gem::Specification.new('mruby-cocoa') do |spec|
+ spec.license = 'MIT'
+ spec.authors = 'MobiRuby developers'
+
+ build.cc = 'clang'
+ spec.cflags = %w(-std=c99)
+
+ # spec.mruby_cflags = ''
+ # spec.mruby_ldflags = ''
+ spec.mruby_libs = %w(-framework Foundation)
+ # spec.mruby_includes = []
+
+ # spec.rbfiles = Dir.glob("#{dir}/mrblib/*.rb")
+ spec.objs << ["#{LIBFFI_DIR}/lib/libffi.a"]
+ # spec.test_rbfiles = Dir.glob("#{dir}/test/*.rb")
+ # spec.test_objs = Dir.glob("#{dir}/test/*.{c,cpp,m,asm,S}").map { |f| f.relative_path_from(dir).pathmap("#{build_dir}/%X.o") }
+ # spec.test_preload = 'test/assert.rb'
+end
View
0  src/mrb/.gitignore → mrblib/.gitignore
File renamed without changes
View
0  src/mrb/block.rb → mrblib/block.rb
File renamed without changes
View
2  src/mrb/cocoa.rb → mrblib/cocoa.rb
@@ -22,4 +22,4 @@ def params_to_types(rettype_rb, *args)
[objc_name, "#{rettype}:@#{types.map{|t|t.objc_type_encode}.join}"]
end
end
-end
+end
View
0  src/mrb/object.rb → mrblib/object.rb
File renamed without changes
View
0  src/mrb/object_ivar.rb → mrblib/object_ivar.rb
File renamed without changes
View
3  src/mrb/object_method.rb → mrblib/object_method.rb
@@ -101,5 +101,4 @@ def _super(name, *args, &block)
raise "Unknown method #{name}"
end
end
-
-end
+end
View
2  src/mrb/object_property.rb → mrblib/object_property.rb
@@ -61,4 +61,4 @@ def self.property(name, type, *options_)
# declare property
nil
end
-end
+end
View
0  src/mrb/protocol.rb → mrblib/protocol.rb
File renamed without changes
View
5 src/cocoa_block.m
@@ -91,8 +91,9 @@
void
init_cocoa_block(mrb_state *mrb, struct RClass* module)
{
- struct RClass *block_class = mrb_define_class_under(mrb, module, "Block", cfunc_state(mrb)->closure_class);
- cocoa_state(mrb)->block_class = block_class;
+ struct cocoa_state *cs = cocoa_state(mrb, module);
+ struct RClass *block_class = mrb_define_class_under(mrb, module, "Block", cfunc_state(mrb, NULL)->closure_class);
+ set_cocoa_state(mrb, (struct RObject*)block_class, cs);
mrb_define_method(mrb, block_class, "addr", cocoa_block_addr, ARGS_NONE());
}
View
2  src/cocoa_bridgesupport.h
@@ -10,7 +10,7 @@
#include "cocoa.h"
void
-init_cocoa_bridge_support(mrb_state *mrb);
+init_cocoa_bridge_support(mrb_state *mrb, struct RClass* module);
const char*
cocoa_bridgesupport_struct_lookup(mrb_state *mrb, const char *name);
View
35 src/cocoa_bridgesupport.m
@@ -25,9 +25,10 @@
struct BridgeSupportConstTable *const_table,
struct BridgeSupportEnumTable *enum_table)
{
- cocoa_state(mrb)->struct_table = struct_table;
- cocoa_state(mrb)->const_table = const_table;
- cocoa_state(mrb)->enum_table = enum_table;
+ struct cocoa_state *cs = cocoa_state(mrb, NULL);
+ cs->struct_table = struct_table;
+ cs->const_table = const_table;
+ cs->enum_table = enum_table;
}
@@ -35,11 +36,11 @@
const char*
cocoa_bridgesupport_struct_lookup(mrb_state *mrb, const char *name)
{
- if(cocoa_state(mrb)->struct_table == NULL) {
+ if(cocoa_state(mrb, NULL)->struct_table == NULL) {
return NULL;
}
- struct BridgeSupportStructTable *cur = cocoa_state(mrb)->struct_table;
+ struct BridgeSupportStructTable *cur = cocoa_state(mrb, NULL)->struct_table;
while(cur->name) {
if(strcmp(name, cur->name)==0) {
return cur->definition;
@@ -53,7 +54,7 @@
mrb_value
cocoa_struct_const_missing(mrb_state *mrb, mrb_value klass)
{
- if(cocoa_state(mrb)->const_table == NULL) {
+ if(cocoa_state(mrb, NULL)->const_table == NULL) {
return mrb_nil_value();
}
@@ -82,7 +83,7 @@
mrb_value
cocoa_const_const_missing(mrb_state *mrb, mrb_value klass)
{
- if(cocoa_state(mrb)->const_table == NULL) {
+ if(cocoa_state(mrb, NULL)->const_table == NULL) {
return mrb_nil_value();
}
@@ -90,7 +91,7 @@
mrb_get_args(mrb, "o", &name);
char *namestr = mrb_string_value_ptr(mrb, name);
- struct BridgeSupportConstTable *ccur = cocoa_state(mrb)->const_table;
+ struct BridgeSupportConstTable *ccur = cocoa_state(mrb, NULL)->const_table;
while(ccur->name) {
if(strcmp(namestr, ccur->name)==0) {
mrb_value type = objc_type_to_cfunc_type(mrb, ccur->type);
@@ -101,7 +102,7 @@
++ccur;
}
- struct BridgeSupportEnumTable *ecur = cocoa_state(mrb)->enum_table;
+ struct BridgeSupportEnumTable *ecur = cocoa_state(mrb, NULL)->enum_table;
while(ecur->name) {
if(strcmp(namestr, ecur->name)==0) {
return ecur->value;
@@ -117,17 +118,19 @@
* initialize function
*/
void
-init_cocoa_bridge_support(mrb_state *mrb)
+init_cocoa_bridge_support(mrb_state *mrb, struct RClass* module)
{
- struct RClass *struct_module = mrb_define_module_under(mrb, cocoa_state(mrb)->namespace, "Struct");
- cocoa_state(mrb)->struct_module = struct_module;
+ struct cocoa_state *cs = cocoa_state(mrb, module);
+
+ struct RClass *struct_module = mrb_define_module_under(mrb, module, "Struct");
mrb_define_class_method(mrb, struct_module, "const_missing", cocoa_struct_const_missing, ARGS_REQ(1));
- struct RClass *const_module = mrb_define_module_under(mrb, cocoa_state(mrb)->namespace, "Const");
- cocoa_state(mrb)->const_module = const_module;
+ struct RClass *const_module = mrb_define_module_under(mrb, module, "Const");
mrb_define_class_method(mrb, const_module, "const_missing", cocoa_const_const_missing, ARGS_REQ(1));
mrb_define_class_method(mrb, const_module, "method_missing", cocoa_const_const_missing, ARGS_REQ(1));
- cocoa_state(mrb)->const_table = NULL;
- cocoa_state(mrb)->struct_table = NULL;
+ cs->struct_module = struct_module;
+ cs->const_module = const_module;
+ cs->const_table = NULL;
+ cs->struct_table = NULL;
}
View
7 src/cocoa_obj_hook.m
@@ -71,11 +71,12 @@ void swizzle(Class c, SEL orig, SEL patch)
if([self retainCount] == 2) {
for(int i = 0; i < cocoa_vm_count; ++i) {
mrb_state *mrb = cocoa_mrb_states[i];
- MrbObjectMap *assoc = objc_getAssociatedObject(self, cocoa_state(mrb)->object_association_key);
+ struct cocoa_state *state = cocoa_state(mrb, NULL);
+ MrbObjectMap *assoc = objc_getAssociatedObject(self, state->object_association_key);
if(assoc) {
- mrb_value keeper = mrb_gv_get(mrb, cocoa_state(mrb)->sym_obj_holder);
+ mrb_value keeper = mrb_gv_get(mrb, state->sym_obj_holder);
mrb_value mrb_obj = assoc.mrb_obj;
- mrb_funcall_argv(mrb, keeper, cocoa_state(mrb)->sym_delete, 1, &mrb_obj);
+ mrb_funcall_argv(mrb, keeper, state->sym_delete, 1, &mrb_obj);
}
}
}
View
76 src/cocoa_object.m
@@ -40,7 +40,8 @@
cocoa_object_new_with_id(mrb_state *mrb, id pointer)
{
//printf("new_with_id=%p\n", pointer);
- MrbObjectMap *assoc = objc_getAssociatedObject(pointer, cocoa_state(mrb)->object_association_key);
+ struct cocoa_state *cs = cocoa_state(mrb, NULL);
+ MrbObjectMap *assoc = objc_getAssociatedObject(pointer, cs->object_association_key);
if(assoc) {
return assoc.mrb_obj;
}
@@ -60,23 +61,23 @@
struct RClass *klass;
mrb_sym class_name_sym = mrb_intern(mrb, class_name);
- if (mrb_const_defined_at(mrb, cocoa_state(mrb)->namespace, class_name_sym)) {
- klass = (struct RClass *)mrb_object(mrb_const_get(mrb, mrb_obj_value(cocoa_state(mrb)->namespace), class_name_sym));
+ if (mrb_const_defined_at(mrb, cs->namespace, class_name_sym)) {
+ klass = (struct RClass *)mrb_object(mrb_const_get(mrb, mrb_obj_value(cs->namespace), class_name_sym));
}
else {
- klass = cocoa_state(mrb)->object_class;
+ klass = cs->object_class;
}
mrb_value self = mrb_obj_value(Data_Wrap_Struct(mrb, klass, &cocoa_object_data_type, data));
if(assoc == NULL) {
assoc = [[MrbObjectMap alloc] init];
- objc_setAssociatedObject(pointer, cocoa_state(mrb)->object_association_key, assoc, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ objc_setAssociatedObject(pointer, cs->object_association_key, assoc, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[assoc release];
}
assoc.mrb_obj = self;
- mrb_value keeper = mrb_gv_get(mrb, cocoa_state(mrb)->sym_obj_holder);
+ mrb_value keeper = mrb_gv_get(mrb, cs->sym_obj_holder);
mrb_funcall_argv(mrb, keeper, mrb_intern(mrb, "push"), 1, &self);
return self;
@@ -92,12 +93,14 @@
static mrb_value
cocoa_object_class_new(mrb_state *mrb, mrb_value klass)
{
+ struct cocoa_state *cs = cocoa_state(mrb, mrb_obj_ptr(klass)->c);
+
mrb_value pointer_mrb;
mrb_get_args(mrb, "o", &pointer_mrb);
id pointer = cfunc_pointer_ptr(pointer_mrb);
//printf("new=%p\n", pointer);
- MrbObjectMap *assoc = objc_getAssociatedObject(pointer, cocoa_state(mrb)->object_association_key);
+ MrbObjectMap *assoc = objc_getAssociatedObject(pointer, cs->object_association_key);
if(assoc) {
//puts(">>>");
//mrb_p(mrb, assoc.mrb_obj);
@@ -122,19 +125,19 @@
klass2 = mrb_class_get(mrb, class_name);
}
else {
- klass2 = cocoa_state(mrb)->object_class;
+ klass2 = cs->object_class;
}
mrb_value self = mrb_obj_value(Data_Wrap_Struct(mrb, mrb_class_ptr(klass), &cocoa_object_data_type, data));
if(assoc == NULL) {
assoc = [[MrbObjectMap alloc] init];
- objc_setAssociatedObject(pointer, cocoa_state(mrb)->object_association_key, assoc, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ objc_setAssociatedObject(pointer, cs->object_association_key, assoc, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
[assoc release];
}
assoc.mrb_obj = self;
- mrb_value keeper = mrb_gv_get(mrb, cocoa_state(data->mrb)->sym_obj_holder);
+ mrb_value keeper = mrb_gv_get(mrb, cs->sym_obj_holder);
mrb_funcall_argv(mrb, keeper, mrb_intern(mrb, "push"), 1, &self);
return self;
@@ -150,6 +153,8 @@
static mrb_value
cocoa_object_class_refer(mrb_state *mrb, mrb_value klass)
{
+ struct cocoa_state *cs = cocoa_state(mrb, mrb_obj_ptr(klass)->c);
+
mrb_value pointer;
mrb_get_args(mrb, "o", &pointer);
@@ -158,7 +163,7 @@
return mrb_nil_value();
}
- MrbObjectMap *assoc = objc_getAssociatedObject(obj, cocoa_state(mrb)->object_association_key);
+ MrbObjectMap *assoc = objc_getAssociatedObject(obj, cs->object_association_key);
if(assoc) {
//printf("refer assoc=%p\n",obj);
return assoc.mrb_obj;
@@ -181,22 +186,22 @@
const char *class_name = object_getClassName(obj);
struct RClass *klass2;
mrb_sym class_name_sym = mrb_intern(mrb, class_name);
- if (mrb_const_defined_at(mrb, cocoa_state(mrb)->namespace, class_name_sym)) {
- klass2 = (struct RClass *)mrb_object(mrb_const_get(mrb, mrb_obj_value(cocoa_state(mrb)->namespace), class_name_sym));
+ if (mrb_const_defined_at(mrb, cs->namespace, class_name_sym)) {
+ klass2 = (struct RClass *)mrb_object(mrb_const_get(mrb, mrb_obj_value(cs->namespace), class_name_sym));
}
else {
- klass2 = cocoa_state(mrb)->object_class;
+ klass2 = cs->object_class;
}
mrb_value self = mrb_obj_value(Data_Wrap_Struct(mrb, klass2, &cocoa_object_data_type, data));
mrb_obj_iv_set(mrb, (struct RObject*)mrb_object(self), mrb_intern(mrb, "parent_pointer"), pointer); // keep for GC
if(assoc == NULL && obj) {
assoc = [[[MrbObjectMap alloc] init] autorelease];
- objc_setAssociatedObject(obj, cocoa_state(mrb)->object_association_key, assoc, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
+ objc_setAssociatedObject(obj, cs->object_association_key, assoc, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
assoc.mrb_obj = self;
- mrb_value keeper = mrb_gv_get(mrb, cocoa_state(mrb)->sym_obj_holder);
+ mrb_value keeper = mrb_gv_get(mrb, cs->sym_obj_holder);
mrb_funcall_argv(mrb, keeper, mrb_intern(mrb, "push"), 1, &self);
//mrb_p(mrb, self);
//mrb_p(mrb, keeper);
@@ -212,8 +217,10 @@
static Class
mruby_class_to_objc_class(mrb_state *mrb, struct RClass *klass)
{
+ struct cocoa_state *cs = cocoa_state(mrb, klass->c);
+
Class objc_class;
- if(cocoa_st_lookup(cocoa_state(mrb)->cocoa_classes, (cocoa_st_data_t)klass, (void*)&objc_class)) {
+ if(cocoa_st_lookup(cs->cocoa_classes, (cocoa_st_data_t)klass, (void*)&objc_class)) {
return objc_class;
}
@@ -468,6 +475,8 @@
static mrb_value
cocoa_object_class_inherited(mrb_state *mrb, mrb_value klass)
{
+ struct cocoa_state *cs = cocoa_state(mrb, mrb_obj_ptr(klass)->c);
+
mrb_value subclass;
mrb_get_args(mrb, "o", &subclass);
@@ -482,7 +491,7 @@
Class super_class = mruby_class_to_objc_class(mrb, (struct RClass *)mrb_object(klass));
objc_klass = objc_allocateClassPair(super_class, class_name, 0);
- cocoa_st_insert(cocoa_state(mrb)->cocoa_classes, (cocoa_st_data_t)mrb_object(subclass), (cocoa_st_data_t)objc_klass);
+ cocoa_st_insert(cs->cocoa_classes, (cocoa_st_data_t)mrb_object(subclass), (cocoa_st_data_t)objc_klass);
}
return mrb_nil_value();
@@ -554,10 +563,12 @@
mrb_value
cocoa_object_class_set(mrb_state *mrb, mrb_value klass)
{
+ struct cocoa_state *cs = cocoa_state(mrb, mrb_obj_ptr(klass)->c);
+
mrb_value pointer, val;
mrb_get_args(mrb, "oo", &pointer, &val);
- id *valp = cfunc_pointer_ptr(mrb_funcall(mrb, val, "to_ffi_value", 1, mrb_obj_value(cocoa_state(mrb)->object_class)));
+ id *valp = cfunc_pointer_ptr(mrb_funcall(mrb, val, "to_ffi_value", 1, mrb_obj_value(cs->object_class)));
*((id*)cfunc_pointer_ptr(pointer)) = *valp;
return val;
@@ -567,6 +578,8 @@
mrb_value
cocoa_class_load_cocoa_class(mrb_state *mrb, mrb_value klass)
{
+ struct cocoa_state *cs = cocoa_state(mrb, mrb_obj_ptr(klass)->c);
+
mrb_value class_name_mrb;
mrb_get_args(mrb, "o", &class_name_mrb);
const char *class_name = mrb_sym2name(mrb, mrb_symbol(class_name_mrb));
@@ -575,8 +588,8 @@
mrb_raisef(mrb, E_NAME_ERROR, "Can't load %s class in Cocoa", class_name);
}
- struct RClass *object_class = cocoa_state(mrb)->object_class;
- return mrb_obj_value(mrb_define_class_under(mrb, cocoa_state(mrb)->namespace, class_name, object_class));
+ struct RClass *object_class = cs->object_class;
+ return mrb_obj_value(mrb_define_class_under(mrb, cs->namespace, class_name, object_class));
}
@@ -606,8 +619,10 @@
mrb_value
cocoa_class_register(mrb_state *mrb, mrb_value klass)
{
+ struct cocoa_state *cs = cocoa_state(mrb, mrb_obj_ptr(klass)->c);
+
Class objc_class;
- if(cocoa_st_lookup(cocoa_state(mrb)->cocoa_classes, (cocoa_st_data_t)mrb_object(klass), (void*)&objc_class)) {
+ if(cocoa_st_lookup(cs->cocoa_classes, (cocoa_st_data_t)mrb_object(klass), (void*)&objc_class)) {
objc_registerClassPair(objc_class);
}
@@ -618,8 +633,10 @@
mrb_value
cocoa_class_addr(mrb_state *mrb, mrb_value klass)
{
+ struct cocoa_state *cs = cocoa_state(mrb, mrb_obj_ptr(klass)->c);
+
Class objc_class;
- if(cocoa_st_lookup(cocoa_state(mrb)->cocoa_classes, (cocoa_st_data_t)mrb_object(klass), (void*)&objc_class)) {
+ if(cocoa_st_lookup(cs->cocoa_classes, (cocoa_st_data_t)mrb_object(klass), (void*)&objc_class)) {
return cfunc_pointer_new_with_pointer(mrb, objc_class, 0);
}
@@ -641,11 +658,14 @@
void
init_cocoa_object(mrb_state *mrb, struct RClass* module)
{
- struct RClass *object_class = mrb_define_class_under(mrb, module, "Object", cfunc_state(mrb)->pointer_class);
- cocoa_state(mrb)->object_class = object_class;
- cocoa_state(mrb)->sym_obj_holder = mrb_intern(mrb, "$mobiruby_obj_holder");
- cocoa_state(mrb)->sym_delete = mrb_intern(mrb, "delete");
- cocoa_state(mrb)->cocoa_classes = cocoa_st_init_pointertable();
+ struct cocoa_state *cs = cocoa_state(mrb, module);
+ struct RClass *object_class = mrb_define_class_under(mrb, module, "Object", cfunc_state(mrb, NULL)->pointer_class);
+ set_cocoa_state(mrb, (struct RObject*)object_class, cs);
+
+ cs->object_class = object_class;
+ cs->sym_obj_holder = mrb_intern(mrb, "$mobiruby_obj_holder");
+ cs->sym_delete = mrb_intern(mrb, "delete");
+ cs->cocoa_classes = cocoa_st_init_pointertable();
mrb_define_class_method(mrb, object_class, "objc_addMethod", cocoa_object_class_objc_addMethod, ARGS_ANY());
mrb_define_class_method(mrb, object_class, "objc_addClassMethod", cocoa_object_class_objc_addClassMethod, ARGS_ANY());
View
23 src/cocoa_type.m
@@ -32,7 +32,7 @@
#define case_type(encode, tclass) \
case encode: \
- return cfunc_type_with_pointer(mrb, cfunc_state(mrb)->tclass, pointer_count);
+ return cfunc_type_with_pointer(mrb, cfunc_state(mrb, NULL)->tclass, pointer_count);
static mrb_value
@@ -41,7 +41,7 @@
mrb_value type_class = mrb_obj_value(type_rclass);
if(pointer_count > 0) {
- mrb_value cfunc_module = mrb_obj_value(cfunc_state(mrb)->namespace);
+ mrb_value cfunc_module = mrb_obj_value(cfunc_state(mrb, NULL)->namespace);
for(int i = 0; i < pointer_count; ++i) {
type_class = mrb_funcall(mrb, cfunc_module, "Pointer", 1, type_class);
}
@@ -80,23 +80,23 @@
break;
case '*': // CString
- return cfunc_type_with_pointer(mrb, cfunc_state(mrb)->pointer_class, pointer_count);
+ return cfunc_type_with_pointer(mrb, cfunc_state(mrb, NULL)->pointer_class, pointer_count);
case '@': // id(Object)
if(*(encode+1) == '?') { // block
- return cfunc_type_with_pointer(mrb, cocoa_state(mrb)->block_class, pointer_count);
+ return cfunc_type_with_pointer(mrb, cocoa_state(mrb, NULL)->block_class, pointer_count);
}
if(*(encode+1) == '"') {
encode += 2;
for(; *encode != '"'; ++encode) { /* no op */ }
}
- return cfunc_type_with_pointer(mrb, cocoa_state(mrb)->object_class, pointer_count);
+ return cfunc_type_with_pointer(mrb, cocoa_state(mrb, NULL)->object_class, pointer_count);
case '#': // Class (class)
- return cfunc_type_with_pointer(mrb, cfunc_state(mrb)->pointer_class, pointer_count);
+ return cfunc_type_with_pointer(mrb, cfunc_state(mrb, NULL)->pointer_class, pointer_count);
case ':': // SEL (selector)
- return cfunc_type_with_pointer(mrb, cfunc_state(mrb)->pointer_class, pointer_count);
+ return cfunc_type_with_pointer(mrb, cfunc_state(mrb, NULL)->pointer_class, pointer_count);
case '[': // array [size type]
{
@@ -122,8 +122,9 @@
char *name = malloc(size + 1);
memcpy(name, name1, size);
name[size] = '\0';
- if (mrb_const_defined(mrb, mrb_obj_value(cocoa_state(mrb)->struct_module), mrb_intern(mrb, name))) {
- mrb_value klass = mrb_const_get(mrb, mrb_obj_value(cocoa_state(mrb)->struct_module), mrb_intern(mrb, name));
+ struct cocoa_state *cs = cocoa_state(mrb, NULL);
+ if (mrb_const_defined(mrb, mrb_obj_value(cs->struct_module), mrb_intern(mrb, name))) {
+ mrb_value klass = mrb_const_get(mrb, mrb_obj_value(cs->struct_module), mrb_intern(mrb, name));
free(name);
return cfunc_type_with_pointer(mrb, mrb_class_ptr(klass), pointer_count);
}
@@ -131,7 +132,7 @@
const char* def = cocoa_bridgesupport_struct_lookup(mrb, name);
if(def) {
mrb_value elements = mrb_ary_new(mrb);
- mrb_value klass = mrb_obj_value(mrb_define_class_under(mrb, cocoa_state(mrb)->struct_module, name, cfunc_state(mrb)->struct_class));
+ mrb_value klass = mrb_obj_value(mrb_define_class_under(mrb, cs->struct_module, name, cfunc_state(mrb, NULL)->struct_class));
char *defstr = malloc(strlen(def) + 1);
strcpy(defstr, def);
@@ -171,7 +172,7 @@
// TODO: should support anonymous struct
// generate struct from type encode {id*@^i}
free(name);
- return cfunc_type_with_pointer(mrb, cfunc_state(mrb)->struct_class, pointer_count);
+ return cfunc_type_with_pointer(mrb, cfunc_state(mrb, NULL)->struct_class, pointer_count);
}
}
View
41 src/init_cocoa.m
@@ -15,19 +15,12 @@
#include "mruby/class.h"
#include "mruby/proc.h"
#include "mruby/dump.h"
+#include "cfunc_pointer.h"
#import <Foundation/Foundation.h>
#include <setjmp.h>
#include "ffi.h"
-extern const char mruby_cocoa_data_cocoa[];
-extern const char mruby_cocoa_data_object[];
-extern const char mruby_cocoa_data_object_property[];
-extern const char mruby_cocoa_data_object_method[];
-extern const char mruby_cocoa_data_object_ivar[];
-extern const char mruby_cocoa_data_block[];
-extern const char mruby_cocoa_data_protocol[];
-
size_t cocoa_state_offset = 0;
@@ -36,22 +29,15 @@
mrb_state **cocoa_mrb_states = NULL;
int cocoa_vm_count = 0;
-static
-void load_irep(mrb_state* mrb, const const char* data)
+
+mrb_value
+cocoa_mrb_state(mrb_state *mrb, mrb_value klass)
{
- int n = mrb_read_irep(mrb, data);
- if (n >= 0) {
- mrb_irep *irep = mrb->irep[n];
- struct RProc *proc = mrb_proc_new(mrb, irep);
- proc->target_class = mrb->object_class;
- mrb_run(mrb, proc, mrb_nil_value());
- }
- else if (mrb->exc) {
- longjmp(*(jmp_buf*)mrb->jmp, 1);
- }
+ return cfunc_pointer_new_with_pointer(mrb, mrb, false);
}
-void init_cocoa_module(mrb_state *mrb)
+void
+mrb_mruby_cocoa_gem_init(mrb_state *mrb)
{
if(cocoa_vm_count >= MAX_COCOA_MRB_STATE_COUNT - 1) {
puts("Too much open vm"); // TODO
@@ -66,21 +52,14 @@ void init_cocoa_module(mrb_state *mrb)
cocoa_mrb_states[cocoa_vm_count++] = mrb;
struct RClass *ns = mrb_define_module(mrb, "Cocoa");
- cocoa_state(mrb)->namespace = ns;
+ struct cocoa_state *state = mrb_malloc(mrb, sizeof(struct cocoa_state));
+ set_cocoa_state(mrb, ns, state);
init_objc_hook();
init_cocoa_module_type(mrb, ns);
init_cocoa_object(mrb, ns);
init_cocoa_block(mrb, ns);
- init_cocoa_bridge_support(mrb);
-
- load_irep(mrb, mruby_cocoa_data_cocoa);
- load_irep(mrb, mruby_cocoa_data_object);
- load_irep(mrb, mruby_cocoa_data_object_property);
- load_irep(mrb, mruby_cocoa_data_object_method);
- load_irep(mrb, mruby_cocoa_data_object_ivar);
- load_irep(mrb, mruby_cocoa_data_block);
- load_irep(mrb, mruby_cocoa_data_protocol);
+ init_cocoa_bridge_support(mrb, ns);
}
void close_cocoa_module(mrb_state *mrb)
View
101 test/Makefile
@@ -1,101 +0,0 @@
-# makefile discription.
-# basic build file for cocoa library
-
-# project-specific macros
-# extension of the executable-file is modifiable(.exe .out ...)
-BASEDIR = .
-REQUIRES := $(wildcard $(BASEDIR)/t/_*.rb)
-TESTSRCS := $(filter-out $(REQUIRES),$(wildcard $(BASEDIR)/t/*.rb))
-TESTOBJS := $(patsubst %.rb,%.o,$(TESTSRCS))
-TESTS := $(patsubst %.rb,%.exe,$(TESTSRCS))
-REQUIREOBJS := $(patsubst %.rb,%.o,$(REQUIRES))
-
-LIBR := $(BASEDIR)/../lib/libmruby-cocoa.a
-EXCEPT1 := $(MRBCSRC)
-MRBC := $(BASEDIR)/../vendors/bin/mrbc
-
-# libraries, includes
-LIBS = -framework Foundation -ldl -lm
-INCLUDES = -I$(BASEDIR)/../include
-
-# libffi
-LIBFFI_CFLAGS = $(shell pkg-config libffi --cflags)
-LIBFFI_LIBS = $(shell pkg-config libffi --libs)
-
-ifeq ($(shell uname -s),Darwin)
- LDFLAGS = -Wl,-allow_stack_execute
-else
- LDFLAGS = -Wl,--export-dynamic
-endif
-
-ifeq ($(strip $(COMPILE_MODE)),)
- # default compile option
- COMPILE_MODE = debug
-endif
-
-ifeq ($(COMPILE_MODE),debug)
- CFLAGS = -g -O3 -rdynamic
-else ifeq ($(COMPILE_MODE),release)
- CFLAGS = -O3 -rdynamic
-else ifeq ($(COMPILE_MODE),small)
- CFLAGS = -Os -rdynamic
-endif
-
-ALL_CFLAGS = -Wall -Werror-implicit-function-declaration -std=c99 $(CFLAGS) $(CFUNC_CFLAGS) $(MRUBY_CFLAGS) $(LIBFFI_CFLAGS)
-
-
-
-##############################
-# internal variables
-
-export CP := cp
-export RM_F := rm -f
-export CAT := cat
-
-
-##############################
-# generic build targets, rules
-
-.PHONY : all
-all : $(TESTS)
-
-.PHONY : run
-run : $(TESTS)
- @echo
- @echo "*** Start tests ***"
- @echo
- @for command in $(TESTS) ; do \
- if $$command ; then \
- echo ; \
- else \
- echo "*** Failed tests ***" ; \
- exit 1 ; \
- fi ; \
- done
- @echo "*** Passed tests ***"
-
-# executable constructed using linker from object files
-$(TESTS) : %.exe : %.o $(REQUIREOBJS)
- echo $(REQUIREOBJS)
- $(LL) $< -o $@ $(REQUIREOBJS) $(CFLAGS) $(LDFLAGS) $(LIBR) $(CFUNC_LIBS) $(LIBFFI_LIBS) $(MRUBY_LIBS) $(LIBS)
-
--include $(OBJS:.o=.d) $(OBJMRB:.o=.d)
-
-# mrby complie
-$(TESTOBJS) : %.o : %.rb
- cat $< | ruby -e "print STDIN.read.gsub(/#\s*BEGIN\s+C.*/m,'')" | $(MRBC) -o- -Btest_irep - > $(basename $<).m
- echo "const char* appname = \"$(basename $<)\";" >> $(basename $<).m
- cat main.m >> $(basename $<).m
- cat $< | ruby -e 's=STDIN.read;print s.gsub(/.*#\s*BEGIN\s+C(.*)/m,"\\1") if s.match(/#\s*BEGIN\s+C/)' >> $(basename $<).m
- $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(basename $<).m -o $@
-
-$(REQUIREOBJS) : %.o : %.rb
- $(MRBC) -Bmruby_data_`basename $(basename $<)` $<
- $(CC) $(ALL_CFLAGS) -MMD $(INCLUDES) -c $(basename $<).m -o $@
-
-# clean up
-.PHONY : clean #cleandep
-clean :
- @echo "make: removing targets, objects and depend files of `pwd`"
- -$(RM_F) $(TESTS) $(TESTOBJS) $(TESTS)
- -$(RM_F) $(TESTOBJS:.o=.d) $(TESTOBJS:.o=.m)
View
1  test/bridgesupport_struct.rb
@@ -0,0 +1 @@
+assert_equal 8, ::Cocoa::Struct::MobiCocoaStruct2.size
View
2  test/const.rb
@@ -0,0 +1,2 @@
+assert_equal 12345, Cocoa::Const::test1.to_i
+assert_equal 1, Cocoa::Const::enum1
View
112 test/define.m
@@ -0,0 +1,112 @@
+#import "cocoa.h"
+#import "Foundation/Foundation.h"
+#include <objc/runtime.h>
+#include <objc/message.h>
+
+struct BridgeSupportStructTable struct_table[] = {
+ {.name = "MobiCocoaStruct1", .definition = "i:i"},
+ {.name = "MobiCocoaStruct2", .definition = "i1:i:i2:i"},
+ {.name = NULL, .definition = NULL}
+};
+
+const double test1 = 12345;
+
+struct BridgeSupportConstTable const_table[] = {
+ {.name = "test1", .type = "d", .value = (void*)&test1},
+ {.name = NULL, .type=NULL, .value = NULL}
+};
+
+struct BridgeSupportEnumTable enum_table[] = {
+ {.name="enum1"}, // 1
+ {.name = NULL}
+};
+
+struct MobiCocoaStruct1 {
+ int i;
+};
+
+
+@interface MobiCocoaTest1 : NSObject {
+ int i;
+ id obj;
+ NSString *prop2;
+ struct MobiCocoaStruct1 struct1;
+}
+@property(retain, getter=prop1_, readonly) NSString *prop1;
+@property(retain) NSString *prop2;
+@property(assign) struct MobiCocoaStruct1 struct1;
+@end
+
+
+@implementation MobiCocoaTest1
+
+@dynamic prop1;
+@synthesize prop2, struct1;
+
++ (NSString*)classMethod1
+{
+ return @"classMethod1Test";
+}
+
+- (id)init {
+ if(self = [super init]) {
+ i = 10;
+ obj = @"Test";
+ }
+
+ return self;
+}
+
+- (void)set_i:(int)value
+{
+ i = value;
+}
+
+- (NSString*)prop1_
+{
+ return @"PROP1";
+}
+
+- (NSString*)intToString:(int)value
+{
+ return [NSString stringWithFormat: @"value=%d", value];
+}
+
+- (NSString*)uint16ToString:(unsigned short)value
+{
+ return [NSString stringWithFormat: @"value=%hu", value];
+}
+
+- (int)testBlocks1:(int (^)(int))block
+{
+ int result = block(2) * block(3);
+ return result;
+}
+
+- (int)testBlocks2:(int (^)(int))block
+{
+ int (^myBlock)(int) = ^(int num) {
+ return block(1+num) * block(2+num);
+ };
+ int result = myBlock(1);
+ return result;
+}
+
+@end
+
+
+int MobiCocoaTest2Ruby1(id obj)
+{
+ return (int)objc_msgSend(obj, sel_getUid("ruby_method1"));
+}
+
+int MobiCocoaTest2Ruby2(id obj)
+{
+ return (int)objc_msgSend(obj, sel_getUid("ruby_method2:"), 3);
+}
+
+int MobiCocoaTest2Ruby3(id obj)
+{
+ return (int)objc_msgSend([obj class], sel_getUid("ruby_method3:"), 3);
+}
+
View
174 test/main.m
@@ -1,140 +1,66 @@
#include "cocoa.h"
-#include "cfunc.h"
#include "mruby.h"
#include "mruby/dump.h"
#include "mruby/proc.h"
#include "mruby/compile.h"
-#include <dlfcn.h>
-#import <Foundation/Foundation.h>
+const char* cocoa_assert_rb = " \
+class MobiRubyTest \n\
+ def initialize(label) \n\
+ @label = label \n\
+ @index = 0 \n\
+ end \n\
+\
+ def run(&block) \n\
+ begin \n\
+ instance_eval(&block) \n\
+ rescue Exception => e \n\
+ str = \n\"#{@label} ##{@index}\" \n\
+ $asserts.push(['Error: ', str, '', e]) \n\
+ $kill_test += 1 \n\
+ print('X') \n\
+ end \n\
+ end \n\
+\
+ def assert(result, label='') \n\
+ @index += 1 \n\
+ str = \"#{@label} ##{@index} #{label}\" \n\
+ if !result \n\
+ $asserts.push(['Fail: ', str, ''])\n \n\
+ $ko_test += 1 \n\
+ print('F') \n\
+ else \n\
+ $ok_test += 1 \n\
+ print('.') \n\
+ end \n\
+ end \n\
+\
+ def assert_equal(a, b) \n\
+ assert(a===b, \n\"<#{a.inspect}> expected but was <#{b.inspect}>\") \n\
+ end \n\
+\
+ def assert_not_equal(a, b) \n\
+ assert(!(a===b), \n\"<#{a.inspect}> not expected but was <#{b.inspect}>\") \n\
+ end \n\
+end \n\
+\
+def mobiruby_test(label, &block) \n\
+ MobiRubyTest.new(label).run(&block) \n\
+end";
-struct mrb_state_ud {
- struct cfunc_state cfunc_state;
- struct cocoa_state cocoa_state;
-};
-const char* assert_rb =
-"$ok_test = 0" "\n"
-"$ko_test = 0" "\n"
-"$kill_test = 0" "\n"
-"$asserts = []" "\n"
-""
-"def assert(test, failure_message = 'Assertion failed')" "\n"
-" begin" "\n"
-" if test" "\n"
-" $ok_test += 1" "\n"
-" print('.')" "\n"
-" else" "\n"
-" $asserts.push(['Fail: ', failure_message])" "\n"
-" $ko_test += 1" "\n"
-" print('F')" "\n"
-" end" "\n"
-" rescue Exception => e" "\n"
-" $asserts.push(['Error: ', failure_message, e])" "\n"
-" $kill_test += 1" "\n"
-" print('X')" "\n"
-" end" "\n"
-"end" "\n"
-""
-"def assert_equal(expected, actual, failure_message = nil)" "\n"
-" failure_message ||= \"#{actual} expected '#{expected}' assertion failed\"" "\n"
-" assert(expected === actual, failure_message)" "\n"
-"end" "\n"
-""
-"def assert_not_equal(expected, actual, failure_message = nil)" "\n"
-" assert(!(expected === actual), \"#{actual} expected not '#{expected}' assertion failed\")" "\n"
-"end" "\n"
-""
-"def assert_raise(*args)" "\n"
-" failure_message = args.last.is_a?(String) ? args.pop : nil" "\n"
-" begin" "\n"
-" yield" "\n"
-" assert(false, \"#{args} expected but none was thrown.\")" "\n"
-" rescue Exception => e" "\n"
-" assert(args.include?(e.class), \"#{args} expected but was #{e}.\")" "\n"
-" end" "\n"
-"end" "\n"
-""
-// This method copy from mruby/test/assert.rb" "\n"
-// License: MITL mruby developers" "\n"
-// Report the test result and print all assertions" "\n"
-// which were reported broken." "\n"
-"def test_results()" "\n"
-" print \"\\n\"" "\n"
-" $asserts.each do |err, str, e|" "\n"
-" print(err);" "\n"
-" print(str);" "\n"
-" if e" "\n"
-" print(\" => \")" "\n"
-" print(e.message)" "\n"
-" end" "\n"
-" print(\"\\n\")" "\n"
-" end" "\n"
-""
-" $total_test = $ok_test.+($ko_test)" "\n"
-" print('Total: ')" "\n"
-" print($total_test)" "\n"
-" print(\"\\n\")" "\n"
-""
-" print(' OK: ')" "\n"
-" print($ok_test)" "\n"
-" print(\"\\n\")" "\n"
-" print(' KO: ')" "\n"
-" print($ko_test)" "\n"
-" print(\"\\n\")" "\n"
-" print('Crash: ')" "\n"
-" print($kill_test)" "\n"
-" print(\"\\n\")" "\n"
-""
-" ($ko_test + $kill_test) == 0" "\n"
-"end";
+struct BridgeSupportStructTable struct_table[];
+struct BridgeSupportConstTable const_table[];
+struct BridgeSupportEnumTable enum_table[];
-static
-void mrb_state_init(mrb_state *mrb)
+void
+mrb_mruby_cocoa_gem_test_init(mrb_state *mrb)
{
- mrb->ud = malloc(sizeof(struct mrb_state_ud));
- init_cfunc_module(mrb, mrb_state_init);
- init_cocoa_module(mrb);
-
- void *dlh = dlopen(NULL, RTLD_LAZY);
- void (*fp)(mrb_state*) = dlsym(dlh, "load_rubyvm");
- if(fp) {
- fp(mrb);
- }
-}
-
-
-int main(int argc, char *argv[])
-{
- NSAutoreleasePool *pool;
- pool = [[NSAutoreleasePool alloc] init];
-
- printf("%s: ", appname);
-
- cfunc_state_offset = cfunc_offsetof(struct mrb_state_ud, cfunc_state);
- cocoa_state_offset = cocoa_offsetof(struct mrb_state_ud, cocoa_state);
-
- mrb_state *mrb = mrb_open();
- mrb_state_init(mrb);
-
- mrb_load_string(mrb, assert_rb);
+ load_cocoa_bridgesupport(mrb, struct_table, const_table, enum_table);
+ mrb_load_string(mrb, cocoa_assert_rb);
if (mrb->exc) {
mrb_p(mrb, mrb_obj_value(mrb->exc));
exit(1);
}
-
- int n = mrb_read_irep(mrb, test_irep);
- mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
- if (mrb->exc) {
- mrb_p(mrb, mrb_obj_value(mrb->exc));
- mrb_load_string(mrb, "test_results();");
- exit(1);
- }
- if(mrb_test(mrb_load_string(mrb, "test_results()"))) {
- exit(0);
- }
- else {
- exit(1);
- }
}
View
0  test/t/nsstring.rb → test/nsstring.rb
File renamed without changes
View
42 test/t/object_call_method.rb → test/object_call_method.rb
@@ -30,45 +30,3 @@
test1 = Cocoa::MobiCocoaTest1._alloc._init
result = test1._testBlocks2 block
assert_equal 12, result.value
-
-
-############
-# BEGIN C
-
-@interface MobiCocoaTest1 : NSObject
-@end
-
-
-@implementation MobiCocoaTest1
-
-+ (NSString*)classMethod1
-{
- return @"classMethod1Test";
-}
-
-- (NSString*)intToString:(int)value
-{
- return [NSString stringWithFormat: @"value=%d", value];
-}
-
-- (NSString*)uint16ToString:(unsigned short)value
-{
- return [NSString stringWithFormat: @"value=%hu", value];
-}
-
-- (int)testBlocks1:(int (^)(int))block
-{
- int result = block(2) * block(3);
- return result;
-}
-
-- (int)testBlocks2:(int (^)(int))block
-{
- int (^myBlock)(int) = ^(int num) {
- return block(1+num) * block(2+num);
- };
- int result = myBlock(1);
- return result;
-}
-
-@end
View
0  test/t/object_declare_ivar.rb → test/object_declare_ivar.rb
File renamed without changes
View
0  test/t/object_declare_property.rb → test/object_declare_property.rb
File renamed without changes
View
39 test/t/object_inherit.rb → test/object_inherit.rb
@@ -49,42 +49,3 @@ class Cocoa::MobiCocoaTest2 < Cocoa::MobiCocoaTest1
test2 = Cocoa::MobiCocoaTest2._alloc._init
result2 = test2._intToString(CFunc::Int(10))
assert_equal "value=10", result2._UTF8String.to_s
-
-
-############
-# BEGIN C
-#import "objc/runtime.h"
-
-@interface MobiCocoaTest1 : NSObject
-@end
-
-
-@implementation MobiCocoaTest1
-
-+ (NSString*)classMethod1
-{
- return @"classMethod1Test";
-}
-
-- (NSString*)intToString:(int)value
-{
- return [NSString stringWithFormat: @"value=%d", value];
-}
-
-@end
-
-
-int MobiCocoaTest2Ruby1(id obj)
-{
- return (int)objc_msgSend(obj, sel_getUid("ruby_method1"));
-}
-
-int MobiCocoaTest2Ruby2(id obj)
-{
- return (int)objc_msgSend(obj, sel_getUid("ruby_method2:"), 3);
-}
-
-int MobiCocoaTest2Ruby3(id obj)
-{
- return (int)objc_msgSend([obj class], sel_getUid("ruby_method3:"), 3);
-}
View
29 test/t/object_use_ivar.rb → test/object_use_ivar.rb
@@ -20,32 +20,3 @@ def _S(str)
test1.ivar[:obj] = _S("Test123")
obj = test1.ivar[:obj]
assert_equal "Test123", obj._UTF8String.to_s
-
-
-############
-# BEGIN C
-
-@interface MobiCocoaTest1 : NSObject {
- int i;
- id obj;
-}
-@end
-
-
-@implementation MobiCocoaTest1
-
-- (id)init {
- if(self = [super init]) {
- i = 10;
- obj = @"Test";
- }
-
- return self;
-}
-
-- (void)set_i:(int)value
-{
- i = value;
-}
-
-@end
View
24 test/object_use_property.rb
@@ -0,0 +1,24 @@
+def _S(str)
+ Cocoa::NSString._stringWithUTF8String(str)
+end
+
+# create instance
+test1 = Cocoa::MobiCocoaTest1._alloc._init
+
+# get property
+result = test1[:prop1]
+assert_equal "PROP1", result._UTF8String.to_s
+
+# get property
+test1[:prop2] = _S("PROP2")
+result = test1[:prop2]
+assert_equal "PROP2", result._UTF8String.to_s
+
+# update propperty
+test1[:prop2] = _S("PROP2_")
+result = test1[:prop2]
+assert_equal "PROP2_", result._UTF8String.to_s
+
+# get struct value from property
+result = test1[:struct1]
+assert_equal 10, result[:i]
View
0  test/t/protocol.rb → test/protocol.rb
File renamed without changes
View
1  test/t/.gitignore
@@ -1 +0,0 @@
-*.m
View
23 test/t/bridgesupport_struct.rb
@@ -1,23 +0,0 @@
-assert_equal 8, ::Cocoa::Struct::MobiCocoaStruct2.size
-
-
-############
-# BEGIN C
-
-struct BridgeSupportStructTable struct_table[] = {
- {.name = "MobiCocoaStruct2", .definition = "i1:i:i2:i"},
- {.name = NULL, .definition = NULL}
-};
-
-struct BridgeSupportConstTable const_table[] = {
- {.name = NULL, .type=NULL, .value = NULL}
-};
-
-struct BridgeSupportEnumTable enum_table[] = {
- {.name = NULL}
-};
-
-void
-load_rubyvm(mrb_state *mrb) {
- load_cocoa_bridgesupport(mrb, struct_table, const_table, enum_table);
-}
View
28 test/t/const.rb
@@ -1,28 +0,0 @@
-assert_equal 12345, Cocoa::Const::test1.to_i
-assert_equal 1, Cocoa::Const::enum1
-
-
-############
-# BEGIN C
-
-struct BridgeSupportStructTable struct_table[] = {
- {.name = NULL, .definition = NULL}
-};
-
-const double test1 = 12345;
-
-struct BridgeSupportConstTable const_table[] = {
- {.name = "test1", .type = "d", .value = (void*)&test1},
- {.name = NULL, .type=NULL, .value = NULL}
-};
-
-struct BridgeSupportEnumTable enum_table[] = {
- {.name="enum1"}, // 1
- {.name = NULL}
-};
-
-void
-load_rubyvm(mrb_state *mrb) {
- enum_table[0].value = mrb_fixnum_value(1);
- load_cocoa_bridgesupport(mrb, struct_table, const_table, enum_table);
-}
View
83 test/t/object_use_property.rb
@@ -1,83 +0,0 @@
-def _S(str)
- Cocoa::NSString._stringWithUTF8String(str)
-end
-
-# create instance
-test1 = Cocoa::MobiCocoaTest1._alloc._init
-
-# get property
-result = test1[:prop1]
-assert_equal "PROP1", result._UTF8String.to_s
-
-# get property
-test1[:prop2] = _S("PROP2")
-result = test1[:prop2]
-assert_equal "PROP2", result._UTF8String.to_s
-
-# update propperty
-test1[:prop2] = _S("PROP2_")
-result = test1[:prop2]
-assert_equal "PROP2_", result._UTF8String.to_s
-
-# get struct value from property
-result = test1[:struct1]
-assert_equal 100, result[:i]
-
-
-############
-# BEGIN C
-#include <objc/runtime.h>
-
-struct BridgeSupportStructTable struct_table[] = {
- {.name = "MobiCocoaStruct1", .definition = "i:i"},
- {.name = NULL, .definition = NULL}
-};
-
-struct BridgeSupportConstTable const_table[] = {
- {.name = NULL, .type=NULL, .value = NULL}
-};
-
-struct BridgeSupportEnumTable enum_table[] = {
- {.name = NULL}
-};
-
-void
-load_rubyvm(mrb_state *mrb) {
- load_cocoa_bridgesupport(mrb, struct_table, const_table, enum_table);
-}
-
-struct MobiCocoaStruct1 {
- int i;
-};
-
-@interface MobiCocoaTest1 : NSObject {
- NSString *prop2;
- struct MobiCocoaStruct1 struct1;
-}
-@property(retain, getter=prop1_, readonly) NSString *prop1;
-@property(retain) NSString *prop2;
-@property(assign) struct MobiCocoaStruct1 struct1;
-
-@end
-
-
-@implementation MobiCocoaTest1
-
-@dynamic prop1;
-@synthesize prop2, struct1;
-
-
-- (id)init {
- if(self = [super init]) {
- struct1.i = 100;
- }
-
- return self;
-}
-
-- (NSString*)prop1_
-{
- return @"PROP1";
-}
-
-@end
Please sign in to comment.
Something went wrong with that request. Please try again.