You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2011/06/24 01:42:09 UTC
[lucy-commits] svn commit: r1139120 - in /incubator/lucy/trunk/clownfish: lib/Clownfish.xs
lib/Clownfish/Binding/Core/Class.pm src/CFCBindClass.c src/CFCBindClass.h
Author: marvin
Date: Thu Jun 23 23:42:09 2011
New Revision: 1139120
URL: http://svn.apache.org/viewvc?rev=1139120&view=rev
Log:
Port vtable definition generation code from Clownfish::Binding::Core::Class to
C.
Modified:
incubator/lucy/trunk/clownfish/lib/Clownfish.xs
incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm
incubator/lucy/trunk/clownfish/src/CFCBindClass.c
incubator/lucy/trunk/clownfish/src/CFCBindClass.h
Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1139120&r1=1139119&r2=1139120&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Thu Jun 23 23:42:09 2011
@@ -1806,3 +1806,10 @@ CODE:
RETVAL = S_sv_eat_c_string(CFCBindClass_struct_definition(self));
OUTPUT: RETVAL
+SV*
+_vtable_definition(self)
+ CFCBindClass *self;
+CODE:
+ RETVAL = S_sv_eat_c_string(CFCBindClass_vtable_definition(self));
+OUTPUT: RETVAL
+
Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm?rev=1139120&r1=1139119&r2=1139120&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Core/Class.pm Thu Jun 23 23:42:09 2011
@@ -49,53 +49,6 @@ cfish_ZombieCharBuf $full_var_name = {
END_STUFF
}
-# Return C code defining the class's VTable.
-sub _vtable_definition {
- my $self = shift;
- my $client = $self->_get_client;
- my $parent = $client->get_parent;
- my $methods = $client->methods;
- my $vt_type = $client->full_vtable_type;
- my $cnick = $client->get_cnick;
- my $vtable_var = $client->full_vtable_var;
- my $struct_sym = $client->full_struct_sym;
- my $vt = $vtable_var . "_vt";
- my $name_var = _full_name_var($self);
- my $cb_var = _full_callbacks_var($self);
-
- # Create a pointer to the parent class's vtable.
- my $parent_ref
- = defined $parent
- ? $parent->full_vtable_var
- : "NULL"; # No parent, e.g. Obj or inert classes.
-
- # Spec functions which implement the methods, casting to quiet compiler.
- my @implementing_funcs
- = map { "(cfish_method_t)" . $_->full_func_sym } @$methods;
- my $method_string = join( ",\n ", @implementing_funcs );
- my $num_methods = scalar @implementing_funcs;
-
- return <<END_VTABLE
-
-$vt_type $vt = {
- CFISH_VTABLE, /* vtable vtable */
- {1}, /* ref.count */
- $parent_ref, /* parent */
- (cfish_CharBuf*)&$name_var,
- 0, /* flags */
- NULL, /* "void *x" member reserved for future use */
- sizeof($struct_sym), /* obj_alloc_size */
- offsetof(cfish_VTable, methods)
- + $num_methods * sizeof(cfish_method_t), /* vt_alloc_size */
- &$cb_var, /* callbacks */
- {
- $method_string
- }
-};
-
-END_VTABLE
-}
-
sub to_c_header {
my $self = shift;
my $client = $self->_get_client;
Modified: incubator/lucy/trunk/clownfish/src/CFCBindClass.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCBindClass.c?rev=1139120&r1=1139119&r2=1139120&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCBindClass.c (original)
+++ incubator/lucy/trunk/clownfish/src/CFCBindClass.c Thu Jun 23 23:42:09 2011
@@ -98,6 +98,73 @@ CFCBindClass_struct_definition(CFCBindCl
return struct_def;
}
+// Return C code defining the class's VTable.
+char*
+CFCBindClass_vtable_definition(CFCBindClass *self) {
+ CFCClass *client = self->client;
+ CFCClass *parent = CFCClass_get_parent(client);
+ CFCMethod **methods = CFCClass_methods(client);
+ const char *vt_type = CFCClass_full_vtable_type(client);
+ const char *vt_var = CFCClass_full_vtable_var(client);
+ const char *struct_sym = CFCClass_full_struct_sym(client);
+
+ // Create a pointer to the parent class's vtable.
+ const char *parent_ref = parent
+ ? CFCClass_full_vtable_var(parent)
+ : "NULL"; // No parent, e.g. Obj or inert classes.
+
+ // Spec functions which implement the methods, casting to quiet compiler.
+ char *method_str = CFCUtil_strdup("");
+ int num_methods = 0;
+ for (; methods[num_methods] != NULL; num_methods++) {
+ CFCMethod *method = methods[num_methods];
+ const char *implementing_sym = CFCMethod_implementing_func_sym(method);
+ size_t size = strlen(method_str) + strlen(implementing_sym) + 50;
+ method_str = (char*)REALLOCATE(method_str, size);
+ if (strlen(method_str)) {
+ strcat(method_str, ",\n");
+ }
+ strcat(method_str, " (cfish_method_t)");
+ strcat(method_str, implementing_sym);
+ }
+
+ char pattern[] =
+ "\n"
+ "%s %s_vt = {\n"
+ " CFISH_VTABLE, /* vtable vtable */\n"
+ " {1}, /* ref.count */\n"
+ " %s, /* parent */\n"
+ " (cfish_CharBuf*)&%s,\n"
+ " 0, /* flags */\n"
+ " NULL, /* \"void *x\" member reserved for future use */\n"
+ " sizeof(%s), /* obj_alloc_size */\n"
+ " offsetof(cfish_VTable, methods)\n"
+ " + %d * sizeof(cfish_method_t), /* vt_alloc_size */\n"
+ " &%s, /* callbacks */\n"
+ " {\n"
+ "%s\n"
+ " }\n"
+ "};\n\n";
+
+ size_t size = sizeof(pattern)
+ + strlen(vt_type)
+ + strlen(vt_var)
+ + strlen(parent_ref)
+ + strlen(self->full_name_var)
+ + strlen(struct_sym)
+ + 10 /* num_methods */
+ + strlen(self->full_callbacks_var)
+ + strlen(method_str)
+ + 40;
+ char *vtable_def = (char*)MALLOCATE(size);
+ sprintf(vtable_def, pattern, vt_type, vt_var, parent_ref,
+ self->full_name_var, struct_sym, num_methods,
+ self->full_callbacks_var, method_str);
+
+ FREEMEM(method_str);
+ return vtable_def;
+}
+
CFCClass*
CFCBindClass_get_client(CFCBindClass *self) {
return self->client;
Modified: incubator/lucy/trunk/clownfish/src/CFCBindClass.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCBindClass.h?rev=1139120&r1=1139119&r2=1139120&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCBindClass.h (original)
+++ incubator/lucy/trunk/clownfish/src/CFCBindClass.h Thu Jun 23 23:42:09 2011
@@ -37,6 +37,9 @@ CFCBindClass_destroy(CFCBindClass *self)
char*
CFCBindClass_struct_definition(CFCBindClass *self);
+char*
+CFCBindClass_vtable_definition(CFCBindClass *self);
+
struct CFCClass*
CFCBindClass_get_client(struct CFCBindClass *self);