Browse Source

[js] Use native maps when ES6 is enabled. (#11698)

* [js] Use native maps when ES6 is enabled.

* [js] Use square brackets in ES6 map toString.

* [js] make the ES6 map conform to the public API.

* [js] better key value iterator for ES6 maps

* [js] remove technically unrequired null check in HaxeKeyValueIterator for better inlining

---------

Co-authored-by: ALANVF <[email protected]>
Zeta 1 year ago
parent
commit
f283debce7

+ 63 - 0
std/js/_std/haxe/ds/IntMap.hx

@@ -22,6 +22,68 @@
 
 
 package haxe.ds;
 package haxe.ds;
 
 
+#if (js_es >= 6)
+@:coreApi class IntMap<T> implements haxe.Constraints.IMap<Int, T> {
+	private var m:js.lib.Map<Int, T>;
+
+	public inline function new():Void {
+		m = new js.lib.Map();
+	}
+
+	public inline function set(key:Int, value:T):Void {
+		m.set(key, value);
+	}
+
+	public inline function get(key:Int):Null<T> {
+		return m.get(key);
+	}
+
+	public inline function exists(key:Int):Bool {
+		return m.has(key);
+	}
+
+	public inline function remove(key:Int):Bool {
+		return m.delete(key);
+	}
+
+	public inline function keys():Iterator<Int> {
+		return new js.lib.HaxeIterator(m.keys());
+	}
+
+	public inline function iterator():Iterator<T> {
+		return m.iterator();
+	}
+
+	public inline function keyValueIterator():KeyValueIterator<Int, T> {
+		return m.keyValueIterator();
+	}
+
+	public inline function copy():IntMap<T> {
+		var copied = new IntMap();
+		copied.m = new js.lib.Map(m);
+		return copied;
+	}
+
+	public function toString():String {
+		var s = new StringBuf();
+		s.add("[");
+		var it = keyValueIterator();
+		for (i in it) {
+			s.add(i.key);
+			s.add(" => ");
+			s.add(Std.string(i.value));
+			if (it.hasNext())
+				s.add(", ");
+		}
+		s.add("]");
+		return s.toString();
+	}
+
+	public inline function clear():Void {
+		m.clear();
+	}
+}
+#else
 @:coreApi class IntMap<T> implements haxe.Constraints.IMap<Int, T> {
 @:coreApi class IntMap<T> implements haxe.Constraints.IMap<Int, T> {
 	private var h:Dynamic;
 	private var h:Dynamic;
 
 
@@ -98,3 +160,4 @@ package haxe.ds;
 		h = {};
 		h = {};
 	}
 	}
 }
 }
+#end

+ 63 - 0
std/js/_std/haxe/ds/ObjectMap.hx

@@ -25,9 +25,71 @@ package haxe.ds;
 import js.Syntax;
 import js.Syntax;
 import js.Lib;
 import js.Lib;
 
 
+#if (js_es >= 6)
 @:coreApi
 @:coreApi
 class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
 class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
+	private var m:js.lib.Map<K, V>;
 
 
+	public inline function new():Void {
+		m = new js.lib.Map();
+	}
+
+	public inline function set(key:K, value:V):Void {
+		m.set(key, value);
+	}
+
+	public inline function get(key:K):Null<V> {
+		return m.get(key);
+	}
+
+	public inline function exists(key:K):Bool {
+		return m.has(key);
+	}
+
+	public inline function remove(key:K):Bool {
+		return m.delete(key);
+	}
+
+	public inline function keys():Iterator<K> {
+		return new js.lib.HaxeIterator(m.keys());
+	}
+
+	public inline function iterator():Iterator<V> {
+		return m.iterator();
+	}
+
+	public inline function keyValueIterator():KeyValueIterator<K, V> {
+		return m.keyValueIterator();
+	}
+
+	public inline function copy():ObjectMap<K, V> {
+		var copied = new ObjectMap();
+		copied.m = new js.lib.Map(m);
+		return copied;
+	}
+
+	public function toString():String {
+		var s = new StringBuf();
+		s.add("[");
+		var it = keyValueIterator();
+		for (i in it) {
+			s.add(Std.string(i.key));
+			s.add(" => ");
+			s.add(Std.string(i.value));
+			if (it.hasNext())
+				s.add(", ");
+		}
+		s.add("]");
+		return s.toString();
+	}
+
+	public inline function clear():Void {
+		m.clear();
+	}
+}
+#else
+@:coreApi
+class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
 	static inline function assignId(obj:{}):Int {
 	static inline function assignId(obj:{}):Int {
 		return Syntax.code('({0}.__id__ = {1})', obj, Lib.getNextHaxeUID());
 		return Syntax.code('({0}.__id__ = {1})', obj, Lib.getNextHaxeUID());
 	}
 	}
@@ -123,3 +185,4 @@ class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
 		h = {__keys__: {}};
 		h = {__keys__: {}};
 	}
 	}
 }
 }
+#end

+ 62 - 1
std/js/_std/haxe/ds/StringMap.hx

@@ -26,7 +26,68 @@ import js.lib.Object;
 import haxe.Constraints.IMap;
 import haxe.Constraints.IMap;
 import haxe.DynamicAccess;
 import haxe.DynamicAccess;
 
 
-#if (js_es >= 5)
+#if (js_es >= 6)
+@:coreApi class StringMap<T> implements IMap<String, T> {
+	private var m:js.lib.Map<String, T>;
+
+	public inline function new():Void {
+		m = new js.lib.Map();
+	}
+
+	public inline function set(key:String, value:T):Void {
+		m.set(key, value);
+	}
+
+	public inline function get(key:String):Null<T> {
+		return m.get(key);
+	}
+
+	public inline function exists(key:String):Bool {
+		return m.has(key);
+	}
+
+	public inline function remove(key:String):Bool {
+		return m.delete(key);
+	}
+
+	public inline function keys():Iterator<String> {
+		return new js.lib.HaxeIterator(m.keys());
+	}
+
+	public inline function iterator():Iterator<T> {
+		return m.iterator();
+	}
+
+	public inline function keyValueIterator():KeyValueIterator<String, T> {
+		return m.keyValueIterator();
+	}
+
+	public inline function copy():StringMap<T> {
+		var copied = new StringMap();
+		copied.m = new js.lib.Map(m);
+		return copied;
+	}
+
+	public function toString():String {
+		var s = new StringBuf();
+		s.add("[");
+		var it = keyValueIterator();
+		for (i in it) {
+			s.add(i.key);
+			s.add(" => ");
+			s.add(Std.string(i.value));
+			if (it.hasNext())
+				s.add(", ");
+		}
+		s.add("]");
+		return s.toString();
+	}
+
+	public inline function clear():Void {
+		m.clear();
+	}
+}
+#elseif (js_es == 5)
 @:coreApi class StringMap<T> implements IMap<String, T> {
 @:coreApi class StringMap<T> implements IMap<String, T> {
 	var h:Dynamic;
 	var h:Dynamic;
 
 

+ 47 - 0
std/js/lib/HaxeKeyValueIterator.hx

@@ -0,0 +1,47 @@
+/*
+ * Copyright (C)2005-2024 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+package js.lib;
+
+class HaxeKeyValueIterator<K, V> {
+	final jsIterator:js.lib.Iterator<KeyValue<K, V>>;
+	var lastStep:js.lib.Iterator.IteratorStep<KeyValue<K, V>>;
+
+	public inline function new(jsIterator:js.lib.Iterator<KeyValue<K, V>>) {
+		this.jsIterator = jsIterator;
+		lastStep = jsIterator.next();
+	}
+
+	public inline function hasNext():Bool {
+		return !lastStep.done;
+	}
+
+	public inline function next():{key:K, value:V} {
+		var v = lastStep.value;
+		lastStep = jsIterator.next();
+		return {key: v.key, value: v.value};
+	}
+
+	public static inline function keyValueIterator<K, V>(jsIterator:js.lib.Iterator<KeyValue<K, V>>) {
+		return new HaxeKeyValueIterator(jsIterator);
+	}
+}

+ 2 - 2
std/js/lib/Map.hx

@@ -104,8 +104,8 @@ extern class Map<K, V> {
 		return new HaxeIterator(this.values());
 		return new HaxeIterator(this.values());
 	}
 	}
 
 
-	inline function keyValueIterator():HaxeIterator<KeyValue<K, V>> {
-		return new HaxeIterator(this.entries());
+	inline function keyValueIterator():HaxeKeyValueIterator<K, V> {
+		return new HaxeKeyValueIterator(this.entries());
 	}
 	}
 }
 }