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.
 
 
 
 
 
 

72 lines
2.9 KiB

=proto
int duk_put_prop(duk_context *ctx, int obj_index);
=stack
[ ... obj! ... key! val! ] -> [ ... obj! ... ]
=summary
<p>Write <tt>val</tt> to the property <tt>key</tt> of a value at
<tt>obj_index</tt>. <tt>key</tt> and <tt>val</tt> are removed
from the stack. Return code and error throwing behavior:</p>
<ul>
<li>If the property write succeeds, returns non-zero.</li>
<li>If the property write fails, throws an error if current function is
strict, returns 0 otherwise. However, errors thrown by the "setter"
function of an accessor property are unconditional.</li>
<li>If the value at <tt>obj_index</tt> is not
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-9.10">object coercible</a>,
throws an error.</li>
<li>If <tt>obj_index</tt> is invalid, throws an error.</li>
</ul>
<p>The property write is equivalent to the Ecmascript expression:</p>
<pre class="ecmascript-code">
obj[key] = val
</pre>
<p>The exact rules of when a property write succeeds or fails are the same
as for Ecmascript code making the above assignment.</p>
<p>For semantics, see
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-11.2.1">Property Accessors</a>,
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.7.2">PutValue (V, W)</a>,
and <a href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.12.5">[[Put]] (P, V, Throw)</a>.
Both the target value and the <tt>key</tt> are coerced:</p>
<ul>
<li>The target value is automatically coerced to an object. However, the object
is transitory so writing its properties is not very useful. Moreover,
Ecmascript semantics prevent new properties from being created for such
transitory objects (see
<a href="http://www.ecma-international.org/ecma-262/5.1/#sec-8.7.2">PutValue (V, W)</a>,
step 7 of the special [[Put]] variant).</li>
<li>The <tt>key</tt> argument is internally coerced to a string. There is
an internal fast path for arrays and numeric indices which avoids an
explicit string coercion, so use a numeric <tt>key</tt> when applicable.</p>
</ul>
<div class="note">
In Ecmascript an assignment expression has the value of the right-hand-side
expression, regardless of whether or not the assignment succeeds. The return
value for this API call is not specified by Ecmascript or available to
Ecmascript code: the API call returns zero or non-zero depending on whether
the assignment succeeded or not (with the zero value promoted to an error in
strict code).
</div>
<p>If the key is a fixed string you can avoid one API call and use the
<tt><a href="#duk_put_prop_string">duk_put_prop_string()</a></tt> variant.
Similarly, if the key is an array index, you can use the
<tt><a href="#duk_put_prop_index">duk_put_prop_index()</a></tt> variant.</p>
=example
duk_push_string(ctx, "key");
duk_push_string(ctx, "value");
rc = duk_put_prop(ctx, -3);
printf("rc=%d\n", rc);
=tags
property
=seealso
duk_put_prop_string
duk_put_prop_index