Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support lt, ge, le in xs

  • Loading branch information...
commit 437d54dccf8d61241e79021dff5ab292bb01a87b 1 parent fcdfc73
@tokuhirom authored
View
1  .gitignore
@@ -32,3 +32,4 @@ nytprof/
nytprof.out
*.o
lib/Nana/Node.pm
+xs/operator.c
View
6 Build.PL
@@ -24,6 +24,12 @@ my $class = Module::Build->subclass(
unlink 'lib/Nana/Parser.c';
unlink 'lib/Nana/Parser.o';
}
+ unless ($self->up_to_date('xs/operator.c.PL' => 'xs/operator.c')) {
+ $self->run_perl_script('xs/operator.c.PL', [], ['xs/operator.c']) or die "xs/operator.c.PL failed";
+ $self->add_to_cleanup('xs/operator.c');
+ unlink 'lib/Nana/Parser.c';
+ unlink 'lib/Nana/Parser.o';
+ }
$self->SUPER::ACTION_code(@_);
}
...
View
38 lib/Nana/Parser.xs
@@ -163,27 +163,7 @@ const char *tora_stringify_type(value_type_t t) {
abort();
}
-bool tora_op_gt(SV* lhs, SV*rhs) {
- value_type_t lhs_t = tora_detect_value_type(lhs);
- value_type_t rhs_t = tora_detect_value_type(rhs);
-
- if (lhs_t == VALUE_TYPE_UNDEF) {
- croak("You cannot use undefined value as left side value in greater than operator.");
- }
- if (rhs_t == VALUE_TYPE_UNDEF) {
- croak("You cannot use undefined value as right side value in greater than operator.");
- }
- if (lhs_t == VALUE_TYPE_INT || lhs_t == VALUE_TYPE_DOUBLE) {
- return SvNV(lhs) > SvNV(rhs) ? TRUE : FALSE;
- } else if (lhs_t == VALUE_TYPE_STR) {
- char * lhs_c = SvPV_nolen(lhs);
- char * rhs_c = SvPV_nolen(rhs);
- return strGT(lhs_c, rhs_c);
- } else {
- croak("greater than operator does not support this type: %s", tora_stringify_type(lhs_t));
- }
- abort();
-}
+#include "../../xs/operator.c"
MODULE = Nana::Parser PACKAGE = Nana::Parser
@@ -284,3 +264,19 @@ void
tora_op_gt(SV *lhs, SV*rhs)
PPCODE:
XPUSHs(tora_op_gt(lhs, rhs) ? json_true : json_false);
+
+void
+tora_op_lt(SV *lhs, SV*rhs)
+ PPCODE:
+ XPUSHs(tora_op_lt(lhs, rhs) ? json_true : json_false);
+
+void
+tora_op_le(SV *lhs, SV*rhs)
+ PPCODE:
+ XPUSHs(tora_op_le(lhs, rhs) ? json_true : json_false);
+
+void
+tora_op_ge(SV *lhs, SV*rhs)
+ PPCODE:
+ XPUSHs(tora_op_ge(lhs, rhs) ? json_true : json_false);
+
View
40 lib/Nana/Translator/Perl/Runtime.pm
@@ -250,46 +250,6 @@ sub tora_op_ne {
}
}
-sub tora_op_lt {
- my ($lhs, $rhs) = @_;
-
- my $flags = B::svref_2object(\$lhs)->FLAGS;
- if ($flags & (B::SVp_IOK | B::SVp_NOK) and !( $flags & B::SVp_POK )) {
- # IV or NV
- return $lhs < $rhs ? true() : false();
- } elsif ($flags & B::SVp_POK) {
- return $lhs lt $rhs ? true() : false();
- } else {
- die "OOPS";
- }
-}
-
-sub tora_op_le {
- my ($lhs, $rhs) = @_;
- my $flags = B::svref_2object(\$lhs)->FLAGS;
- if ($flags & (B::SVp_IOK | B::SVp_NOK) and !( $flags & B::SVp_POK )) {
- # IV or NV
- return $lhs <= $rhs ? true() : false();
- } elsif ($flags & B::SVp_POK) {
- return $lhs le $rhs ? true() : false();
- } else {
- die "OOPS";
- }
-}
-
-sub tora_op_ge {
- my ($lhs, $rhs) = @_;
- my $flags = B::svref_2object(\$lhs)->FLAGS;
- if ($flags & (B::SVp_IOK | B::SVp_NOK) and !( $flags & B::SVp_POK )) {
- # IV or NV
- return $lhs >= $rhs ? true() : false();
- } elsif ($flags & B::SVp_POK) {
- return $lhs ge $rhs ? true() : false();
- } else {
- die "OOPS";
- }
-}
-
sub tora_op_add {
my ($lhs, $rhs) = @_;
my $flags = B::svref_2object(\$lhs)->FLAGS;
View
36 xs/operator.c.PL
@@ -0,0 +1,36 @@
+use Text::MicroTemplate qw(render_mt build_mt);
+open my $ofh, '>', 'xs/operator.c'
+ or die $!;
+my $data = [
+ ['gt', 'greater than', '>', 'strGT'],
+ ['lt', 'less than', '<', 'strLT'],
+ ['le', 'less than or equals', '<=', 'strLE'],
+ ['ge', 'greater than or equals', '>=', 'strGE'],
+];
+print $ofh build_mt(template => <<'...', escape_func => sub { $_[0] })->($data);
+? for (@{$_[0]}) {
+? my ($type, $long, $op, $strop) = @{$_};
+bool tora_op_<?= $type ?>(SV* lhs, SV*rhs) {
+ value_type_t lhs_t = tora_detect_value_type(lhs);
+ value_type_t rhs_t = tora_detect_value_type(rhs);
+
+ if (lhs_t == VALUE_TYPE_UNDEF) {
+ croak("You cannot use undefined value as left side value in <?= $long ?> operator.");
+ }
+ if (rhs_t == VALUE_TYPE_UNDEF) {
+ croak("You cannot use undefined value as right side value in <?= $long ?> operator.");
+ }
+ if (lhs_t == VALUE_TYPE_INT || lhs_t == VALUE_TYPE_DOUBLE) {
+ return SvNV(lhs) <?= $op ?> SvNV(rhs) ? TRUE : FALSE;
+ } else if (lhs_t == VALUE_TYPE_STR) {
+ char * lhs_c = SvPV_nolen(lhs);
+ char * rhs_c = SvPV_nolen(rhs);
+ return <?= $strop ?>(lhs_c, rhs_c);
+ } else {
+ croak("<?= $long ?> operator does not support this type: %s", tora_stringify_type(lhs_t));
+ }
+ abort();
+}
+? }
+
+...
Please sign in to comment.
Something went wrong with that request. Please try again.