Debian Codegraph @

pypy/doc/discussion/improve-rpython.rst

raw
1 Possible improvements of the rpython language
2 =============================================
3
4 Improve the interpreter API
5 ---------------------------
6
7 - Rationalize the modules, and the names, of the different functions needed to
8 implement a pypy module. A typical rpython file is likely to contain many
9 `import` statements::
10
11 from pypy.interpreter.baseobjspace import W_Root
12 from pypy.interpreter.gateway import ObjSpace, W_Root
13 from pypy.interpreter.argument import Arguments
14 from pypy.interpreter.typedef import TypeDef, GetSetProperty
15 from pypy.interpreter.typedef import interp_attrproperty, interp_attrproperty_w
16 from pypy.interpreter.gateway import interp2app
17 from pypy.interpreter.error import OperationError
18 from rpython.rtyper.lltypesystem import rffi, lltype
19
20 - A more direct declarative way to write Typedef::
21
22 class W_Socket(W_Root):
23 _typedef_name_ = 'socket'
24 _typedef_base_ = W_EventualBaseClass
25
26 @interp2app_method("connect", ['self', ObjSpace, W_Root])
27 def connect_w(self, space, w_addr):
28 ...
29
30 - Support for metaclasses written in rpython. For a sample, see the skipped test
31 `pypy.objspace.std.test.TestTypeObject.test_metaclass_typedef`
32
33 RPython language
34 ----------------
35
36 - Arithmetic with unsigned integer, and between integer of different signedness,
37 when this is not ambiguous. At least, comparison and assignment with
38 constants should be allowed.
39
40 - Allocate variables on the stack, and pass their address ("by reference") to
41 llexternal functions. For a typical usage, see
42 `rpython.rlib.rsocket.RSocket.getsockopt_int`.
43
44 Extensible type system for llexternal
45 -------------------------------------
46
47 llexternal allows the description of a C function, and conveys the same
48 information about the arguments as a C header. But this is often not enough.
49 For example, a parameter of type `int*` is converted to
50 `rffi.CArrayPtr(rffi.INT)`, but this information is not enough to use the
51 function. The parameter could be an array of int, a reference to a single value,
52 for input or output...
53
54 A "type system" could hold this additional information, and automatically
55 generate some conversion code to ease the usage of the function from
56 rpython. For example::
57
58 # double frexp(double x, int *exp);
59 frexp = llexternal("frexp", [rffi.DOUBLE, OutPtr(rffi.int)], rffi.DOUBLE)
60
61 `OutPtr` indicates that the parameter is output-only, which need not to be
62 initialized, and which *value* is returned to the caller. In rpython the call
63 becomes::
64
65 fraction, exponent = frexp(value)
66
67 Also, we could imagine that one item in the llexternal argument list corresponds
68 to two parameters in C. Here, OutCharBufferN indicates that the caller will pass
69 a rpython string; the framework will pass buffer and length to the function::
70
71 # ssize_t write(int fd, const void *buf, size_t count);
72 write = llexternal("write", [rffi.INT, CharBufferAndSize], rffi.SSIZE_T)
73
74 The rpython code that calls this function is very simple::
75
76 written = write(fd, data)
77
78 compared with the present::
79
80 count = len(data)
81 buf = rffi.get_nonmovingbuffer(data)
82 try:
83 written = rffi.cast(lltype.Signed, os_write(
84 rffi.cast(rffi.INT, fd),
85 buf, rffi.cast(rffi.SIZE_T, count)))
86 finally:
87 rffi.free_nonmovingbuffer(data, buf)
88
89 Typemaps are very useful for large APIs where the same conversions are needed in
90 many places. XXX example