<NME> NmeCffi.h
<BEF> #ifndef NME_CFFI_H
#define NME_CFFI_H



#ifdef HXCPP_JS_PRIME
#include "NmeJsPrime.h"
#include <Utils.h>

#else

extern vkind gObjectKind;

inline value ObjectToAbstract(Object *inObject)
{
   struct releaser
   {
      static void release_object(value inValue)
      {
         if (val_is_kind(inValue,gObjectKind))
         {
            Object *obj = (Object *)val_to_kind(inValue,gObjectKind);
            if (obj)
               obj->DecRef();
         }
      }
   };

   inObject->IncRef();
   value result = alloc_abstract(gObjectKind,inObject);
   val_gc(result,releaser::release_object);
   return result;
}

      length = inRHS.length;
      __s = inRHS.__s;
   }
   inline HxWString(const wchar_t *inS,int inLen=-1, bool inAllocGcString=true)
   {
      if (inAllocGcString)
      {
         if (inLen<0)
         {
            inLen = 0;
            while(inS[inLen]) inLen++;
         }
         __s = (wchar_t *)alloc_string_len((const char *)inS,(inLen+1)*sizeof(wchar_t)-1);
         length = inLen;
      }
      else
      {
         __s = inS;
         length = inLen;
      }
   }
   inline HxWString() : length(0), __s(0) { }

   inline int size() const { return length; }
   inline const wchar_t *c_str() const { return __s; }


   int length;
   const wchar_t *__s;
};

inline std::wstring valToStdWString(value inVal)
{
   return val_wstring(inVal);
}


#endif


#include <nme/Object.h>



inline HxString valToHxString(value inValue,bool inAllowNull=true)
{
   if (val_is_null(inValue))
   {
      if (inAllowNull)
         return HxString();
      return HxString("");
   }
#ifdef HXCPP_JS_PRIME
   return inValue.as<std::string>();
#else
   return HxString( val_string(inValue), val_strlen(inValue), false );
#endif
}

inline std::string valToStdString(value inValue,bool inAllowNull=true)
{
#ifdef HXCPP_JS_PRIME
   return inValue.as<std::string>();
#else
   const char *str = val_string(inValue);
   if (!str)
      return inAllowNull ? std::string() : std::string("");

   return std::string( str, str+val_strlen(inValue));
#endif
}


#ifdef HXCPP_JS_PRIME
inline const std::string &hxToStdString(const HxString &inValue) { return inValue; }
#else
inline std::string hxToStdString(const HxString &inValue)
{
   return std::string( inValue.__s, inValue.__s+inValue.length );
}
#endif

inline HxWString valToHxWString(value inValue,bool inAllowNull=true)
{
   if (val_is_null(inValue))
   {
      if (inAllowNull)
         return HxWString();
      return HxWString(L"");
   }
#ifdef HXCPP_JS_PRIME
   return inValue.as<std::wstring>();
#else
   //return HxWString( val_wstring(inValue), val_wstrlen(inValue), false );
   return HxWString( val_wstring(inValue), -1, false );
#endif
}

namespace nme
{

// In nme namespace
struct CffiBytes
{
   #ifndef HXCPP_JS_PRIME

   unsigned char *data;
   int length;
   CffiBytes( unsigned char *inData=0, int inLength=0) : data(inData), length(inLength) {}

   int getLength() { return length; };
   unsigned char *getData() { return data; };
   const unsigned char *readData() { return data; };

   #else
   protected:
      unsigned char *data;
      int length;
      value bound;

   public:

   CffiBytes() : bound( value::null() ), data(0), length(0) {}
   CffiBytes(value inValue) : bound(inValue)
   {
      if (bound.isNull())
      {
         data = 0;
         length = 0;
      }
      else
      {
         bound.call<void>("bind");
         data = (unsigned char *)bound["byteOffset"].as<int>();
         length = bound["byteLength"].as<int>();
      }
   }
   CffiBytes(CffiBytes&& v) : bound(v.bound), data(v.data), length(v.length)
   {
      v.bound = value::null();
   }

   CffiBytes(const CffiBytes& v) : bound(v.bound)
   {
      if (!bound.isNull())
         bound.call<void>("bind");
      data = v.data;
      length = v.length;
   }
   CffiBytes& operator=(CffiBytes&& v)
   {
      if (!bound.isNull())
         bound.call<void>("unbind");
      bound = v.bound;
      v.bound = value::null();
      return *this;
   }

   CffiBytes& operator=(const CffiBytes& v)
   {
      if (!v.bound.isNull())
         bound.call<void>("bind");
      if (!bound.isNull())
         bound.call<void>("unbind");
      bound = v.bound;
      return *this;
   }

   ~CffiBytes()
   {
      if (!bound.isNull())
      {
         bound.call<void>("unbind");
      }
   }

   int getLength() { return length; };
   unsigned char *getData() {
      if (data)
         bound.call<void>("setEdited");
      return data;
   };
   const unsigned char *readData() { return data; };

   #endif
};

inline CffiBytes getByteData(value inValue)
{
   #ifndef HXCPP_JS_PRIME
   if (val_is_object(inValue))
   {
      static field bField = 0;
      static field lengthField = 0;
      if (bField==0)
      {
         bField = val_id("b");
         lengthField = val_id("length");
      }
      value b = val_field(inValue, bField);
      value len = val_field(inValue, lengthField);
      if (val_is_string(b) && val_is_int(len))
         return CffiBytes( (unsigned char *)val_string(b), val_int(len) );
      if (val_is_buffer(b) && val_is_int(len))
         return CffiBytes( (unsigned char *)buffer_data(val_to_buffer(b)), val_int(len) );
   }
   #else
   if (!inValue.isNull())
   {
      value b = inValue["b"];
      if (!b.isUndefined())
         return CffiBytes(b);
   }
   #endif
   return CffiBytes();
}

inline bool resizeByteData(value inValue, int inNewLen)
{
   #ifndef HXCPP_JS_PRIME
   if (!val_is_object(inValue))
      return false;

   static field bField = 0;
   static field lengthField = 0;
   if (bField==0)
   {
      bField = val_id("b");
      lengthField = val_id("length");
   }
   value len = val_field(inValue, lengthField);
   if (!val_is_int(len))
      return false;
   int oldLen = val_int(len);
   value b = val_field(inValue, bField);
   if (val_is_string(b))
   {
      if (inNewLen>oldLen)
      {
         value newString = alloc_raw_string(inNewLen);
         memcpy( (char *)val_string(newString), val_string(b), inNewLen);
         alloc_field(inValue, bField, newString );
      }
      alloc_field(inValue, lengthField, alloc_int(inNewLen) );
   }
   return true;
   #else
   if (!inValue.isNull())
   {
      value b = inValue["b"];
      if (!b.isUndefined())
      {
         b.call<void>("resizeByteData",inNewLen);
         return true;
      }
   }
   return false;
   #endif
}






extern vkind gObjectKind;

#ifdef HXCPP_JS_PRIME
inline value ObjectToAbstract(Object *inObject) { return inObject->toAbstract(); }

template<typename OBJ>
inline bool AbstractToObject(value inValue, OBJ *&outObj)
{
   outObj = 0;
   Object *obj = Object::toObject(inValue);
   if (obj)
      outObj = dynamic_cast<OBJ *>(obj);
   return outObj;
}

struct BufferData : Object
{
   BufferData();

   NmeObjectType getObjectType() { return notBytes; }

   unsigned char *getData() { return data; }
   int getDataSize() { return allocLen; }

   void setDataSize(int inSize, bool keepData);
   void swapData(std::vector<unsigned char > &ioData);
   void verify(const char *inWhere);

   static BufferData *fromStream(class ObjectStreamIn &inStream);
   void encodeStream(class ObjectStreamOut &inStream);

   static int totalSize;

   private:
      unsigned char *data;
      int allocLen;

      BufferData(const BufferData &);
      void operator = (const BufferData &);
   protected:
      ~BufferData();
};
typedef BufferData *buffer;

inline buffer val_to_buffer(value bytes)
{
   buffer result = 0;
   AbstractToObject(bytes,result);
   return result;
}
inline unsigned char *buffer_data(buffer inBuffer)
{
   return inBuffer ? inBuffer->getData() : 0;
}
inline int buffer_size(buffer inBuffer) { return inBuffer ? inBuffer->getDataSize() : 0; }


#else

namespace
{
   inline void release_object(value inValue)
   {
      if (val_is_kind(inValue,gObjectKind))
      {
         Object *obj = (Object *)val_to_kind(inValue,gObjectKind);
         if (obj)
            obj->DecRef();
      }
   }
}

inline value ObjectToAbstract(Object *inObject)
{
   inObject->IncRef();
   value result = alloc_abstract(gObjectKind,inObject);
   val_gc(result,release_object);
   return result;
}


template<typename OBJ>
bool AbstractToObject(value inValue, OBJ *&outObj)
{
   outObj = 0;
   if ( ! val_is_kind(inValue,gObjectKind) )
      return false;
   Object *obj = (Object *)val_to_kind(inValue,gObjectKind);
   outObj = dynamic_cast<OBJ *>(obj);
   return outObj;
}
#endif


}


#endif

<MSG> Use anon-namespace to avoid multiple symbol warnings.

<DFF> @@ -11,24 +11,26 @@ namespace nme
 
 extern vkind gObjectKind;
 
-inline value ObjectToAbstract(Object *inObject)
+
+namespace
 {
-   struct releaser
+   inline void release_object(value inValue)
    {
-      static void release_object(value inValue)
+      if (val_is_kind(inValue,gObjectKind))
       {
-         if (val_is_kind(inValue,gObjectKind))
-         {
-            Object *obj = (Object *)val_to_kind(inValue,gObjectKind);
-            if (obj)
-               obj->DecRef();
-         }
+         Object *obj = (Object *)val_to_kind(inValue,gObjectKind);
+         if (obj)
+            obj->DecRef();
       }
-   };
+   }
+}
 
+
+inline value ObjectToAbstract(Object *inObject)
+{
    inObject->IncRef();
    value result = alloc_abstract(gObjectKind,inObject);
-   val_gc(result,releaser::release_object);
+   val_gc(result,release_object);
    return result;
 }
 

{'commit': 'diff --git a/haxelib.json b/haxelib.json\nindex 48a59d67..94ed97e0 100644\n--- a/haxelib.json\n+++ b/haxelib.json\n@@ -5,7 +5,7 @@\n   "tags": [ "cpp", "neko", "nme", "opengl", "sdl", "android", "ios"  ],\n   "description": "NME provides a backend for native iOS, Android, Windows, Mac and Linux applications, using a Flash inspired API",\n   "version": "5.1.0",\n-  "binaryversion": "34",\n+  "binaryversion": "35",\n   "releasenote": "see Changes.md",\n   "contributors": [ "gamehaxe" ],\n   "dependencies": {}\ndiff --git a/include/nme/NmeCffi.h b/include/nme/NmeCffi.h\nindex d4c26ce2..8def02e0 100644\n--- a/include/nme/NmeCffi.h\n+++ b/include/nme/NmeCffi.h\n@@ -11,24 +11,26 @@ namespace nme\n \n extern vkind gObjectKind;\n \n-inline value ObjectToAbstract(Object *inObject)\n+\n+namespace\n {\n-   struct releaser\n+   inline void release_object(value inValue)\n    {\n-      static void release_object(value inValue)\n+      if (val_is_kind(inValue,gObjectKind))\n       {\n-         if (val_is_kind(inValue,gObjectKind))\n-         {\n-            Object *obj = (Object *)val_to_kind(inValue,gObjectKind);\n-            if (obj)\n-               obj->DecRef();\n-         }\n+         Object *obj = (Object *)val_to_kind(inValue,gObjectKind);\n+         if (obj)\n+            obj->DecRef();\n       }\n-   };\n+   }\n+}\n \n+\n+inline value ObjectToAbstract(Object *inObject)\n+{\n    inObject->IncRef();\n    value result = alloc_abstract(gObjectKind,inObject);\n-   val_gc(result,releaser::release_object);\n+   val_gc(result,release_object);\n    return result;\n }\n \n', 'commit_subject': 'Use anon-namespace to avoid multiple symbol warnings.', 'repo_name': 'haxenme/nme', 'committer': {'name': 'Hugh', 'email': 'addcc332247940d36e349261d1b5f2717fc37e0e@gmail.com', 'time_sec': '1419227883', 'tz_offset': '480', 'date': {'seconds': '1419227883'}}, 'commit_tree': '2728b4cd7b4e1e12a00178c28998093107571d8a', 'commit_message': 'Use anon-namespace to avoid multiple symbol warnings.\n', 'language_name': 'Shell', 'license': 'mit', 'hunks': '@@ -11,24 +11,26 @@ namespace nme\n \n extern vkind gObjectKind;\n \n-inline value ObjectToAbstract(Object *inObject)\n+\n+namespace\n {\n-   struct releaser\n+   inline void release_object(value inValue)\n    {\n-      static void release_object(value inValue)\n+      if (val_is_kind(inValue,gObjectKind))\n       {\n-         if (val_is_kind(inValue,gObjectKind))\n-         {\n-            Object *obj = (Object *)val_to_kind(inValue,gObjectKind);\n-            if (obj)\n-               obj->DecRef();\n-         }\n+         Object *obj = (Object *)val_to_kind(inValue,gObjectKind);\n+         if (obj)\n+            obj->DecRef();\n       }\n-   };\n+   }\n+}\n \n+\n+inline value ObjectToAbstract(Object *inObject)\n+{\n    inObject->IncRef();\n    value result = alloc_abstract(gObjectKind,inObject);\n-   val_gc(result,releaser::release_object);\n+   val_gc(result,release_object);\n    return result;\n }\n \n', 'addition_count': 13, 'deletion_count': 11, 'src_file': 'a/include/nme/NmeCffi.h', 'tgt_file': 'b/include/nme/NmeCffi.h', 'file_extension': '.h', 'before_file': '#ifndef NME_CFFI_H\n#define NME_CFFI_H\n\n\n\n#ifdef HXCPP_JS_PRIME\n#include "NmeJsPrime.h"\n#include <Utils.h>\n\n#else\n\nextern vkind gObjectKind;\n\ninline value ObjectToAbstract(Object *inObject)\n{\n   struct releaser\n   {\n      static void release_object(value inValue)\n      {\n         if (val_is_kind(inValue,gObjectKind))\n         {\n            Object *obj = (Object *)val_to_kind(inValue,gObjectKind);\n            if (obj)\n               obj->DecRef();\n         }\n      }\n   };\n\n   inObject->IncRef();\n   value result = alloc_abstract(gObjectKind,inObject);\n   val_gc(result,releaser::release_object);\n   return result;\n}\n\n      length = inRHS.length;\n      __s = inRHS.__s;\n   }\n   inline HxWString(const wchar_t *inS,int inLen=-1, bool inAllocGcString=true)\n   {\n      if (inAllocGcString)\n      {\n         if (inLen<0)\n         {\n            inLen = 0;\n            while(inS[inLen]) inLen++;\n         }\n         __s = (wchar_t *)alloc_string_len((const char *)inS,(inLen+1)*sizeof(wchar_t)-1);\n         length = inLen;\n      }\n      else\n      {\n         __s = inS;\n         length = inLen;\n      }\n   }\n   inline HxWString() : length(0), __s(0) { }\n\n   inline int size() const { return length; }\n   inline const wchar_t *c_str() const { return __s; }\n\n\n   int length;\n   const wchar_t *__s;\n};\n\ninline std::wstring valToStdWString(value inVal)\n{\n   return val_wstring(inVal);\n}\n\n\n#endif\n\n\n#include <nme/Object.h>\n\n\n\ninline HxString valToHxString(value inValue,bool inAllowNull=true)\n{\n   if (val_is_null(inValue))\n   {\n      if (inAllowNull)\n         return HxString();\n      return HxString("");\n   }\n#ifdef HXCPP_JS_PRIME\n   return inValue.as<std::string>();\n#else\n   return HxString( val_string(inValue), val_strlen(inValue), false );\n#endif\n}\n\ninline std::string valToStdString(value inValue,bool inAllowNull=true)\n{\n#ifdef HXCPP_JS_PRIME\n   return inValue.as<std::string>();\n#else\n   const char *str = val_string(inValue);\n   if (!str)\n      return inAllowNull ? std::string() : std::string("");\n\n   return std::string( str, str+val_strlen(inValue));\n#endif\n}\n\n\n#ifdef HXCPP_JS_PRIME\ninline const std::string &hxToStdString(const HxString &inValue) { return inValue; }\n#else\ninline std::string hxToStdString(const HxString &inValue)\n{\n   return std::string( inValue.__s, inValue.__s+inValue.length );\n}\n#endif\n\ninline HxWString valToHxWString(value inValue,bool inAllowNull=true)\n{\n   if (val_is_null(inValue))\n   {\n      if (inAllowNull)\n         return HxWString();\n      return HxWString(L"");\n   }\n#ifdef HXCPP_JS_PRIME\n   return inValue.as<std::wstring>();\n#else\n   //return HxWString( val_wstring(inValue), val_wstrlen(inValue), false );\n   return HxWString( val_wstring(inValue), -1, false );\n#endif\n}\n\nnamespace nme\n{\n\n// In nme namespace\nstruct CffiBytes\n{\n   #ifndef HXCPP_JS_PRIME\n\n   unsigned char *data;\n   int length;\n   CffiBytes( unsigned char *inData=0, int inLength=0) : data(inData), length(inLength) {}\n\n   int getLength() { return length; };\n   unsigned char *getData() { return data; };\n   const unsigned char *readData() { return data; };\n\n   #else\n   protected:\n      unsigned char *data;\n      int length;\n      value bound;\n\n   public:\n\n   CffiBytes() : bound( value::null() ), data(0), length(0) {}\n   CffiBytes(value inValue) : bound(inValue)\n   {\n      if (bound.isNull())\n      {\n         data = 0;\n         length = 0;\n      }\n      else\n      {\n         bound.call<void>("bind");\n         data = (unsigned char *)bound["byteOffset"].as<int>();\n         length = bound["byteLength"].as<int>();\n      }\n   }\n   CffiBytes(CffiBytes&& v) : bound(v.bound), data(v.data), length(v.length)\n   {\n      v.bound = value::null();\n   }\n\n   CffiBytes(const CffiBytes& v) : bound(v.bound)\n   {\n      if (!bound.isNull())\n         bound.call<void>("bind");\n      data = v.data;\n      length = v.length;\n   }\n   CffiBytes& operator=(CffiBytes&& v)\n   {\n      if (!bound.isNull())\n         bound.call<void>("unbind");\n      bound = v.bound;\n      v.bound = value::null();\n      return *this;\n   }\n\n   CffiBytes& operator=(const CffiBytes& v)\n   {\n      if (!v.bound.isNull())\n         bound.call<void>("bind");\n      if (!bound.isNull())\n         bound.call<void>("unbind");\n      bound = v.bound;\n      return *this;\n   }\n\n   ~CffiBytes()\n   {\n      if (!bound.isNull())\n      {\n         bound.call<void>("unbind");\n      }\n   }\n\n   int getLength() { return length; };\n   unsigned char *getData() {\n      if (data)\n         bound.call<void>("setEdited");\n      return data;\n   };\n   const unsigned char *readData() { return data; };\n\n   #endif\n};\n\ninline CffiBytes getByteData(value inValue)\n{\n   #ifndef HXCPP_JS_PRIME\n   if (val_is_object(inValue))\n   {\n      static field bField = 0;\n      static field lengthField = 0;\n      if (bField==0)\n      {\n         bField = val_id("b");\n         lengthField = val_id("length");\n      }\n      value b = val_field(inValue, bField);\n      value len = val_field(inValue, lengthField);\n      if (val_is_string(b) && val_is_int(len))\n         return CffiBytes( (unsigned char *)val_string(b), val_int(len) );\n      if (val_is_buffer(b) && val_is_int(len))\n         return CffiBytes( (unsigned char *)buffer_data(val_to_buffer(b)), val_int(len) );\n   }\n   #else\n   if (!inValue.isNull())\n   {\n      value b = inValue["b"];\n      if (!b.isUndefined())\n         return CffiBytes(b);\n   }\n   #endif\n   return CffiBytes();\n}\n\ninline bool resizeByteData(value inValue, int inNewLen)\n{\n   #ifndef HXCPP_JS_PRIME\n   if (!val_is_object(inValue))\n      return false;\n\n   static field bField = 0;\n   static field lengthField = 0;\n   if (bField==0)\n   {\n      bField = val_id("b");\n      lengthField = val_id("length");\n   }\n   value len = val_field(inValue, lengthField);\n   if (!val_is_int(len))\n      return false;\n   int oldLen = val_int(len);\n   value b = val_field(inValue, bField);\n   if (val_is_string(b))\n   {\n      if (inNewLen>oldLen)\n      {\n         value newString = alloc_raw_string(inNewLen);\n         memcpy( (char *)val_string(newString), val_string(b), inNewLen);\n         alloc_field(inValue, bField, newString );\n      }\n      alloc_field(inValue, lengthField, alloc_int(inNewLen) );\n   }\n   return true;\n   #else\n   if (!inValue.isNull())\n   {\n      value b = inValue["b"];\n      if (!b.isUndefined())\n      {\n         b.call<void>("resizeByteData",inNewLen);\n         return true;\n      }\n   }\n   return false;\n   #endif\n}\n\n\n\n\n\n\nextern vkind gObjectKind;\n\n#ifdef HXCPP_JS_PRIME\ninline value ObjectToAbstract(Object *inObject) { return inObject->toAbstract(); }\n\ntemplate<typename OBJ>\ninline bool AbstractToObject(value inValue, OBJ *&outObj)\n{\n   outObj = 0;\n   Object *obj = Object::toObject(inValue);\n   if (obj)\n      outObj = dynamic_cast<OBJ *>(obj);\n   return outObj;\n}\n\nstruct BufferData : Object\n{\n   BufferData();\n\n   NmeObjectType getObjectType() { return notBytes; }\n\n   unsigned char *getData() { return data; }\n   int getDataSize() { return allocLen; }\n\n   void setDataSize(int inSize, bool keepData);\n   void swapData(std::vector<unsigned char > &ioData);\n   void verify(const char *inWhere);\n\n   static BufferData *fromStream(class ObjectStreamIn &inStream);\n   void encodeStream(class ObjectStreamOut &inStream);\n\n   static int totalSize;\n\n   private:\n      unsigned char *data;\n      int allocLen;\n\n      BufferData(const BufferData &);\n      void operator = (const BufferData &);\n   protected:\n      ~BufferData();\n};\ntypedef BufferData *buffer;\n\ninline buffer val_to_buffer(value bytes)\n{\n   buffer result = 0;\n   AbstractToObject(bytes,result);\n   return result;\n}\ninline unsigned char *buffer_data(buffer inBuffer)\n{\n   return inBuffer ? inBuffer->getData() : 0;\n}\ninline int buffer_size(buffer inBuffer) { return inBuffer ? inBuffer->getDataSize() : 0; }\n\n\n#else\n\nnamespace\n{\n   inline void release_object(value inValue)\n   {\n      if (val_is_kind(inValue,gObjectKind))\n      {\n         Object *obj = (Object *)val_to_kind(inValue,gObjectKind);\n         if (obj)\n            obj->DecRef();\n      }\n   }\n}\n\ninline value ObjectToAbstract(Object *inObject)\n{\n   inObject->IncRef();\n   value result = alloc_abstract(gObjectKind,inObject);\n   val_gc(result,release_object);\n   return result;\n}\n\n\ntemplate<typename OBJ>\nbool AbstractToObject(value inValue, OBJ *&outObj)\n{\n   outObj = 0;\n   if ( ! val_is_kind(inValue,gObjectKind) )\n      return false;\n   Object *obj = (Object *)val_to_kind(inValue,gObjectKind);\n   outObj = dynamic_cast<OBJ *>(obj);\n   return outObj;\n}\n#endif\n\n\n}\n\n\n#endif\n', 'message': 'Use anon-namespace to avoid multiple symbol warnings.\n', 'author_name': 'Hugh', 'lang': 'h'}