Skip to content
Open
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
36 changes: 36 additions & 0 deletions std/cpp/_std/haxe/ds/WeakRef.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C)2005-2019 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 haxe.ds;

@:coreApi
class WeakRef<T:{}> {
var h:Dynamic;

public function new(target:T) {
h = untyped __global__.__hxcpp_weak_ref_create(target);
}

public function get():Null<T> {
return untyped __global__.__hxcpp_weak_ref_get(h);
}
}
45 changes: 45 additions & 0 deletions std/haxe/ds/WeakRef.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (C)2005-2019 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 haxe.ds;

/**
A weak reference to an object. The reference does not prevent the
target from being collected by the garbage collector.

If the target has been collected, `get()` returns `null`.
**/
class WeakRef<T:{}> {
/**
Creates a new weak reference to `target`.
**/
public function new(target:T) {
throw new haxe.exceptions.NotImplementedException("Not implemented for this platform");
}

/**
Returns the referenced object, or `null` if it has been collected.
**/
public function get():Null<T> {
return null;
}
}
76 changes: 76 additions & 0 deletions std/js/_std/haxe/ds/WeakMap.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C)2005-2019 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 haxe.ds;

@:coreApi(check = Off)
class WeakMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
var h:js.lib.WeakMap<V>;

public inline function new():Void {
h = new js.lib.WeakMap();
}

public inline function set(key:K, value:V):Void {
h.set(cast key, value);
}

public inline function get(key:K):Null<V> {
return h.get(cast key);
}

public inline function exists(key:K):Bool {
return h.has(cast key);
}

public inline function remove(key:K):Bool {
return h.delete(cast key);
}

public function keys():Iterator<K> {
throw new haxe.exceptions.NotImplementedException("JS WeakMaps do not support enumeration");
}

public function iterator():Iterator<V> {
throw new haxe.exceptions.NotImplementedException("JS WeakMaps do not support enumeration");
}

public inline function keyValueIterator():KeyValueIterator<K, V> {
throw new haxe.exceptions.NotImplementedException("JS WeakMaps do not support enumeration");
}

public function copy():WeakMap<K, V> {
throw new haxe.exceptions.NotImplementedException("JS WeakMaps do not support enumeration");
}

public function toString():String {
throw new haxe.exceptions.NotImplementedException("JS WeakMaps do not support enumeration");
}

public inline function clear():Void {
h = new js.lib.WeakMap();
}

public function size():Int {
throw new haxe.exceptions.NotImplementedException("JS WeakMaps do not support enumeration");
}
}
36 changes: 36 additions & 0 deletions std/js/_std/haxe/ds/WeakRef.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (C)2005-2019 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 haxe.ds;

@:coreApi
class WeakRef<T:{}> {
var h:js.lib.WeakRef<T>;

public inline function new(target:T) {
h = new js.lib.WeakRef(target);
}

public inline function get():Null<T> {
return h.deref();
}
}
18 changes: 12 additions & 6 deletions std/jvm/_std/haxe/ds/WeakMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ import java.lang.ref.ReferenceQueue;
cachedIndex = -1;
#end
queue = new ReferenceQueue();
nBuckets = 4;
hashes = new NativeArray(4);
entries = new NativeArray(4);
_size = 0;
nOccupied = 0;
upperBound = Std.int(4 * HASH_UPPER + .5);
}

@:analyzer(ignore)
Expand Down Expand Up @@ -418,13 +424,13 @@ import java.lang.ref.ReferenceQueue;
}

public function clear():Void {
hashes = null;
entries = null;
queue = new ReferenceQueue();
nBuckets = 0;
nBuckets = 4;
hashes = new NativeArray(4);
entries = new NativeArray(4);
_size = 0;
nOccupied = 0;
upperBound = 0;
upperBound = Std.int(4 * HASH_UPPER + .5);
#if !no_map_cache
cachedEntry = null;
cachedIndex = -1;
Expand Down Expand Up @@ -465,7 +471,7 @@ import java.lang.ref.ReferenceQueue;

// guarantee: Whatever this function is, it will never return 0 nor 1
extern private static inline function hash(s:Dynamic):HashType {
var k:Int = untyped s.hashCode();
var k:Int = (cast s : java.lang.Object).hashCode();
// k *= 357913941;
// k ^= k << 24;
// k += ~357913941;
Expand Down Expand Up @@ -512,7 +518,7 @@ private class Entry<K, V> extends WeakReference<K> {
}

final inline public function keyEquals(k:K):Bool {
return k != null && untyped k.equals(get());
return k != null && (cast k : java.lang.Object).equals(get());
}
}

Expand Down
38 changes: 38 additions & 0 deletions std/jvm/_std/haxe/ds/WeakRef.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C)2005-2019 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 haxe.ds;

import java.lang.ref.WeakReference;

@:coreApi
class WeakRef<T:{}> {
var h:WeakReference<T>;

public function new(target:T) {
h = new WeakReference(target);
}

public function get():Null<T> {
return h.get();
}
}
108 changes: 108 additions & 0 deletions std/lua/_std/haxe/ds/WeakMap.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (C)2005-2019 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 haxe.ds;

@:coreApi(check = Off)
class WeakMap<K:{}, V> implements haxe.Constraints.IMap<K, V> {
var h:Dynamic;

public inline function new():Void {
h = lua.Syntax.code("setmetatable({}, {__mode = 'k'})");
}

public inline function set(key:K, value:V):Void {
untyped h[key] = value;
}

public inline function get(key:K):Null<V> {
return untyped h[key];
}

public inline function exists(key:K):Bool {
return untyped h[key] != null;
}

public function remove(key:K):Bool {
untyped {
if (h[key] == null)
return false;
h[key] = null;
return true;
}
}

public function keys():Iterator<K>
untyped {
var cur = next(h, null);
return {
next: function() {
var ret = cur;
cur = untyped next(h, cur);
return ret;
},
hasNext: function() return cur != null
}
}

public function iterator():Iterator<V> {
var itr = keys();
return untyped {
hasNext: itr.hasNext,
next: function() return h[itr.next()]
};
}

@:runtime public inline function keyValueIterator():KeyValueIterator<K, V> {
return new haxe.iterators.MapKeyValueIterator(this);
}

public function copy():WeakMap<K, V> {
var copied = new WeakMap();
for (key in keys())
copied.set(key, get(key));
return copied;
}

public function toString():String {
var s = new StringBuf();
s.add("[");
var it = keys();
for (i in it) {
s.add(Std.string(i));
s.add(" => ");
s.add(Std.string(get(i)));
if (it.hasNext())
s.add(", ");
}
s.add("]");
return s.toString();
}

public inline function clear():Void {
h = lua.Syntax.code("setmetatable({}, {__mode = 'k'})");
}

public function size():Int {
return lua.Syntax.code("(function() local s = 0; for _ in pairs({0}) do s = s + 1 end; return s end)()", h);
}
}
Loading
Loading