You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.3 KiB

<h2 id="duktapebuiltins">Duktape built-ins</h2>
<p>This section describes Duktape-specific built-in objects, methods, and
values.</p>
<h3>Additional global object properties</h3>
<table>
<thead>
<tr>
<th>Property</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr><td class="propname">__duk__</td><td>The Duktape built-in object. Contains miscellaneous implementation specific stuff.</td></tr>
<tr><td class="propname">print</td><td>Non-standard, browser-like function for writing entries to <tt>stdout</tt>.</td></tr>
<tr><td class="propname">alert</td><td>Non-standard, browser-like function for writing entries to <tt>stderr</tt>.</td></tr>
</tbody>
</table>
<h3>The __duk__ object</h3>
<table>
<thead>
<tr>
<th>Property</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr><td class="propname">build</td><td>Opaque build information string. Describes when and how the Duktape distributable sources were built.</td></tr>
<tr><td class="propname">version</td><td>Duktape version number: <tt>(major * 10000) + (minor * 100) + patch</tt>.</td></tr>
<tr><td class="propname">Buffer</td><td>Buffer constructor (function).</td></tr>
<tr><td class="propname">Pointer</td><td>Pointer constructor (function).</td></tr>
<tr><td class="propname">Thread</td><td>Thread constructor (function).</td></tr>
</tbody>
</table>
<p>The <tt>version</tt> property allows version-based feature detection and
behavior. Version numbers can be compared directly: a logically higher version
will also be numerically higher. For example:</p>
<pre class="ecmascript-code">
if (typeof __duk__ !== 'object') {
print('not Duktape');
} else if (__duk__.version &gt;= 10203) {
print('Duktape 1.2.3 or higher');
} else if (__duk__.version &gt;= 700) {
print('Duktape 0.7.0 or higher (but lower than 1.2.3)');
} else {
print('Duktape lower than 0.7.0');
}
</pre>
<p>Remember to check for existence of <tt>__duk__</tt> when doing feature
detection. Your code should typically work on as many engines as possible.
Avoid the common pitfall of using a direct identifier reference in the check:</p>
<pre class="ecmascript-code">
// Bad idea: ReferenceError if missing
if (!__duk__) {
print('not Duktape');
}
// Better: check through 'this' (bound to global)
if (!this.__duk__) {
print('not Duktape');
}
// Better: use typeof to check also type explicitly
if (typeof __duk__ !== 'object') {
print('not Duktape');
}
</pre>
<h3>__duk__.Buffer (constructor)</h3>
<table>
<thead>
<tr>
<th>Property</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr><td colspan="2">No properties at the moment.</td></tr>
</tbody>
</table>
<p>The Buffer constructor is a function which can be called both as an
ordinary function and as a constructor:</p>
<ul>
<li>When called as a function, coerces the first argument to a buffer using
the custom <tt>ToBuffer</tt> coercion. The return value is a plain
buffer (not a Buffer object).</li>
<li>When called as a constructor, coerces the first argument to a buffer
using the custom <tt>ToBuffer</tt> coercion. Returns a Buffer object
whose internal value is the buffer resulting from the coercion. The
internal prototype of the newly created Buffer will be the
<tt>__duk__.Buffer.prototype</tt> object.</li>
</ul>
<h3>__duk__.Buffer.prototype</h3>
<table>
<thead>
<tr>
<th>Property</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr><td class="propname">toString</td><td>Convert Buffer to a printable string.</td></tr>
<tr><td class="propname">valueOf</td><td>Return the primitive buffer value held by Buffer.</td></tr>
</tbody>
</table>
<p><tt>toString()</tt> and <tt>valueOf</tt> accept both plain buffers and
Buffer objects as their <tt>this</tt> binding. This allows code such as:</p>
<pre class="ecmascript-code">
print(plain_buf.toString());
</pre>
<h3>__duk__.Pointer (constructor)</h3>
<table>
<thead>
<tr>
<th>Property</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr><td colspan="2">No properties at the moment.</td></tr>
</tbody>
</table>
<p>The Pointer constructor is a function which can be called both as an
ordinary function and as a constructor:</p>
<ul>
<li>When called as a function, coerces the first argument to a pointer using
the custom <tt>ToPointer</tt> coercion. The return value is a plain
pointer (not a Pointer object).</li>
<li>When called as a constructor, coerces the first argument to a pointer
using the custom <tt>ToPointer</tt> coercion. Returns a Pointer object
whose internal value is the pointer resulting from the coercion. The
internal prototype of the newly created Pointer will be the
<tt>__duk__.Pointer.prototype</tt> object.</li>
</ul>
<h3>__duk__.Pointer.prototype</h3>
<table>
<thead>
<tr>
<th>Property</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr><td class="propname">toString</td><td>Convert Pointer to a printable string.</td></tr>
<tr><td class="propname">valueOf</td><td>Return the primitive pointer value held by Pointer.</td></tr>
</tbody>
</table>
<p><tt>toString()</tt> and <tt>valueOf</tt> accept both plain pointers and
Pointer objects as their <tt>this</tt> binding. This allows code such as:</p>
<pre class="ecmascript-code">
print(plain_ptr.toString());
</pre>
<h3>__duk__.Thread (constructor)</h3>
<table>
<thead>
<tr>
<th>Property</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr><td class="propname">resume</td><td>Resume target thread with a value or an error.</td></tr>
<tr><td class="propname">yield</td><td>Yield a value or an error from current thread.</td></tr>
<tr><td class="propname">current</td><td>Get currently running Thread object.</td></tr>
</tbody>
</table>
<p>The Thread constructor is a function which can be called both as an
ordinary function and as a constructor. The behavior is the same in both
cases:</p>
<ul>
<li>The first argument is checked to be a function (if not, a <tt>TypeError</tt>
is thrown). The return value is a new thread whose initial function is
recorded to be the argument function (this function will start executing
when the new thread is first resumed). The internal prototype of the
newly created Thread will be the <tt>__duk__.Thread.prototype</tt> object.</li>
</ul>
<h3>__duk__.Thread.prototype</h3>
<table>
<thead>
<tr>
<th>Property</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr><td colspan="2">No properties at the moment.</td></tr>
</tbody>
</table>