Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions std/js/_std/haxe/ds/IntMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,68 @@

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> {
private var h:Dynamic;

Expand Down Expand Up @@ -98,3 +160,4 @@ package haxe.ds;
h = {};
}
}
#end
63 changes: 63 additions & 0 deletions std/js/_std/haxe/ds/ObjectMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,71 @@ package haxe.ds;
import js.Syntax;
import js.Lib;

#if (js_es >= 6)
@:coreApi
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 {
return Syntax.code('({0}.__id__ = {1})', obj, Lib.getNextHaxeUID());
}
Expand Down Expand Up @@ -123,3 +185,4 @@ class ObjectMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
h = {__keys__: {}};
}
}
#end
63 changes: 62 additions & 1 deletion std/js/_std/haxe/ds/StringMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,68 @@ import js.lib.Object;
import haxe.Constraints.IMap;
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> {
var h:Dynamic;

Expand Down
47 changes: 47 additions & 0 deletions std/js/lib/HaxeKeyValueIterator.hx
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 2 additions & 2 deletions std/js/lib/Map.hx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ extern class Map<K, V> {
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());
}
}

Expand Down
Loading