Class: InvocationContext

Inherits:
AbstractInvocationContext show all
Includes:
Enumerable
Defined in:
lib/hypertube-ruby-sdk/sdk/invocation_context.rb

Overview

InvocationContext is a class that represents a context for invoking commands. It implements several interfaces for different types of interactions. This class is used to construct chains of invocations, representing expressions of interaction that have not yet been executed.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/foundations/invocation-context article on Hypertube Guides}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractInvocationContext

#get_enum_item

Constructor Details

#initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false, is_stateless = false) ⇒ InvocationContext

Returns a new instance of InvocationContext.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 18

def initialize(runtime_name, connection_type, tcp_ip_address, command, is_executed = false, is_stateless = false)
  @is_executed = is_executed
  @is_stateless = is_stateless
  @runtime_name = runtime_name
  @connection_type = connection_type
  @tcp_ip_address = tcp_ip_address
  @current_command = command
  @response_command = nil
  # generates error: ruby/lib/RuntimeBridge.rb-ruby-sdk/sdk/internal/invocation_context.rb:17: warning: finalizer references object to be finalized
  # ObjectSpace.define_finalizer(self, self.finalize(@current_command, @is_executed, @interpreter, @connection_type, @tcp_ip_address))
end

Instance Attribute Details

#current_commandObject (readonly)

Returns the value of attribute current_command.



16
17
18
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 16

def current_command
  @current_command
end

#response_commandObject (readonly)

Returns the value of attribute response_command.



16
17
18
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 16

def response_command
  @response_command
end

Instance Method Details

#[](*indexes) ⇒ Object



48
49
50
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 48

def [](*indexes)
  get_index(*indexes).execute
end

#[]=(*indexes_and_value) ⇒ Object



52
53
54
55
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 52

def []=(*indexes_and_value)
  value = indexes_and_value.pop
  set_index(indexes_and_value, value).execute
end

#build_command(command) ⇒ Object



388
389
390
391
392
393
394
395
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 388

def build_command(command)
  return @current_command if command.nil? || command.payload.nil?

  command.payload.each_index do |i|
    command.payload[i] = encapsulate_payload_item(command.payload[i])
  end
  command.prepend_arg_to_payload(@current_command)
end

#build_invocation_context(local_command) ⇒ Object



383
384
385
386
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 383

def build_invocation_context(local_command)
  InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), @is_executed,
                        @is_stateless)
end

#create_instance(*args) ⇒ InvocationContext

Creates a new instance of a class in the target runtime. Invokes a constructor on the target runtime and registers the returned context for updates.

Parameters:

  • args (Array)

    The arguments to class constructor

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to create the instance.

See Also:



144
145
146
147
148
149
150
151
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 144

def create_instance(*args)
  local_command = Command.new(@runtime_name, CommandType::CREATE_CLASS_INSTANCE, [*args])
  create_instance_inv_ctx = InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address,
                                                  build_command(local_command), @is_executed, @is_stateless)

  # create_instance_inv_ctx.register_for_update
  create_instance_inv_ctx
end

#create_nullInvocationContext

Creates a null object of a specific type in the target runtime.

Returns:

  • (InvocationContext)

    An InvocationContext instance with the command to create a null object.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/null-handling/create-null-object article on Hypertube Guides}


299
300
301
302
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 299

def create_null
  local_command = Command.new(@runtime_name, CommandType::CREATE_NULL, [])
  build_invocation_context(local_command)
end

#encapsulate_payload_item(payload_item) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 397

def encapsulate_payload_item(payload_item)
  if payload_item.is_a? Command
    if payload_item.command_type == CommandType::VALUE || payload_item.command_type == CommandType::ARRAY
      return payload_item
    end

    payload_item.payload.each_index do |i|
      payload_item.payload[i] = encapsulate_payload_item(payload_item.payload[i])
    end
    payload_item

  elsif payload_item.is_a? InvocationContext
    payload_item.current_command

  elsif payload_item.is_a? Array
    copied_array = payload_item.map { |item| encapsulate_payload_item(item) }
    Command.new(@runtime_name, CommandType::ARRAY, copied_array)
  elsif TypesHandler.primitive_or_none?(payload_item)
    Command.new(@runtime_name, CommandType::VALUE, payload_item.nil? ? [nil] : [*payload_item])
  else
    raise TypeError, "Unsupported payload item type: #{payload_item.class} for payload item: #{payload_item}."
  end
end

#executeInvocationContext

Executes the current command. Because invocation context is building the intent of executing particular expression on target environment, we call the initial state of invocation context as non-materialized. The non-materialized context wraps either single command or chain of recursively nested commands. Commands are becoming nested through each invocation of methods on Invocation Context. Each invocation triggers the creation of new Invocation Context instance wrapping the current command with new parent command valid for invoked method. Developer can decide on any moment of the materialization for the context taking full control of the chunks of the expression being transferred and processed on target runtime.

Returns:

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/foundations/execute-method article on Hypertube Guides}


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 65

def execute
  raise 'currentCommand is undefined in InvocationContext execute method' if @current_command.nil?

  return self if @is_stateless && @current_command.command_type != CommandType::PASS_BY_VALUE

  @response_command = Interpreter.execute(@current_command, @connection_type, @tcp_ip_address)

  if @response_command.command_type == CommandType::EXCEPTION
    exception = ExceptionThrower.throw_exception(@response_command)
    SdkMessageHelper.send_message_to_app_insights('SdkException', exception.message)
    raise(exception)
  end

  if @current_command.command_type == CommandType::CREATE_CLASS_INSTANCE
    @current_command = @response_command
    @is_executed = true
    return self
  end

  @response_command = process_update_invocation_context_commands(@response_command) unless @is_stateless

  InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, @response_command, true, @is_stateless)
end

#execute_asyncObject



89
90
91
92
93
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 89

def execute_async
  Thread.new do
    execute
  end
end

#get_enum_nameInvocationContext

Retrieves the name of an enum from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the enum name.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/enums/using-enum-type article on Hypertube Guides}


275
276
277
278
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 275

def get_enum_name
  local_command = Command.new(@runtime_name, CommandType::GET_ENUM_NAME, [])
  build_invocation_context(local_command)
end

#get_enum_valueInvocationContext

Retrieves the value of an enum from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the enum value.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/enums/using-enum-type article on Hypertube Guides}


283
284
285
286
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 283

def get_enum_value
  local_command = Command.new(@runtime_name, CommandType::GET_ENUM_VALUE, [])
  build_invocation_context(local_command)
end

#get_index(*indexes) ⇒ InvocationContext

Retrieves the value at a specific indexes in an array from the target runtime.

Parameters:

  • indexes (Array)

    The indexes to pass to the get index command.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the index.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/arrays-and-collections/one-dimensional-arrays article on Hypertube Guides}


221
222
223
224
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 221

def get_index(*indexes)
  local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_ITEM, [*indexes])
  build_invocation_context(local_command)
end

#get_instance_field(field_name) ⇒ InvocationContext

Retrieves the value of an instance field from the target runtime.

Parameters:

  • field_name (String)

    The name of the instance field to get.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the instance field.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/fields-and-properties/getting-and-setting-values-for-instance-fields-and-properties article on Hypertube Guides}


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 176

def get_instance_field(field_name)
  if @is_stateless
    value = GetInstanceFieldHelper.get_field_value(@current_command, field_name)
    return_command =
      if TypesHandler.primitive_or_none?(value)
        Command.new(@runtime_name, CommandType::VALUE, value)
      elsif value.is_a?(Command)
        value
      else
        raise ArgumentError, "Unsupported type for stateless get_instance_field: #{value.class}"
      end

    return InvocationContext.new(
      @runtime_name,
      @connection_type,
      @tcp_ip_address,
      return_command,
      false,
      @is_stateless
    )
  end

  local_command = Command.new(@runtime_name, CommandType::GET_INSTANCE_FIELD, [field_name])
  build_invocation_context(local_command)
end

#get_instance_method_as_delegate(method_name, *args) ⇒ InvocationContext

Retrieves an instance method as a delegate from the target runtime. # @see Refer to this article on Hypertube Guides

Parameters:

  • method_name (String)

    The name of the instance method to retrieve as a delegate.

  • args (Array)

    The arguments to pass to the instance method.

Returns:

  • (InvocationContext)

    An InvocationContext instance with the command to get the instance method as a delegate.



319
320
321
322
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 319

def get_instance_method_as_delegate(method_name, *args)
  local_command = Command.new(@runtime_name, CommandType::GET_INSTANCE_METHOD_AS_DELEGATE, [method_name, *args])
  build_invocation_context(local_command)
end

#get_rankInvocationContext

Retrieves the rank of an array from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the rank.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/arrays-and-collections/multidimensional-arrays article on Hypertube Guides}


247
248
249
250
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 247

def get_rank
  local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_RANK, [])
  build_invocation_context(local_command)
end

#get_ref_valueInvocationContext

Retrieves the value of a reference from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the ref value.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/methods-arguments/passing-arguments-by-reference-with-ref-keyword article on Hypertube Guides}


291
292
293
294
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 291

def get_ref_value
  local_command = Command.new(@runtime_name, CommandType::GET_REF_VALUE, [])
  build_invocation_context(local_command)
end

#get_sizeInvocationContext

Retrieves the number of elements from the target runtime.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the size.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/arrays-and-collections/one-dimensional-arrays article on Hypertube Guides}


239
240
241
242
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 239

def get_size
  local_command = Command.new(@runtime_name, CommandType::ARRAY_GET_SIZE, [])
  build_invocation_context(local_command)
end

#get_static_field(field_name) ⇒ InvocationContext

Retrieves the value of a static field from the target runtime.

Parameters:

  • field_name (String)

    The name of the static field to get.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to get the static field.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/fields-and-properties/getting-and-setting-values-for-static-fields-and-properties article on Hypertube Guides}


157
158
159
160
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 157

def get_static_field(field_name)
  local_command = Command.new(@runtime_name, CommandType::GET_STATIC_FIELD, [field_name])
  build_invocation_context(local_command)
end

#get_static_method_as_delegate(method_name, *args) ⇒ InvocationContext

Retrieves a static method as a delegate from the target runtime.

Parameters:

  • method_name (String)

    The name of the static method to retrieve as a delegate.

  • args (Array)

    The arguments to pass to the static method.

Returns:

  • (InvocationContext)

    An InvocationContext instance with the command to get the static method as a delegate.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/delegates-and-events/using-delegates article on Hypertube Guides}


309
310
311
312
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 309

def get_static_method_as_delegate(method_name, *args)
  local_command = Command.new(@runtime_name, CommandType::GET_STATIC_METHOD_AS_DELEGATE, [method_name, *args])
  build_invocation_context(local_command)
end

#get_valueObject

Retrieves the value of the current command from the target runtime.

Returns:

  • (Object)

    The value of the current command.

See Also:



367
368
369
370
371
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 367

def get_value
  return @current_command.payload[0] if @current_command.command_type == CommandType::VALUE &&
                                          TypesHandler.primitive_or_none?(@current_command.payload[0])
  self
end

#invoke_generic_method(method_name, *args) ⇒ InvocationContext

Invokes a generic method on the target runtime.

Parameters:

  • method_name (String)

    The name of the generic method to invoke.

  • args (Array)

    The arguments to pass to the generic method.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to invoke the generic method.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/generics/calling-generic-instance-method article on Hypertube Guides}


267
268
269
270
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 267

def invoke_generic_method(method_name, *args)
  local_command = Command.new(@runtime_name, CommandType::INVOKE_GENERIC_METHOD, [method_name, *args])
  build_invocation_context(local_command)
end

#invoke_generic_static_method(method_name, *args) ⇒ InvocationContext

Invokes a generic static method on the target runtime.

Parameters:

  • method_name (String)

    The name of the generic static method to invoke.

  • args (Array)

    The arguments to pass to the generic static method.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to invoke the generic static method.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/generics/calling-generic-static-method article on Hypertube Guides}


257
258
259
260
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 257

def invoke_generic_static_method(method_name, *args)
  local_command = Command.new(@runtime_name, CommandType::INVOKE_GENERIC_STATIC_METHOD, [method_name, *args])
  build_invocation_context(local_command)
end

#invoke_instance_method(method_name, *args) ⇒ InvocationContext

Invokes an instance method on the target runtime.

Parameters:

  • method_name (String)

    The name of the instance method to invoke.

  • args (Array)

    The arguments to pass to the instance method.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to invoke the instance method.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/calling-methods/creating-instance-and-calling-instance-methods article on Hypertube Guides}


134
135
136
137
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 134

def invoke_instance_method(method_name, *args)
  local_command = Command.new(@runtime_name, CommandType::INVOKE_INSTANCE_METHOD, [method_name, *args])
  build_invocation_context(local_command)
end

#invoke_static_method(method_name, *args) ⇒ InvocationContext

Invokes a static method on the target runtime.

Parameters:

  • method_name (String)

    The name of the static method to invoke.

  • args (Array)

    The arguments to pass to the static method.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to invoke the static method.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/calling-methods/invoking-static-method article on Hypertube Guides}


110
111
112
113
114
115
116
117
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 110

def invoke_static_method(method_name, *args)
  local_command = Command.new(@runtime_name, CommandType::INVOKE_STATIC_METHOD, [method_name, *args])
  if @is_stateless
    local_inv_ctx = build_invocation_context(local_command)
    return local_inv_ctx.send(:pass_by_value)
  end
  build_invocation_context(local_command)
end

#iteratorObject

def finalize(command, is_executed, interpreter, connection_type, tcp_ip_address)

  proc do
    if command.command_type == CommandType::REFERENCE && is_executed == true
      destruct_command = Command.new(@runtime_name, CommandType::DESTRUCT_REFERENCE, command.payload)
      interpreter.execute(destruct_command, connection_type, tcp_ip_address)
    end
  end
end


39
40
41
42
43
44
45
46
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 39

def iterator
  if ![CommandType::REFERENCE, CommandType::ARRAY_GET_ITEM,
       CommandType::ARRAY_SET_ITEM].include? @current_command.command_type
    raise TypeError, "Object is not iterable for command type #{@current_command.command_type}"
  else
    InvocationContextIterator.new(self)
  end
end

#process_update_invocation_context_commands(response_command) ⇒ Command

Processes ValueForUpdate commands in the response payload. Scans response payload for ValueForUpdate commands, updates corresponding InvocationContexts.

Parameters:

  • response_command (Command)

    The response command to process.

Returns:

  • (Command)

    The updated response command.



99
100
101
102
103
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 99

def process_update_invocation_context_commands(response_command)
  # Ruby version: simply return the response_command unchanged
  # Update processing logic can be added here if needed
  response_command
end

#register_for_updateInvocationContext

Registers the current context for updates by wrapping it in a REGISTER_FOR_UPDATE command.

Returns:



377
378
379
380
381
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 377

def register_for_update
  local_command = Command.new(@runtime_name, CommandType::REGISTER_FOR_UPDATE, [])
  InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, build_command(local_command), @is_executed,
                        @is_stateless)
end

#retrieve_arrayObject, Array

Retrieves an array from the target runtime. Retrieves an array from the target runtime.

Returns:

  • (Object)

    A new InvocationContext instance that wraps the command to retrieve the array.

  • (Array)

    The retrieved array.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/arrays-and-collections/one-dimensional-arrays article on Hypertube Guides}
  • to this {https://www.hypertube.dev/guides/v2/ruby/arrays-and-collections/one-dimensional-arrays article on Hypertube Guides}


330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 330

def retrieve_array
  array_inv_ctx = self
  unless @is_stateless
    retrieve_array_command = Command.new(@runtime_name, CommandType::RETRIEVE_ARRAY, [])
    local_inv_ctx = InvocationContext.new(
      @runtime_name,
      @connection_type,
      @tcp_ip_address,
      build_command(retrieve_array_command)
    )
    array_inv_ctx = local_inv_ctx.execute
  end

  payload = array_inv_ctx.current_command.payload
  return [] if payload.empty?

  if payload[0].is_a?(Command)
    invocation_contexts = Array.new(payload.length)
    payload.each_with_index do |item, i|
      invocation_contexts[i] = InvocationContext.new(
        @runtime_name,
        @connection_type,
        @tcp_ip_address,
        item,
        @is_executed,
        @is_stateless
      )
    end
    return invocation_contexts
  end

  payload
end

#set_index(indexes, value) ⇒ InvocationContext

Sets the value at a specific index in an array in the target runtime.

Parameters:

  • indexes (Array)

    The indexes where new value should be set

  • value (Object)

    The new value to set at the indexes.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to set the index.

See Also:



231
232
233
234
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 231

def set_index(indexes, value)
  local_command = Command.new(@runtime_name, CommandType::ARRAY_SET_ITEM, [indexes, value])
  build_invocation_context(local_command)
end

#set_instance_field(field_name, value) ⇒ InvocationContext

Sets the value of an instance field in the target runtime.

Parameters:

  • field_name (String)

    The name of the instance field to set.

  • value (Object)

    The new value of the instance field.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to set the instance field.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/fields-and-properties/getting-and-setting-values-for-instance-fields-and-properties article on Hypertube Guides}


207
208
209
210
211
212
213
214
215
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 207

def set_instance_field(field_name, value)
  local_command = Command.new(@runtime_name, CommandType::SET_INSTANCE_FIELD, [field_name, value])
  new_command = build_command(local_command)
  if @is_stateless
    @current_command = new_command
    return self
  end
  InvocationContext.new(@runtime_name, @connection_type, @tcp_ip_address, new_command, @is_executed, @is_stateless)
end

#set_static_field(field_name, value) ⇒ InvocationContext

Sets the value of a static field in the target runtime.

Parameters:

  • field_name (String)

    The name of the static field to set.

  • value (Object)

    The new value of the static field.

Returns:

  • (InvocationContext)

    A new InvocationContext instance that wraps the command to set the static field.

See Also:

  • to this {https://www.hypertube.dev/guides/v2/ruby/fields-and-properties/getting-and-setting-values-for-static-fields-and-properties article on Hypertube Guides}


167
168
169
170
# File 'lib/hypertube-ruby-sdk/sdk/invocation_context.rb', line 167

def set_static_field(field_name, value)
  local_command = Command.new(@runtime_name, CommandType::SET_STATIC_FIELD, [field_name, value])
  build_invocation_context(local_command)
end