Skip to content
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
2 changes: 2 additions & 0 deletions extra/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
all : [std] allow setting haxe.Exception.stack (#12213)
all : [std] Serializer: implement reset method (#12068)
all : [std] use Vectors in haxe.zip (#11034)
all : [std] add haxe.IHandle interface (#12766)
all : [std] add haxe.GcFinalizer with handle-based API (#12766)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude left these in again...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding a section above that for next release would both make release process a bit easier and allow Claude to add changelog lines properly?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case we'll probably want to make it a requirement to put changelog entries with every PR to avoid having to filter through stuff at release time?

all : [messageReparting] pretty errors as default message reporting (#11587)
all : [messageReporting] add config to use absolute positions (#11439)
all : [display] diagnostics as json rpc (#11412)
Expand Down
91 changes: 91 additions & 0 deletions std/cpp/_std/haxe/GcFinalizer.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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;

import haxe.atomic.AtomicBool;

private class Registration<T> {
public var heldValue:Null<T>;
public var callback:Null<T->Void>;
public var cancelled:AtomicBool;
public var weakTarget:cpp.vm.WeakRef<Dynamic>;

public function new(target:Dynamic, heldValue:T, callback:T->Void) {
this.weakTarget = new cpp.vm.WeakRef<Dynamic>(target);
this.heldValue = heldValue;
this.callback = callback;
this.cancelled = new AtomicBool(false);
}
}

private class Handle<T> implements IHandle {
var reg:Registration<T>;

public function new(reg:Registration<T>) {
this.reg = reg;
}

public function close():Void {
if (reg.cancelled.compareExchange(false, true) == false) {
reg.callback = null;
reg.heldValue = null;
}
}
}

@:coreApi
class GcFinalizer<T> {
var callback:T->Void;
var registrations:Array<Registration<T>>;

public function new(callback:T->Void) {
this.callback = callback;
this.registrations = [];
}

function pollQueue():Void {
var i = registrations.length - 1;
while (i >= 0) {
var reg = registrations[i];
if (reg.weakTarget.get() == null) {
if (reg.cancelled.compareExchange(false, true) == false) {
reg.callback(reg.heldValue);
reg.callback = null;
reg.heldValue = null;
}
registrations.splice(i, 1);
} else if (reg.cancelled.load()) {
// close() already nulled fields
registrations.splice(i, 1);
}
i--;
}
}

public function register(target:{}, heldValue:T):IHandle {
pollQueue();
var reg = new Registration(target, heldValue, callback);
registrations.push(reg);
return new Handle(reg);
}
}
74 changes: 74 additions & 0 deletions std/eval/_std/haxe/GcFinalizer.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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;

import haxe.atomic.AtomicBool;

private class Registration<T> {
public var heldValue:Null<T>;
public var cancelled:AtomicBool;
public var callback:Null<T->Void>;

public function new(heldValue:T, callback:T->Void) {
this.heldValue = heldValue;
this.cancelled = new AtomicBool(false);
this.callback = callback;
}
}

private class Handle<T> implements IHandle {
var reg:Registration<T>;

public function new(reg:Registration<T>) {
this.reg = reg;
}

public function close():Void {
if (reg.cancelled.compareExchange(false, true) == false) {
reg.callback = null;
reg.heldValue = null;
}
}
}

@:coreApi
class GcFinalizer<T> {
var callback:T->Void;

public function new(callback:T->Void) {
this.callback = callback;
}

public function register(target:{}, heldValue:T):IHandle {
var reg = new Registration(heldValue, callback);
eval.vm.Gc.finalise(function(_) {
if (reg.cancelled.compareExchange(false, true) == false) {
reg.callback(reg.heldValue);
reg.callback = null;
reg.heldValue = null;
}
}, target);

return new Handle(reg);
}
}
55 changes: 55 additions & 0 deletions std/haxe/GcFinalizer.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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;

/**
A GC finalizer registry that invokes a callback when a watched object
is garbage-collected. Modeled after JavaScript's `FinalizationRegistry`.

The callback receives a held value (not the collected object itself),
which is safe because the object may already be in an invalid state.

Not all targets support GC finalizers. On unsupported targets, the
constructor throws `NotImplementedException`.
**/
class GcFinalizer<T> {
/**
Creates a new `GcFinalizer` with the given cleanup `callback`.
The callback will be invoked with the held value when a registered
target object is garbage-collected.
**/
public function new(callback:T->Void) {
Comment thread
Aidan63 marked this conversation as resolved.
throw new haxe.exceptions.NotImplementedException("Not implemented for this platform");
}

/**
Registers `target` for clean-up. When `target` is garbage-collected,
the callback will be invoked with `heldValue`.

Returns an `IHandle` handle. Calling `close()` on the handle
cancels the registration, preventing the callback from firing.
**/
public function register(target:{}, heldValue:T):IHandle {
return null;
}
}
33 changes: 33 additions & 0 deletions std/haxe/IHandle.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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;

/**
A general-purpose interface for handles that can be released
by calling `close()`. Used by `GcFinalizer` to return cancellation
handles, and intended for thread callback handles, coroutine
scheduler handles, and similar patterns.
**/
interface IHandle {
function close():Void;
}
38 changes: 38 additions & 0 deletions std/js/_std/haxe/GcFinalizer.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;

@:coreApi
class GcFinalizer<T> {
var h:js.lib.FinalizationRegistry<T>;

public inline function new(callback:T->Void) {
h = new js.lib.FinalizationRegistry(callback);
}

public inline function register(target:{}, heldValue:T):IHandle {
var token = {};
h.register(target, heldValue, token);
return cast {close: function() h.unregister(token)};
}
}
49 changes: 49 additions & 0 deletions std/js/lib/FinalizationRegistry.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package js.lib;
/*
* 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.
*/

/**
A `FinalizationRegistry` object lets you request a callback when a value is garbage-collected.

Documentation [FinalizationRegistry](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry) by
[Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/contributors.txt),
licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
**/
@:native("FinalizationRegistry")
extern class FinalizationRegistry<T> {
/**
Creates a new `FinalizationRegistry` with the given cleanup callback.
**/
function new(cleanupCallback:T->Void);

/**
Registers a target object with the registry, associating it with a held value
and an optional unregister token.
**/
function register(target:{}, heldValue:T, ?unregisterToken:{}):Void;

/**
Unregisters any registrations associated with the given unregister token.
Returns `true` if at least one registration was removed.
**/
function unregister(unregisterToken:{}):Bool;
}
Loading
Loading