File: C:/Ruby27-x64/share/doc/ruby/html/Fiddle/Closure.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class Fiddle::Closure - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "../";
var index_rel_prefix = "../";
</script>
<script src="../js/navigation.js" defer></script>
<script src="../js/search.js" defer></script>
<script src="../js/search_index.js" defer></script>
<script src="../js/searcher.js" defer></script>
<script src="../js/darkfish.js" defer></script>
<link href="../css/fonts.css" rel="stylesheet">
<link href="../css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="../index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="../table_of_contents.html#pages">Pages</a>
<a href="../table_of_contents.html#classes">Classes</a>
<a href="../table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div class="nav-section">
<h3>Table of Contents</h3>
<ul class="link-list" role="directory">
<li><a href="#class-Fiddle::Closure-label-Description">Description</a>
<li><a href="#class-Fiddle::Closure-label-Example">Example</a>
</ul>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="../Object.html">Object</a>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-i-to_i">#to_i</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-Fiddle::Closure">
<h1 id="class-Fiddle::Closure" class="class">
class Fiddle::Closure
</h1>
<section class="description">
<h2 id="class-Fiddle::Closure-label-Description">Description<span><a href="#class-Fiddle::Closure-label-Description">¶</a> <a href="#top">↑</a></span></h2>
<p>An FFI closure wrapper, for handling callbacks.</p>
<h2 id="class-Fiddle::Closure-label-Example">Example<span><a href="#class-Fiddle::Closure-label-Example">¶</a> <a href="#top">↑</a></span></h2>
<pre class="ruby"><span class="ruby-identifier">closure</span> = <span class="ruby-constant">Class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">Fiddle</span><span class="ruby-operator">::</span><span class="ruby-constant">Closure</span>) {
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">call</span>
<span class="ruby-value">10</span>
<span class="ruby-keyword">end</span>
}.<span class="ruby-identifier">new</span>(<span class="ruby-constant">Fiddle</span><span class="ruby-operator">::</span><span class="ruby-constant">TYPE_INT</span>, [])
<span class="ruby-comment">#=> #<#<Class:0x0000000150d308>:0x0000000150d240></span>
<span class="ruby-identifier">func</span> = <span class="ruby-constant">Fiddle</span><span class="ruby-operator">::</span><span class="ruby-constant">Function</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">closure</span>, [], <span class="ruby-constant">Fiddle</span><span class="ruby-operator">::</span><span class="ruby-constant">TYPE_INT</span>)
<span class="ruby-comment">#=> #<Fiddle::Function:0x00000001516e58></span>
<span class="ruby-identifier">func</span>.<span class="ruby-identifier">call</span>
<span class="ruby-comment">#=> 10</span>
</pre>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section class="attribute-method-details" class="method-section">
<header>
<h3>Attributes</h3>
</header>
<div id="attribute-i-args" class="method-detail">
<div class="method-heading attribute-method-heading">
<span class="method-name">args</span><span
class="attribute-access-type">[R]</span>
</div>
<div class="method-description">
<p>arguments of the FFI closure</p>
</div>
</div>
<div id="attribute-i-ctype" class="method-detail">
<div class="method-heading attribute-method-heading">
<span class="method-name">ctype</span><span
class="attribute-access-type">[R]</span>
</div>
<div class="method-description">
<p>the C type of the return of the FFI closure</p>
</div>
</div>
</section>
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
new(ret, args, abi = Fiddle::DEFAULT)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Construct a new <a href="Closure.html"><code>Closure</code></a> object.</p>
<ul><li>
<p><code>ret</code> is the C type to be returned</p>
</li><li>
<p><code>args</code> is an <a href="../Array.html"><code>Array</code></a> of arguments, passed to the callback function</p>
</li><li>
<p><code>abi</code> is the abi of the closure</p>
</li></ul>
<p>If there is an error in preparing the ffi_cif or ffi_prep_closure, then a <a href="../RuntimeError.html"><code>RuntimeError</code></a> will be raised.</p>
<div class="method-source-code" id="new-source">
<pre>static VALUE
initialize(int rbargc, VALUE argv[], VALUE self)
{
VALUE ret;
VALUE args;
VALUE abi;
fiddle_closure * cl;
ffi_cif * cif;
ffi_closure *pcl;
ffi_status result;
int i, argc;
if (2 == rb_scan_args(rbargc, argv, "21", &ret, &args, &abi))
abi = INT2NUM(FFI_DEFAULT_ABI);
Check_Type(args, T_ARRAY);
argc = RARRAY_LENINT(args);
TypedData_Get_Struct(self, fiddle_closure, &closure_data_type, cl);
cl->argv = (ffi_type **)xcalloc(argc + 1, sizeof(ffi_type *));
for (i = 0; i < argc; i++) {
int type = NUM2INT(RARRAY_AREF(args, i));
cl->argv[i] = INT2FFI_TYPE(type);
}
cl->argv[argc] = NULL;
rb_iv_set(self, "@ctype", ret);
rb_iv_set(self, "@args", args);
cif = &cl->cif;
pcl = cl->pcl;
result = ffi_prep_cif(cif, NUM2INT(abi), argc,
INT2FFI_TYPE(NUM2INT(ret)),
cl->argv);
if (FFI_OK != result)
rb_raise(rb_eRuntimeError, "error prepping CIF %d", result);
#if USE_FFI_CLOSURE_ALLOC
result = ffi_prep_closure_loc(pcl, cif, callback,
(void *)self, cl->code);
#else
result = ffi_prep_closure(pcl, cif, callback, (void *)self);
cl->code = (void *)pcl;
i = mprotect(pcl, sizeof(*pcl), PROT_READ | PROT_EXEC);
if (i) {
rb_sys_fail("mprotect");
}
#endif
if (FFI_OK != result)
rb_raise(rb_eRuntimeError, "error prepping closure %d", result);
return self;
}</pre>
</div>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-to_i" class="method-detail ">
<div class="method-heading">
<span class="method-name">to_i</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the memory address for this closure</p>
<div class="method-source-code" id="to_i-source">
<pre>static VALUE
to_i(VALUE self)
{
fiddle_closure * cl;
void *code;
TypedData_Get_Struct(self, fiddle_closure, &closure_data_type, cl);
code = cl->code;
return PTR2NUM(code);
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>