Skip to content

Commit

Permalink
Improvements in object model:
Browse files Browse the repository at this point in the history
- get_class and instantiate vtable functions
- builtin typeof gives the Class
- method new in Class
- Fix tests that used the naive typeof
  • Loading branch information
NotFound committed May 31, 2012
1 parent 100c3b4 commit bc6a98b
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 12 deletions.
9 changes: 3 additions & 6 deletions t/base/features.t
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -175,17 +175,14 @@ function test_pmc(test)
function test_array(test) function test_array(test)
{ {
var arr = [ "a", "b", "c", "d" ]; var arr = [ "a", "b", "c", "d" ];
string clname = typeof(arr); test.ok(arr instanceof "ResizablePMCArray", "array literal has expected type");
test.is_string(clname, "ResizablePMCArray", "array literal has expected type");
test.is(elements(arr), 4, "array literal has expected size"); test.is(elements(arr), 4, "array literal has expected size");
test.is_string(arr[2], "c", "array element initialized"); test.is_string(arr[2], "c", "array element initialized");
string sarr[] = [ "foo", "bar" ]; string sarr[] = [ "foo", "bar" ];
clname = typeof(sarr); test.ok(sarr instanceof "ResizableStringArray", "string array has expected type");
test.is_string(clname, "ResizableStringArray", "string array has expected type");
int iarr[] = [ 42 ]; int iarr[] = [ 42 ];
clname = typeof(iarr); test.ok(iarr instanceof "ResizableIntegerArray", "int array has expected type");
test.is_string(clname, "ResizableIntegerArray", "int array has expected type");
} }
function test_hash(test) function test_hash(test)
Expand Down
33 changes: 33 additions & 0 deletions winxedxx_classes.cxx
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -465,6 +465,11 @@ WxxClass::WxxClass(const std::string &name) :
(*wxxclassreg)[name] = this; (*wxxclassreg)[name] = this;
} }


std::string WxxClass::get_string()
{
return clname;
}

std::string WxxClass::class_name() const std::string WxxClass::class_name() const
{ {
return clname; return clname;
Expand All @@ -485,6 +490,23 @@ WxxClass::memberfun WxxClass::getfunction(const std::string &fname)
return regfun[fname]; return regfun[fname];
} }


WxxObjectPtr WxxClass::get_class()
{
return this;
}

WxxObjectPtr WxxClass::instantiate()
{
return new WxxInstance(*this);
}

WxxObjectPtr WxxClass::call_method(const std::string &methname, WxxObjectArray &args)
{
if (methname == "new")
return instantiate();
else
return WxxDefault::call_method(methname, args);
}


//************************************************************* //*************************************************************


Expand All @@ -496,11 +518,22 @@ WxxInstance::WxxInstance(const std::string &clname) :
throw wxx_error("class not found: " + clname); throw wxx_error("class not found: " + clname);
} }


WxxInstance::WxxInstance(WxxClass &classobj) :
WxxDefault(classobj.class_name()),
cl(& classobj)
{
}

std::string WxxInstance::class_name() const std::string WxxInstance::class_name() const
{ {
return cl->class_name(); return cl->class_name();
} }


WxxObjectPtr WxxInstance::get_class()
{
return cl;
}

WxxObjectPtr WxxInstance::call_method(const std::string &methname, WxxObjectArray &args) WxxObjectPtr WxxInstance::call_method(const std::string &methname, WxxObjectArray &args)
{ {
WxxClass::memberfun fun = cl->getfunction(methname); WxxClass::memberfun fun = cl->getfunction(methname);
Expand Down
15 changes: 14 additions & 1 deletion winxedxx_default.cxx
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ WxxObjectPtr WxxDefault::shift_pmc()
return winxedxxnull; return winxedxxnull;
} }


WxxObjectPtr WxxDefault::get_class()
{
//notimplemented("get_class");
//return winxedxxnull;
return this;
}

WxxObjectPtr WxxDefault::instantiate()
{
notimplemented("instantiate");
return winxedxxnull;
}

WxxObjectPtr WxxDefault::operator()(WxxObjectArray &args) WxxObjectPtr WxxDefault::operator()(WxxObjectArray &args)
{ {
notimplemented("invoke"); notimplemented("invoke");
Expand All @@ -191,7 +204,7 @@ WxxObjectPtr WxxDefault::call_method(const std::string &methname, WxxObjectArray
return winxedxxnull; return winxedxxnull;
} }


void WxxDefault::notimplemented(const std::string &funcname) void WxxDefault::notimplemented(const std::string &funcname) const
{ {
throw wxx_error(funcname + " not implemented in " + name); throw wxx_error(funcname + " not implemented in " + name);
} }
Expand Down
4 changes: 3 additions & 1 deletion winxedxx_default.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ class WxxDefault : public WxxObject
void set_attr_str(const std::string &s, const WxxObjectPtr &value); void set_attr_str(const std::string &s, const WxxObjectPtr &value);
WxxObjectPtr get_iter(); WxxObjectPtr get_iter();
WxxObjectPtr shift_pmc(); WxxObjectPtr shift_pmc();
WxxObjectPtr get_class();
WxxObjectPtr instantiate();
WxxObjectPtr operator()(WxxObjectArray &args); WxxObjectPtr operator()(WxxObjectArray &args);
WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args); WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args);
private: private:
std::string name; std::string name;
std::map<std::string, WxxObjectPtr> attributes; std::map<std::string, WxxObjectPtr> attributes;
void notimplemented(const std::string &funcname); void notimplemented(const std::string &funcname) const;
}; };




Expand Down
7 changes: 7 additions & 0 deletions winxedxx_integer.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -219,11 +219,16 @@ class WxxClass : public WxxDefault
typedef WxxObjectPtr (*memberfun)(WxxObjectPtr &, const WxxObjectArray &); typedef WxxObjectPtr (*memberfun)(WxxObjectPtr &, const WxxObjectArray &);


WxxClass(const std::string &name); WxxClass(const std::string &name);
std::string get_string();

std::string class_name() const; std::string class_name() const;
void addattribute(const std::string &attrname); void addattribute(const std::string &attrname);
void addfunction(const std::string &fname, memberfun); void addfunction(const std::string &fname, memberfun);
memberfun getfunction(const std::string &fname); memberfun getfunction(const std::string &fname);
static WxxClass * getclass(const std::string &name); static WxxClass * getclass(const std::string &name);
WxxObjectPtr get_class();
WxxObjectPtr instantiate();
WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args);
private: private:
std::string clname; std::string clname;
std::vector<std::string> attrs; std::vector<std::string> attrs;
Expand All @@ -234,7 +239,9 @@ class WxxInstance : public WxxDefault
{ {
public: public:
WxxInstance(const std::string &clname); WxxInstance(const std::string &clname);
WxxInstance(WxxClass &cassobjl);
std::string class_name() const; std::string class_name() const;
WxxObjectPtr get_class();
WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args); WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args);
private: private:
WxxClass *cl; WxxClass *cl;
Expand Down
14 changes: 13 additions & 1 deletion winxedxx_null.cxx
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ WxxObjectPtr WxxNull::shift_pmc()
return winxedxxnull; return winxedxxnull;
} }


WxxObjectPtr WxxNull::get_class()
{
nullaccess("get_class");
return winxedxxnull;
}

WxxObjectPtr WxxNull::instantiate()
{
nullaccess("instantiate");
return winxedxxnull;
}

WxxObjectPtr WxxNull::operator()(WxxObjectArray &args) WxxObjectPtr WxxNull::operator()(WxxObjectArray &args)
{ {
nullaccess("invoke"); nullaccess("invoke");
Expand All @@ -172,7 +184,7 @@ WxxObjectPtr WxxNull::call_method(const std::string &methname, WxxObjectArray &a
return winxedxxnull; return winxedxxnull;
} }


void WxxNull::nullaccess(const std::string &funcname) void WxxNull::nullaccess(const std::string &funcname) const
{ {
throw wxx_error("Null WxxObject in " + funcname); throw wxx_error("Null WxxObject in " + funcname);
} }
Expand Down
4 changes: 3 additions & 1 deletion winxedxx_null.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ class WxxNull : public WxxObject
void set_attr_str(const std::string &s, const WxxObjectPtr &value); void set_attr_str(const std::string &s, const WxxObjectPtr &value);
WxxObjectPtr get_iter(); WxxObjectPtr get_iter();
WxxObjectPtr shift_pmc(); WxxObjectPtr shift_pmc();
WxxObjectPtr get_class();
WxxObjectPtr instantiate();
WxxObjectPtr operator()(WxxObjectArray &args); WxxObjectPtr operator()(WxxObjectArray &args);
WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args); WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args);
private: private:
void nullaccess(const std::string &funcname); void nullaccess(const std::string &funcname) const;
}; };




Expand Down
2 changes: 2 additions & 0 deletions winxedxx_object.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class WxxObject : public WxxRefcounted
virtual WxxObjectPtr get_iter() = 0; virtual WxxObjectPtr get_iter() = 0;
virtual WxxObjectPtr shift_pmc() = 0; virtual WxxObjectPtr shift_pmc() = 0;


virtual WxxObjectPtr get_class() = 0;
virtual WxxObjectPtr instantiate() = 0;
virtual WxxObjectPtr operator()(WxxObjectArray &args) = 0; virtual WxxObjectPtr operator()(WxxObjectArray &args) = 0;
virtual WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args) = 0; virtual WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args) = 0;
}; };
Expand Down
10 changes: 10 additions & 0 deletions winxedxx_objectptr.cxx
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ void WxxObjectPtr::push(const std::string &str)
arr->push(str); arr->push(str);
} }


WxxObjectPtr WxxObjectPtr::get_class()
{
return object->get_class();
}

WxxObjectPtr WxxObjectPtr::instantiate()
{
return object->instantiate();
}

WxxObjectPtr WxxObjectPtr::operator()(const WxxObjectArray &args) WxxObjectPtr WxxObjectPtr::operator()(const WxxObjectArray &args)
{ {
return (*object)(const_cast<WxxObjectArray &>(args)); return (*object)(const_cast<WxxObjectArray &>(args));
Expand Down
2 changes: 2 additions & 0 deletions winxedxx_types.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class WxxObjectPtr
void push(const std::string &str); void push(const std::string &str);
WxxObjectPtr get_iter(); WxxObjectPtr get_iter();
WxxObjectPtr shift_pmc(); WxxObjectPtr shift_pmc();
WxxObjectPtr get_class();
WxxObjectPtr instantiate();
WxxObjectPtr operator()(const WxxObjectArray &args); WxxObjectPtr operator()(const WxxObjectArray &args);
WxxObjectPtr call_method(const std::string &methname); WxxObjectPtr call_method(const std::string &methname);
WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args); WxxObjectPtr call_method(const std::string &methname, WxxObjectArray &args);
Expand Down
3 changes: 1 addition & 2 deletions winxedxx_util.cxx
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -306,8 +306,7 @@ std::string wxx_chomp(const std::string &src)


WxxObjectPtr wxx_typeof(WxxObjectPtr &thing) WxxObjectPtr wxx_typeof(WxxObjectPtr &thing)
{ {
// Bogus implementation that allows some tests return thing.get_class();
return thing.class_name();
} }


std::string wxx_join(const std::string &sep, const WxxObjectPtr &arr) std::string wxx_join(const std::string &sep, const WxxObjectPtr &arr)
Expand Down

0 comments on commit bc6a98b

Please sign in to comment.