The newly released ScriptEase ISDK v. 4.30d provides a useful new feature called the jseWeakAPIReference
flag that allows tighter and quicker binding between C++ objects and JavaScript objects. Without weak referencing, you can run into a lifetime problem when trying to map a C++ object to an equivalent JavaScript object, as C++ needs a reference to the
jseVariable, and the jseVariable needs a reference to the C++ object. These two different environments then need to retain a lock on each other, so how do either of them know when it's time to clean up?
Weak referencing solves this "catch-22" situation by allowing you to link to a C++ object directly, without locking in its lifetime or needing to add external reference counting. You can continue to
access a weak reference variable, but the garbage collector will ignore it. To use weak referencing, the changes you need to make to your code are surprisingly minimal. Just build with the JSE_WEAK_REFERENCE defined in the jseopt.h file and then apply the jseWeakAPIReference flag to any variable you want to retrieve in this matter. Once an object is marked as a having a
weak reference, the ScriptEase engine will not consider this object to be held by the external code. The variable will then remain valid only as long as the underlying ScriptEase JavaScript object is valid.
However, when using this flag, the calling code must install a delete callback in order to receive notification of when the ScriptEase engine has determined the referenced variable is no longer needed.
In summary, with jseWeakAPIReference, you can now create a
direct C++/JavaScript mapping between two objects that will not lock lifetimes at either end. Just remember to implement the delete callback on the object so you know when JavaScript believes it is no longer in use.
Have a question about how to use ScriptEase:ISDK? Let Dr. Scripter know at http://support.nombasxxx.com/
. |