I'm still getting used to nixysa but in addition to the bug I found
yesterday where null objects were not being returned properly when
using the by_pointer binding model (posted this as an issue on
code.google.com), I found shortly after another error related to a
named variable substitution that doesn't exist. Patch for both
changes:
--- a/plugin/third_party/nixysa/by_pointer_binding.py
+++ b/plugin/third_party/nixysa/by_pointer_binding.py
@@ -334,6 +341,7 @@ static void Deallocate(NPObject *header) {
}
NPAPIObject *GetNPObject(NPP npp, ${Class} *object) {
+ if(object == NULL) return NULL;
NPAPIObject *npobject = static_cast<NPAPIObject *>(
NPN_CreateObject(npp, &npclass));
npobject->set_value(object);
@@ -398,11 +406,11 @@ if (NPVARIANT_IS_OBJECT(${input})) {
if (npobject->_class == ${ClassGlueNS}::GetNPClass()) {
${variable} = static_cast<${ClassGlueNS}::NPAPIObject *>
(npobject);
} else {
- *error_handle = "Error in " ${context}
+ *error_handle = "Error in " NPAPI_GLUE_EXCEPTION_CONTEXT
": type mismatch.";
}
} else {
- *error_handle = "Error in " ${context}
+ *error_handle = "Error in " NPAPI_GLUE_EXCEPTION_CONTEXT
": was expecting an object.";
}
${success} = ${variable} != NULL;
Another issue I ran into was garbage collection. I wanted to use a
factory design pattern in my app.
Think "var cam = flash.media.Camera.getCamera(0);" only in JavaScript.
While [binding_model=by_pointer] avoids copy constructors and
assignment operators, theres no notification when an object gets
destroyed.
I ended up modifying by_pointer_binding.py to do this and inherited my
classes from a reference counting base class. This is clearly not
ideal as it will break any implementations that don't implement the
reference counting functions I have defined but for me it does exactly
what I wanted it to.
What I'm wondering is if anyone here has thought about this and has
any other ideas? I'd be happy to change the code if need be.
Patch for reference counting [binding_model=by_pointer] classes:
--- a/plugin/third_party/nixysa/by_pointer_binding.py
+++ b/plugin/third_party/nixysa/by_pointer_binding.py
@@ -298,10 +298,17 @@ class NPAPIObject : public NPObject {
${Class} *value_;
public:
NPAPIObject(NPP npp): npp_(npp), value_() { }
+ ~NPAPIObject() { set_value(NULL); }
NPP npp() { return npp_; }
${Class} *value() { return value_; }
${Class} *value_mutable() { return value_; }
- void set_value(${Class} *value) { value_ = value; }
+ void set_value(${Class} *value) {
+ if(value_) {
+ if(value) value->duplicate();
+ value_->release();
+ }
+ value_ = value;
+ }
};