<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class=""><br class=""></div><div class="">@Michael this is probably for you.</div><div class=""><br class=""></div><div class="">I am trying to implement javascript command registration. The idea is that a javascript module can call something like:</div><div class=""><br class=""></div><div class=""><blockquote style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); margin: 0px 0px 0px 40px; border: none; padding: 0px;" class="">OvmsCommand.Register(basecommand, name, title, callbackfn, usage, min, max)</blockquote><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class=""><br class=""></div></div><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class="">Then we reflect that into MyCommandApp.RegisterCommand, and keep a track of which command is for which javascript callbackfn. When the command is executed, we pass it into duktape.</div><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class=""><br class=""></div><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class="">I also have tracking for javascript module loading and unloading, so I can DeregisterCommand() if duktape is reloaded (and also protected against commands being registered in short-lived scripts run from the command line).</div><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class=""><br class=""></div><div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0);" class="">To implement this, I need to store the callbackfn as a persistent reference to a duktape javascript function.</div><div class=""><br class=""></div><div class="">The issue with callback function references in duktape is summarised here:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><a href="https://wiki.duktape.org/howtonativepersistentreferences" class="">https://wiki.duktape.org/howtonativepersistentreferences</a></div><div class=""><br class=""></div><div class=""><i class="">When a Duktape/C function is called, Duktape places the call arguments on the value stack. While the arguments are on the value stack, they're guaranteed to be reachable and the Duktape/C function can safely work with the arguments.<br class=""><br class="">However, when the Duktape/C function returns, the value stack is unwound and references in the function's value stack frame are lost. If the last reference to a particular value was in the function's value stack frame, the value will be garbage collected when the function return is processed.</i></div></blockquote><div class=""><br class=""></div><div class="">The standard approach is to store the reference back in the duktape duk_push_global_stash so it won’t get garbage-collected. But, that seems messy.</div><div class=""><br class=""></div><div class="">I see that Michael has already implemented something that seems similar in ovms_script.{h, cpp}, for the async http callbacks. Presumably to avoid this issue. But, the approach seems very different, and I am not sure if it is stopping _all_ garbage collection for the duration of the async query, or just that particular object being garbage collected. The work seems extensive (quite a few objects involved).</div><div class=""><br class=""></div><div class="">So @Michael, any suggestions for this? I don’t want to reinvent the wheel...</div><div class=""><br class=""></div><div class="">Regards, Mark.</div></body></html>