Module: Jsii

Defined in:
jsii.rb,
jsii/enum.rb,
jsii/type.rb,
jsii/utils.rb,
jsii/errors.rb,
jsii/kernel.rb,
jsii/object.rb,
jsii/struct.rb,
jsii/assembly.rb,
jsii/kernel/api.rb,
jsii/serializer.rb,
jsii/kernel/async.rb,
jsii/object/registry.rb,
jsii/kernel/callbacks.rb,
jsii/object/overrides.rb,
jsii/object/static_constants.rb,
jsii/object/kernel_forwarding.rb,
jsii/object/dynamic_invocation.rb

Overview

Top-level namespace for the JSII Ruby runtime. See Kernel for the Node sidecar bridge, Object for the proxy base class, Struct for value-typed data classes, and Serializer for the wire codec.

Defined Under Namespace

Modules: FqnExtension, Serializer, StaticConstants, Type, Utils Classes: Assembly, Enum, Error, JSIIError, Kernel, Object, RuntimeError, Struct

Class Method Summary collapse

Class Method Details

.downcast(obj, klass) ⇒ Jsii::Object

Unsafely cast a JSII object proxy to a different JSII type/interface.

Parameters:

  • obj (Jsii::Object)

    the object proxy to cast.

  • klass (Class, Module)

    the target JSII class or interface module.

Returns:

  • (Jsii::Object)

    a new proxy instance representing the target type.

Raises:

  • (TypeError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'jsii.rb', line 12

def self.downcast(obj, klass)
  return nil if obj.nil?
  raise TypeError, "Expected a Jsii::Object" unless obj.is_a?(Jsii::Object)

  if klass.is_a?(Class)
    instance = klass.allocate
  else
    instance = Jsii::Object.allocate.extend(klass)
  end
  instance.instance_variable_set(:@jsii_ref, obj.jsii_ref)
  # A downcast is a VIEW, not a replacement: the registry entry for a ref is
  # what the kernel's callback dispatcher resolves to when invoking guest
  # overrides, so overwriting it with a bare proxy silently stops every
  # override on the original object from firing. Only claim the entry when
  # nothing live holds it.
  Jsii::Object.register_instance(instance) unless Jsii::Object.find_by_ref(obj.jsii_ref)
  instance
end