Permalink
Browse files

fixed memory leak and added neko support (Mac only)

  • Loading branch information...
1 parent 1ba6616 commit 16b77ed809123737dfa65233a9b1951c9b9b7b34 @andyli committed May 6, 2010
View
@@ -1,6 +1,11 @@
import hxSerial.Serial;
import haxe.io.BytesOutput;
+#if neko
+import neko.Sys;
+#elseif cpp
+import cpp.Sys;
+#end
class Test extends haxe.unit.TestCase{
public function test():Void {
@@ -9,19 +14,19 @@ class Test extends haxe.unit.TestCase{
var s = new Serial("/dev/tty.usbserial-A4001tkb",true);
this.assertTrue(s.isSetup);
- cpp.Sys.sleep(2);
+ Sys.sleep(2);
for (i in 0...2){
this.assertTrue(s.writeByte(65));
- cpp.Sys.sleep(1.5);
+ Sys.sleep(1.5);
this.assertEquals(2,s.available());
this.assertEquals(66,s.readByte());
this.assertEquals(67,s.readByte());
}
for (i in 0...2){
this.assertEquals(1,s.writeBytes("A"));
- cpp.Sys.sleep(1.5);
+ Sys.sleep(1.5);
this.assertEquals(2,s.available());
this.assertEquals("BC",s.readBytes(2));
}
View
@@ -1,7 +1,16 @@
+-cmd cd project
+-cmd haxelib run hxcpp build.xml
+-cmd cd ..
+#-cmd cp ndll/Windows/hxSerial.ndll bin/hxSerial.ndll
+-cmd cp ndll/Mac/hxSerial.ndll bin/hxSerial.ndll
+#-cmd cp ndll/Linux/hxSerial.ndll bin/hxSerial.ndll
+
-cpp bin
+#-neko bin/Test.n
-main Test
--lib hxSerial
+#-lib hxSerial
-cmd cd bin
#-cmd Test.exe
-cmd ./Test
+#-cmd neko Test
View
@@ -1,8 +1,19 @@
package hxSerial;
+#if neko
+import neko.Lib;
+#elseif cpp
+import cpp.Lib;
+#end
+
class Serial {
static public function getDeviceList():Array<String> {
- return cast(_enumerateDevices(),String).split('\n');
+ #if neko
+ var str:String = neko.NativeString.toString(_enumerateDevices());
+ #else
+ var str:String = _enumerateDevices();
+ #end
+ return str == null ? [] : str.split('\n');
}
public var portName(default,null):String;
@@ -31,7 +42,11 @@ class Serial {
}
public function readBytes(length:Int):String {
+ #if neko
+ return neko.NativeString.toString(_readBytes(handle,length));
+ #else
return _readBytes(handle,length);
+ #end
}
public function writeByte(byte:Int):Bool {
@@ -57,13 +72,13 @@ class Serial {
private var handle:Int;
- private static var _enumerateDevices = cpp.Lib.load("hxSerial","enumerateDevices",0);
- private static var _setup = cpp.Lib.load("hxSerial","setup",2);
- private static var _writeBytes = cpp.Lib.load("hxSerial","writeBytes",3);
- private static var _readBytes = cpp.Lib.load("hxSerial","readBytes",2);
- private static var _writeByte = cpp.Lib.load("hxSerial","writeByte",2);
- private static var _readByte = cpp.Lib.load("hxSerial","readByte",1);
- private static var _flush = cpp.Lib.load("hxSerial","flush",3);
- private static var _available = cpp.Lib.load("hxSerial","available",1);
- private static var _breakdown = cpp.Lib.load("hxSerial","breakdown",1);
+ private static var _enumerateDevices = Lib.load("hxSerial","enumerateDevices",0);
+ private static var _setup = Lib.load("hxSerial","setup",2);
+ private static var _writeBytes = Lib.load("hxSerial","writeBytes",3);
+ private static var _readBytes = Lib.load("hxSerial","readBytes",2);
+ private static var _writeByte = Lib.load("hxSerial","writeByte",2);
+ private static var _readByte = Lib.load("hxSerial","readByte",1);
+ private static var _flush = Lib.load("hxSerial","flush",3);
+ private static var _available = Lib.load("hxSerial","available",1);
+ private static var _breakdown = Lib.load("hxSerial","breakdown",1);
}
View
Binary file not shown.
View
Binary file not shown.
View
Binary file not shown.
Binary file not shown.
View
@@ -212,7 +212,9 @@ value enumerateDevices() {
if (strlen(str) > 0) str[strlen(str)-1] = '\0';
- return alloc_string(str);
+ value ret = alloc_string(str);
+ free(str);
+ return ret;
}
DEFINE_PRIM(enumerateDevices,0);
@@ -398,7 +400,9 @@ value readBytes(value a, value b) {
//---------------------------------------------
buffer[length] = '\0';
- return alloc_string(buffer);
+ value ret = alloc_string(buffer);
+ free(buffer);
+ return ret;
}
DEFINE_PRIM(readBytes,2);
@@ -428,7 +432,7 @@ DEFINE_PRIM(writeByte,2);
value readByte(value a) {
int fd = val_int(a);
- char * buffer = (char*) malloc(2);
+ char buffer[2];
//---------------------------------------------
#if defined( TARGET_OSX ) || defined( TARGET_LINUX )

0 comments on commit 16b77ed

Please sign in to comment.