Skip to content

Commit 9782bf8

Browse files
author
Jan-Olof Hendig
committed
Indentation fixes
1 parent bd1feb0 commit 9782bf8

File tree

1 file changed

+105
-105
lines changed

1 file changed

+105
-105
lines changed

doc/Language/nativecall.pod6

Lines changed: 105 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ NativeCall provides a "symbol" trait for you to specify the name of the native
4040
routine in your library that may be different from your Perl subroutine name.
4141
4242
=begin code :skip-test
43-
unit module Foo;
44-
use NativeCall;
45-
our sub init() is native('foo') is symbol('FOO_INIT') { * }
43+
unit module Foo;
44+
use NativeCall;
45+
our sub init() is native('foo') is symbol('FOO_INIT') { * }
4646
=end code
4747
4848
Inside of "libfoo" there is a routine called "FOO_INIT" but, since we're
@@ -152,33 +152,33 @@ can serve this role. This means you can expose libraries that work on handles
152152
by writing a class like this:
153153
154154
=begin code
155-
use NativeCall;
155+
use NativeCall;
156+
157+
class FooHandle is repr('CPointer') {
158+
# Here are the actual NativeCall functions.
159+
sub Foo_init() returns FooHandle is native("foo") { * }
160+
sub Foo_free(FooHandle) is native("foo") { * }
161+
sub Foo_query(FooHandle, Str) returns int8 is native("foo") { * }
162+
sub Foo_close(FooHandle) returns int8 is native("foo") { * }
163+
164+
# Here are the methods we use to expose it to the outside world.
165+
method new {
166+
Foo_init();
167+
}
168+
169+
method query(Str $stmt) {
170+
Foo_query(self, $stmt);
171+
}
172+
173+
method close {
174+
Foo_close(self);
175+
}
156176
157-
class FooHandle is repr('CPointer') {
158-
# Here are the actual NativeCall functions.
159-
sub Foo_init() returns FooHandle is native("foo") { * }
160-
sub Foo_free(FooHandle) is native("foo") { * }
161-
sub Foo_query(FooHandle, Str) returns int8 is native("foo") { * }
162-
sub Foo_close(FooHandle) returns int8 is native("foo") { * }
163-
164-
# Here are the methods we use to expose it to the outside world.
165-
method new {
166-
Foo_init();
167-
}
168-
169-
method query(Str $stmt) {
170-
Foo_query(self, $stmt);
171-
}
172-
173-
method close {
174-
Foo_close(self);
175-
}
176-
177-
# Free data when the object is garbage collected.
178-
submethod DESTROY {
179-
Foo_free(self);
180-
}
177+
# Free data when the object is garbage collected.
178+
submethod DESTROY {
179+
Foo_free(self);
181180
}
181+
}
182182
=end code
183183
184184
Note that the CPointer representation can do nothing more than hold a C pointer.
@@ -203,12 +203,12 @@ Example of invoking a function pointer "$fptr" returned by a function "f", using
203203
signature defining the desired function parameters and return value:
204204
205205
=begin code :skip-test
206-
sub f() returns Pointer is native('mylib') { * }
206+
sub f() returns Pointer is native('mylib') { * }
207207
208-
my $fptr = f();
209-
my &newfunc = nativecast(:(Str, size_t --> int32), $fptr);
208+
my $fptr = f();
209+
my &newfunc = nativecast(:(Str, size_t --> int32), $fptr);
210210
211-
say newfunc("test", 4);
211+
say newfunc("test", 4);
212212
=end code
213213
214214
=head1 Arrays
@@ -224,16 +224,16 @@ a much more primitive CArray type, which you must use if working with C arrays.
224224
Here is an example of passing a C array.
225225
226226
=begin code :skip-test
227-
sub RenderBarChart(Str, int32, CArray[Str], CArray[num64]) is native("chart") { * }
228-
my @titles := CArray[Str].new;
229-
@titles[0] = 'Me';
230-
@titles[1] = 'You';
231-
@titles[2] = 'Hagrid';
232-
my @values := CArray[num64].new;
233-
@values[0] = 59.5e0;
234-
@values[1] = 61.2e0;
235-
@values[2] = 180.7e0;
236-
RenderBarChart('Weights (kg)', 3, @titles, @values);
227+
sub RenderBarChart(Str, int32, CArray[Str], CArray[num64]) is native("chart") { * }
228+
my @titles := CArray[Str].new;
229+
@titles[0] = 'Me';
230+
@titles[1] = 'You';
231+
@titles[2] = 'Hagrid';
232+
my @values := CArray[num64].new;
233+
@values[0] = 59.5e0;
234+
@values[1] = 61.2e0;
235+
@values[2] = 180.7e0;
236+
RenderBarChart('Weights (kg)', 3, @titles, @values);
237237
=end code
238238
239239
Note that binding was used to C<@titles>, I<not> assignment! If you assign, you
@@ -261,11 +261,11 @@ subroutine, otherwise the C function may stomp all over Perl's memory
261261
leading to possibly unpredictable behaviour:
262262
263263
=begin code :skip-test
264-
my $ints = CArray[int32].new;
265-
my $number_of_ints = 10;
266-
$ints[$number_of_ints - 1] = 0; # extend the array to 10 items
264+
my $ints = CArray[int32].new;
265+
my $number_of_ints = 10;
266+
$ints[$number_of_ints - 1] = 0; # extend the array to 10 items
267267
268-
my $n = get_n_ints($ints, $number_of_ints);
268+
my $n = get_n_ints($ints, $number_of_ints);
269269
=end code
270270
271271
The memory management of arrays is important to understand. When you create an
@@ -315,20 +315,20 @@ to them (with =) doesn't work. Instead, you have to bind new values to the priva
315315
members:
316316
317317
=begin code :skip-test
318-
class MyStruct is repr('CStruct') {
319-
has CArray[num64] $!arr;
320-
has Str $!str;
321-
has Point $!point; # Point is a user-defined class
322-
323-
submethod TWEAK {
324-
my $arr := CArray[num64].new;
325-
$arr[0] = 0.9e0;
326-
$arr[1] = 0.2e0;
327-
$!arr := $arr;
328-
$!str := 'Perl 6 is fun';
329-
$!point := Point.new;
330-
}
318+
class MyStruct is repr('CStruct') {
319+
has CArray[num64] $!arr;
320+
has Str $!str;
321+
has Point $!point; # Point is a user-defined class
322+
323+
submethod TWEAK {
324+
my $arr := CArray[num64].new;
325+
$arr[0] = 0.9e0;
326+
$arr[1] = 0.2e0;
327+
$!arr := $arr;
328+
$!str := 'Perl 6 is fun';
329+
$!point := Point.new;
331330
}
331+
}
332332
=end code
333333
334334
As you may have predicted by now, a null is represented by the type object of the
@@ -341,14 +341,14 @@ attributes the same way a C compiler would lay them out in a similar
341341
C<union> definition; using the C<CUnion> representation:
342342
343343
=begin code
344-
use NativeCall;
344+
use NativeCall;
345345
346-
class MyUnion is repr('CUnion') {
347-
has int32 $.flags32;
348-
has int64 $.flags64;
349-
}
346+
class MyUnion is repr('CUnion') {
347+
has int32 $.flags32;
348+
has int64 $.flags64;
349+
}
350350
351-
say nativesizeof(MyUnion.new); # 8, ie. max(sizeof(MyUnion.flags32), sizeof(MyUnion.flags64))
351+
say nativesizeof(MyUnion.new); # 8, ie. max(sizeof(MyUnion.flags32), sizeof(MyUnion.flags64))
352352
=end code
353353
354354
=head2 Embedding CStructs and CUnions
@@ -359,17 +359,17 @@ as usual, and to do the latter we use the C<HAS> declarator
359359
instead:
360360
361361
=begin code :skip-test
362-
class MyStruct is repr('CStruct') {
363-
has Point $.point; # referenced
364-
has int32 $.flags;
365-
}
362+
class MyStruct is repr('CStruct') {
363+
has Point $.point; # referenced
364+
has int32 $.flags;
365+
}
366366
367-
say nativesizeof(MyStruct.new); # 16, ie. sizeof(struct Point *) + sizeof(int32_t)
367+
say nativesizeof(MyStruct.new); # 16, ie. sizeof(struct Point *) + sizeof(int32_t)
368368
369-
class MyStruct2 is repr('CStruct') {
370-
HAS Point $.point; # embedded
371-
has int32 $.flags;
372-
}
369+
class MyStruct2 is repr('CStruct') {
370+
HAS Point $.point; # embedded
371+
has int32 $.flags;
372+
}
373373
374374
say nativesizeof(MyStruct2.new); # 24, ie. sizeof(struct Point) + sizeof(int32_t)
375375
=end code
@@ -389,39 +389,39 @@ embedded in a C<CStruct>.
389389
You have to call C<.deref> on it to access the embedded type.
390390
391391
=begin code :skip-test
392-
my Pointer[int32] $p; #For a pointer on int32;
393-
my Pointer[MyCstruct] $p2 = some_c_routine();
394-
my MyCstruct $mc = $p2.deref;
395-
say $mc.field1;
392+
my Pointer[int32] $p; #For a pointer on int32;
393+
my Pointer[MyCstruct] $p2 = some_c_routine();
394+
my MyCstruct $mc = $p2.deref;
395+
say $mc.field1;
396396
=end code
397397
398398
It's quite common for a native function to return a pointer to an array of elements. Typed pointers can be dereferenced as an array to obtain individual elements.
399399
400400
=begin code :skip-test
401-
my $n = 5;
402-
# returns a pointer to an array of length $n
403-
my Pointer[Point] $plot = some_other_c_routine($n);
404-
# display the 5 elements in the array
405-
for 1 .. $n -> $i {
406-
my $x = $plot[$i - 1].x;
407-
my $y = $plot[$i - 1].y;
408-
say "$i: ($x, $y)";
409-
}
401+
my $n = 5;
402+
# returns a pointer to an array of length $n
403+
my Pointer[Point] $plot = some_other_c_routine($n);
404+
# display the 5 elements in the array
405+
for 1 .. $n -> $i {
406+
my $x = $plot[$i - 1].x;
407+
my $y = $plot[$i - 1].y;
408+
say "$i: ($x, $y)";
409+
}
410410
=end code
411411
412412
Pointers can also be updated to reference successive elements in the array:
413413
414414
=begin code :skip-test
415-
my Pointer[Point] $elem = $plot;
416-
# show differences between successive points
417-
for 1 ..^ $n {
418-
my Point $lo = $elem.deref;
419-
++$elem; # equivalent to $elem = $elem.add(1);
420-
my Point $hi = (++$elem).deref;
421-
my $dx = $hi.x = $lo.x;
422-
my $dy = $hi.y = $lo.y;
423-
say "$_: delta ($dx, $dy)";
424-
}
415+
my Pointer[Point] $elem = $plot;
416+
# show differences between successive points
417+
for 1 ..^ $n {
418+
my Point $lo = $elem.deref;
419+
++$elem; # equivalent to $elem = $elem.add(1);
420+
my Point $hi = (++$elem).deref;
421+
my $dx = $hi.x = $lo.x;
422+
my $dy = $hi.y = $lo.y;
423+
say "$_: delta ($dx, $dy)";
424+
}
425425
=end code
426426
427427
=head1 Buffers and Blobs
@@ -533,7 +533,7 @@ Variables exported by a library – also named "global" or "extern"
533533
variables – can be accessed using C<cglobal>. For example:
534534
535535
=for code :skip-test
536-
my $var := cglobal('libc.so.6', 'errno', int32)
536+
my $var := cglobal('libc.so.6', 'errno', int32)
537537
538538
This code binds to C<$var> a new L<Proxy|/type/Proxy> object that
539539
redirects all its accesses to the integer variable named "errno" as
@@ -595,7 +595,7 @@ You'll need to install MySQL server locally; on Debian-esque systems
595595
it can be installed with something like:
596596
597597
=for code :lang<shell>
598-
sudo apt-get install mysql-server
598+
sudo apt-get install mysql-server
599599
600600
Prepare your system along these lines before trying out the examples:
601601
@@ -609,14 +609,14 @@ Prepare your system along these lines before trying out the examples:
609609
Here is an example of a Windows API call:
610610
611611
=begin code :method<False>
612-
use NativeCall;
612+
use NativeCall;
613613
614-
sub MessageBoxA(int32, Str, Str, int32)
615-
returns int32
616-
is native('user32')
617-
{ * }
614+
sub MessageBoxA(int32, Str, Str, int32)
615+
returns int32
616+
is native('user32')
617+
{ * }
618618
619-
MessageBoxA(0, "We have NativeCall", "ohai", 64);
619+
MessageBoxA(0, "We have NativeCall", "ohai", 64);
620620
=end code
621621
622622
=end pod

0 commit comments

Comments
 (0)