Module: HelperMethods::ClassMethods

Included in:
HelperMethods
Defined in:
lib/swxruby/helper_module.rb

Instance Method Summary collapse

Instance Method Details

#calculate_bytecode_length(bytecode) ⇒ Object



23
24
25
26
# File 'lib/swxruby/helper_module.rb', line 23

def calculate_bytecode_length(bytecode)
	# Calculate bytecode length *without* counting the init array or init object action
	bytecode.join.sub(/^(#{ActionCodes::INIT_ARRAY}|#{ActionCodes::INIT_OBJECT})/, '').length/2
end

#generate_push_statement(bytecode) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/swxruby/helper_module.rb', line 28

def generate_push_statement(bytecode)
	unpushed_data = []
	# Iterate over the bytecode array in reverse and add all of the unpushed
	# data to 'unpushed_data'
	bytecode.reverse_each do |bytecode_chunk|
		if bytecode_chunk.begins_with?('96') || bytecode_chunk.begins_with?('1D') then break else unpushed_data << bytecode_chunk end
	end
	
	# Since we iterated over the bytecode in reverse, unpushed_data is reversed, so 
	# reverse it again before passing it into #calculate_bytecode_length
	bytecode_length = calculate_bytecode_length(unpushed_data.reverse)
	
	 # TODO: Replace with constant
	'96' + integer_to_hexadecimal(bytecode_length, 2)
end

#integer_to_hexadecimal(integer, number_of_bytes = 1) ⇒ Object



44
45
46
# File 'lib/swxruby/helper_module.rb', line 44

def integer_to_hexadecimal(integer, number_of_bytes=1)
	make_little_endian("%0#{number_of_bytes*2}X" % integer)
end

#make_little_endian(hex_string) ⇒ Object



48
49
50
51
52
# File 'lib/swxruby/helper_module.rb', line 48

def make_little_endian(hex_string)
	# split into an array of string pairs
	# reverse the array and join back into a string
	pad_string_to_byte_boundary(hex_string).scan(/../).reverse.join
end

#pad_string_to_byte_boundary(hex_string) ⇒ Object



54
55
56
57
# File 'lib/swxruby/helper_module.rb', line 54

def pad_string_to_byte_boundary(hex_string)
	hex_string += '0' if hex_string.length % 2 == 1
	hex_string
end

#string_length_in_bytes_hex(string, number_of_bytes) ⇒ Object

Returns a string with the length of the passed hex string in bytes padded to display in ‘number_of_bytes’ bytes.



61
62
63
64
# File 'lib/swxruby/helper_module.rb', line 61

def string_length_in_bytes_hex(string, number_of_bytes)
																								 # Divide length in chars by 2 to get length in bytes
	bytecode_length_in_hex = integer_to_hexadecimal(string.length/2, number_of_bytes)
end