Class: CloudRailSi::ServiceCode::Interpreter
- Inherits:
-
Object
- Object
- CloudRailSi::ServiceCode::Interpreter
- Defined in:
- lib/cloudrail_si/servicecode/Interpreter.rb
Instance Attribute Summary collapse
-
#sandbox ⇒ Object
readonly
Returns the value of attribute sandbox.
Class Method Summary collapse
Instance Method Summary collapse
- #call_function(function_name, *parameters) ⇒ Object
- #call_function_sync(function_name, *parameters) ⇒ Object
- #get_parameter(idx) ⇒ Object
-
#initialize(sandbox) ⇒ Interpreter
constructor
A new instance of Interpreter.
- #load_as_string(saved_state) ⇒ Object
- #resume_function(function_name, *parameters) ⇒ Object
- #run ⇒ Object
- #run_sync ⇒ Object
- #save_as_string ⇒ Object
Constructor Details
#initialize(sandbox) ⇒ Interpreter
Returns a new instance of Interpreter.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 52 def initialize(sandbox) @sandbox = sandbox @COMMAND_LIST = [ CallFunc.new, Break.new, Clone.new, Create.new, Delete.new, Get.new, JumpRel.new, Pull.new, Push.new, Return.new, Set.new, Size.new, Uint8ToBase64.new, Sha1.new, Md5.new, Base64Encode.new, StreamToString.new, StringToStream.new, MakeJoinedStream.new, MakeLimitedStream.new, RequestCall.new, Out.new, AwaitCodeRedirect.new, GetMimeType.new, Conditional.new('if==than', lambda { |c| (c == 0) }, false), Conditional.new('if>=than', lambda { |c| (c >= 0) }, true), Conditional.new('if>than', lambda { |c| (c > 0) }, true), Conditional.new('if<=than', lambda { |c| (c <= 0) }, true), Conditional.new('if<than', lambda { |c| (c < 0) }, true), Conditional.new('if!=than', lambda { |c| (c != 0) }, false), ThrowError.new, Parse.new, Stringify.new, MathCombine.new('math.add', lambda { |elems| elems.reduce(0, :+) }), MathCombine.new('math.multiply', lambda { |elems| elems.reduce(1, :*) }), MathCombine.new('math.max', lambda { |elems | elems.max }), MathCombine.new('math.min', lambda{ |elems | elems | elems.min }), Floor.new, GetKeyArray.new, GetKeyValueArrays.new, Concat.new, Format.new, IndexOf.new, LastIndexOf.new, Split.new, Substr.new, Substring.new, StringTransform.new('string.lowerCase', lambda { |str| str.downcase }), StringTransform.new('string.upperCase', lambda { |str| str.upcase }), StringTransform.new('string.urlEncode', lambda { |str| URI::encode_www_form_component(str) }), StringTransform.new('string.urlDecode', lambda { |str| URI::decode_www_form_component(str, Encoding::UTF_8) }) ] @COMMANDS = {} @COMMAND_LIST.each { |command| @COMMANDS[command.get_identifier()] = command } end |
Instance Attribute Details
#sandbox ⇒ Object (readonly)
Returns the value of attribute sandbox.
50 51 52 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 50 def sandbox @sandbox end |
Class Method Details
.decode_command_parameters(command) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 163 def self.decode_command_parameters(command) command_parameters = command.clone[1..-1] # first param was function name (0..command_parameters.length).each do |i| if (Helper.is_string(command_parameters[i])) if (command_parameters[i].index('$') === 0) command_parameters[i] = VarAddress.new(command_parameters[i][1..-1]) elsif (command_parameters[i].index('\\$') === 0) command_parameters[i] = command_parameters[i][1..-1] end end end return command_parameters end |
Instance Method Details
#call_function(function_name, *parameters) ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 114 def call_function(function_name, *parameters) @sandbox.create_new_stack_level(function_name, 0) Helper.add_all(@sandbox.current_parameters(), parameters) if (@sandbox.current_function_code().nil?) = "Service code error: function #{function_name} not found" raise Errors::InternalError.new() end return run_sync() # this was an async run end |
#call_function_sync(function_name, *parameters) ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 124 def call_function_sync(function_name, *parameters) @sandbox.create_new_stack_level(function_name, 0) Helper.add_all(@sandbox.current_parameters(), parameters) if (@sandbox.current_function_code().nil?) = "Service code error: function #{function_name} not found" raise Errors::InternalError.new() end return run_sync() end |
#get_parameter(idx) ⇒ Object
177 178 179 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 177 def get_parameter(idx) @sandbox.get_parameter(idx, 0) end |
#load_as_string(saved_state) ⇒ Object
185 186 187 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 185 def load_as_string(saved_state) @sandbox.persistent_storage = JSON.parse(saved_state) end |
#resume_function(function_name, *parameters) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 189 def resume_function(function_name, *parameters) if (@sandbox.parameters_stack.length === 0) first_parameters = [] @sandbox.parameters_stack.push(first_parameters) else first_parameters = @sandbox.parameters_stack[0] end (0..parameters.length).each do |i| if (Helper.is_object(parameters[i])) m = parameters[i] Helper.clear(m) Helper.put_all(m, first_parameters[i]) else if (i >= first_parameters.length) first_parameters.push(parameters[i]) else first_parameters[i] = parameters[i] end end end return run_sync() end |
#run ⇒ Object
134 135 136 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 134 def run raise Errors::InternalError.new('interpreter.run not supported.') end |
#run_sync ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 138 def run_sync begin while (@sandbox.current_service_code_line() < @sandbox.current_function_code().length && @sandbox.current_service_code_line() >= 0) command = @sandbox.current_function_code()[@sandbox.current_service_code_line()] Helper.log "\n\n\n#{command}" if (@COMMANDS[command[0]].nil?) raise Errors::InternalError.new("Unknown command: #{command[0]}") end command_parameters = Interpreter.decode_command_parameters(command) @COMMANDS[command[0]].execute(@sandbox, command_parameters) return if !@sandbox.thrown_error.nil? @sandbox.increment_current_service_code_line(1) while ((@sandbox.current_service_code_line() >= @sandbox.current_function_code().length || @sandbox.current_service_code_line() < 0) && @sandbox.code_function_name_stack.length > 1) @sandbox.return_from_function() end end rescue => e raise e if (e.class.name == CloudRailSi::Errors::UserError) = 'Service code error in function ' + @sandbox.current_function_name() + ' at line ' + @sandbox.current_service_code_line().to_s + ' with message: ' + e. raise CloudRailSi::Errors::InternalError.new() end end |
#save_as_string ⇒ Object
181 182 183 |
# File 'lib/cloudrail_si/servicecode/Interpreter.rb', line 181 def save_as_string @sandbox.peristent_storage.to_json end |