Module: ObjectSpace

Defined in:
opal/opal/corelib/object_space.rb

Overview

helpers: respond_to, falsy, truthy

Defined Under Namespace

Classes: WeakMap

Class Method Summary collapse

Class Method Details

.define_finalizer(obj, aproc = undefined, &block) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'opal/opal/corelib/object_space.rb', line 36

def define_finalizer(obj, aproc = undefined, &block)
  %x{
    if ($truthy(block)) aproc = block;
    if ($falsy(aproc)) aproc = #{proc};
    if (!$respond_to(aproc, '$call')) {
      #{raise ArgumentError, "Wrong type argument #{aproc.class} (should be callable)"};
    }
    var id = #{obj.__id__};
    add_caller(id, aproc);
    try {
      registry.register(obj, id, obj);
    }
    catch (e) {
      delete_callers(id);
      #{raise ArgumentError, "cannot define finalizer for #{obj.class}"};
    }
    return [0, aproc];
  }
end

.undefine_finalizer(obj) ⇒ Object


56
57
58
59
60
61
62
63
# File 'opal/opal/corelib/object_space.rb', line 56

def undefine_finalizer(obj)
  %{
    var id = #{obj.__id__};
    registry.unregister(obj);
    delete_callers(id);
    return obj;
  }
end