ccxvii / mujs

An embeddable Javascript interpreter in C.
http://mujs.com/
ISC License
812 stars 98 forks source link

Is userdata function 'js_newcconstructor' missing a 'js_Finalize' callback? #151

Closed weberkai closed 3 years ago

weberkai commented 3 years ago

Hi!

Thanks for your great project!

It is really awesome!

In the userdata example, there is this function call: js_newcconstructor(J, new_File, new_File, "File", 1);

For example, let's say the 'new_File' constructor function allocates memory with the malloc function.

If in the .js file I call:

var test = new File('abc'); //Creates the object and allocates some memory with malloc
test = null; //Dereference the object and the garbage collector can claim the memory

When the garbage collector claims the memory, how can I tell him to free the previously allocated memory in the constructor function too?

Maybe the 'js_newcconstructor' signature could be changed like this:

void js_newcconstructor(js_State *J,
    js_CFunction fun, js_CFunction con, js_Finalize finalize
    const char *name, int length);

So the garbage collector could call the 'finalize' function and this callback function could free the memory allocated by the constructor function.

Thanks!

weberkai commented 3 years ago

Hi!

I'm closing this issue because I will first try using a 'finalize' function in 'js_newuserdata(J, TAG, stdin)'.

Thanks!