WARNING! Access to this system is limited to authorised users only.
Unauthorised users may be subject to prosecution.
Unauthorised access to this system is a criminal offence under Australian law (Federal Crimes Act 1914 Part VIA)
It is a criminal offence to:
(1) Obtain access to data without authority. -Penalty 2 years imprisonment.
(2) Damage, delete, alter or insert data without authority. -Penalty 10 years imprisonment.
User activity is monitored and recorded. Anyone using this system expressly consents to such monitoring and recording.

To protect your data, the CISO officer has suggested users to enable 2FA as soon as possible.
Currently 2.7% of users enabled 2FA.

universe.py 19.6 KB
Newer Older
1
2
from __future__ import print_function

Stefan Marr's avatar
Stefan Marr committed
3
from som.interpreter.interpreter import Interpreter
Stefan Marr's avatar
Stefan Marr committed
4
from som.interpreter.bytecodes   import Bytecodes 
Stefan Marr's avatar
Stefan Marr committed
5
6
from som.vm.symbol_table         import SymbolTable
from som.vmobjects.object        import Object
7
8
9
from som.vmobjects.clazz         import Class
from som.vmobjects.array         import Array
from som.vmobjects.symbol        import Symbol
Stefan Marr's avatar
Stefan Marr committed
10
from som.vmobjects.method        import Method
Stefan Marr's avatar
Stefan Marr committed
11
from som.vmobjects.integer       import Integer
12
13
14
15
16
from som.vmobjects.string        import String
from som.vmobjects.block         import Block
from som.vmobjects.frame         import Frame
from som.vmobjects.big_integer   import BigInteger
from som.vmobjects.double        import Double
Stefan Marr's avatar
Stefan Marr committed
17

18
19
import som.compiler.sourcecode_compiler as sourcecode_compiler

Stefan Marr's avatar
Stefan Marr committed
20
import os
21
import sys
Stefan Marr's avatar
Stefan Marr committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

class Universe(object):
    
    def __init__(self):
        self._interpreter    = Interpreter(self)
        self._symbol_table   = SymbolTable()
        
        self._globals        = {}
        
        self._nilObject      = None
        self._trueObject     = None
        self._falseObject    = None
        self._objectClass    = None
        self._classClass     = None
        self._metaclassClass = None
        
        self._nilClass       = None
        self._integerClass   = None
        self._bigintegerClass= None
        self._arrayClass     = None
        self._methodClass    = None
        self._symbolClass    = None
        self._frameClass     = None
        self._primitiveClass = None
        self._systemClass    = None
        self._blockClass     = None
        self._doubleClass    = None
        
        self._last_exit_code = 0
        self._classpath      = None
        self._dump_bytecodes = False        
53
54
55
56
    
    @property
    def nilObject(self):
        return self._nilObject
Stefan Marr's avatar
Stefan Marr committed
57
    
58
59
60
61
62
63
64
65
    @property
    def trueObject(self):
        return self._trueObject
    
    @property
    def falseObject(self):
        return self._falseObject
    
Stefan Marr's avatar
Stefan Marr committed
66
67
68
    @property
    def primitiveClass(self):
        return self._primitiveClass
69
70
71
72
    
    @property
    def metaclassClass(self):
        return self._metaclassClass
Stefan Marr's avatar
Stefan Marr committed
73
74
        
    
75
76
77
78
79
80
81
82
    def _create_bootstrap_method(self):
        # Create a fake bootstrap method to simplify later frame traversal
        bootstrap_method = self.new_method(self.symbol_for("bootstrap"), 1, 0)
        bootstrap_method.set_bytecode(0, Bytecodes.halt)
        bootstrap_method.set_number_of_locals(self.new_integer(0))
        bootstrap_method.set_maximum_number_of_stack_elements(self.new_integer(2))
        bootstrap_method.set_holder(self._systemClass)
    
Stefan Marr's avatar
Stefan Marr committed
83
84
    def interpret(self, arguments):
        # Check for command line switches
85
        arguments = self.handle_arguments(arguments)
Stefan Marr's avatar
Stefan Marr committed
86
87
88
89
90
91

        # Initialize the known universe
        self.initialize(arguments)
        
        self.start()
    
92
    def handle_arguments(self, arguments):
Stefan Marr's avatar
Stefan Marr committed
93
94
95
96
97
98
99
100
        got_classpath  = False
        remaining_args = []

        i = 0
        while i < len(arguments):
            if arguments[i] == "-cp":
                if i + 1 >= len(arguments):
                    self._print_usage_and_exit()
101
                self.setup_classpath(arguments[i + 1])
Stefan Marr's avatar
Stefan Marr committed
102
103
104
105
106
107
108
109
110
111
                i += 1    # skip class path
                got_classpath = True
            elif arguments[i] == "-d":
                self._dump_bytecodes = True
            else:
                remaining_args.append(arguments[i])
            i += 1 
    
        if not got_classpath:
            # Get the default class path of the appropriate size
112
            self._classpath = self._setup_default_classpath()
Stefan Marr's avatar
Stefan Marr committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126

        # check remaining args for class paths, and strip file extension
        i = 0
        while i < len(remaining_args):
            split = self._get_path_class_ext(remaining_args[i])

            if split[0] != "":  # there was a path
                self._classpath.insert(0, split[0])
        
            remaining_args[i] = split[1]
            i += 1
        
        return remaining_args
    
127
    def setup_classpath(self, cp):
Stefan Marr's avatar
Stefan Marr committed
128
129
        self._classpath = cp.split(os.pathsep)
    
130
131
132
    def _setup_default_classpath(self):
        return ['.']
    
Stefan Marr's avatar
Stefan Marr committed
133
134
135
136
137
138
139
    # take argument of the form "../foo/Test.som" and return
    # "../foo", "Test", "som"
    def _get_path_class_ext(self, path):
        (path, file_name) = os.path.split(path)
        (file_name, ext)  = os.path.splitext(file_name)
        return (path, file_name, ext[1:])

140
    def _initialize_object_system(self):
Stefan Marr's avatar
Stefan Marr committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
        # Allocate the nil object
        self._nilObject = Object(None)

        # Allocate the Metaclass classes
        self._metaclassClass = self.new_metaclass_class()

        # Allocate the rest of the system classes
        self._objectClass     = self.new_system_class()
        self._nilClass        = self.new_system_class()
        self._classClass      = self.new_system_class()
        self._arrayClass      = self.new_system_class()
        self._symbolClass     = self.new_system_class()
        self._methodClass     = self.new_system_class()
        self._integerClass    = self.new_system_class()
        self._bigintegerClass = self.new_system_class()
        self._frameClass      = self.new_system_class()
        self._primitiveClass  = self.new_system_class()
        self._stringClass     = self.new_system_class()
        self._doubleClass     = self.new_system_class()

        # Setup the class reference for the nil object
162
        self._nilObject.set_class(self._nilClass)
Stefan Marr's avatar
Stefan Marr committed
163
164
165
166
167
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

        # Initialize the system classes
        self._initialize_system_class(self._objectClass,                  None, "Object")
        self._initialize_system_class(self._classClass,      self._objectClass, "Class")
        self._initialize_system_class(self._metaclassClass,   self._classClass, "Metaclass")
        self._initialize_system_class(self._nilClass,        self._objectClass, "Nil")
        self._initialize_system_class(self._arrayClass,      self._objectClass, "Array")
        self._initialize_system_class(self._methodClass,      self._arrayClass, "Method")
        self._initialize_system_class(self._symbolClass,     self._objectClass, "Symbol")
        self._initialize_system_class(self._integerClass,    self._objectClass, "Integer")
        self._initialize_system_class(self._bigintegerClass, self._objectClass, "BigInteger")
        self._initialize_system_class(self._frameClass,       self._arrayClass, "Frame")
        self._initialize_system_class(self._primitiveClass,  self._objectClass, "Primitive")
        self._initialize_system_class(self._stringClass,     self._objectClass, "String")
        self._initialize_system_class(self._doubleClass,     self._objectClass, "Double")

        # Load methods and fields into the system classes
        self._load_system_class(self._objectClass)
        self._load_system_class(self._classClass)
        self._load_system_class(self._metaclassClass)
        self._load_system_class(self._nilClass)
        self._load_system_class(self._arrayClass)
        self._load_system_class(self._methodClass)
        self._load_system_class(self._symbolClass)
        self._load_system_class(self._integerClass)
        self._load_system_class(self._bigintegerClass)
        self._load_system_class(self._frameClass)
        self._load_system_class(self._primitiveClass)
        self._load_system_class(self._stringClass)
        self._load_system_class(self._doubleClass)

        # Load the generic block class
Stefan Marr's avatar
Stefan Marr committed
195
        self._blockClass = self.load_class(self.symbol_for("Block"))
Stefan Marr's avatar
Stefan Marr committed
196
197

        # Setup the true and false objects
Stefan Marr's avatar
Stefan Marr committed
198
199
        self._trueObject  = self._new_instance(self.load_class(self.symbol_for("True")))
        self._falseObject = self._new_instance(self.load_class(self.symbol_for("False")))
Stefan Marr's avatar
Stefan Marr committed
200
201

        # Load the system class and create an instance of it
Stefan Marr's avatar
Stefan Marr committed
202
        self._systemClass = self.load_class(self.symbol_for("System"))
Stefan Marr's avatar
Stefan Marr committed
203
204
205
        system_object = self._new_instance(self._systemClass)

        # Put special objects and classes into the dictionary of globals
206
207
208
209
210
211
        self.set_global(self.symbol_for("nil"),    self._nilObject)
        self.set_global(self.symbol_for("true"),   self._trueObject)
        self.set_global(self.symbol_for("false"),  self._falseObject)
        self.set_global(self.symbol_for("system"), self._systemObject)
        self.set_global(self.symbol_for("System"), self._systemClass)
        self.set_global(self.symbol_for("Block"),  self._blockClass)
212
213
        return system_object
    
214
215
216
217
218
219
220
221
222
    def symbol_for(self, string):
        # Lookup the symbol in the symbol table
        result = self._symbol_table.lookup(string)
        if result:
            return result
        
        # Create a new symbol and return it
        result = self.new_symbol(string)
        return result
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
    
    def new_array_with_length(self, length):
        # Allocate a new array and set its class to be the array class
        result = Array(self._nilObject)
        result.set_class(self._arrayClass)

        # Set the number of indexable fields to the given value (length)
        result.set_number_of_indexable_fields_and_clear(length, self._nilObject)

        # Return the freshly allocated array
        return result
  
    def new_array_from_list(self, values):
        # Allocate a new array with the same length as the list
        result = self.new_array_with_length(len(values))

        # Copy all elements from the list into the array
        for i in range(len(values)):
            result.set_indexable_field(i, values[i])
    
        # Return the allocated and initialized array
        return result
  
    def new_array_with_strings(self, strings):
        # Allocate a new array with the same length as the string array
        result = self.new_array_with_length(len(strings))

        # Copy all elements from the string array into the array
        for i in range(len(strings)):
            result.set_indexable_field(i, self.new_string(strings[i]))
    
        # Return the allocated and initialized array
        return result
256
    
257
258
259
260
261
262
263
264
265
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
    def new_block(self, method, context_frame, arguments):
        # Allocate a new block and set its class to be the block class
        result = Block(self._nilObject)
        result.set_class(self._get_block_class(arguments))

        # Set the method and context of block
        result.set_method(method)
        result.set_context(context_frame)

        # Return the freshly allocated block
        return result

    def new_class(self, class_class):
        # Allocate a new class and set its class to be the given class class
        result = Class(class_class.get_number_of_instance_fields(), self)
        result.set_class(class_class)

        # Return the freshly allocated class
        return result

    def new_frame(self, previous_frame, method):
        # Allocate a new frame and set its class to be the frame class
        result = Frame(self._nilObject)
        result.set_class(self._frameClass)

        # Compute the maximum number of stack locations (including arguments,
        # locals and extra buffer to support doesNotUnderstand) and set the
        # number of indexable fields accordingly
        length = (method.get_number_of_arguments() +
                  method.get_number_of_locals().get_embedded_integer() +
                  method.get_maximum_number_of_stack_elements().get_embedded_integer() + 2)
        result.set_number_of_indexable_fields_and_clear(length, self._nilObject)

        # Set the method of the frame and the previous frame
        result.set_method(method)
        if previous_frame:
            result.set_previous_frame(previous_frame)

        # Reset the stack pointer and the bytecode index
        result.reset_stack_pointer()
        result.set_bytecode_index(0)

        # Return the freshly allocated frame
        return result

    def new_method(self, signature, num_bytecodes, num_literals):
        # Allocate a new method and set its class to be the method class
        result = Method(self._nilObject)
        result.set_class(self._methodClass)

        # Set the signature and the number of bytecodes
        result.set_signature(signature)
        result.set_number_of_bytecodes(num_bytecodes)
        result.set_number_of_indexable_fields_and_clear(num_literals, self._nilObject)

        # Return the freshly allocated method
        return result

    def new_instance(self, instance_class):
        # Allocate a new instance and set its class to be the given class
        result = Object(instance_class.get_number_of_instance_fields(), self._nilObject)
        result.set_class(instance_class)
 
        # Return the freshly allocated instance
        return result

 
    def new_integer(self, value):
        # Allocate a new integer and set its class to be the integer class
        result = Integer(self._nilObject)
        result.set_class(self._integerClass)
     
        # Set the embedded integer of the newly allocated integer
        result.set_embedded_integer(value)
     
        # Return the freshly allocated integer
        return result
 
    def new_big_integer(self, value):
        # Allocate a new integer and set its class to be the integer class
        result = BigInteger(self._nilObject)
        result.set_class(self._bigintegerClass)
 
        # Set the embedded integer of the newly allocated integer
341
        result.set_embedded_big_integer(value)
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
 
        # Return the freshly allocated integer
        return result
 
 
    def new_double(self, value):
        # Allocate a new integer and set its class to be the double class
        result = Double(self._nilObject)
        result.set_class(self._doubleClass)
 
        # Set the embedded double of the newly allocated double
        result.set_embedded_double(value)
 
        # Return the freshly allocated double
        return result
    
358
359
360
361
362
363
364
365
366
367
    def new_metaclass_class(self):
        # Allocate the metaclass classes
        result = Class(self)
        result.set_class(Class(self))

        # Setup the metaclass hierarchy
        result.get_class().set_class(result)

        # Return the freshly allocated metaclass class
        return result
368
369
370
371
372
373
374
375
376
377
378

    def new_string(self, embedded_string):
        # Allocate a new string and set its class to be the string class
        result = String(self._nilObject)
        result.set_class(self._stringClass)
 
        # Put the embedded string into the new string
        result.set_embedded_string(embedded_string)
 
        # Return the freshly allocated string
        return result
379
    
380
381
382
383
384
385
386
387
388
389
390
391
392
393
    def new_symbol(self, string):
        # Allocate a new symbol and set its class to be the symbol class
        result = Symbol(self._nilObject)
        result.set_class(self._symbolClass)

        # Put the string into the symbol
        result.set_string(string)

        # Insert the new symbol into the symbol table
        self._symbol_table.insert(result)

        # Return the freshly allocated symbol
        return result
      
394
395
396
397
398
399
400
401
402
403
404
405
406
407
    def new_system_class(self):
        # Allocate the new system class
        system_class = Class(self)

        # Setup the metaclass hierarchy
        system_class.set_class(Class(self))
        system_class.get_class().set_class(self._metaclassClass)

        # Return the freshly allocated system class
        return system_class
    
    def _initialize_system_class(self, system_class, super_class, name):
        # Initialize the superclass hierarchy
        if super_class:
408
            system_class.set_super_class(super_class)
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
            system_class.get_class().set_super_class(super_class.get_class())
        else:
            system_class.get_class().set_super_class(self._classClass)
    

        # Initialize the array of instance fields
        system_class.set_instance_fields(self.new_array_with_length(0))
        system_class.get_class().set_instance_fields(self.new_array_with_length(0))

        # Initialize the array of instance invokables
        system_class.set_instance_invokables(self.new_array_with_length(0))
        system_class.get_class().set_instance_invokables(self.new_array_with_length(0))

        # Initialize the name of the system class
        system_class.set_name(self.symbol_for(name))
424
        system_class.get_class().set_name(self.symbol_for(name + " class"))
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

        # Insert the system class into the dictionary of globals
        self.set_global(system_class.get_name(), system_class)
    
    
    def get_global(self, name):
        # Return the global with the given name if it's in the dictionary of globals
        if self.has_global(name):
            return self._globals[name]

        # Global not found
        return None

    def set_global(self, name, value):
        # Insert the given value into the dictionary of globals
        self._globals[name] = value
  

    def has_global(self, name):
        # Returns if the universe has a value for the global of the given name
        return name in self._globals
446
    
447
    def _get_block_class(self, number_of_arguments = None):
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
        if not number_of_arguments:
            # Get the generic block class
            return self._blockClass
        
        # Compute the name of the block class with the given number of
        # arguments
        name = self.symbol_for("Block" + str(number_of_arguments))

        # Lookup the specific block class in the dictionary of globals and
        # return it
        if self.has_global(name):
            return self.get_global(name)

        # Get the block class for blocks with the given number of arguments
        result = self._loadClass(name, None)

        # Add the appropriate value primitive to the block class
        result.add_instance_primitive(Block.get_evaluation_primitive(number_of_arguments, self))

        # Insert the block class into the dictionary of globals
        self.set_global(name, result)

        # Return the loaded block class
        return result

    def load_class(self, name):
        # Check if the requested class is already in the dictionary of globals
        if self.has_global(name):
            return self.get_global(name)

        # Load the class
        result = self._load_class(name, None)

        # Load primitives (if necessary) and return the resulting class
        if result and result.has_primitives():
            result.load_primitives()
    
        return result

    def _load_system_class(self, system_class):
        # Load the system class
        result = self._load_class(system_class.get_name(), system_class)

        # Load primitives if necessary
        if result.has_primitives():
            result.load_primitives()

    def _load_class(self, name, system_class):
        # Try loading the class from all different paths
        for cpEntry in self._classpath:
            try:
                # Load the class from a file and return the loaded class
500
                result = sourcecode_compiler.compile_class_from_file(cpEntry, name.get_string(), system_class, self)
501
                if self._dump_bytecodes:
502
503
                    Disassembler.dump(result.get_class())
                    Disassembler.dump(result)
504
505
506
507
508
509
510
511

                return result
            except IOError:
                # Continue trying different paths
                pass

        # The class could not be found.
        return None
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
    
    def load_shell_class(self, stmt):
        # Load the class from a stream and return the loaded class
        result = sourcecode_compiler.compile_class_from_string(stmt, None, self)
        if self._dumpBytecodes:
            Disassembler.dump(result)
        return result

    @classmethod
    def error_print(cls, msg):
        print(msg, file=sys.stderr, end="")

    @classmethod
    def error_println(cls, msg = ""):
        print(msg, file=sys.stderr)

    @classmethod
529
    def std_print(cls, msg):
530
        print(msg, end="")
531

532
    @classmethod
533
    def std_println(cls, msg=""):
534
        print(msg)
535
536
537
538
539
540
541
542

def main(args):
    u = Universe()
    u.interpret(args[1:])
    u.exit(0)

if __name__ == '__main__':
    main(sys.argv)