mirror of https://github.com/svaarala/duktape.git
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.
81 lines
3.1 KiB
81 lines
3.1 KiB
11 years ago
|
=proto
|
||
11 years ago
|
duk_bool_t duk_get_prop(duk_context *ctx, duk_idx_t obj_index);
|
||
11 years ago
|
|
||
|
=stack
|
||
|
[ ... obj! ... key! ] -> [ ... obj! ... val! ] (if key exists)
|
||
|
[ ... obj! ... key! ] -> [ ... obj! ... undefined! ] (if key doesn't exist)
|
||
|
|
||
|
=summary
|
||
11 years ago
|
<p>Get the property <code>key</code> of a value at <code>obj_index</code>.
|
||
11 years ago
|
Return code and error throwing behavior:</p>
|
||
11 years ago
|
<ul>
|
||
10 years ago
|
<li>If the property exists, <code>1</code> is returned and <code>key</code>
|
||
11 years ago
|
is replaced by the property value on the value stack. However,
|
||
|
if the property is an accessor, the "getter" function may throw
|
||
|
an error.</li>
|
||
11 years ago
|
<li>If the property does not exist, 0 is returned and <code>key</code>
|
||
|
is replaced by <code>undefined</code> on the value stack.</li>
|
||
|
<li>If the value at <code>obj_index</code> is not
|
||
11 years ago
|
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.10">object coercible</a>,
|
||
11 years ago
|
throws an error.</li>
|
||
11 years ago
|
<li>If <code>obj_index</code> is invalid, throws an error.</li>
|
||
11 years ago
|
</ul>
|
||
11 years ago
|
|
||
11 years ago
|
<p>The property read is equivalent to the Ecmascript expression:</p>
|
||
11 years ago
|
<pre class="ecmascript-code">
|
||
11 years ago
|
obj[key]
|
||
11 years ago
|
</pre>
|
||
|
|
||
|
<p>For semantics, see
|
||
11 years ago
|
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1">Property Accessors</a>,
|
||
11 years ago
|
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.7.1">GetValue (V)</a>,
|
||
|
and <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.3">[[Get]] (P)</a>.
|
||
11 years ago
|
Both the target value and the <code>key</code> are coerced:</p>
|
||
11 years ago
|
<ul>
|
||
|
<li>The target value is automatically coerced to an object. For instance,
|
||
11 years ago
|
a string is converted to a <code>String</code> and you can access its
|
||
|
<code>"length"</code> property.</li>
|
||
|
<li>The <code>key</code> argument is internally coerced to a string. There is
|
||
11 years ago
|
an internal fast path for arrays and numeric indices which avoids an
|
||
11 years ago
|
explicit string coercion, so use a numeric <code>key</code> when applicable.</p>
|
||
11 years ago
|
</ul>
|
||
|
|
||
|
<p>If the key is a fixed string you can avoid one API call and use the
|
||
11 years ago
|
<code><a href="#duk_get_prop_string">duk_get_prop_string()</a></code> variant.
|
||
11 years ago
|
Similarly, if the key is an array index, you can use the
|
||
11 years ago
|
<code><a href="#duk_get_prop_index">duk_get_prop_index()</a></code> variant.</p>
|
||
11 years ago
|
|
||
10 years ago
|
<div include="object-can-be-any-value.html" />
|
||
|
|
||
11 years ago
|
=example
|
||
|
/* reading [global object].Math.PI */
|
||
|
duk_push_global_object(ctx); /* -> [ global ] */
|
||
|
duk_push_string(ctx, "Math"); /* -> [ global "Math" ] */
|
||
|
duk_get_prop(ctx, -2); /* -> [ global Math ] */
|
||
|
duk_push_string(ctx, "PI"); /* -> [ global Math "PI" ] */
|
||
|
duk_get_prop(ctx, -2); /* -> [ global Math PI ] */
|
||
11 years ago
|
printf("Math.PI is %lf\n", (double) duk_get_number(ctx, -1));
|
||
11 years ago
|
duk_pop_n(ctx, 3);
|
||
|
|
||
|
/* reading a configuration value, cfg_idx is normalized
|
||
|
* index of a configuration object.
|
||
|
*/
|
||
|
duk_push_string(ctx, "mySetting");
|
||
|
if (duk_get_prop(ctx, cfg_idx)) {
|
||
|
const char *str_value = duk_to_string(ctx, -1);
|
||
|
printf("configuration setting present, value: %s\n", str_value);
|
||
|
} else {
|
||
|
printf("configuration setting missing\n");
|
||
|
}
|
||
|
duk_pop(ctx); /* remember to pop, regardless of whether or not present */
|
||
|
|
||
11 years ago
|
=tags
|
||
|
property
|
||
|
|
||
|
=seealso
|
||
|
duk_get_prop_string
|
||
|
duk_get_prop_index
|
||
10 years ago
|
|
||
|
=introduced
|
||
|
1.0.0
|