Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # under lib/UV
- ## Handle.pm6
- use UV::Utils;
- use NativeCall;
- unit module UV::Handle;
- enum HandleType is export <
- UV_UNKNOWN_HANDLE
- UV_ASYNC
- UV_CHECK
- UV_FS_EVENT
- UV_FS_POLL
- UV_HANDLE
- UV_IDLE
- UV_NAMED_PIPE
- UV_POLL
- UV_PREPARE
- UV_PROCESS
- UV_STREAM
- UV_TCP
- UV_TIMER
- UV_TTY
- UV_UDP
- UV_SIGNAL
- UV_FILE
- UV_HANDLE_TYPE_MAX
- >;
- class Handle does FreeAble is repr('CPointer') is export {
- sub uv_handle_size(int32 --> size_t) is native(&find-uv) { * }
- method size(::Handle::U: HandleType $ht --> size_t) {
- uv_handle_size(my int32 $ = $ht);
- }
- sub uv_new_handle( --> Handle) is native(&find-uvh) { * }
- method new() {
- uv_new_handle();
- }
- sub uv_is_active(Handle --> int32) is native(&find-uv) { * }
- method is-active( --> Bool) {
- uv_is_active(self) != 0;
- }
- sub uv_is_closing(Handle --> int32) is native(&find-uv) { * }
- method is-closing( --> Bool) {
- uv_is_closing(self) != 0;
- }
- sub uv_close(Handle, & (Handle)) is native(&find-uv) { * }
- method close(&cb:(Handle)) {
- uv_close(self, &cb);
- }
- sub uv_ref(Handle) is native(&find-uv) { * }
- method ref() {
- uv_ref(self);
- }
- sub uv_unref(Handle) is native(&find-uv) { * }
- method unref() {
- uv_unref(self);
- }
- sub uv_has_ref(Handle --> int32) is native(&find-uv) { * }
- method has-ref( --> Bool) {
- uv_has_ref(self) != 0;
- }
- sub uv_send_buffer_size(Handle, Pointer[int32] --> int32) is native(&find-uv) { * }
- method send-buffer-size() is rw {
- Proxy.new(
- FETCH => method () {
- uv_send_buffer_size(self, nativecast(Pointer[int32], CArray[int32].new(0)));
- },
- STORE => method (int32 $value) {
- uv_send_buffer_size(self, nativecast(Pointer[int32], CArray[int32].new($value)));
- }
- )
- }
- sub uv_recv_buffer_size(Handle, Pointer[int32] --> int32) is native(&find-uv) { * }
- method recv-buffer-size() is rw {
- Proxy.new(
- FETCH => method () {
- uv_recv_buffer_size(self, nativecast(Pointer[int32], CArray[int32].new(0)));
- },
- STORE => method (int32 $value) {
- uv_recv_buffer_size(self, nativecast(Pointer[int32], CArray[int32].new($value)));
- }
- )
- }
- sub uv_fileno_win(Handle, Pointer[void] is rw --> int32) is native(&find-uv) is symbol("uv_fileno") { * }
- sub uv_fileno_linux(Handle, int32 is rw --> int32) is native(&find-uv) is symbol("uv_fileno") { * }
- method fileno() {
- if $*Distro.is-win {
- my $ptr = Pointer[void].new;
- return (uv_fileno_win(self, $ptr), $ptr.address);
- } else {
- my int32 $ptr = 0;
- return (uv_fileno_linux(self, $ptr), $ptr);
- }
- }
- sub uv_handle_get_data(Handle --> Pointer[void]) is native(&find-uvdata) { * }
- sub uv_handle_set_data(Handle, Pointer[void]) is native(&find-uvdata) { * }
- method data() is rw {
- Proxy.new(
- FETCH => method () { uv_handle_get_data(self); },
- STORE => method (Pointer[void] $value) { uv_handle_set_data(self, $value); }
- );
- }
- sub uv_handle_get_loop(Handle --> Pointer) is native(&find-uvdata) { * }
- method loop() {
- uv_handle_get_loop(self);
- }
- sub uv_handle_get_type(Handle --> int32) is native(&find-uvdata) { * }
- method type( --> HandleType) {
- HandleType(uv_handle_get_type(self));
- }
- }
- sub uv_handle_type_name(int32 --> Str) is native(&find-uv) { * }
- sub handle-typename(Handle $handle --> Str) is export {
- uv_handle_type_name(int32($handle));
- }
- role CastToHandle is export {
- method handle() {
- nativecast(Handle, self);
- }
- }
- ## Loop.pm6
- use UV::Utils;
- use UV::Handle;
- use NativeCall;
- unit module UV::Loop;
- enum RunMode is export <UV_RUN_DEFAULT UV_RUN_ONCE UV_RUN_NOWAIT>;
- class X::Loop is Exception { }
- class Loop does FreeAble is repr('CPointer') is export {
- sub uv_default_loop( --> Loop) is native(&find-uv) { * }
- method default(::Loop::U: --> Loop) {
- uv_default_loop();
- }
- sub uv_loop_size( --> size_t) is native(&find-uv) { * }
- method size(::Loop::U: --> size_t) {
- uv_loop_size();
- }
- sub uv_new_loop( --> Loop) is native(&find-uvh) { * }
- method new( --> Loop) {
- uv_new_loop();
- }
- sub uv_loop_init(Loop --> int32) is native(&find-uv) { * }
- method init( --> int32) {
- uv_loop_init(self);
- }
- sub uv_loop_get_data(Loop --> Pointer[void]) is native(&find-uvdata) { * }
- sub uv_loop_set_data(Loop, Pointer[void]) is native(&find-uvdata) { * }
- method data() is rw {
- Proxy.new(
- FETCH => method () { uv_loop_get_data(self); },
- STORE => method (Pointer[void] $value) { uv_loop_set_data(self, $value); }
- );
- }
- sub uv_loop_close(Loop --> int32) is native(&find-uv) { * }
- method close( --> int32) {
- uv_loop_close(self);
- }
- sub uv_run(Loop, int32 --> int32) is native(&find-uv) { * }
- method run(RunMode $mode --> int32) {
- uv_run(self, my int32 $ = $mode);
- }
- sub uv_loop_alive(Loop --> int32) is native(&find-uv) { * }
- method alive( --> Bool) {
- uv_loop_alive(self) != 0;
- }
- sub uv_stop(Loop) is native(&find-uv) { * }
- method stop() {
- uv_stop(self);
- }
- sub uv_backend_fd(Loop --> int32) is native(&find-uv) { * }
- method fd( --> int32) {
- uv_backend_fd(self);
- }
- sub uv_backend_timeout(Loop --> int32) is native(&find-uv) { * }
- method timeout( --> int32) {
- uv_backend_timeout(self);
- }
- sub uv_now(Loop --> uint64) is native(&find-uv) { * }
- method now(--> uint64) {
- uv_now(self);
- }
- sub uv_update_time(Loop) is native(&find-uv) { * }
- method update-time() {
- uv_update_time(self);
- }
- sub uv_walk(Loop, & (Handle, Pointer[void]), Pointer[void]) is native(&find-uv) { * }
- method walk(&cb:(Handle, Pointer[void]), Pointer[void] $arg) {
- uv_walk(self, &cb, $arg);
- }
- sub uv_loop_fork(Loop --> int32) is native(&find-uv) { * }
- method fork() {
- uv_loop_fork(self);
- }
- }
- ## Timer.pm6
- use UV::Loop;
- use UV::Utils;
- use NativeCall;
- unit module UV::Timer;
- class Timer does FreeAble is repr('CPointer') is export {
- sub uv_new_timer( --> Timer) is native(&find-uvh) { * }
- method new() {
- uv_new_timer();
- }
- sub uv_timer_init(Loop, Timer --> int32) is native(&find-uv) { * }
- method init(Loop $loop --> int32) {
- uv_timer_init($loop, self);
- }
- sub uv_timer_start(Timer, & (Timer), uint64, uint64 --> int32) is native(&find-uv) { * }
- method start(&cb:(Timer), uint64 $timeout, uint64 $repeat) {
- uv_timer_start(self, &cb, $timeout, $repeat);
- }
- sub uv_timer_stop(Timer --> int32) is native(&find-uv) { * }
- method stop( --> int32) {
- uv_timer_stop(self);
- }
- sub uv_timer_again(Timer) is native(&find-uv) { * }
- method again( --> int32) {
- uv_timer_again(self);
- }
- sub uv_timer_get_repeat(Timer --> uint64) is native(&find-uv) { * }
- sub uv_timer_set_repeat(Timer, uint64) is native(&find-uv) { * }
- method repeat() is rw {
- Proxy.new(
- FETCH => method () { uv_timer_get_repeat(self); },
- STORE => method (uint64 $repeat) {
- uv_timer_set_repeat(self, $repeat);
- }
- );
- }
- }
- ## Utils.pm6
- use NativeCall;
- unit module UV::Utils;
- sub find-uv() is export {
- "libuv.so"
- }
- sub find-uvh() is export {
- "./libuvhelp.so"
- }
- sub find-uvdata() is export {
- sub uv_version( --> uint32) is native(&find-uv) { * }
- return uv_version() < 0x010900 ?? find-uvh() !! find-uv();
- }
- role FreeAble is export {
- sub uv_free(Pointer[void]) is native(&find-uvh) { * }
- method free() {
- uv_free(nativecast(Pointer[void], self));
- }
- }
- # test.p6
- #!/usr/bin/env perl6
- use lib "./lib";
- use UV::Loop;
- use UV::Timer;
- use UV::Handle;
- use NativeCall;
- my Loop $loop .= new;
- my Timer $timer .= new;
- say "123";
- my $ptr = nativecast(Pointer[void], $loop);
- dd $ptr;
- $loop.data = $ptr;
- $loop.init;
- dd $loop.data;
- $timer.init($loop);
- $timer.start(&callback, 100, 2);
- $loop.run(UV_RUN_DEFAULT);
- nativecast(Handle, $timer).close(Callable);
- $loop.close;
- sub callback(Timer $timer) {
- say "trimer!";
- $timer.handle.loop.stop;
- }
- $loop.free;
- $timer.free;
- ## uvhelp.c
- #include <uv.h>
- #include <stdlib.h>
- #define EXPORT_NEW(class, name) \
- class * uv_new_##name(void) { \
- return (class *)malloc(sizeof(class)); \
- }
- #if UV_VERSION_HEX < 0x010900
- #define EXPORT_GET(class, name, memeber, type) \
- type uv_##name##_get_##memeber(class * obj ) { \
- return obj->memeber; \
- }
- #define EXPORT_SET(class, name, memeber, type) \
- void uv_##name##_set_##memeber(class * obj, type value) { \
- obj->memeber = value; \
- }
- #else
- #define EXPORT_GET(class, name, memeber, type)
- #define EXPORT_SET(class, name, memeber, type)
- #endif
- void uvh_free(void* ptr) {
- free(ptr);
- }
- /* uv_loop_t */
- EXPORT_NEW(uv_loop_t, loop)
- EXPORT_GET(uv_loop_t, loop, data, void*)
- EXPORT_SET(uv_loop_t, loop, data, void*)
- /* uv_handle_t */
- EXPORT_NEW(uv_handle_t, handle)
- EXPORT_GET(uv_handle_t, handle, data, void*)
- EXPORT_SET(uv_handle_t, handle, data, void*)
- EXPORT_GET(uv_handle_t, handle, loop, uv_loop_t*)
- EXPORT_GET(uv_handle_t, handle, type, uv_handle_type)
- /* uv_req_t */
- EXPORT_NEW(uv_req_t, req)
- EXPORT_GET(uv_req_t, req, data, void*)
- EXPORT_SET(uv_req_t, req, data, void*)
- EXPORT_GET(uv_req_t, req, type, uv_req_type)
- /* uv_timer_t */
- EXPORT_NEW(uv_timer_t, timer)
- /* uv_prepare_t */
- EXPORT_NEW(uv_prepare_t, prepare)
- /* uv_check_t */
- EXPORT_NEW(uv_check_t, check)
- /* uv_idle_t */
- EXPORT_NEW(uv_idle_t, idle)
- /* uv_async_t */
- EXPORT_NEW(uv_async_t, async)
- /* uv_poll_t */
- EXPORT_NEW(uv_poll_t, poll)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement