Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
537 lines (468 sloc) 13.8 KB
from async_io import Event, Queue
from evaluator.loader import from_object
from evaluator.sourcemaps import TraceEntry
from rpython.rlib.objectmodel import specialize, always_inline
from rpython.rlib.rstring import UnicodeBuilder
from rpython.rtyper.lltypesystem import rffi
from space.customobject import CustomObject_instantiate
from space import *
import core
import naming
import os
import pathobj
#import stdlib
import sys
import time
import uv_handle
import uv_stream
import uv_timer
import uv_util
import vectormath
import vector
# The base environment
module = Module(u'base', {
u'builtin': Builtin.interface,
u'greenlet': core.Greenlet.interface,
u'interface': Interface.interface,
u'Id': Id.interface,
u'dict': Dict.interface,
u'Module': Module.interface,
u'module': Module.interface, # TODO: deprecate and then remove
u'object': Object.interface,
u'list': List.interface,
u'multimethod': Multimethod.interface,
u'float': Float.interface,
u'float_repr': FloatRepr.interface,
u'int': Integer.interface,
u'bool': Boolean.interface,
u'str': String.interface,
u'null': null,
u'true': true,
u'false': false,
u'path': pathobj.Path.interface,
u'property': Property.interface,
u'Uint8Data': Uint8Data.interface,
u'Uint8Array': Uint8Array.interface,
u'Uint8Slice': Uint8Slice.interface,
u'Uint8Builder': Uint8Builder.interface,
u'Utf8Decoder': Utf8Decoder.interface,
u'StringBuilder': StringBuilder_.interface,
u'schedule': Builtin(core.schedule, u'schedule'),
u'set': Set.interface,
u'slice': Slice.interface,
u'DocRef': naming.DocRef.interface,
u'Event': Event.interface,
u'Queue': Queue.interface,
u'Timer': uv_timer.Timer.interface,
u'Handle': uv_handle.Handle.interface,
u'Stream': uv_stream.Stream.interface,
u'TTY': uv_stream.TTY.interface,
u'Pipe': uv_stream.Pipe.interface,
# The new vector interface, lets see how it fares.
u'Numeric': vector.Numeric.interface,
}, frozen=True)
@signature(String, Module, optional=1)
def module_instantiate(name, extends):
return Module(name.string, {}, extends)
# we may later want to do the same for the stuff you see above.
for error in all_errors:
module.setattr_force(, error.interface)
for _, error in uv_util.errors:
module.setattr_force(, error.interface)
for name, value in operators.by_symbol.iteritems():
module.setattr_force(name, value)
for name, value in vectormath.by_symbol.iteritems():
module.setattr_force(name, value)
def builtin(fn):
name = fn.__name__.rstrip('_').decode('utf-8')
module.setattr_force(name, Builtin(fn, name))
return fn
@signature(Object, Float, optional=1)
def get_name(obj, stale):
if stale is None:
name = naming.get_name(obj)
name = naming.get_name(obj, stale.number)
if name is not None:
return String(name)
return null
@signature(Object, Object, optional=1)
def load(program, path):
if path is None:
path = null
path = pathobj.to_path(path)
return from_object(program, path)
def class_(argv):
exnihilo = argv[0]
parent = Object.interface
name = String(u"customobject")
assert 1 <= len(argv) <= 3
if len(argv) > 1:
parent = argv[1]
if len(argv) > 2:
name = argv[2]
assert isinstance(exnihilo, Exnihilo)
methods = {}
for key, index in
methods[key] =[index]
interface = Interface(
cast(parent, Interface, u"parent"),
cast(name, String, u"name").string,
return interface
def iter_(obj):
return obj.iter()
def hash_(obj):
return Integer(obj.hash())
def repr_(obj):
return String(obj.repr())
def reversed_(obj):
return ReversedListIterator(reversed(obj.contents))
class ReversedListIterator(Object):
_immutable_fields_ = ['iterator']
def __init__(self, iterator):
self.iterator = iterator
def iter(self):
return self
@ReversedListIterator.method(u"next", signature(ReversedListIterator))
def ReversedListIterator_next(self):
@signature(Object, Object)
def getitem(obj, index):
return obj.getitem(index)
@signature(Object, Object, Object)
def setitem(obj, index, value):
return obj.setitem(index, value)
def listattr(obj):
return List(obj.listattr())
## The interface for analysing the interface.
def list_methods(interface):
out = []
for name in interface.methods:
return List(out)
def list_multimethods(interface):
out = []
for record in interface.multimethods:
types = []
for ref in record.vec:
interface = ref.weakref()
if interface is not None:
if len(types) == len(record.vec):
row = Exnihilo()
row.setattr(u'multimethod', record.multimethod)
row.setattr(u'types', List(types))
return List(out)
@signature(Object, String)
def getattr(obj, index):
return obj.getattr(index.string)
@signature(Object, String, Object, optional=1)
def getattr_or(obj, index, default):
if default is None:
default = null
return obj.getattr_or(index.string, default)
@signature(Object, String, Object)
def setattr(obj, index, value):
return obj.setattr(index.string, value)
def ord_(string):
if len(string.string) != 1:
raise unwind(LError(u"ord expects a char"))
return Integer(ord(string.string[0]))
def chr_(value):
return String(unichr(value.value))
@signature(Object, Object)
def isinstance_(value, which_list):
if isinstance(which_list, List):
whichs = which_list.contents
whichs = [which_list]
interface = get_interface(value)
while interface is not null:
if interface in whichs:
return true
# There should be exactly one recursively defined interface.
if interface.parent is interface:
return false
interface = interface.parent
return false
@signature(String, Integer, optional=1)
def parse_int(string, base):
return Integer(parse_int_(string, base))
def parse_float(string):
return FloatRepr(string)
# And and or are macros in the compiler. These are
# convenience functions, likely not often used.
# erm. Actually 'and' function is used by chaining.
@signature(Object, Object)
def and_(a, b):
return boolean(is_true(a) and is_true(b))
@signature(Object, Object)
def or_(a, b):
return boolean(is_true(a) or is_true(b))
def len_(obj):
return obj.getattr(u'length')
def not_(a):
return boolean(is_false(a))
def encode_utf8(value):
return to_uint8array(value.string.encode('utf-8'))
def decode_utf8(value):
return String(value.to_str().decode('utf-8'))
except UnicodeDecodeError as error:
raise space.unwind(space.LError(u"unicode decode failed"))
def time_(argv):
return Float(time.time())
def getcwd():
return pathobj.getcwd()
def chdir(obj):
return null
@signature(Integer, Integer, Integer, optional=2)
def range_(start, stop, step):
if stop is None:
stop = start.value
start = 0
start = start.value
stop = stop.value
if step is None:
step = 1
step = step.value
if step == 0:
raise unwind(LTypeError(u"step==0"))
return Range(start, stop, step)
class Range(Object):
__slots__ = ['start', 'stop', 'step', 'sign', 'current']
_immutable_fields_ = ['start', 'stop', 'step', 'sign']
def __init__(self, start, stop, step):
self.current = start
self.stop = stop
self.step = step
self.sign = +1 if step >= 0 else -1
def iter(self):
return self
@Range.method(u"next", signature(Range))
def Range_next(self):
if self.current*self.sign < self.stop*self.sign:
i = self.current
self.current += self.step
return Integer(i)
raise StopIteration()
def super_(interface):
return interface.parent
#def attach_debugger(debugger):
# ec = main.get_ec()
# ec.debug_hook = debugger
# return null
import rlibuv as uv
@signature(space.Integer, optional=1)
def exit(obj):
ec = core.get_ec()
ec.exit_status = 0 if obj is None else int(obj.value)
ec.enqueue(ec.current) # Trick to ensure we get Discard -exception here
return core.switch([ec.eventloop]) # Once they are created.
def getcurrent():
return core.get_ec().current
def new_log():
queue = Queue()
if queue in core.g.log.loggers:
raise unwind(LError(u"queue has been registered twice."))
return queue
def print_(argv):
core.g.log.other(u"info", List(argv))
return null
@signature(Object, String, optional=1)
def info(value, type):
if type is None:
core.g.log.other(u"info", value)
core.g.log.other(type.string, value)
return null
def print_traceback(exception):
return null
def format_traceback(exception):
return String(format_traceback_raw(exception))
def format_traceback_raw(exception, in_exception_repr=False):
traceback = exception.getattr(u"traceback")
if not isinstance(traceback, space.List):
raise space.unwind(space.LError(u"Expected null or list as .traceback: %s" % traceback.repr()))
out = u""
if len(traceback.contents) > 0:
out = u"\033[31mTraceback:\033[36m\n"
for entry in reversed(traceback.contents):
if not isinstance(entry, TraceEntry):
name, col0, lno0, col1, lno1 = entry.pc_location()
out += u" %s: %d,%d : %d,%d\n" % (name.repr(), lno0, col0, lno1, col1)
out += u"\033[31m"
out += space.get_interface(exception).name
out += u":\033[0m"
return out + u" " + exception.repr()
except Unwinder as unwinder:
if in_exception_repr:
return out + u" ... Second error during exception repr"
return (out + u" ... Error during exception repr\n"
+ format_traceback_raw(unwinder.exception, True))
from rpython.rtyper.lltypesystem import rffi, lltype, llmemory
from rpython.rlib import rgil
import rlibuv as uv
import uv_callback
@signature(Object, variadic=True)
def work(func, args):
if not core.g.work_pool:
# The function will be called in separate thread,
# so allocate GIL here
core.g.work_pool = WorkPool()
req = lltype.malloc(uv.work_ptr.TO, flavor='raw', zero=True)
work = Work(func, args)
core.g.work_pool.push(req, work)
response = uv_callback.after_work(req)
response.wait(uv.queue_work(, req,
work_cb, uv_callback.after_work.cb))
if work.unwinder:
raise work.unwinder
return work.retval
core.g.work_pool.pop(req), flavor='raw')
def work_cb(handle):
work = core.g.work_pool.peek(handle)
#must_leave = False
# must_leave = space.threadlocals.try_enter_thread(space)
# Should check for separate threads here and crash
# if the callback comes from a thread that has no execution context.
work.retval =
except Unwinder as unwinder:
work.unwinder = unwinder
except Exception as e:
os.write(2, "SystemError: callback raised ")
os.write(2, str(e))
os.write(2, "\n")
# if must_leave:
# space.threadlocals.leave_thread(space)
class WorkPool:
def __init__(self):
self.table = {}
def peek(self, handle):
return self.table[rffi.cast_ptr_to_adr(handle)]
def push(self, handle, value):
self.table[rffi.cast_ptr_to_adr(handle)] = value
def pop(self, handle):
return self.table.pop(rffi.cast_ptr_to_adr(handle))
class Work:
def __init__(self, func, args):
self.func = func
self.args = args
self.retval = null
self.unwinder = None
def guess_handle(num):
return Integer(uv_stream.uv.guess_handle(num.value))
def instantiate_(i):
if interface == Object:
return Exnihilo()
if isinstance(i, Interface):
return CustomObject(i)
raise OldError(u"Cannot instantiate from non-interface")
def register_finalizer(obj):
return null
# @builtin
# @signature(Object)
# def finalize_on_exit(obj):
# ec = core.get_ec()
# ec.must_finalize_on_quit[obj] = true
# return null
def on_exit(argv):
ec = core.get_ec()
return null