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/10/29 02:08:10 UTC
[lucy-commits] svn commit: r1190698 - in /incubator/lucy/trunk/clownfish: include/CFC.h
lib/Clownfish.xs lib/Clownfish/Binding/Perl/Constructor.pm
src/CFCPerlConstructor.c src/CFCPerlConstructor.h typemap
Author: marvin
Date: Sat Oct 29 00:08:09 2011
New Revision: 1190698
URL: http://svn.apache.org/viewvc?rev=1190698&view=rev
Log:
Start porting the Clownfish-Perl constructor bindings to C.
Added:
incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.c (with props)
incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.h (with props)
Modified:
incubator/lucy/trunk/clownfish/include/CFC.h
incubator/lucy/trunk/clownfish/lib/Clownfish.xs
incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Perl/Constructor.pm
incubator/lucy/trunk/clownfish/typemap
Modified: incubator/lucy/trunk/clownfish/include/CFC.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/include/CFC.h?rev=1190698&r1=1190697&r2=1190698&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/include/CFC.h (original)
+++ incubator/lucy/trunk/clownfish/include/CFC.h Sat Oct 29 00:08:09 2011
@@ -42,5 +42,6 @@
#include "CFCPerlSub.h"
#include "CFCPerlMethod.h"
#include "CFCPerlClass.h"
+#include "CFCPerlConstructor.h"
#include "CFCPerlTypeMap.h"
Modified: incubator/lucy/trunk/clownfish/lib/Clownfish.xs
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish.xs?rev=1190698&r1=1190697&r2=1190698&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish.xs (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish.xs Sat Oct 29 00:08:09 2011
@@ -1957,6 +1957,33 @@ CODE:
OUTPUT: RETVAL
+MODULE = Clownfish PACKAGE = Clownfish::Binding::Perl::Constructor
+
+SV*
+_new(klass, alias)
+ CFCClass *klass;
+ const char *alias;
+CODE:
+ CFCPerlConstructor *self = CFCPerlConstructor_new(klass, alias);
+ RETVAL = S_cfcbase_to_perlref(self);
+ CFCBase_decref((CFCBase*)self);
+OUTPUT: RETVAL
+
+void
+DESTROY(self)
+ CFCPerlConstructor *self;
+PPCODE:
+ CFCPerlConstructor_destroy(self);
+
+SV*
+_get_init_func(self)
+ CFCPerlConstructor *self;
+CODE:
+ CFCFunction *func = CFCPerlConstructor_get_init_func(self);
+ RETVAL = S_cfcbase_to_perlref(func);
+OUTPUT: RETVAL
+
+
MODULE = Clownfish PACKAGE = Clownfish::Binding::Perl::Class
SV*
Modified: incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Perl/Constructor.pm
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Perl/Constructor.pm?rev=1190698&r1=1190697&r2=1190698&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Perl/Constructor.pm (original)
+++ incubator/lucy/trunk/clownfish/lib/Clownfish/Binding/Perl/Constructor.pm Sat Oct 29 00:08:09 2011
@@ -19,46 +19,19 @@ use warnings;
package Clownfish::Binding::Perl::Constructor;
use base qw( Clownfish::Binding::Perl::Subroutine );
use Carp;
-use Clownfish::ParamList;
+use Clownfish::Util qw( verify_args );
-our %init_func;
+our %new_PARAMS = (
+ class => undef,
+ alias => undef,
+);
sub new {
my ( $either, %args ) = @_;
- my $class = delete $args{class};
- my $alias = delete $args{alias};
- my $init_func_name = $alias =~ s/^(\w+)\|(\w+)$/$1/ ? $2 : 'init';
- my $class_name = $class->get_class_name;
-
- # Find the implementing function.
- my $func;
- for my $function ( @{ $class->functions } ) {
- next unless $function->micro_sym eq $init_func_name;
- $func = $function;
- last;
- }
- confess("Missing or invalid init() function for $class_name")
- unless $func;
-
- my $self = $either->SUPER::new(
- param_list => $func->get_param_list,
- class_name => $class_name,
- use_labeled_params => 1,
- alias => $alias,
- %args
- );
- $init_func{$self} = $func;
- return $self;
+ confess $@ unless verify_args( \%new_PARAMS, %args );
+ return _new( @args{qw( class alias )} );
}
-sub DESTROY {
- my $self = shift;
- delete $init_func{$self};
- $self->SUPER::DESTROY;
-}
-
-sub _get_init_func { $init_func{ +shift } }
-
sub xsub_def {
my $self = shift;
my $c_name = $self->c_name;
Added: incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.c
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.c?rev=1190698&view=auto
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.c (added)
+++ incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.c Sat Oct 29 00:08:09 2011
@@ -0,0 +1,102 @@
+/* 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.
+ */
+
+#include <string.h>
+#include <stdio.h>
+
+#ifndef true
+ #define true 1
+ #define false 0
+#endif
+
+#define CFC_NEED_PERLSUB_STRUCT_DEF 1
+#include "CFCPerlSub.h"
+#include "CFCPerlConstructor.h"
+#include "CFCClass.h"
+#include "CFCFunction.h"
+#include "CFCParamList.h"
+#include "CFCPerlTypeMap.h"
+#include "CFCVariable.h"
+#include "CFCUtil.h"
+
+struct CFCPerlConstructor {
+ CFCPerlSub sub;
+ CFCFunction *init_func;
+};
+
+CFCPerlConstructor*
+CFCPerlConstructor_new(CFCClass *klass, const char *alias) {
+ CFCPerlConstructor *self
+ = (CFCPerlConstructor*)CFCBase_allocate(sizeof(CFCPerlConstructor),
+ "Clownfish::Binding::Perl::Constructor");
+ return CFCPerlConstructor_init(self, klass, alias);
+}
+
+CFCPerlConstructor*
+CFCPerlConstructor_init(CFCPerlConstructor *self, CFCClass *klass,
+ const char *alias) {
+ // Extract alias from the alias spec, which may include a pipe. If it
+ // does, then the Perl-space alias is on the left, and the name of the
+ // init function is on the right: alias|init_func
+ CFCUTIL_NULL_CHECK(alias);
+ char *real_alias = CFCUtil_strdup(alias);
+ char *init_func_name;
+ char *alias_end = strchr(alias, '|');
+ if (alias_end) {
+ size_t alias_len = alias_end - alias;
+ real_alias[alias_len] = '\0';
+ init_func_name = CFCUtil_strdup(alias_end + 1);
+ }
+ else {
+ init_func_name = CFCUtil_strdup("init");
+ }
+
+ const char *class_name = CFCClass_get_class_name(klass);
+
+ // Find the implementing function.
+ self->init_func = NULL;
+ CFCFunction **funcs = CFCClass_functions(klass);
+ for (size_t i = 0; funcs[i] != NULL; i++) {
+ CFCFunction *func = funcs[i];
+ const char *func_name = CFCFunction_micro_sym(func);
+ if (strcmp(init_func_name, func_name) == 0) {
+ self->init_func = (CFCFunction*)CFCBase_incref((CFCBase*)func);
+ break;
+ }
+ }
+ if (!self->init_func) {
+ CFCUtil_die("Missing or invalid '%s' function for '%s'",
+ init_func_name, class_name);
+ }
+ CFCParamList *param_list = CFCFunction_get_param_list(self->init_func);
+ CFCPerlSub_init((CFCPerlSub*)self, param_list, class_name, real_alias,
+ true);
+ FREEMEM(init_func_name);
+ FREEMEM(real_alias);
+ return self;
+}
+
+void
+CFCPerlConstructor_destroy(CFCPerlConstructor *self) {
+ CFCBase_decref((CFCBase*)self->init_func);
+ CFCPerlSub_destroy((CFCPerlSub*)self);
+}
+
+CFCFunction*
+CFCPerlConstructor_get_init_func(CFCPerlConstructor *self) {
+ return self->init_func;
+}
+
Propchange: incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.c
------------------------------------------------------------------------------
svn:eol-style = native
Added: incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.h
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.h?rev=1190698&view=auto
==============================================================================
--- incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.h (added)
+++ incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.h Sat Oct 29 00:08:09 2011
@@ -0,0 +1,45 @@
+/* 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.
+ */
+
+#ifndef H_CFCPERLCONSTRUCTOR
+#define H_CFCPERLCONSTRUCTOR
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct CFCPerlConstructor CFCPerlConstructor;
+struct CFCClass;
+
+CFCPerlConstructor*
+CFCPerlConstructor_new(struct CFCClass *klass, const char *alias);
+
+CFCPerlConstructor*
+CFCPerlConstructor_init(CFCPerlConstructor *self, struct CFCClass *klass,
+ const char *alias);
+
+void
+CFCPerlConstructor_destroy(CFCPerlConstructor *self);
+
+struct CFCFunction*
+CFCPerlConstructor_get_init_func(CFCPerlConstructor *self);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_CFCPERLCONSTRUCTOR */
+
Propchange: incubator/lucy/trunk/clownfish/src/CFCPerlConstructor.h
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/lucy/trunk/clownfish/typemap
URL: http://svn.apache.org/viewvc/incubator/lucy/trunk/clownfish/typemap?rev=1190698&r1=1190697&r2=1190698&view=diff
==============================================================================
--- incubator/lucy/trunk/clownfish/typemap (original)
+++ incubator/lucy/trunk/clownfish/typemap Sat Oct 29 00:08:09 2011
@@ -34,6 +34,7 @@ CFCBindCore* CLOWNFISH_BINDING_CORE
CFCBindClass* CLOWNFISH_BINDING_CORE_TYPE
CFCPerlSub* CLOWNFISH_BINDING_PERL_SUBROUTINE
CFCPerlClass* CLOWNFISH_BINDING_PERL_COMMON
+CFCPerlConstructor* CLOWNFISH_BINDING_PERL_COMMON
CFCPerlMethod* CLOWNFISH_BINDING_PERL_COMMON
INPUT