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.
49 lines
1.5 KiB
49 lines
1.5 KiB
name: duk_set_finalizer
|
|
|
|
proto: |
|
|
void duk_set_finalizer(duk_context *ctx, duk_idx_t idx);
|
|
|
|
stack: |
|
|
[ ... val! ... finalizer! ] -> [ ... val! ... ]
|
|
|
|
summary: |
|
|
<p>Set the finalizer of the value at <code>idx</code> to the value at
|
|
stack top. If the target value is not an object an error is thrown. The
|
|
finalizer value can be an arbitrary one; non-function values are treated
|
|
as if no finalizer was set. To delete a finalizer from an object, set it
|
|
to <code>undefined</code>.</p>
|
|
|
|
<div class="note">
|
|
Finalizer on a Proxy object is currently unsupported, set the finalizer on
|
|
the target instead. When trying to set a finalizer on the Proxy object an
|
|
internal finalizer property is written on the target object but an internal
|
|
"have finalizer" flag gets set only on the Proxy, and as a result the finalizer
|
|
won't run on either the target or the Proxy.
|
|
</div>
|
|
|
|
<div class="note">
|
|
At present the finalizer is stored as a hidden Symbol; a finalizer cannot
|
|
be set if the object is non-extensible (sealed or frozen), so set the
|
|
finalizer before sealing/freezing the object.
|
|
</div>
|
|
|
|
example: |
|
|
duk_ret_t my_finalizer(duk_context *ctx) {
|
|
/* Object being finalized is at stack index 0. */
|
|
printf("object being finalized\n");
|
|
return 0;
|
|
}
|
|
|
|
/* Create an object whose finalizer is my_finalizer(). */
|
|
duk_push_object(ctx);
|
|
duk_push_c_function(ctx, my_finalizer, 1 /*nargs*/);
|
|
duk_set_finalizer(ctx, -2);
|
|
|
|
tags:
|
|
- object
|
|
- finalizer
|
|
|
|
seealso:
|
|
- duk_get_finalizer
|
|
|
|
introduced: 1.0.0
|
|
|