From d61cc09e5b7b8e77ad5c379f32e9370c206efeb6 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Fri, 18 Sep 2015 13:50:09 +0300 Subject: [PATCH] Draft native finalizer section --- HowtoFinalization.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/HowtoFinalization.md b/HowtoFinalization.md index b4f4564..93b69fe 100644 --- a/HowtoFinalization.md +++ b/HowtoFinalization.md @@ -49,6 +49,36 @@ mark-and-sweep finalizer Cleaning up... ``` +## Using a native finalizer + +The finalizer function can also be a Duktape/C function. Here's an example +of setting up a finalizer from C code: + +```c +/* Dummy object for finalization test. */ +duk_push_object(ctx); + +/* Finalizer gets a single argument, the object being finalized. */ +duk_push_c_function(ctx, my_finalizer, 1 /*nargs*/); + +/* Set finalizer. */ +duk_set_finalizer(ctx, -2); /* [ object func ] -> [ object ] */ + +/* Object-with-finalizer left on stack top. */ +``` + +The finalizer itself is an ordinary Duktape/C function: + +```c +duk_ret_t my_finalizer(duk_context *ctx) { + /* Value stack index 0 has the object to be finalized. */ +} +``` + +FIXME: add a concrete native resource, e.g. a FILE handle + +FIXME: use an internal property, tolerate multiple finalization + ## Adding a finalizer to a prototype object If you have many objects of the same type, you can add a finalizer to the