lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nwelln...@apache.org
Subject [1/2] lucy-clownfish git commit: Check for extra positional args
Date Fri, 27 Nov 2015 13:58:22 GMT
Repository: lucy-clownfish
Updated Branches:
  refs/heads/master b8ba6d8c4 -> 976199e97


Check for extra positional args


Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/3c79eefc
Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/3c79eefc
Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/3c79eefc

Branch: refs/heads/master
Commit: 3c79eefc4845508d4ac4ceb5580506771de634f9
Parents: b8ba6d8
Author: Nick Wellnhofer <wellnhofer@aevum.de>
Authored: Fri Nov 27 14:01:13 2015 +0100
Committer: Nick Wellnhofer <wellnhofer@aevum.de>
Committed: Fri Nov 27 14:01:13 2015 +0100

----------------------------------------------------------------------
 compiler/src/CFCPerlMethod.c      | 31 ++++++++++++++++---------------
 runtime/perl/t/binding/018-host.t |  5 ++++-
 2 files changed, 20 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3c79eefc/compiler/src/CFCPerlMethod.c
----------------------------------------------------------------------
diff --git a/compiler/src/CFCPerlMethod.c b/compiler/src/CFCPerlMethod.c
index 06f92e4..120c6c4 100644
--- a/compiler/src/CFCPerlMethod.c
+++ b/compiler/src/CFCPerlMethod.c
@@ -316,6 +316,15 @@ S_xsub_def_positional_args(CFCPerlMethod *self, CFCClass *klass) {
             min_required = i + 1;
         }
     }
+    char *num_args_cond;
+    if (min_required < num_vars) {
+        const char cond_pattern[] = "items < %u || items > %u";
+        num_args_cond = CFCUtil_sprintf(cond_pattern, (unsigned)min_required,
+                                        (unsigned)num_vars);
+    }
+    else {
+        num_args_cond = CFCUtil_sprintf("items != %u", (unsigned)num_vars);
+    }
     char *xs_name_list = num_vars > 0
                          ? CFCUtil_strdup(CFCVariable_get_name(arg_vars[0]))
                          : CFCUtil_strdup("");
@@ -329,17 +338,6 @@ S_xsub_def_positional_args(CFCPerlMethod *self, CFCClass *klass) {
                                        NULL);
         }
     }
-    const char num_args_pattern[] =
-        "if (items %s %u) { CFISH_THROW(CFISH_ERR, \"Usage: %%s(%s)\", GvNAME(CvGV(cv)));
}";
-    char *num_args_check;
-    if (min_required < num_vars) {
-        num_args_check = CFCUtil_sprintf(num_args_pattern, "<", min_required,
-                                         xs_name_list);
-    }
-    else {
-        num_args_check = CFCUtil_sprintf(num_args_pattern, "!=", num_vars,
-                                         xs_name_list);
-    }
 
     char *retval_decl;
     if (CFCType_is_void(return_type)) {
@@ -360,7 +358,9 @@ S_xsub_def_positional_args(CFCPerlMethod *self, CFCClass *klass) {
         "\n"
         "    CFISH_UNUSED_VAR(cv);\n"
         "    SP -= items;\n"
-        "    %s;\n"
+        "    if (%s) {\n"
+        "        CFISH_THROW(CFISH_ERR, \"Usage: %%s(%s)\", GvNAME(CvGV(cv)));\n"
+        "    }\n"
         "\n"
         "    /* Extract vars from Perl stack. */\n"
         "    %s\n"
@@ -371,15 +371,16 @@ S_xsub_def_positional_args(CFCPerlMethod *self, CFCClass *klass) {
         "}\n";
     char *xsub
         = CFCUtil_sprintf(pattern, self->sub.c_name, self->sub.c_name,
-                          arg_decls, meth_type_c, retval_decl,
-                          num_args_check, self_assign, arg_assigns, body);
+                          arg_decls, meth_type_c, retval_decl, num_args_cond,
+                          xs_name_list, self_assign, arg_assigns, body);
 
-    FREEMEM(num_args_check);
     FREEMEM(arg_assigns);
     FREEMEM(arg_decls);
     FREEMEM(meth_type_c);
     FREEMEM(self_assign);
     FREEMEM(body);
+    FREEMEM(num_args_cond);
+    FREEMEM(xs_name_list);
     FREEMEM(retval_decl);
     return xsub;
 }

http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3c79eefc/runtime/perl/t/binding/018-host.t
----------------------------------------------------------------------
diff --git a/runtime/perl/t/binding/018-host.t b/runtime/perl/t/binding/018-host.t
index 3d6e4c7..310d487 100644
--- a/runtime/perl/t/binding/018-host.t
+++ b/runtime/perl/t/binding/018-host.t
@@ -16,7 +16,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 4;
+use Test::More tests => 5;
 use Clownfish qw( to_clownfish );
 
 my %complex_data_structure = (
@@ -45,3 +45,6 @@ my $string = Clownfish::String->new("string");
 eval { $string->substring(offset => 0, len => 1, foo => 1) };
 like( $@, qr/Invalid parameter/, "Die on invalid parameter" );
 
+eval { $string->length(undef) };
+like( $@, qr/Usage: length/, "Die on extra parameter" );
+


Mime
View raw message