Permalink
Browse files

NativeFunction, Selector, SendSite

Convert a bunch more to C++. ffi is in and tested about 50% of the way.
  • Loading branch information...
1 parent 85c9419 commit fceff3d851aefaf0063012f9033d0921a0a30999 Evan Phoenix committed Apr 10, 2008
View
@@ -0,0 +1,7 @@
+test:
+ cd test; $(MAKE) test
+
+coverage:
+ cd test; $(MAKE) coverage
+
+.PHONY: test coverage
View
@@ -68,4 +68,14 @@ namespace rubinius {
set(state, (size_t)total->n2i(), val);
return val;
}
+
+ bool Array::includes_p(STATE, OBJECT val) {
+ size_t max = size();
+
+ for(size_t i = 0; i < max; i++) {
+ if(tuple->at(i) == val) return true;
+ }
+
+ return false;
+ }
}
View
@@ -169,7 +169,8 @@ namespace rubinius {
}
void Bignum::init(STATE) {
- state->add_cleanup(BASIC_CLASS(bignum), Bignum::cleanup);
+ TypeInfo *ti = state->get_type_info(state->globals.bignum);
+ ti->cleanup = Bignum::cleanup;
}
Bignum* Bignum::create(STATE, native_int num) {
View
@@ -21,6 +21,10 @@ namespace rubinius {
constants->store(state, sym, val);
}
+ void Module::set_const(STATE, char* name, OBJECT val) {
+ constants->store(state, state->symbol(name), val);
+ }
+
OBJECT Module::get_const(STATE, OBJECT sym) {
return constants->fetch(state, sym);
}
View
@@ -19,6 +19,7 @@ namespace rubinius {
void setup(STATE);
void setup(STATE, char* name, Module* under = NULL);
void set_const(STATE, OBJECT sym, OBJECT val);
+ void set_const(STATE, char* name, OBJECT val);
OBJECT get_const(STATE, OBJECT sym);
OBJECT get_const(STATE, char* sym);
};
@@ -34,6 +35,10 @@ namespace rubinius {
static bool is_a(OBJECT obj) {
return obj->obj_type == ClassType;
}
+
+ void set_object_type(size_t type) {
+ object_type = Object::i2n(type);
+ }
};
class MetaClass : public Class {
View
@@ -12,8 +12,7 @@ namespace rubinius {
#if (CONFIG_WORDSIZE != 64)
if(num > FIXNUM_MAX) {
/* Number is too big for Fixnum. Use Bignum. */
- assert(0);
- // return Bignum::new_unsigned(state, num);
+ return Bignum::new_unsigned(state, num);
}
#endif
return APPLY_TAG((native_int)num, TAG_FIXNUM);
@@ -27,6 +26,22 @@ namespace rubinius {
}
}
+ OBJECT Object::ll2n(STATE, long long num) {
+ if(num > FIXNUM_MAX || num < FIXNUM_MIN) {
+ return Bignum::from_ll(state, num);
+ } else {
+ return APPLY_TAG(num, TAG_FIXNUM);
+ }
+ }
+
+ OBJECT Object::ull2n(STATE, unsigned long long num) {
+ if(num > FIXNUM_MAX) {
+ return Bignum::from_ull(state, num);
+ } else {
+ return APPLY_TAG(num, TAG_FIXNUM);
+ }
+ }
+
OBJECT Object::class_object() {
OBJECT cls = klass;
while(cls->reference_p() && (MetaClass::is_a(cls) || !Class::is_a(cls))) {
View
@@ -20,7 +20,7 @@
#define REG(k) (*((regex_t**)((k)->bytes)))
-#define BASIC_CLASS(blah) state->globals.blah
+#define BASIC_CLASS(blah) G(blah)
#define NEW_STRUCT(obj, str, kls, kind) \
obj = (typeof(obj))state->new_struct(kls, sizeof(kind)); \
str = (kind *)BYTES_OF(obj)
@@ -34,7 +34,16 @@ namespace rubinius {
void Regexp::init(STATE) {
onig_init();
- state->add_cleanup(BASIC_CLASS(regexpdata), Regexp::cleanup);
+ G(regexp) = state->new_class("Regexp", G(object), 0);
+ G(regexp)->object_type = Object::i2n(RegexpType);
+
+ G(regexpdata) = state->new_class("RegexpData", G(object), 0);
+ G(regexpdata)->object_type = Object::i2n(RegexpDataType);
+
+ G(matchdata) = state->new_class("MatchData", G(object), 0);
+
+ TypeInfo* ti = state->get_type_info(G(regexpdata));
+ ti->cleanup = Regexp::cleanup;
}
char *Regexp::version(STATE) {
@@ -128,7 +137,7 @@ namespace rubinius {
return (Regexp*)Qnil;
}
- Regexp* o_reg = (Regexp*)state->om->new_object(state->globals.regexp, Regexp::fields);
+ Regexp* o_reg = (Regexp*)state->om->new_object(G(regexp), Regexp::fields);
SET(o_reg, source, pattern);
SET(o_reg, data, o_regdata);
@@ -173,7 +182,7 @@ namespace rubinius {
}
static OBJECT get_match_data(STATE, OnigRegion *region, String* string, Regexp* regexp, int max) {
- MatchData* md = (MatchData*)state->om->new_object(state->globals.matchdata, MatchData::fields);
+ MatchData* md = (MatchData*)state->om->new_object(G(matchdata), MatchData::fields);
SET(md, source, string->string_dup(state));
SET(md, regexp, regexp);
Tuple* tup = Tuple::create(state, 2);
View
@@ -0,0 +1,57 @@
+#include "builtin_selector.hpp"
+
+namespace rubinius {
+ void Selector::init(STATE) {
+ state->globals.selectors = LookupTable::create(state);
+ Class* cls = state->new_class("Selector", Selector::fields);
+ cls->set_object_type(SelectorType);
+
+ state->globals.selector = cls;
+
+ cls->set_const(state, state->symbol("ALL"), state->globals.selectors);
+ }
+
+ Selector* Selector::create(STATE, OBJECT name) {
+ Selector* sel = (Selector*)state->new_object(state->globals.selector);
+ SET(sel, name, name);
+ SET(sel, send_sites, Array::create(state, 1));
+
+ return sel;
+ }
+
+ Selector* Selector::lookup(STATE, OBJECT name) {
+ Selector* sel = (Selector*)state->globals.selectors->fetch(state, name);
+ if(!sel->nil_p()) return sel;
+
+ sel = Selector::create(state, name);
+ state->globals.selectors->store(state, name, sel);
+
+ return sel;
+ }
+
+ OBJECT Selector::associate(STATE, SendSite* ss) {
+ send_sites->append(state, ss);
+ return ss;
+ }
+
+ bool Selector::includes_p(STATE, SendSite* ss) {
+ return send_sites->includes_p(state, ss);
+ }
+
+ void Selector::clear(STATE) {
+ size_t sz = send_sites->size();
+ SendSite* ss;
+
+ for(size_t i = 0; i < sz; i++) {
+ ss = (SendSite*)send_sites->get(state, i);
+ ss->initialize(state);
+ }
+ }
+
+ void Selector::clear_by_name(STATE, OBJECT name) {
+ Selector* sel = (Selector*)state->globals.selectors->fetch(state, name);
+ if(sel->nil_p()) return;
+
+ sel->clear(state);
+ }
+};
View
@@ -0,0 +1,34 @@
+#ifndef RBX_BUILTIN_SELECTOR_HPP
+#define RBX_BUILTIN_SELECTOR_HPP
+
+#include "prelude.hpp"
+#include "vm.hpp"
+#include "objectmemory.hpp"
+#include "objects.hpp"
+
+#include "builtin_sendsite.hpp"
+
+namespace rubinius {
+ class SendSite;
+
+ class Selector : public BuiltinType {
+ public:
+
+ static const size_t fields = 2;
+
+ OBJECT name;
+ Array* send_sites;
+
+ static void init(STATE);
+ static Selector* create(STATE, OBJECT name);
+ static Selector* lookup(STATE, OBJECT name);
+ static void clear_by_name(STATE, OBJECT name);
+
+ OBJECT associate(STATE, SendSite* ss);
+ void clear(STATE);
+ bool includes_p(STATE, SendSite* ss);
+
+ };
+};
+
+#endif
View
@@ -0,0 +1,31 @@
+#include "builtin_sendsite.hpp"
+#include "builtin_selector.hpp"
+
+namespace rubinius {
+ void SendSite::init(STATE) {
+ state->globals.send_site = state->new_class("SendSite", 0);
+ state->globals.send_site->set_object_type(SendSiteType);
+ }
+
+ SendSite* SendSite::create(STATE, OBJECT name) {
+ SendSite* ss = (SendSite*)state->new_struct(state->globals.send_site, sizeof(SendSite));
+ SET(ss, name, name);
+ SET(ss, sender, Qnil);
+ SET(ss, selector, Selector::lookup(state, name));
+ ss->hits = ss->misses = 0;
+
+ ss->initialize(state);
+ ss->selector->associate(state, ss);
+
+ return ss;
+ }
+
+ void SendSite::initialize(STATE) {
+ data1 = data2 = data3 = Qnil;
+ }
+
+ /* Indicates that the object hasn't been specialized in anyway */
+ bool SendSite::basic_p(STATE) {
+ return data1 == Qnil;
+ }
+};
View
@@ -0,0 +1,36 @@
+#ifndef RBX_BUILTIN_SENDSITE_HPP
+#define RBX_BUILTIN_SENDSITE_HPP
+
+#include "prelude.hpp"
+#include "vm.hpp"
+#include "objectmemory.hpp"
+#include "objects.hpp"
+
+namespace rubinius {
+ class Selector;
+
+ class SendSite : public BuiltinType {
+ public:
+
+ static const size_t object_fields = 6;
+
+ OBJECT name;
+ OBJECT sender;
+ Selector* selector;
+ OBJECT data1;
+ OBJECT data2;
+ OBJECT data3;
+
+ int data4;
+ size_t hits, misses;
+ void* c_data;
+
+ static void init(STATE);
+ static SendSite* create(STATE, OBJECT name);
+ void set_sender(STATE, OBJECT cm);
+ void initialize(STATE);
+ bool basic_p(STATE);
+ };
+};
+
+#endif
View
@@ -72,7 +72,7 @@ namespace rubinius {
char* String::byte_address(STATE) {
return (char*)data->bytes;
}
-
+
int String::string_equal_p(STATE, OBJECT a, OBJECT b) {
String* self = (String*)a;
String* other = (String*)b;
View
@@ -0,0 +1,13 @@
+#ifndef RBX_BUILTIN_TASK_HPP
+#define RBX_BUILTIN_TASK_HPP
+
+#include "prelude.hpp"
+#include "vm.hpp"
+#include "objectmemory.hpp"
+#include "objects.hpp"
+
+namespace rubinius {
+ class Task : public BuiltinType {
+
+ };
+}
View
@@ -0,0 +1,19 @@
+#include "prelude.hpp"
+#include "object.hpp"
+#include "objects.hpp"
+#include "vm.hpp"
+#include "objectmemory.hpp"
+
+#include "cpu.hpp"
+
+namespace rubinius {
+
+ Message::Message(STATE, Array* ary) {
+ this->state = state;
+ arguments = ary;
+ }
+
+ OBJECT Message::get_argument(size_t index) {
+ return arguments->get(state, index);
+ }
+}
View
@@ -0,0 +1,16 @@
+#ifndef RBX_MESSAGE_HPP
+#define RBX_MESSAGE_HPP
+
+namespace rubinius {
+ class Message {
+ public:
+ Array* arguments;
+ STATE;
+
+ Message::Message(STATE, Array* ary);
+ OBJECT get_argument(size_t index);
+
+ };
+}
+
+#endif
Oops, something went wrong.

0 comments on commit fceff3d

Please sign in to comment.