flex-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ha...@apache.org
Subject [05/12] git commit: [flex-asjs] [refs/heads/develop] - [Consistency] Set default Endian to BIG ENDIAN in javascript to be consistent with flash [Performance] optimization of int/unit/short read/writes to avoid using DataView, substantial gains
Date Wed, 03 Aug 2016 07:40:42 GMT
[Consistency] Set default Endian to BIG ENDIAN in javascript to be consistent with flash
[Performance] optimization of int/unit/short read/writes to avoid using DataView, substantial
gains


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/6288c2dd
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/6288c2dd
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/6288c2dd

Branch: refs/heads/develop
Commit: 6288c2ddff4872464faa9bc6ce327e77730a47e1
Parents: d12bcdd
Author: greg-dove <greg.dove@gmail.com>
Authored: Mon Jul 25 15:20:33 2016 +1200
Committer: greg-dove <greg.dove@gmail.com>
Committed: Mon Jul 25 15:20:33 2016 +1200

----------------------------------------------------------------------
 .../flex/org/apache/flex/utils/BinaryData.as    | 151 +++++++------------
 .../main/flex/org/apache/flex/utils/Endian.as   |  29 +---
 2 files changed, 61 insertions(+), 119 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/6288c2dd/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as
index 942b394..769b006 100644
--- a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as
+++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/BinaryData.as
@@ -112,7 +112,10 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         return getTypedArray();
     }
     COMPILE::JS
-    private var _endian:String = Endian.defaultEndian;
+    private var _endian:String = Endian.BIG_ENDIAN;
+
+    COMPILE::JS
+    private var _sysEndian:Boolean = _endian == Endian.systemEndian;
 
     /**
      *  Indicates the byte order for the data.
@@ -142,6 +145,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         if (value == Endian.BIG_ENDIAN || Endian.LITTLE_ENDIAN) {
             COMPILE::JS {
                 _endian = value;
+                _sysEndian = value == Endian.systemEndian;
             }
             COMPILE::SWF {
                 ba.endian = value;
@@ -267,19 +271,11 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
         COMPILE::JS
         {
-            var view:Int16Array;
-
-            ensureWritableBytes(2);
-            if(_endian == Endian.defaultEndian)
-            {
-                view = new Int16Array(ba, _position, 1);
-                view[0] = short;
-            }
-            else
-            {
-                var dv:DataView = new DataView(ba);
-                dv.setInt16(_position,short,_endian == Endian.LITTLE_ENDIAN);
+            if (!_sysEndian) {
+                short = (((short & 0xff00) >>> 8) | ((short & 0xff) <<8
));
             }
+            ensureWritableBytes(2);
+            new Int16Array(ba, _position, 1)[0] = short;
             _position += 2;
         }
     }
@@ -300,19 +296,11 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
         COMPILE::JS
         {
-            var view:Uint32Array;
-
-            ensureWritableBytes(4);
-            if(_endian == Endian.defaultEndian)
-            {
-                view = new Uint32Array(ba, _position, 1);
-                view[0] = unsigned;
-            }
-            else
-            {
-                var dv:DataView = new DataView(ba);
-                dv.setUint32(_position,unsigned,_endian == Endian.LITTLE_ENDIAN);
+            if (!_sysEndian) {
+                unsigned = ((unsigned & 0xff000000) >>> 24) | ((unsigned &
0x00ff0000) >> 8) | ((unsigned & 0x0000ff00) << 8) | (unsigned << 24);
             }
+            ensureWritableBytes(4);
+            new Uint32Array(ba, _position, 1)[0] = unsigned;
             _position += 4;
         }
     }
@@ -325,28 +313,19 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
      *  @playerversion AIR 2.6
      *  @productversion FlexJS 0.0
      */
-    public function writeInt(integer:int):void
+    public function writeInt(val:int):void
     {
         COMPILE::SWF
         {
-            ba.writeInt(integer);
+            ba.writeInt(val);
         }
         COMPILE::JS
         {
-            var view:Int32Array;
-
-            ensureWritableBytes(4);
-
-            if(_endian == Endian.defaultEndian)
-            {
-                view = new Int32Array(ba, _position, 1);
-                view[0] = integer;
-            }
-            else
-            {
-                var dv:DataView = new DataView(ba);
-                dv.setInt32(_position,integer,_endian == Endian.LITTLE_ENDIAN);
+            if (!_sysEndian) {
+                val = (((val & 0xff000000) >>> 24) | ((val & 0x00ff0000)
>> 8) | ((val & 0x0000ff00) << 8) | (val << 24)) >> 0;
             }
+            ensureWritableBytes(4);
+            new Int32Array(ba, _position, 1)[0] = val;
             _position += 4;
         }
     }
@@ -370,7 +349,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
 
             ensureWritableBytes(4);
 
-            if(_endian == Endian.defaultEndian)
+            if(_sysEndian)
             {
                 view = new Float32Array(ba, _position, 1);
                 view[0] = value;
@@ -402,7 +381,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
 
             ensureWritableBytes(8);
 
-            if(_endian == Endian.defaultEndian)
+            if(_sysEndian)
             {
                 view = new Float64Array(ba, _position, 1);
                 view[0] = value;
@@ -453,10 +432,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
         COMPILE::JS
         {
-
-            var view:Int8Array;
-
-            view = new Int8Array(ba, _position, 1);
+            var view:Int8Array = new Int8Array(ba, _position, 1);
             _position++;
             return view[0];
         }
@@ -476,9 +452,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
         COMPILE::JS
         {
-            var view:Uint8Array;
-
-            view = new Uint8Array(ba, _position, 1);
+            var view:Uint8Array = new Uint8Array(ba, _position, 1);
             _position++;
             return view[0];
         }
@@ -595,19 +569,13 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
         COMPILE::JS
         {
-            var view:Int16Array;
-
-            if(_endian == Endian.defaultEndian)
-            {
-                view = new Int16Array(ba, _position, 1);
-                _position += 2;
-                return view[0];
+            var ret:int = new Int16Array(ba, _position, 1)[0];
+            if (!_sysEndian) {
+                //special case conversion for short int return value to 32 bit int
+                ret = ((((ret & 0xff00) >> 8) | ((ret & 0xff) << 8))
<< 16) >> 16;
             }
-
-            var dv:DataView = new DataView(ba);
-            var i:int = dv.getInt16(_position,_endian == Endian.LITTLE_ENDIAN);
             _position += 2;
-            return i;
+            return ret;
         }
     }
 
@@ -627,18 +595,12 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
         COMPILE::JS
         {
-            var view:Uint32Array;
-
-            if(_endian == Endian.defaultEndian)
-            {
-                view = new Uint32Array(ba, _position, 1);
-                _position += 4;
-                return view[0];
+            var ret:uint = new Uint32Array(ba, _position, 1)[0];
+            if (!_sysEndian) {
+                ret = (((ret & 0xff000000) >>> 24) | ((ret & 0x00ff0000)
>>> 8) | ((ret & 0x0000ff00) << 8) | (ret << 24)) >>> 0;
             }
-            var dv:DataView = new DataView(ba);
-            var i:uint = dv.getUint32(_position,_endian == Endian.LITTLE_ENDIAN);
             _position += 4;
-            return i;
+            return ret;
         }
     }
 
@@ -657,18 +619,13 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
         COMPILE::JS
         {
-            var view:Uint16Array;
 
-            if(_endian == Endian.defaultEndian)
-            {
-                view = new Uint16Array(ba, _position, 1);
-                _position += 2;
-                return view[0];
+            var ret:uint = new Uint16Array(ba, _position, 1)[0];
+            if (!_sysEndian) {
+                ret = ((ret & 0xff00) >> 8 ) | ((ret & 0xff) << 8);
             }
-            var dv:DataView = new DataView(ba);
-            var i:uint = dv.getUint16(_position,_endian == Endian.LITTLE_ENDIAN);
             _position += 2;
-            return i;
+            return ret;
         }
     }
 
@@ -688,19 +645,12 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
         COMPILE::JS
         {
-            var view:Int32Array;
-
-            if(_endian == Endian.defaultEndian)
-            {
-                view = new Int32Array(ba, _position, 1);
-                _position += 4;
-                return view[0];
+            var ret:int = new Int32Array(ba, _position, 1)[0];
+            if (!_sysEndian) {
+                ret = (((ret & 0xff000000) >>> 24) | ((ret & 0x00ff0000)
>>> 8) | ((ret & 0x0000ff00) << 8) | (ret << 24)) >> 0;
             }
-            var dv:DataView = new DataView(ba);
-            var i:uint = dv.getInt32(_position,_endian == Endian.LITTLE_ENDIAN);
             _position += 4;
-            return i;
-
+            return ret;
         }
     }
 
@@ -720,7 +670,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         COMPILE::JS {
             var view:Float32Array;
 
-            if(_endian == Endian.defaultEndian)
+            if(_sysEndian)
             {
                 view = new Float32Array(ba, _position, 1);
                 _position += 4;
@@ -750,7 +700,7 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         COMPILE::JS {
             var view:Float64Array;
 
-            if(_endian == Endian.defaultEndian)
+            if(_sysEndian)
             {
                 view = new Float64Array(ba, _position, 1);
                 _position += 8;
@@ -991,11 +941,12 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
         }
     }
 
-	
+
     /**
      *  Writes a UTF-8 string to the byte stream.
      *  The length of the UTF-8 string in bytes is written first, as a 16-bit integer,
      *  followed by the bytes representing the characters of the string.
+     *  If the byte length of the string is larger than 65535 this will throw a RangeError
      *
      *  @param The string value to be written.
      *
@@ -1108,13 +1059,17 @@ public class BinaryData implements IBinaryDataInput, IBinaryDataOutput
             bytes = new Uint8Array(out);
         }
         if (prependLength) {
+            var len:uint = bytes.length;
+            if (len > 0xffff) {
+                //throw error, similar to swf ByteArray behavior:
+                throw new RangeError("UTF max string length of 65535 bytes exceeded : BinaryData.writeUTF");
+            }
             var temp:Uint8Array = new Uint8Array(bytes.length + 2);
             temp.set(bytes , 2);
-            var len:uint = bytes.length;
-            //preconvert to alternate endian if needed
-            new Uint16Array(temp.buffer,0,2)[0] =
-                    (_endian == Endian.defaultEndian) ?
-                            len : ((len & 0xff) >> 8) | (len << 8);
+            //pre-convert to alternate endian if needed
+            new Uint16Array(temp.buffer,0,1)[0] =
+                    _sysEndian ? len : (((len & 0xff00) >> 8) | ((len & 0xff)
<< 8));
+
             bytes = temp;
         }
         return bytes;

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/6288c2dd/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/Endian.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/Endian.as b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/Endian.as
index 289d363..5d7a595 100644
--- a/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/Endian.as
+++ b/frameworks/projects/Core/src/main/flex/org/apache/flex/utils/Endian.as
@@ -48,25 +48,12 @@ package org.apache.flex.utils
 		 */
 		public static const LITTLE_ENDIAN:String = "littleEndian";
 
-		/**
-		 *  Indicates an unknown default endianness (when using BinaryData).
-		 *  You cannot use this value to set the endian value of a BinaryData
-		 *  It is used to check the defaultEndian value for an unexpected result
-		 *
-		 *
-		 *  @langversion 3.0
-		 *  @playerversion Flash 10.2
-		 *  @playerversion AIR 2.6
-		 *  @productversion FlexJS 0.7.0
-		 */
-		public static const UNKNOWN_ENDIAN:String = "unknownEndian";
-
-
 
 		/**
 		 *  Indicates the default endianness on the system.
 		 *  In swf targets this is always BIG_ENDIAN. When targeting
-		 *  javascript it may differ depending on the target environment.
+		 *  javascript it may differ depending on the target environment,
+		 *  but is Endian.LITTLE_ENDIAN for most machines/browsers.
 		 *
 		 *
 		 *  @langversion 3.0
@@ -74,27 +61,27 @@ package org.apache.flex.utils
 		 *  @playerversion AIR 2.6
 		 *  @productversion FlexJS 0.7.0
 		 */
-		public static function get defaultEndian():String {
+		public static function get systemEndian():String {
 			COMPILE::SWF {
 				return BIG_ENDIAN;
 			}
 			COMPILE::JS {
-				return _defaultEndian;
+				return _sysEndian;
 			}
 		}
 
 
 		COMPILE::JS
-		private static function _detectDefaultEndian():String{
-            delete Endian["_detectDefaultEndian"];
+		private static function _detectSystemEndian():String{
+            delete Endian["_detectSystemEndian"];
 			var tester:Uint8Array = new Uint8Array([102,108,101,120]);
 			var checker:Uint32Array = new Uint32Array(tester.buffer);
 			var check:uint = checker[0];
-			return (check == 1718379896) ? BIG_ENDIAN : (check == 2019912806) ? LITTLE_ENDIAN : UNKNOWN_ENDIAN;
+			return (check == 1718379896) ? BIG_ENDIAN : LITTLE_ENDIAN ;
 		}
 
 		COMPILE::JS
-		private static var _defaultEndian:String = _detectDefaultEndian();
+		private static var _sysEndian:String = _detectSystemEndian();
 
 	}
 }
\ No newline at end of file


Mime
View raw message