Author: nwellnhof
Date: Sat Jul 14 13:48:39 2012
New Revision: 1361544
URL: http://svn.apache.org/viewvc?rev=1361544&view=rev
Log:
LUCY-233 Initial implementation of Method class
Added:
lucy/trunk/core/Lucy/Object/Method.c
lucy/trunk/core/Lucy/Object/Method.cfh
Modified:
lucy/trunk/clownfish/src/CFCBindAliases.c
lucy/trunk/clownfish/src/CFCBindClass.c
lucy/trunk/clownfish/src/CFCBindCore.c
lucy/trunk/clownfish/src/CFCBindMethod.c
lucy/trunk/core/Lucy/Object/VTable.c
lucy/trunk/core/Lucy/Object/VTable.cfh
Modified: lucy/trunk/clownfish/src/CFCBindAliases.c
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCBindAliases.c?rev=1361544&r1=1361543&r2=1361544&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCBindAliases.c (original)
+++ lucy/trunk/clownfish/src/CFCBindAliases.c Sat Jul 14 13:48:39 2012
@@ -86,6 +86,9 @@ struct alias aliases[] = {
{"Cfish_Hash_Store_Str", "Lucy_Hash_Store_Str"},
{"Cfish_Hash_Store", "Lucy_Hash_Store"},
+ {"cfish_Method", "lucy_Method"},
+ {"CFISH_METHOD", "LUCY_METHOD"},
+
{"cfish_VArray", "lucy_VArray"},
{"CFISH_VARRAY", "LUCY_VARRAY"},
{"cfish_VA_new", "lucy_VA_new"},
Modified: lucy/trunk/clownfish/src/CFCBindClass.c
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCBindClass.c?rev=1361544&r1=1361543&r2=1361544&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCBindClass.c (original)
+++ lucy/trunk/clownfish/src/CFCBindClass.c Sat Jul 14 13:48:39 2012
@@ -305,7 +305,7 @@ CFCBindClass_to_c_data(CFCBindClass *sel
// Create offset in bytes for the method from the top of the VTable
// object.
char *offset_str = CFCUtil_cat(CFCUtil_strdup(""),
- "(offsetof(cfish_VTable, methods) + ",
+ "(offsetof(cfish_VTable, method_ptrs) +
",
meth_num_str,
" * sizeof(cfish_method_t))", NULL);
offsets = CFCUtil_cat(offsets, "size_t ", full_offset_sym, " = ",
Modified: lucy/trunk/clownfish/src/CFCBindCore.c
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCBindCore.c?rev=1361544&r1=1361543&r2=1361544&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCBindCore.c (original)
+++ lucy/trunk/clownfish/src/CFCBindCore.c Sat Jul 14 13:48:39 2012
@@ -214,7 +214,6 @@ S_write_parcel_h(CFCBindCore *self) {
"\n"
"typedef struct cfish_MethodMetaData {\n"
" const char *name;\n"
- " size_t name_len;\n"
" cfish_method_t func;\n"
" cfish_method_t callback_func;\n"
" size_t *offset;\n"
@@ -316,11 +315,23 @@ S_write_parcel_c(CFCBindCore *self) {
" /* Bootstrap VTables.\n"
" */\n"
" cfish_VTable_override(CFISH_VTABLE,
(cfish_method_t)lucy_VTable_make_obj, Lucy_VTable_Make_Obj_OFFSET);\n"
+ " cfish_VTable_override(CFISH_OBJ,
(cfish_method_t)lucy_Obj_destroy, Lucy_Obj_Destroy_OFFSET);\n"
" CFISH_CHARBUF->vtable = CFISH_VTABLE;\n"
+ " CFISH_CHARBUF->parent = CFISH_OBJ;\n"
" CFISH_CHARBUF->obj_alloc_size = sizeof(cfish_CharBuf);\n"
+ " cfish_VTable_override(CFISH_CHARBUF,
(cfish_method_t)lucy_Obj_dec_refcount, Lucy_Obj_Dec_RefCount_OFFSET);\n"
+ " cfish_VTable_override(CFISH_CHARBUF,
(cfish_method_t)lucy_Obj_inc_refcount, Lucy_Obj_Inc_RefCount_OFFSET);\n"
" cfish_VTable_override(CFISH_CHARBUF,
(cfish_method_t)lucy_CB_cat_trusted_str, Lucy_CB_Cat_Trusted_Str_OFFSET);\n"
+ " cfish_VTable_override(CFISH_CHARBUF,
(cfish_method_t)lucy_CB_clone, Lucy_CB_Clone_OFFSET);\n"
+ " cfish_VTable_override(CFISH_CHARBUF,
(cfish_method_t)lucy_CB_destroy, Lucy_CB_Destroy_OFFSET);\n"
" cfish_VTable_override(CFISH_CHARBUF,
(cfish_method_t)lucy_CB_grow, Lucy_CB_Grow_OFFSET);\n"
" cfish_VTable_override(CFISH_CHARBUF,
(cfish_method_t)lucy_CB_vcatf, Lucy_CB_VCatF_OFFSET);\n"
+ " CFISH_VARRAY->vtable = CFISH_VTABLE;\n"
+ " CFISH_VARRAY->obj_alloc_size = sizeof(cfish_VArray);\n"
+ " cfish_VTable_override(CFISH_VARRAY,
(cfish_method_t)lucy_VA_grow, Lucy_VA_Grow_OFFSET);\n"
+ " cfish_VTable_override(CFISH_VARRAY,
(cfish_method_t)lucy_VA_push, Lucy_VA_Push_OFFSET);\n"
+ " CFISH_METHOD->vtable = CFISH_VTABLE;\n"
+ " CFISH_METHOD->obj_alloc_size = sizeof(cfish_Method);\n"
"\n";
}
Modified: lucy/trunk/clownfish/src/CFCBindMethod.c
URL:
http://svn.apache.org/viewvc/lucy/trunk/clownfish/src/CFCBindMethod.c?rev=1361544&r1=1361543&r2=1361544&view=diff
==============================================================================
--- lucy/trunk/clownfish/src/CFCBindMethod.c (original)
+++ lucy/trunk/clownfish/src/CFCBindMethod.c Sat Jul 14 13:48:39 2012
@@ -228,10 +228,9 @@ CFCBindMeth_typedef_dec(struct CFCMethod
char*
CFCBindMeth_method_meta_def(CFCMethod *method) {
- const char *full_md_sym = CFCMethod_full_method_meta_sym(method);
- const char *macro_sym = CFCMethod_get_macro_sym(method);
- unsigned macro_sym_len = strlen(macro_sym);
- const char *impl_sym = CFCMethod_implementing_func_sym(method);
+ const char *full_md_sym = CFCMethod_full_method_meta_sym(method);
+ const char *macro_sym = CFCMethod_get_macro_sym(method);
+ const char *impl_sym = CFCMethod_implementing_func_sym(method);
const char *full_override_sym = "NULL";
if ((CFCMethod_public(method) || CFCMethod_abstract(method))
@@ -245,20 +244,20 @@ CFCBindMeth_method_meta_def(CFCMethod *m
char pattern[] =
"cfish_MethodMetaData %s = {\n"
- " \"%s\", %u, /* name and name_len */\n"
+ " \"%s\", /* name */\n"
" (cfish_method_t)%s, /* func */\n"
" (cfish_method_t)%s, /* callback_func */\n"
" &%s /* offset */\n"
"};\n";
size_t size = sizeof(pattern)
+ strlen(full_md_sym)
- + macro_sym_len
+ + strlen(macro_sym)
+ strlen(impl_sym)
+ strlen(full_override_sym)
+ strlen(full_offset_sym)
+ 30;
char *def = (char*)MALLOCATE(size);
- sprintf(def, pattern, full_md_sym, macro_sym, macro_sym_len, impl_sym,
+ sprintf(def, pattern, full_md_sym, macro_sym, impl_sym,
full_override_sym, full_offset_sym);
FREEMEM(full_offset_sym);
Added: lucy/trunk/core/Lucy/Object/Method.c
URL:
http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Object/Method.c?rev=1361544&view=auto
==============================================================================
--- lucy/trunk/core/Lucy/Object/Method.c (added)
+++ lucy/trunk/core/Lucy/Object/Method.c Sat Jul 14 13:48:39 2012
@@ -0,0 +1,44 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define C_LUCY_METHOD
+#define LUCY_USE_SHORT_NAMES
+#define CHY_USE_SHORT_NAMES
+
+#include "Lucy/Object/VTable.h"
+#include "Lucy/Object/Method.h"
+#include "Lucy/Object/CharBuf.h"
+
+Method*
+Method_new(const CharBuf *name, lucy_method_t callback_func, size_t offset) {
+ Method *self = (Method*)VTable_Make_Obj(METHOD);
+ return Method_init(self, name, callback_func, offset);
+}
+
+Method*
+Method_init(Method *self, const CharBuf *name, lucy_method_t callback_func,
+ size_t offset) {
+ self->name = CB_Clone(name);
+ self->callback_func = callback_func;
+ self->offset = offset;
+ return self;
+}
+
+void
+Method_destroy(Method *self) {
+ DECREF(self->name);
+}
+
Added: lucy/trunk/core/Lucy/Object/Method.cfh
URL:
http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Object/Method.cfh?rev=1361544&view=auto
==============================================================================
--- lucy/trunk/core/Lucy/Object/Method.cfh (added)
+++ lucy/trunk/core/Lucy/Object/Method.cfh Sat Jul 14 13:48:39 2012
@@ -0,0 +1,39 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+parcel Lucy;
+
+/** Method metadata.
+ */
+
+class Lucy::Object::Method inherits Lucy::Object::Obj {
+
+ CharBuf *name;
+ lucy_method_t callback_func;
+ size_t offset;
+
+ inert Method*
+ new(const CharBuf *name, lucy_method_t callback_func, size_t offset);
+
+ inert Method*
+ init(Method *self, const CharBuf *name, lucy_method_t callback_func,
+ size_t offset);
+
+ public void
+ Destroy(Method *self);
+}
+
+
Modified: lucy/trunk/core/Lucy/Object/VTable.c
URL:
http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Object/VTable.c?rev=1361544&r1=1361543&r2=1361544&view=diff
==============================================================================
--- lucy/trunk/core/Lucy/Object/VTable.c (original)
+++ lucy/trunk/core/Lucy/Object/VTable.c Sat Jul 14 13:48:39 2012
@@ -17,6 +17,7 @@
#define C_LUCY_VTABLE
#define C_LUCY_OBJ
#define C_LUCY_CHARBUF
+#define C_LUCY_METHOD
#define LUCY_USE_SHORT_NAMES
#define CHY_USE_SHORT_NAMES
@@ -28,6 +29,7 @@
#include "Lucy/Object/Err.h"
#include "Lucy/Object/Hash.h"
#include "Lucy/Object/LockFreeRegistry.h"
+#include "Lucy/Object/Method.h"
#include "Lucy/Object/Num.h"
#include "Lucy/Object/VArray.h"
#include "Lucy/Util/Atomic.h"
@@ -43,7 +45,7 @@ LockFreeRegistry *VTable_registry = NULL
VTable*
VTable_allocate(size_t num_methods) {
- size_t vt_alloc_size = offsetof(cfish_VTable, methods)
+ size_t vt_alloc_size = offsetof(cfish_VTable, method_ptrs)
+ num_methods * sizeof(cfish_method_t);
VTable *self = (VTable*)Memory_wrapped_calloc(vt_alloc_size, 1);
self->vt_alloc_size = vt_alloc_size;
@@ -60,18 +62,23 @@ VTable_init(VTable *self, VTable *parent
self->flags = flags;
self->x = x;
self->obj_alloc_size = obj_alloc_size;
- self->method_meta = method_meta;
+ self->methods = VA_new(0);
if (parent) {
- size_t parent_methods_size = parent->vt_alloc_size
- - offsetof(cfish_VTable, methods);
- memcpy(self->methods, parent->methods, parent_methods_size);
+ size_t parent_ptrs_size = parent->vt_alloc_size
+ - offsetof(cfish_VTable, method_ptrs);
+ memcpy(self->method_ptrs, parent->method_ptrs, parent_ptrs_size);
}
cfish_MethodMetaData **md_objs = (cfish_MethodMetaData**)method_meta;
for (uint32_t i = 0; md_objs[i] != NULL; i++) {
cfish_MethodMetaData *const md_obj = md_objs[i];
VTable_override(self, md_obj->func, *md_obj->offset);
+ CharBuf *method_name = CB_newf("%s", md_obj->name);
+ Method *method = Method_new(method_name, md_obj->callback_func,
+ *md_obj->offset);
+ VA_Push(self->methods, (Obj*)method);
+ DECREF(method_name);
}
return self;
@@ -200,17 +207,14 @@ VTable_singleton(const CharBuf *class_na
Hash_Store(meths, (Obj*)scrunched, (Obj*)CFISH_TRUE);
}
for (VTable *vtable = parent; vtable; vtable = vtable->parent) {
- cfish_MethodMetaData **md_objs
- = (cfish_MethodMetaData**)vtable->method_meta;
- for (uint32_t i = 0; md_objs[i] != NULL; i++) {
- cfish_MethodMetaData *const md_obj = md_objs[i];
- if (md_obj->callback_func) {
- ZCB_Assign_Str(callback_name, md_obj->name,
- md_obj->name_len);
- S_scrunch_charbuf((CharBuf*)callback_name, scrunched);
+ uint32_t max = VA_Get_Size(vtable->methods);
+ for (uint32_t i = 0; i < max; i++) {
+ Method *method = (Method*)VA_Fetch(vtable->methods, i);
+ if (method->callback_func) {
+ S_scrunch_charbuf(method->name, scrunched);
if (Hash_Fetch(meths, (Obj*)scrunched)) {
- VTable_Override(singleton, md_obj->callback_func,
- *md_obj->offset);
+ VTable_Override(singleton, method->callback_func,
+ method->offset);
}
}
}
Modified: lucy/trunk/core/Lucy/Object/VTable.cfh
URL:
http://svn.apache.org/viewvc/lucy/trunk/core/Lucy/Object/VTable.cfh?rev=1361544&r1=1361543&r2=1361544&view=diff
==============================================================================
--- lucy/trunk/core/Lucy/Object/VTable.cfh (original)
+++ lucy/trunk/core/Lucy/Object/VTable.cfh Sat Jul 14 13:48:39 2012
@@ -31,8 +31,8 @@ class Lucy::Object::VTable inherits Lucy
void *x; /* Reserved for future expansion */
size_t obj_alloc_size;
size_t vt_alloc_size;
- void *method_meta;
- lucy_method_t[1] methods; /* flexible array */
+ VArray *methods;
+ lucy_method_t[1] method_ptrs; /* flexible array */
inert LockFreeRegistry *registry;
inert size_t offset_of_parent;