Class: CloudRailSi::ServiceCode::Sandbox

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudrail_si/servicecode/Sandbox.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_code, persistent_storage, instance_dependency_storage) ⇒ Sandbox

Returns a new instance of Sandbox.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 18

def initialize(service_code, persistent_storage, instance_dependency_storage)
    @local_variables_stack = []
    @parameters_stack = []
    @code_function_name_stack = []
    @code_line_stack = []
    @persistent_storage = []
    @service_code = service_code
    @persistent_storage = persistent_storage
    @instance_dependency_storage = instance_dependency_storage
    @LIST_MAX_ADD_JUMP_ALLOWED = 32
    @thrown_error = nil
end

Instance Attribute Details

#code_function_name_stackObject (readonly)

Returns the value of attribute code_function_name_stack.



9
10
11
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 9

def code_function_name_stack
  @code_function_name_stack
end

#code_line_stackObject (readonly)

Returns the value of attribute code_line_stack.



12
13
14
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 12

def code_line_stack
  @code_line_stack
end

#instance_dependency_storageObject (readonly)

Returns the value of attribute instance_dependency_storage.



11
12
13
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 11

def instance_dependency_storage
  @instance_dependency_storage
end

#local_variables_stackObject (readonly)

Returns the value of attribute local_variables_stack.



10
11
12
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 10

def local_variables_stack
  @local_variables_stack
end

#thrown_errorObject (readonly)

Returns the value of attribute thrown_error.



8
9
10
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 8

def thrown_error
  @thrown_error
end

Instance Method Details

#call_function(function_name, parameters) ⇒ Object



296
297
298
299
300
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 296

def call_function(function_name, parameters)
    create_new_stack_level(function_name, -1)
    parameter_stack = current_parameters()
    Helper.add_all(parameter_stack, parameters)
end

#compare_variables(a_obj, b_obj, command_id, type_check) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 301

def compare_variables(a_obj, b_obj, command_id, type_check)
    a_obj = Helper.resolve(self, a_obj, false)
    b_obj = Helper.resolve(self, b_obj, false)
    if (!type_check && (a_obj.nil? || b_obj.nil?))
        return 0 if (a_obj.nil? && b_obj.nil?)
        return -1
    end

    if ((a_obj.is_a? Numeric) && (b_obj.is_a? Numeric))
        # doesn't matter if one is int and other is float
    elsif (a_obj.class != b_obj.class)
        Helper.debug
        return -1 if !type_check
        raise Errors::InternalError.new("Command #{command_id} compares arguments of different types")
    end

    return Helper.compare(a_obj, b_obj)
end

#create_new_stack_level(function_name, code_line) ⇒ Object



36
37
38
39
40
41
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 36

def create_new_stack_level(function_name, code_line)
    @local_variables_stack.push([])
    @parameters_stack.push([])
    @code_function_name_stack.push(function_name)
    @code_line_stack.push(code_line)
end

#current_function_codeObject



53
54
55
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 53

def current_function_code
    @service_code[current_function_name()]
end

#current_function_nameObject



48
49
50
51
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 48

def current_function_name
    return nil if (@code_function_name_stack.length === 0)
    @code_function_name_stack[-1]
end

#current_parametersObject



43
44
45
46
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 43

def current_parameters
    return nil if (@parameters_stack.length === 0)
    @parameters_stack[-1]
end

#current_service_code_lineObject



57
58
59
60
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 57

def current_service_code_line
    return -1 if (@code_line_stack.length === 0)
    return @code_line_stack[-1]
end

#decode_variable_address(var_address) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 148

def decode_variable_address(var_address)
    dec_adr = []
    adr = var_address.instance_variable_get(:@address_string)
    Helper.assert(adr[0] != '$')
    if (adr[0] < '0' || adr[0] > '9')
        dec_adr.push(adr[0])
        adr = adr[1..-1]
    end
    adr_parts = adr.split('.')
    (0..adr_parts.length - 1).each do |i|
        part = adr_parts[i]
        if (Helper.is_number_string(part))
            dec_adr.push(Integer(part))
        else
            dec_adr.push(part)
        end
    end
    return dec_adr
end

#delete_entry(container, var_address) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 266

def delete_entry(container, var_address)
    if (var_address.length > 1)
        next_container = get_entry(container, var_address, false)
        return delete_entry(next_container, var_address[1..-1])
    end
    var_address_part = var_address[0]
    if (Helper.is_array(container))
        if (Helper.is_number_string(var_address_part))
            var_address_part = Integer(var_address_part)
        end
        if (!Helper.is_number(var_address_part) || var_address_part >= container.length)
            raise Errors::InternalError.new('Invalid index while indexing into an array')
        end
        idx = var_address_part
        list = container
        if (idx < list.length)
            list.delete_at(idx)
        end
        while (list.length && list[-1].nil?)
            list.pop()
        end
    elsif (Helper.is_object(container))
        var_address_part = var_address_part.to_s
        container.delete(var_address_part)
    elsif Helper.is_sandbox(container)
        var_address_part = var_address_part.to_s
        container.set(var_address_part, nil)
    end
    return false
end

#delete_variable(var_address_parts, stacklevel = -1)) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 126

def delete_variable(var_address_parts, stacklevel=-1)
    variables = get_stack_for_address_part(var_address_parts[0], stacklevel)
    var_idx = var_address_parts[1]
    if (var_address_parts.length <= 2)
        variables[var_idx] = nil if (var_idx < variables.length)
        return true
    end
    delete_entry(variables[var_idx], var_address_parts[2..-1])
end

#get_entry(container, var_address, empty_is_null) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 217

def get_entry(container, var_address, empty_is_null)
    entry = ''
    var_address_part = var_address[0]
    if (Helper.is_array(container) || Helper.is_string(container))
        if (Helper.is_number_string(var_address_part))
            var_address_part = Integer(var_address_part)
        end
        if (!Helper.is_number(var_address_part))
            var_address_part = 0 #TODO: for paypal, transactions is missing if the count is zero; this "fixes" it but might break other things.
#                        raise Errors::InternalError.new('Invalid index while indexing into an array or string')
        end
        if (!(empty_is_null && var_address_part >= container.length))
            entry = container[var_address_part]
        end
    elsif (Helper.is_object(container))
        if (!Helper.is_string(var_address_part))
            var_address_part = var_address_part.to_s
        end
        if (container.nil? && (!empty_is_null))
            entry = {}
        else
            begin
                entry = container.instance_variable_get('@' + var_address_part) # will fail if var_address_part starts with a number; set to nil if doesn't exist
            rescue
            end
            if !entry
                begin
                    entry = container[var_address_part]
                rescue
                    begin
                        entry = container.send(Helper.to_snake_case(var_address_part))
                    rescue
                        entry = {}
                        Helper.debug
                    end
                end
            end
        end
    elsif Helper.is_sandbox(container)
        if (!Helper.is_string(var_address_part))
            var_address_part = var_address_part.to_s
        end
        entry = container.get(var_address_part)
    end
    if (var_address.length > 1)
        return get_entry(   entry, var_address[1..-1], empty_is_null)
    end
    return entry
end

#get_parameter(idx, stacklevel) ⇒ Object



352
353
354
355
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 352

def get_parameter(idx, stacklevel)
    return nil if (@parameters_stack.length === 0 || stacklevel >= @parameters_stack.length || idx >= @parameters_stack[stacklevel].length)
    @parameters_stack[stacklevel][idx]
end

#get_params_stackObject



356
357
358
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 356

def get_params_stack
    @parameters_stack
end

#get_stack_for_address_part(part, stacklevel) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 135

def get_stack_for_address_part(part, stacklevel)
    if (part == 'L')
        variables = @local_variables_stack[stacklevel]
    elsif (part == 'P')
        variables = @parameters_stack[stacklevel]
    elsif (part == 'S')
        variables = @persistent_storage
    else
        raise Errors::InternalError.new("Could not attribute variable part #{part}")
    end
    return variables
end

#get_variable(var_address, stacklevel = -1,, empty_is_null = false) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 111

def get_variable(var_address, stacklevel=-1, empty_is_null=false)
    if Helper.is_var_address(var_address)
        var_address_parts = decode_variable_address(var_address)
    else
        var_address_parts = var_address
    end
    variables = get_stack_for_address_part(var_address_parts[0], stacklevel)

    return nil if (empty_is_null && var_address_parts[1] >= variables.length)

    local_entry = variables[var_address_parts[1]]
    return local_entry if (var_address_parts.length <= 2)
    get_entry(local_entry, var_address_parts[2..-1], empty_is_null)
end

#increment_current_service_code_line(amount) ⇒ Object



62
63
64
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 62

def increment_current_service_code_line(amount)
    @code_line_stack[-1] += amount if @code_line_stack.length
end

#is_sandboxObject



14
15
16
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 14

def is_sandbox
    true
end

#load_state_from_string(saved_state) ⇒ Object



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 331

def load_state_from_string(saved_state)
    savelist = JSON.parse(saved_state)

    # TODO: Add support for objects such as CustomDate
    @code_function_name_stack = savelist[0]
    @code_line_stack = savelist[1]
    @local_variables_stack = savelist[2]
    merge_stacks = lambda { |json_stack, stack|
        (json_stack.length).times do |i|
            if (i >= stack.length)
                stack.push(json_stack[i])
            else
                stack[i] = json_stack[i]
            end
        end
    }
    json_parameters_stack = savelist[3]
    json_persistent_storage = savelist[4]
    merge_stacks(json_parameters_stack, @parameters_stack)
    merge_stacks(json_persistent_storage, @persistent_storage)
end

#return_from_functionObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 65

def return_from_function
    return if (@code_function_name_stack.length <= 1)

    call_function_command_parameters = Interpreter.decode_command_parameters(@service_code[@code_function_name_stack[-2]][@code_line_stack[-2]])

    (0..call_function_command_parameters.length - 1).each do |i|
        parameter_var = call_function_command_parameters[i]
        if Helper.is_var_address(parameter_var)
            value = @parameters_stack[-1][i - 1]
            set_variable(parameter_var, value, -2)
        end
    end
    @code_function_name_stack.pop()
    @code_line_stack.pop()
    @local_variables_stack.pop()
    @parameters_stack.pop()
    increment_current_service_code_line(1)
end

#save_state_to_stringObject



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 319

def save_state_to_string
    savelist = []
    savelist.push(@code_function_name_stack)
    savelist.push(@code_line_stack)
    savelist.push(@local_variables_stack)
    savelist.push(@parameters_stack)
    savelist.push(@persistent_storage)
    as_json = savelist.to_json
    # TODO: Add support for objects such as CustomDate

    return as_json
end

#set_entry(container, var_address, value) ⇒ Object



168
169
170
171
172
173
174
175
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 168

def set_entry(container, var_address, value)
    if (var_address.length > 1)
        next_container = get_entry(container, var_address[0..-2], false)

        return set_entry(next_container, var_address[1..-1], value)
    end
    var_address_part = var_address[0]

    if (Helper.is_array(container))
        if (Helper.is_number_string(var_address_part))
            var_address_part = Integer(var_address_part)
        end
        if (!Helper.is_number(var_address_part) || var_address_part >= container.length)
            raise Errors::InternalError.new('Invalid index while indexing into an array')
        end
        idx = var_address_part
        list = container
        if (list.length + @LIST_MAX_ADD_JUMP_ALLOWED <= idx)
            (idx - list.length + 1).times do |i|
                list.push(nil)
            end
        end
        list[idx] = value
    elsif (Helper.is_object(container))
        if (!Helper.is_string(var_address_part))
            var_address_part = var_address_part.to_s
        end

        #There are three ways to interpret an address; we try them all
        begin
            container.instance_variable_set('@' + var_address_part, value)
        rescue
        end
        begin
            container.send(Helper.to_snake_case(var_address_part) + "=", value)
        rescue
        end
        begin
            container[var_address_part] = value
        rescue
        end
elsif Helper.is_sandbox(container)
        if (!Helper.is_string(var_address_part))
            var_address_part = var_address_part.to_s
        end
        container.set(var_address_part, value)
    end
    return false
end

#set_thrown_error(thrown_error) ⇒ Object



31
32
33
34
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 31

def set_thrown_error(thrown_error)
    Helper.debug
    @thrown_error = thrown_error
end

#set_variable(var_address, value, stacklevel = -1)) ⇒ Object



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
# File 'lib/cloudrail_si/servicecode/Sandbox.rb', line 83

def set_variable(var_address, value, stacklevel=-1)
    if Helper.is_var_address(var_address)
        var_address_parts = decode_variable_address(var_address)
    else
        var_address_parts = var_address
    end

    variables = get_stack_for_address_part(var_address_parts[0], stacklevel)
    var_idx = var_address_parts[1]
    if (var_address_parts.length <= 2)
        if (variables.length == var_idx)
            variables.push(value)
        elsif (variables.length > var_idx)
            variables[var_idx] = value
        elsif (variables.length + @LIST_MAX_ADD_JUMP_ALLOWED > var_idx)
            (0..var_idx - variables.length + 1).each do |i|
                variables.push(nil)
            end
            variables[var_idx] = value
        else
            raise Errors::InternalError.new("Could not decode variable #{var_address_parts.join(':')}")
        end
        return true
    end

    return set_entry(variables[var_idx], var_address_parts[2..-1], value)
end