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 20.1 KB
Newer Older
Tobias Pape's avatar
Tobias Pape committed
1
from rpython.rlib.rrandom import Random
Carl Friedrich Bolz's avatar
Carl Friedrich Bolz committed
2
from rpython.rlib import jit
Tobias Pape's avatar
Tobias Pape committed
3

Stefan Marr's avatar
Stefan Marr committed
4
from som.interpreter.interpreter import Interpreter
Stefan Marr's avatar
Stefan Marr committed
5
from som.interpreter.bytecodes   import Bytecodes 
6
7
from som.interpreter.frame       import Frame
 
Stefan Marr's avatar
Stefan Marr committed
8
9
from som.vm.symbol_table         import SymbolTable
from som.vmobjects.object        import Object
10
11
12
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
13
from som.vmobjects.method        import Method
Stefan Marr's avatar
Stefan Marr committed
14
from som.vmobjects.integer       import Integer
15
from som.vmobjects.string        import String
16
from som.vmobjects.block         import Block, block_evaluation_primitive
17
from som.vmobjects.biginteger    import BigInteger
18
from som.vmobjects.double        import Double
Stefan Marr's avatar
Stefan Marr committed
19

Stefan Marr's avatar
Stefan Marr committed
20
21
from som.vm.shell import Shell

22
23
import som.compiler.sourcecode_compiler as sourcecode_compiler

Stefan Marr's avatar
Stefan Marr committed
24
import os
Tobias Pape's avatar
Tobias Pape committed
25
26
import time

27

28
29
from rlib.exit  import Exit
from rlib.osext import path_split
Stefan Marr's avatar
Stefan Marr committed
30

Carl Friedrich Bolz's avatar
Carl Friedrich Bolz committed
31
32
33
34

class GlobalVersion(object):
    pass

Stefan Marr's avatar
Stefan Marr committed
35
class Universe(object):
36
37
38
    
    CURRENT = None
    
Carl Friedrich Bolz's avatar
Carl Friedrich Bolz committed
39
    _immutable_fields_ = [
40
41
42
43
44
45
46
            "nilObject",
            "trueObject",
            "falseObject",
            "objectClass",
            "integerClass",
            "doubleClass",
            "primitiveClass",
Carl Friedrich Bolz's avatar
Carl Friedrich Bolz committed
47
48
49
            "_global_version?",
            ]

50
    def __init__(self, avoid_exit = False):
Stefan Marr's avatar
Stefan Marr committed
51
52
53
54
        self._interpreter    = Interpreter(self)
        self._symbol_table   = SymbolTable()
        
        self._globals        = {}
Carl Friedrich Bolz's avatar
Carl Friedrich Bolz committed
55
56
        self._global_version = GlobalVersion()

Tobias Pape's avatar
Tobias Pape committed
57
58
59
60
61
62
        self.nilObject      = None
        self.trueObject     = None
        self.falseObject    = None
        self.objectClass    = None
        self.classClass     = None
        self.metaclassClass = None
Stefan Marr's avatar
Stefan Marr committed
63
        
Tobias Pape's avatar
Tobias Pape committed
64
65
66
67
68
69
70
71
72
        self.nilClass       = None
        self.integerClass   = None
        self.bigintegerClass= None
        self.arrayClass     = None
        self.methodClass    = None
        self.symbolClass    = None
        self.primitiveClass = None
        self.systemClass    = None
        self.blockClass     = None
73
        self.blockClasses   = None
Tobias Pape's avatar
Tobias Pape committed
74
75
        self.stringClass    = None
        self.doubleClass    = None
Stefan Marr's avatar
Stefan Marr committed
76
77
        
        self._last_exit_code = 0
78
        self._avoid_exit     = avoid_exit
Tobias Pape's avatar
Tobias Pape committed
79
        self._dump_bytecodes = False
Stefan Marr's avatar
Stefan Marr committed
80
        self.classpath       = None
Tobias Pape's avatar
Tobias Pape committed
81
82
83
        self.start_time      = time.time() # a float of the time in seconds
        self.random          = Random(abs(int(time.clock() * time.time())))

84
        CURRENT = self
85

86
87
88
89
    def exit(self, error_code):
        if self._avoid_exit:
            self._last_exit_code = error_code
        else:
90
            raise Exit(error_code)
91
92
93
94
    
    def last_exit_code(self):
        return self._last_exit_code
    
95
96
97
    def get_interpreter(self):
        return self._interpreter
    
98
99
100
101
102
103
104
    def execute_method(self, class_name, selector):
        self._initialize_object_system()

        clazz = self.load_class(self.symbol_for(class_name))

        bootstrap_method = self._create_bootstrap_method()
        bootstrap_frame  = self._create_bootstrap_frame(bootstrap_method, clazz)
Stefan Marr's avatar
Stefan Marr committed
105
        
106
107
108
109
110
        # Lookup the invokable on class
        invokable = clazz.get_class(self).lookup_invokable(self.symbol_for(selector))
        
        invokable.invoke(bootstrap_frame, self._interpreter)
        return bootstrap_frame.pop()
Stefan Marr's avatar
Stefan Marr committed
111
    
112
113
    def _create_bootstrap_method(self):
        # Create a fake bootstrap method to simplify later frame traversal
114
        bootstrap_method = self.new_method(self.symbol_for("bootstrap"), 1, [],
115
116
                                           self.new_integer(0),
                                           self.new_integer(2))
117
        bootstrap_method.set_bytecode(0, Bytecodes.halt)
Tobias Pape's avatar
Tobias Pape committed
118
        bootstrap_method.set_holder(self.systemClass)
119
        return bootstrap_method
120
    
121
122
    def _create_bootstrap_frame(self, bootstrap_method, receiver, arguments = None):
        # Create a fake bootstrap frame with the system object on the stack
123
        bootstrap_frame = self._interpreter.new_frame(None, bootstrap_method, None)
124
125
126
        bootstrap_frame.push(receiver)
        
        if arguments:
127
            bootstrap_frame.push(arguments)
128
        return bootstrap_frame
129
130
        
    
Stefan Marr's avatar
Stefan Marr committed
131
132
    def interpret(self, arguments):
        # Check for command line switches
133
        arguments = self.handle_arguments(arguments)
Stefan Marr's avatar
Stefan Marr committed
134
135

        # Initialize the known universe
136
137
        system_object = self._initialize_object_system()
        bootstrap_method = self._create_bootstrap_method()
Stefan Marr's avatar
Stefan Marr committed
138
        
139
140
141
142
143
144
145
        # Start the shell if no filename is given
        if len(arguments) == 0:
            shell = Shell(self, self._interpreter)
            shell.set_bootstrap_method(bootstrap_method)
            shell.start()
            return
        else:
146
147
148
            # Convert the arguments into an array
            arguments_array = self.new_array_with_strings(arguments)
            bootstrap_frame = self._create_bootstrap_frame(bootstrap_method, system_object, arguments_array)
149
            # Lookup the initialize invokable on the system class
Tobias Pape's avatar
Tobias Pape committed
150
            initialize = self.systemClass.lookup_invokable(self.symbol_for("initialize:"))
151
            return initialize.invoke(bootstrap_frame, self._interpreter)
Stefan Marr's avatar
Stefan Marr committed
152
    
153
    def handle_arguments(self, arguments):
Stefan Marr's avatar
Stefan Marr committed
154
155
156
157
158
159
160
161
        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()
162
                self.setup_classpath(arguments[i + 1])
Stefan Marr's avatar
Stefan Marr committed
163
164
165
166
                i += 1    # skip class path
                got_classpath = True
            elif arguments[i] == "-d":
                self._dump_bytecodes = True
Tobias Pape's avatar
Tobias Pape committed
167
168
            elif arguments[i] in ["-h", "--help", "-?"]:
                self._print_usage_and_exit()
Stefan Marr's avatar
Stefan Marr committed
169
170
            else:
                remaining_args.append(arguments[i])
Tobias Pape's avatar
Tobias Pape committed
171
            i += 1
Stefan Marr's avatar
Stefan Marr committed
172
173
174
    
        if not got_classpath:
            # Get the default class path of the appropriate size
Tobias Pape's avatar
Tobias Pape committed
175
            self.classpath = self._setup_default_classpath()
Stefan Marr's avatar
Stefan Marr committed
176
177
178
179
180
181
182

        # 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
Tobias Pape's avatar
Tobias Pape committed
183
                self.classpath.insert(0, split[0])
Stefan Marr's avatar
Stefan Marr committed
184
185
186
187
188
189
        
            remaining_args[i] = split[1]
            i += 1
        
        return remaining_args
    
190
    def setup_classpath(self, cp):
Tobias Pape's avatar
Tobias Pape committed
191
        self.classpath = cp.split(os.pathsep)
Stefan Marr's avatar
Stefan Marr committed
192
    
193
194
195
    def _setup_default_classpath(self):
        return ['.']
    
Stefan Marr's avatar
Stefan Marr committed
196
197
198
    # take argument of the form "../foo/Test.som" and return
    # "../foo", "Test", "som"
    def _get_path_class_ext(self, path):
199
        return path_split(path)
200
201
202
    
    def _print_usage_and_exit(self):
        # Print the usage
203
204
205
206
        std_println("Usage: som [-options] [args...]                          ")
        std_println("                                                         ")
        std_println("where options include:                                   ")
        std_println("    -cp <directories separated by " + os.pathsep     + ">")
Tobias Pape's avatar
Tobias Pape committed
207
208
209
        std_println("        set search path for application classes")
        std_println("    -d  enable disassembling")
        std_println("    -h  print this help")
210
211
212

        # Exit
        self.exit(0)
Stefan Marr's avatar
Stefan Marr committed
213

214
    def _initialize_object_system(self):
Stefan Marr's avatar
Stefan Marr committed
215
        # Allocate the nil object
Tobias Pape's avatar
Tobias Pape committed
216
        self.nilObject = Object(None)
Stefan Marr's avatar
Stefan Marr committed
217
218

        # Allocate the Metaclass classes
Tobias Pape's avatar
Tobias Pape committed
219
        self.metaclassClass = self.new_metaclass_class()
Stefan Marr's avatar
Stefan Marr committed
220
221

        # Allocate the rest of the system classes
Tobias Pape's avatar
Tobias Pape committed
222
223
224
225
226
227
228
229
230
231
232
        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.primitiveClass  = self.new_system_class()
        self.stringClass     = self.new_system_class()
        self.doubleClass     = self.new_system_class()
Stefan Marr's avatar
Stefan Marr committed
233
234

        # Setup the class reference for the nil object
Tobias Pape's avatar
Tobias Pape committed
235
        self.nilObject.set_class(self.nilClass)
Stefan Marr's avatar
Stefan Marr committed
236
237

        # Initialize the system classes
Tobias Pape's avatar
Tobias Pape committed
238
239
240
241
242
243
244
245
246
247
248
249
        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.primitiveClass,  self.objectClass, "Primitive")
        self._initialize_system_class(self.stringClass,     self.objectClass, "String")
        self._initialize_system_class(self.doubleClass,     self.objectClass, "Double")
Stefan Marr's avatar
Stefan Marr committed
250
251

        # Load methods and fields into the system classes
Tobias Pape's avatar
Tobias Pape committed
252
253
254
255
256
257
258
259
260
261
262
263
        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.primitiveClass)
        self._load_system_class(self.stringClass)
        self._load_system_class(self.doubleClass)
Stefan Marr's avatar
Stefan Marr committed
264
265

        # Load the generic block class
Tobias Pape's avatar
Tobias Pape committed
266
        self.blockClass = self.load_class(self.symbol_for("Block"))
Stefan Marr's avatar
Stefan Marr committed
267
268

        # Setup the true and false objects
269
270
271
272
273
274
275
        trueClassName    = self.symbol_for("True")
        trueClass        = self.load_class(trueClassName)
        self.trueObject  = self.new_instance(trueClass)
        
        falseClassName   = self.symbol_for("False")
        falseClass       = self.load_class(falseClassName)
        self.falseObject = self.new_instance(falseClass)
Stefan Marr's avatar
Stefan Marr committed
276
277

        # Load the system class and create an instance of it
Tobias Pape's avatar
Tobias Pape committed
278
279
        self.systemClass = self.load_class(self.symbol_for("System"))
        system_object = self.new_instance(self.systemClass)
Stefan Marr's avatar
Stefan Marr committed
280
281

        # Put special objects and classes into the dictionary of globals
Tobias Pape's avatar
Tobias Pape committed
282
283
284
        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)
285
        self.set_global(self.symbol_for("system"), system_object)
Tobias Pape's avatar
Tobias Pape committed
286
287
        self.set_global(self.symbol_for("System"), self.systemClass)
        self.set_global(self.symbol_for("Block"),  self.blockClass)
288
289
290
291
292
        
        self.set_global(self.symbol_for("Nil"),    self.nilClass)
        
        self.set_global( trueClassName,  trueClass)
        self.set_global(falseClassName, falseClass)
293
294
295
296

        self.blockClasses = [self.blockClass] + \
                [self._make_block_class(i) for i in [1, 2, 3]]

297
298
        return system_object
    
299
300
301
302
303
304
305
306
307
    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
308
309
    
    def new_array_with_length(self, length):
310
        return Array(self.nilObject, length)
311
312
313
314
315
316
317
318
  
    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])
319

320
321
322
323
324
325
326
327
328
329
330
        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 result
331
    
332
    def new_block(self, method, context_frame):
333
        return Block(method, context_frame)
334
335
336

    def new_class(self, class_class):
        # Allocate a new class and set its class to be the given class class
Stefan Marr's avatar
Stefan Marr committed
337
        result = Class(self, class_class.get_number_of_instance_fields())
338
339
340
        result.set_class(class_class)
        return result

341
    def new_frame(self, previous_frame, method, context):
342
343
344
345
346
347
348
        # 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)

349
        result = Frame(self.nilObject, length, method, context, previous_frame)
350
351
352
        result.reset_stack_pointer()
        return result

353
    def new_method(self, signature, num_bytecodes, literals,
354
                   num_locals, maximum_number_of_stack_elements):
355
356
        return Method(literals, num_locals, maximum_number_of_stack_elements,
                      num_bytecodes, signature)
357
358

    def new_instance(self, instance_class):
Tobias Pape's avatar
Tobias Pape committed
359
        result = Object(self.nilObject, instance_class.get_number_of_instance_fields())
360
361
362
363
        result.set_class(instance_class)
        return result

    def new_integer(self, value):
Tobias Pape's avatar
Tobias Pape committed
364
        assert isinstance(value, int)
365
        return Integer(value)
366
 
367
    def new_biginteger(self, value):
368
        return BigInteger(value)
369
370
 
    def new_double(self, value):
371
        return Double(value)
372
    
373
374
375
376
377
378
    def new_metaclass_class(self):
        # Allocate the metaclass classes
        result = Class(self)
        result.set_class(Class(self))

        # Setup the metaclass hierarchy
379
        result.get_class(self).set_class(result)
380
381
382

        # Return the freshly allocated metaclass class
        return result
383
384

    def new_string(self, embedded_string):
385
        return String(embedded_string)
386
    
387
    def new_symbol(self, string):
388
        result = Symbol(string)
389
390
391
392
393

        # Insert the new symbol into the symbol table
        self._symbol_table.insert(result)
        return result
      
394
395
396
397
398
399
    def new_system_class(self):
        # Allocate the new system class
        system_class = Class(self)

        # Setup the metaclass hierarchy
        system_class.set_class(Class(self))
400
        system_class.get_class(self).set_class(self.metaclassClass)
401
402
403
404
405
406
407

        # 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
            system_class.get_class(self).set_super_class(super_class.get_class(self))
410
        else:
411
            system_class.get_class(self).set_super_class(self.classClass)
412
413
414

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

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

        # Initialize the name of the system class
        system_class.set_name(self.symbol_for(name))
423
        system_class.get_class(self).set_name(self.symbol_for(name + " class"))
424
425
426
427
428
429
430

        # 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
Carl Friedrich Bolz's avatar
Carl Friedrich Bolz committed
431
432
433
434
435
436
        # if not, return None
        jit.promote(self)
        return self._get_global(name, self._global_version)

    @jit.elidable
    def _get_global(self, name, version):
437
        return self._globals.get(name, None)
438
439
440
441

    def set_global(self, name, value):
        # Insert the given value into the dictionary of globals
        self._globals[name] = value
Carl Friedrich Bolz's avatar
Carl Friedrich Bolz committed
442
        self._global_version = GlobalVersion()
443
444
445
446

    def has_global(self, name):
        # Returns if the universe has a value for the global of the given name
        return name in self._globals
447
448
449
450
451

    def _get_block_class(self, number_of_arguments):
        return self.blockClasses[number_of_arguments]

    def _make_block_class(self, number_of_arguments):
452
453
454
455
456
        # Compute the name of the block class with the given number of
        # arguments
        name = self.symbol_for("Block" + str(number_of_arguments))

        # Get the block class for blocks with the given number of arguments
Stefan Marr's avatar
Stefan Marr committed
457
        result = self._load_class(name, None)
458
459

        # Add the appropriate value primitive to the block class
460
        result.add_instance_primitive(block_evaluation_primitive(number_of_arguments, self))
461
462
463
464
465
466
467
468
469

        # 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
470
471
472
        result = self.get_global(name)
        if result is not None:
            return result
473
474
475
476
477
478
479
480
481
482
483
484
485
486

        # 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)

487
488
489
490
491
492
493
        if not result:
            error_println(system_class.get_name().get_string()
                   + " class could not be loaded. It is likely that the "
                   + " class path has not been initialized properly. "
                   + "Please make sure that the '-cp' parameter is given on the command-line.")
            self.exit(200)

494
495
496
497
498
499
        # 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
Tobias Pape's avatar
Tobias Pape committed
500
        for cpEntry in self.classpath:
501
502
            try:
                # Load the class from a file and return the loaded class
503
                result = sourcecode_compiler.compile_class_from_file(cpEntry, name.get_string(), system_class, self)
504
                if self._dump_bytecodes:
505
                    from som.compiler.disassembler import dump
506
                    dump(result.get_class(self))
507
                    dump(result)
508
509
510
511
512
513
514
515

                return result
            except IOError:
                # Continue trying different paths
                pass

        # The class could not be found.
        return None
516
517
518
519
    
    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)
Stefan Marr's avatar
Stefan Marr committed
520
        if self._dump_bytecodes:
521
522
            from som.compiler.disassembler import dump
            dump(result)
523
524
        return result

525
def error_print(msg):
Stefan Marr's avatar
Stefan Marr committed
526
    os.write(2, msg or "")
527

528
def error_println(msg = ""):
529
    os.write(2, msg + "\n")
530

531
def std_print(msg):
Stefan Marr's avatar
Stefan Marr committed
532
    os.write(1, msg or "")
533

Stefan Marr's avatar
Stefan Marr committed
534
535
def std_println(msg = ""):
    os.write(1, msg + "\n")
536

537
538
539
540
541
def main(args):
    u = Universe()
    u.interpret(args[1:])
    u.exit(0)

542
def get_current():
543
    return Universe.CURRENT
544

545
if __name__ == '__main__':
546
547
548
549
550
    import sys
    try:
        main(sys.argv)
    except Exit as e:
        sys.exit(e.code)