Commits

nitrobin  committed f63945a

Version 0.4.0 (Drop haxe 2.10 support. Cleanup for haxe 3.1.)

  • Participants
  • Parent commits fc249d6

Comments (0)

Files changed (13)

 This is partial port of ActionScript3 protoc plugin "protoc-gen-as3" for haxe.
 Currently implemented only basic functionality: read/write messages from/to haxe.io.Bytes.
  
-haxe 3 and haxe 2.10 compatible. Core functionality doesn't depend on third-party libraries.
+haxe 3 compatible. Core functionality doesn't depend on third-party libraries.
 
 ## WARNING
 Project is still in alpha. Be careful for production using.

File haxelib.json

     "license": "BSD",
     "tags": [ "protobuf", "serialization", "cross", "neko", "flash", "js", "cpp", "java", "php" ],
     "description": "Crossplatform implementation of 'Google's Protocol Buffers' for haxe.",
-    "version": "0.3.3",
-    "releasenote": "Haxe 3 RC2 support.",
+    "version": "0.4.0",
+    "releasenote": "Drop haxe 2.10 support. Cleanup for haxe 3.1.",
     "contributors": [ "nitrobin" ],
     "dependencies": {}
 }

File protohx/Message.hx

      * number of bytes being read.</p>
      */
     public function mergeFrom(input:Bytes):Void {
-        readFromSlice(new ReadingBuffer(input), 0);
+        readFromSlice(ReadingBuffer.fromBytes(input), 0);
     }
     /**
      * Like <code>mergeFrom()</code>, but does not read until EOF. Instead,
      * @see #writeDelimitedTo()
      */
     public function mergeDelimitedFrom(input:Bytes):Void {
-        ReadUtils.read__TYPE_MESSAGE(new ReadingBuffer(input), this);
+        ReadUtils.read__TYPE_MESSAGE(ReadingBuffer.fromBytes(input), this);
     }
     /**
      * Serializes the message and writes it to <code>output</code>.

File protohx/Protohx.hx

 
 
 typedef PT_Int = Int;
-#if flash
+
+#if PROTOHX_UINT
 typedef PT_UInt = UInt;
 #else
 typedef PT_UInt = Int;
 typedef PT_ArgumentError = protohx.CommonError;
 typedef PT_IllegalOperationError = protohx.CommonError;
 
-#if haxe3
 typedef IntMap<T> = haxe.ds.IntMap<T>;
-#else
-typedef IntMap<T> = IntHash<T>;
-#end
 
 class Protohx {
 
-#if haxe3
-
     public static inline function getLow(i:haxe.Int64):PT_Int {
         return  cast(haxe.Int64.getLow(i), PT_Int);
     }
     }
 
     public static function newInt64(h:PT_Int, l:PT_Int):PT_Int64 {
-        // workaround bug https://code.google.com/p/haxe/issues/detail?id=1674
-        #if java
-        var hh = haxe.Int64.make(h, 0);
-        var ll = haxe.Int64.make(l, 0);
-        return cast haxe.Int64.or(hh, haxe.Int64.ushr(ll, 32));
-        #else
         return PT_Int64.make(h, l);
-        #end
     }
 
     public static function newUInt64(h:PT_Int, l:PT_Int):PT_UInt64 {
-        // workaround bug https://code.google.com/p/haxe/issues/detail?id=1674
-        #if java
-        var hh = haxe.Int64.make(h, 0);
-        var ll = haxe.Int64.make(l, 0);
-        return cast haxe.Int64.or(hh, haxe.Int64.ushr(ll, 32));
-        #else
         return PT_UInt64.make(h, l);
-        #end
     }
 
-#else
-
-    public static inline function getLow(i:haxe.Int64):PT_Int {
-        return  cast(haxe.Int32.toInt(haxe.Int64.getLow(i)), PT_Int);
-    }
-
-    public static inline function getHigh(i:haxe.Int64):PT_Int {
-        return  cast(haxe.Int32.toInt(haxe.Int64.getHigh(i)), PT_Int);
-    }
-
-    public static function newInt64(h:PT_Int, l:PT_Int):PT_Int64 {
-        return PT_Int64.make(haxe.Int32.ofInt(h), haxe.Int32.ofInt(l));
-    }
-
-    public static function newUInt64(h:PT_Int, l:PT_Int):PT_UInt64 {
-        return PT_UInt64.make(haxe.Int32.ofInt(h), haxe.Int32.ofInt(l));
-    }
-
-#end
-
     public static function setOutputEndian(out:haxe.io.Output):Void {
         out.bigEndian = false;
     }

File protohx/ReadUtils.hx

     public static function read__TYPE_FIXED64(input:PT_InputStream):PT_UInt64 {
         var low = input.readInt32();
         var high = input.readInt32();
-#if haxe3
         return Protohx.newUInt64(high, low);
-#else
-        return Protohx.newUInt64(haxe.Int32.toInt(high), haxe.Int32.toInt(low));
-#end
     }
     public static function read__TYPE_FIXED32(input:PT_InputStream):PT_Int {
-#if haxe3
         return input.readInt32();
-#else
-        return haxe.Int32.toInt(input.readInt32());
-#end
     }
     public static function read__TYPE_BOOL(input:PT_InputStream):PT_Bool {
         return read__TYPE_UINT32(input) != 0;
         return read__TYPE_INT32(input);
     }
     public static function read__TYPE_SFIXED32(input:PT_InputStream):PT_Int {
-#if haxe3
         return input.readInt32();
-#else
-        return haxe.Int32.toInt(input.readInt32());
-#end
     }
     public static function read__TYPE_SFIXED64(input:PT_InputStream):PT_Int64 {
         var low = input.readInt32();
         var high = input.readInt32();
-#if haxe3
         return Protohx.newInt64(high, low);
-#else
-        return Protohx.newInt64(haxe.Int32.toInt(high), haxe.Int32.toInt(low));
-#end
     }
     public static function read__TYPE_SINT32(input:PT_InputStream):PT_Int {
         return ZigZag.decode32(read__TYPE_UINT32(input));

File protohx/ReadingBuffer.hx

 import haxe.io.BytesInput;
 
 class ReadingBuffer {
-    public var length (default, null):Int;
-    public var bytesAvailable (default, null):Int;
+    public var length (get, null):Int;
+    public var bytesAvailable (get, null):Int;
 
     private var buf:BytesInput;
 
-    public function new(bytes:Bytes) {
-        this.length = bytes.length;
-        this.bytesAvailable = length;
-        this.buf = new BytesInput(bytes);
+    public function new(buf:BytesInput) {
+        this.buf = buf;
         Protohx.setInputEndian(this.buf);
     }
 
-    public function readBytes(len:Int):Bytes {
+    public inline static function fromBytes(bytes:Bytes, ?pos : Int, ?len : Int) {
+        return new ReadingBuffer(new BytesInput(bytes, pos, len));
+    }
+
+    public inline function get_length():Int {
+        return buf.length;
+    }
+
+    public inline function get_bytesAvailable():Int {
+        return buf.length - buf.position;
+    }
+
+    public inline function readBytes(len:Int):Bytes {
         var b = Bytes.alloc(len);
         buf.readBytes(b, 0, len);
-        bytesAvailable -= len;
         return b;
     }
 
-    public function readUTFBytes(len:Int):String {
-        bytesAvailable -= len;
+    public inline function readUTFBytes(len:Int):String {
         return buf.readString(len);
     }
 
-    public function readInt32() {
-        bytesAvailable -= 4;
+    public inline function readInt32() {
         return buf.readInt32();
     }
 
 //        return buf.readInt32();
 //    }
 
-    public function readUnsignedByte() {
-        bytesAvailable -= 1;
+    public inline function readUnsignedByte() {
         return buf.readByte();
     }
 //    public function readByte() {
 //        bytesAvailable -= 1;
 //        return buf.readByte();
 //    }
-    public function readDouble() {
-        bytesAvailable -= 8;
+    public inline function readDouble() {
         return buf.readDouble();
     }
-    public function readFloat() {
-        bytesAvailable -= 4;
+    public inline function readFloat() {
         return buf.readFloat();
     }
 }

File protohx/WritingBuffer.hx

     }
 
     public function writeInt32(v:Int):Void {
-#if haxe3
         buf.writeInt32(v);
-#else
-        buf.writeInt32(haxe.Int32.ofInt(v));
-#end
         position += 4;
     }
 

File samples/01-core/proto/test.proto

     required MsgType typeOpt  = 10;
 
     optional string msgOpt    = 11;
-     repeated sint32  rnds      = 12;
 }
 
 
     optional sint64 sui64 = 6;
     optional float f = 7;
     optional double d = 8;
+    repeated sint32  rnds      = 9;
 }
 
 message Foo {

File samples/01-core/src/Main.hx

             forInt(1 << i);
         }
     }
+
+    public function testRepeat() {
+        var rnds:Array<PT_Int> = [  ];
+        for( x in 0...100 ) rnds.push(x);
+
+        var obj = new IntTestMessage();
+        obj.rnds = rnds;
+        var copy = copyMsg(obj);
+        for (i in 0...rnds.length) {
+            assertEquals(obj.rnds[i], rnds[i]);
+            assertEquals(copy.rnds[i], rnds[i]);
+        }
+    }
 }
 
 class TestInt64 extends TestBase {

File tools/plugin/bin/plugin.bat

-@cd %~dp0
-@java -jar plugin.jar
+@cd %~dp0
+@java -jar plugin.jar

File tools/plugin/bin/plugin.jar

Binary file modified.

File tools/plugin/plugin.iml

+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+      <sourceFolder url="file://$MODULE_DIR$/src-gen" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="module-library" exported="">
+      <library>
+        <CLASSES>
+          <root url="jar://$MODULE_DIR$/libs/protobuf-java-2.5.0.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+  </component>
+</module>
+

File tools/plugin/src/protohx/Proto2Haxe.java

                         case TYPE_SFIXED64:
                         case TYPE_SINT64:
                         case TYPE_ENUM:
-                            content.append("\t\t\t\tif ((tag & 7) == protohx.WireType.LENGTH_DELIMITED) {\n");
+                            content.append("\t\t\t\tif (cast((tag & 7), PT_UInt) == protohx.WireType.LENGTH_DELIMITED) {\n");
                             content.append("\t\t\t\t\tprotohx.ReadUtils.readPackedRepeated(input, protohx.ReadUtils.read__" + haxeWireType + ", this." + lowerCamelCaseField + ");\n");
                             content.append("\t\t\t\t} else ");
                     }
         if (repeated) {
             haxeType = "Array<" + haxeType + ">";
         }
-        return "\t#if haxe3\n" +
-                "\t@:isVar public var " + lowerCamelCaseField + "(get, set):" + haxeType + ";\n" +
-                "\t#else\n" +
-                "\tpublic var " + lowerCamelCaseField + "(get_" + lowerCamelCaseField + ", set_" + lowerCamelCaseField + "):" + haxeType + ";\n" +
-                "\t#end\n\n";
+        return "\t@:isVar public var " + lowerCamelCaseField + "(get, set):" + haxeType + ";\n";
     }
 
     private static String varHelperSetter(String lowerCamelCaseField, String upperCamelCaseField, String haxeType, boolean repeated, String className) {