daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest May 20th, 2018 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # under lib/UV
  2. ## Handle.pm6
  3.  
  4. use UV::Utils;
  5. use NativeCall;
  6.  
  7. unit module UV::Handle;
  8.  
  9. enum HandleType is export <
  10.   UV_UNKNOWN_HANDLE
  11.   UV_ASYNC
  12.   UV_CHECK
  13.   UV_FS_EVENT
  14.   UV_FS_POLL
  15.   UV_HANDLE
  16.   UV_IDLE
  17.   UV_NAMED_PIPE
  18.   UV_POLL
  19.   UV_PREPARE
  20.   UV_PROCESS
  21.   UV_STREAM
  22.   UV_TCP
  23.   UV_TIMER
  24.   UV_TTY
  25.   UV_UDP
  26.   UV_SIGNAL
  27.   UV_FILE
  28.   UV_HANDLE_TYPE_MAX
  29. >;
  30.  
  31. class Handle does FreeAble is repr('CPointer') is export {
  32.  
  33.     sub uv_handle_size(int32 --> size_t) is native(&find-uv) { * }
  34.  
  35.     method size(::Handle::U: HandleType $ht --> size_t) {
  36.         uv_handle_size(my int32 $ = $ht);
  37.     }
  38.  
  39.     sub uv_new_handle( --> Handle) is native(&find-uvh) { * }
  40.  
  41.     method new() {
  42.         uv_new_handle();
  43.     }
  44.  
  45.     sub uv_is_active(Handle --> int32) is native(&find-uv) { * }
  46.  
  47.     method is-active( --> Bool) {
  48.         uv_is_active(self) != 0;
  49.     }
  50.  
  51.     sub uv_is_closing(Handle --> int32) is native(&find-uv) { * }
  52.  
  53.     method is-closing( --> Bool) {
  54.         uv_is_closing(self) != 0;
  55.     }
  56.  
  57.     sub uv_close(Handle, & (Handle)) is native(&find-uv) { * }
  58.  
  59.     method close(&cb:(Handle)) {
  60.         uv_close(self, &cb);
  61.     }
  62.  
  63.     sub uv_ref(Handle) is native(&find-uv) { * }
  64.  
  65.     method ref() {
  66.         uv_ref(self);
  67.     }
  68.  
  69.     sub uv_unref(Handle) is native(&find-uv) { * }
  70.  
  71.     method unref() {
  72.         uv_unref(self);
  73.     }
  74.  
  75.     sub uv_has_ref(Handle --> int32) is native(&find-uv) { * }
  76.  
  77.     method has-ref( --> Bool) {
  78.         uv_has_ref(self) != 0;
  79.     }
  80.  
  81.     sub uv_send_buffer_size(Handle, Pointer[int32] --> int32) is native(&find-uv) { * }
  82.  
  83.     method send-buffer-size() is rw {
  84.         Proxy.new(
  85.             FETCH => method () {
  86.                 uv_send_buffer_size(self, nativecast(Pointer[int32], CArray[int32].new(0)));
  87.             },
  88.             STORE => method (int32 $value) {
  89.                 uv_send_buffer_size(self, nativecast(Pointer[int32], CArray[int32].new($value)));
  90.             }
  91.         )
  92.     }
  93.  
  94.     sub uv_recv_buffer_size(Handle, Pointer[int32] --> int32) is native(&find-uv) { * }
  95.  
  96.     method recv-buffer-size() is rw {
  97.         Proxy.new(
  98.             FETCH => method () {
  99.                 uv_recv_buffer_size(self, nativecast(Pointer[int32], CArray[int32].new(0)));
  100.             },
  101.             STORE => method (int32 $value) {
  102.                 uv_recv_buffer_size(self, nativecast(Pointer[int32], CArray[int32].new($value)));
  103.             }
  104.         )
  105.     }
  106.  
  107.     sub uv_fileno_win(Handle, Pointer[void] is rw --> int32) is native(&find-uv) is symbol("uv_fileno") { * }
  108.     sub uv_fileno_linux(Handle, int32 is rw       --> int32) is native(&find-uv) is symbol("uv_fileno") { * }
  109.  
  110.     method fileno() {
  111.         if $*Distro.is-win {
  112.             my $ptr = Pointer[void].new;
  113.             return (uv_fileno_win(self, $ptr), $ptr.address);
  114.         } else {
  115.             my int32 $ptr = 0;
  116.             return (uv_fileno_linux(self, $ptr), $ptr);
  117.         }
  118.     }
  119.  
  120.     sub uv_handle_get_data(Handle --> Pointer[void]) is native(&find-uvdata) { * }
  121.     sub uv_handle_set_data(Handle,    Pointer[void]) is native(&find-uvdata) { * }
  122.  
  123.     method data() is rw {
  124.         Proxy.new(
  125.             FETCH => method () { uv_handle_get_data(self); },
  126.             STORE => method (Pointer[void] $value) { uv_handle_set_data(self, $value); }
  127.         );
  128.     }
  129.  
  130.     sub uv_handle_get_loop(Handle --> Pointer) is native(&find-uvdata) { * }
  131.  
  132.     method loop() {
  133.         uv_handle_get_loop(self);
  134.     }
  135.  
  136.     sub uv_handle_get_type(Handle --> int32) is native(&find-uvdata) { * }
  137.  
  138.     method type( --> HandleType) {
  139.         HandleType(uv_handle_get_type(self));
  140.     }
  141. }
  142.  
  143. sub uv_handle_type_name(int32 --> Str) is native(&find-uv) { * }
  144.  
  145. sub handle-typename(Handle $handle --> Str) is export {
  146.     uv_handle_type_name(int32($handle));
  147. }
  148.  
  149. role CastToHandle is export {
  150.     method handle() {
  151.         nativecast(Handle, self);
  152.     }
  153. }
  154.  
  155. ## Loop.pm6
  156.  
  157. use UV::Utils;
  158. use UV::Handle;
  159. use NativeCall;
  160.  
  161. unit module UV::Loop;
  162.  
  163. enum RunMode is export <UV_RUN_DEFAULT UV_RUN_ONCE UV_RUN_NOWAIT>;
  164.  
  165. class X::Loop is Exception { }
  166.  
  167. class Loop does FreeAble is repr('CPointer') is export {
  168.  
  169.     sub uv_default_loop( --> Loop) is native(&find-uv) { * }
  170.  
  171.     method default(::Loop::U: --> Loop) {
  172.         uv_default_loop();
  173.     }
  174.  
  175.     sub uv_loop_size( --> size_t) is native(&find-uv) { * }
  176.  
  177.     method size(::Loop::U: --> size_t) {
  178.         uv_loop_size();
  179.     }
  180.    
  181.     sub uv_new_loop( --> Loop) is native(&find-uvh) { * }
  182.  
  183.     method new( --> Loop) {
  184.         uv_new_loop();
  185.     }
  186.  
  187.     sub uv_loop_init(Loop --> int32) is native(&find-uv) { * }
  188.  
  189.     method init( --> int32) {
  190.         uv_loop_init(self);
  191.     }
  192.  
  193.     sub uv_loop_get_data(Loop --> Pointer[void]) is native(&find-uvdata) { * }
  194.     sub uv_loop_set_data(Loop,    Pointer[void]) is native(&find-uvdata) { * }
  195.    
  196.     method data() is rw {
  197.         Proxy.new(
  198.             FETCH => method () { uv_loop_get_data(self); },
  199.             STORE => method (Pointer[void] $value) { uv_loop_set_data(self, $value); }
  200.         );
  201.     }
  202.  
  203.     sub uv_loop_close(Loop --> int32) is native(&find-uv) { * }
  204.  
  205.     method close( --> int32) {
  206.         uv_loop_close(self);
  207.     }
  208.  
  209.     sub uv_run(Loop, int32 --> int32) is native(&find-uv) { * }
  210.  
  211.     method run(RunMode $mode --> int32) {
  212.         uv_run(self, my int32 $ = $mode);
  213.     }
  214.  
  215.     sub uv_loop_alive(Loop --> int32) is native(&find-uv) { * }
  216.  
  217.     method alive( --> Bool) {
  218.         uv_loop_alive(self) != 0;
  219.     }
  220.  
  221.     sub uv_stop(Loop) is native(&find-uv) { * }
  222.  
  223.     method stop() {
  224.         uv_stop(self);
  225.     }
  226.  
  227.     sub uv_backend_fd(Loop --> int32)  is native(&find-uv) { * }
  228.  
  229.     method fd( --> int32) {
  230.         uv_backend_fd(self);
  231.     }
  232.  
  233.     sub uv_backend_timeout(Loop --> int32) is native(&find-uv) { * }
  234.  
  235.     method timeout( --> int32) {
  236.         uv_backend_timeout(self);
  237.     }
  238.  
  239.     sub uv_now(Loop --> uint64) is native(&find-uv) { * }
  240.  
  241.     method now(--> uint64) {
  242.         uv_now(self);
  243.     }
  244.  
  245.     sub uv_update_time(Loop) is native(&find-uv) { * }
  246.  
  247.     method update-time() {
  248.         uv_update_time(self);
  249.     }
  250.  
  251.     sub uv_walk(Loop, & (Handle, Pointer[void]), Pointer[void]) is native(&find-uv) { * }
  252.  
  253.     method walk(&cb:(Handle, Pointer[void]), Pointer[void] $arg) {
  254.         uv_walk(self, &cb, $arg);
  255.     }
  256.  
  257.     sub uv_loop_fork(Loop --> int32) is native(&find-uv) { * }
  258.  
  259.     method fork() {
  260.         uv_loop_fork(self);
  261.     }
  262. }
  263.  
  264. ## Timer.pm6
  265.  
  266. use UV::Loop;
  267. use UV::Utils;
  268. use NativeCall;
  269.  
  270. unit module UV::Timer;
  271.  
  272. class Timer does FreeAble is repr('CPointer') is export {
  273.  
  274.     sub uv_new_timer( --> Timer) is native(&find-uvh) { * }
  275.  
  276.     method new() {
  277.         uv_new_timer();
  278.     }
  279.  
  280.     sub uv_timer_init(Loop, Timer --> int32) is native(&find-uv) { * }
  281.  
  282.     method init(Loop $loop --> int32) {
  283.         uv_timer_init($loop, self);
  284.     }
  285.  
  286.     sub uv_timer_start(Timer, & (Timer), uint64, uint64 --> int32) is native(&find-uv) { * }
  287.  
  288.     method start(&cb:(Timer), uint64 $timeout, uint64 $repeat) {
  289.         uv_timer_start(self, &cb, $timeout, $repeat);
  290.     }
  291.  
  292.     sub uv_timer_stop(Timer --> int32) is native(&find-uv) { * }
  293.    
  294.     method stop( --> int32) {
  295.         uv_timer_stop(self);
  296.     }
  297.  
  298.     sub uv_timer_again(Timer) is native(&find-uv) { * }
  299.  
  300.     method again( --> int32) {
  301.         uv_timer_again(self);
  302.     }
  303.  
  304.     sub uv_timer_get_repeat(Timer --> uint64) is native(&find-uv) { * }
  305.     sub uv_timer_set_repeat(Timer,    uint64) is native(&find-uv) { * }
  306.  
  307.     method repeat() is rw {
  308.         Proxy.new(
  309.             FETCH => method () { uv_timer_get_repeat(self); },
  310.             STORE => method (uint64 $repeat) {
  311.                 uv_timer_set_repeat(self, $repeat);
  312.             }
  313.         );
  314.     }
  315. }
  316.  
  317. ## Utils.pm6
  318. use NativeCall;
  319.  
  320. unit module UV::Utils;
  321.  
  322. sub find-uv() is export {
  323.     "libuv.so"
  324. }
  325.  
  326. sub find-uvh() is export {
  327.     "./libuvhelp.so"
  328. }
  329.  
  330. sub find-uvdata() is export {
  331.     sub uv_version( --> uint32) is native(&find-uv) { * }
  332.  
  333.     return uv_version() < 0x010900 ?? find-uvh() !! find-uv();
  334. }
  335.  
  336.  
  337. role FreeAble is export {
  338.     sub uv_free(Pointer[void]) is native(&find-uvh) { * }
  339.  
  340.     method free() {
  341.         uv_free(nativecast(Pointer[void], self));
  342.     }
  343. }
  344.  
  345. # test.p6
  346. #!/usr/bin/env perl6
  347.  
  348. use lib "./lib";
  349. use UV::Loop;
  350. use UV::Timer;
  351. use UV::Handle;
  352. use NativeCall;
  353.  
  354. my Loop $loop .= new;
  355. my Timer $timer .= new;
  356.  
  357. say "123";
  358. my $ptr = nativecast(Pointer[void], $loop);
  359. dd $ptr;
  360. $loop.data = $ptr;
  361. $loop.init;
  362. dd $loop.data;
  363. $timer.init($loop);
  364. $timer.start(&callback, 100, 2);
  365. $loop.run(UV_RUN_DEFAULT);
  366. nativecast(Handle, $timer).close(Callable);
  367. $loop.close;
  368.  
  369. sub callback(Timer $timer) {
  370.     say "trimer!";
  371.     $timer.handle.loop.stop;
  372. }
  373.  
  374. $loop.free;
  375. $timer.free;
  376.  
  377. ## uvhelp.c
  378.  
  379. #include <uv.h>
  380. #include <stdlib.h>
  381.  
  382.  
  383. #define EXPORT_NEW(class, name) \
  384.     class * uv_new_##name(void) { \
  385.         return (class *)malloc(sizeof(class)); \
  386.     }
  387.  
  388. #if UV_VERSION_HEX < 0x010900
  389.     #define EXPORT_GET(class, name, memeber, type) \
  390.         type uv_##name##_get_##memeber(class * obj ) { \
  391.             return obj->memeber; \
  392.         }
  393.  
  394.     #define EXPORT_SET(class, name, memeber, type) \
  395.         void uv_##name##_set_##memeber(class * obj, type value) { \
  396.             obj->memeber = value; \
  397.         }
  398. #else
  399.     #define EXPORT_GET(class, name, memeber, type)
  400.     #define EXPORT_SET(class, name, memeber, type)
  401. #endif
  402.  
  403. void uvh_free(void* ptr) {
  404.     free(ptr);
  405. }
  406.  
  407. /* uv_loop_t */
  408. EXPORT_NEW(uv_loop_t, loop)
  409. EXPORT_GET(uv_loop_t, loop, data, void*)
  410. EXPORT_SET(uv_loop_t, loop, data, void*)
  411.  
  412. /* uv_handle_t */
  413. EXPORT_NEW(uv_handle_t, handle)
  414. EXPORT_GET(uv_handle_t, handle, data, void*)
  415. EXPORT_SET(uv_handle_t, handle, data, void*)
  416. EXPORT_GET(uv_handle_t, handle, loop, uv_loop_t*)
  417. EXPORT_GET(uv_handle_t, handle, type, uv_handle_type)
  418.  
  419. /* uv_req_t */
  420. EXPORT_NEW(uv_req_t, req)
  421. EXPORT_GET(uv_req_t, req, data, void*)
  422. EXPORT_SET(uv_req_t, req, data, void*)
  423. EXPORT_GET(uv_req_t, req, type, uv_req_type)
  424.  
  425. /* uv_timer_t */
  426. EXPORT_NEW(uv_timer_t, timer)
  427.  
  428. /* uv_prepare_t */
  429. EXPORT_NEW(uv_prepare_t, prepare)
  430.  
  431. /* uv_check_t */
  432. EXPORT_NEW(uv_check_t, check)
  433.  
  434. /* uv_idle_t */
  435. EXPORT_NEW(uv_idle_t, idle)
  436.  
  437. /* uv_async_t */
  438. EXPORT_NEW(uv_async_t, async)
  439.  
  440. /* uv_poll_t */
  441. EXPORT_NEW(uv_poll_t, poll)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top