Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add native bool and size_t type, bool is in C99 standard
  • Loading branch information
Skarsnik committed Nov 15, 2015
1 parent 6268b83 commit fa1842c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/NativeCall.pm
Expand Up @@ -10,6 +10,8 @@ my constant long is export(:types, :DEFAULT) = NativeCall::Types::long;
my constant longlong is export(:types, :DEFAULT) = NativeCall::Types::longlong;
my constant ulong is export(:types, :DEFAULT) = NativeCall::Types::ulong;
my constant ulonglong is export(:types, :DEFAULT) = NativeCall::Types::ulonglong;
my constant bool is export(:types, :DEFAULT) = NativeCall::Types::bool;
my constant size_t is export(:types, :DEFAULT) = NativeCall::Types::size_t;
my constant void is export(:types, :DEFAULT) = NativeCall::Types::void;
my constant CArray is export(:types, :DEFAULT) = NativeCall::Types::CArray;
my constant Pointer is export(:types, :DEFAULT) = NativeCall::Types::Pointer;
Expand Down
2 changes: 2 additions & 0 deletions lib/NativeCall/Types.pm
Expand Up @@ -5,6 +5,8 @@ our native long is Int is ctype("long") is repr("P6int") { };
our native longlong is Int is ctype("longlong") is repr("P6int") { };
our native ulong is Int is ctype("long") is unsigned is repr("P6int") { };
our native ulonglong is Int is ctype("longlong") is unsigned is repr("P6int") { };
our native size_t is Int is ctype("size_t") is repr("P6int") { };
our native bool is Int is ctype("bool") is repr("P6int") { };
our class void is repr('Uninstantiable') { };
# Expose a Pointer class for working with raw pointers.
our class Pointer is repr('CPointer') { };
Expand Down
6 changes: 6 additions & 0 deletions src/Perl6/Metamodel/NativeHOW.nqp
Expand Up @@ -81,6 +81,12 @@ class Perl6::Metamodel::NativeHOW
elsif $ctype eq 'longdouble' {
$!nativesize := nqp::const::C_TYPE_LONGDOUBLE;
}
elsif $ctype eq 'bool' {
$!nativesize := nqp::const::C_TYPE_BOOL;
}
elsif $ctype eq 'size_t' {
$!nativesize := nqp::const::C_TYPE_SIZE_T;
}
else {
nqp::die("Unhandled C type '$ctype'")
}
Expand Down
10 changes: 10 additions & 0 deletions t/04-nativecall/12-sizeof.c
Expand Up @@ -3,6 +3,8 @@
#else
#define DLLEXPORT extern
#endif
#include <stdbool.h>
#include <stddef.h>

typedef struct {
char foo1;
Expand Down Expand Up @@ -60,3 +62,11 @@ DLLEXPORT int SizeofLng() {
DLLEXPORT int SizeofPtr() {
return sizeof(void *);
}

DLLEXPORT int SizeofBool() {
return sizeof(bool);
}

DLLEXPORT int SizeofSizeT() {
return sizeof(size_t);
}
10 changes: 8 additions & 2 deletions t/04-nativecall/12-sizeof.t
@@ -1,10 +1,11 @@
use v6;
use lib 't/04-nativecall';
use CompileTestLib;
use lib 'lib';
use NativeCall;
use Test;

plan 7;
plan 9;

compile_test_lib('12-sizeof');

Expand Down Expand Up @@ -44,11 +45,16 @@ sub SizeofBuz() returns int32 is native('./12-sizeof') { * }
sub SizeofInt() returns int32 is native('./12-sizeof') { * }
sub SizeofLng() returns int32 is native('./12-sizeof') { * }
sub SizeofPtr() returns int32 is native('./12-sizeof') { * }
sub SizeofBool() returns int32 is native('./12-sizeof') { * }
sub SizeofSizeT() returns int32 is native('./12-sizeof') { * }

is nativesizeof(Foo), SizeofFoo(), 'sizeof(Foo)';
is nativesizeof(Bar), SizeofBar(), 'sizeof(Bar)';
is nativesizeof(Baz), SizeofBaz(), 'sizeof(Baz)';
is nativesizeof(Buz), SizeofBuz(), 'sizeof(Buz)';
is nativesizeof(int32), SizeofInt(), 'sizeof(int)';
is nativesizeof(long), SizeofLng(), 'sizeof(long)';
is nativesizeof(Pointer), SizeofPtr(), 'sizeof(void *)';
is nativesizeof(Pointer), SizeofPtr(), 'sizeof(Pointer)';
is nativesizeof(bool), SizeofBool(), 'sizeof(bool)';
is nativesizeof(size_t), SizeofSizeT(), 'sizeof(size_t)';

0 comments on commit fa1842c

Please sign in to comment.