Skip to content

Commit

Permalink
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions core/src/main/java/org/jruby/Ruby.java
Original file line number Diff line number Diff line change
@@ -3051,17 +3051,25 @@ public synchronized void addEventHook(EventHook hook) {

public synchronized void removeEventHook(EventHook hook) {
EventHook[] hooks = eventHooks;

if (hooks.length == 0) return;
EventHook[] newHooks = new EventHook[hooks.length - 1];
boolean found = false;
for (int i = 0, j = 0; i < hooks.length; i++) {
if (!found && hooks[i] == hook && !found) { // exclude first found
found = true;
continue;

int pivot = -1;
for (int i = 0; i < hooks.length; i++) {
if (hooks[i] == hook) {
pivot = i;
break;
}
newHooks[j] = hooks[i];
j++;
}

if (pivot == -1) return; // No such hook found.

EventHook[] newHooks = new EventHook[hooks.length - 1];
// copy before and after pivot into the new array but don't bother
// to arraycopy if pivot is first/last element of the old list.
if (pivot != 0) System.arraycopy(hooks, 0, newHooks, 0, pivot);
if (pivot != hooks.length-1) System.arraycopy(hooks, pivot + 1, newHooks, pivot, hooks.length - (pivot + 1));

eventHooks = newHooks;
hasEventHooks = newHooks.length > 0;
}

0 comments on commit 74892a4

Please sign in to comment.