The module sample
A module of commands that take a record, a handle or a table, never text that the module must parse. A record is bytes with described fields: the caller and the module read the same layout. A handle is lent for the time of one call, with the rights that the description names and no other right. A table is records of one description in memory of the caller, with a direction: Read, Change or Append. The console calls it with load("sample"), isolate("sample") or connect("sample"): the module has an entry, so the same module also runs as a process that serves its exports. A service holds no export that is wrong on purpose: those are in the module hostile_exports, which no service holds.
What it is for. It is the reference module of the call form for everything that is not an integer or a text: records, lent handles, and tables in each direction. The process tables calls it in three ways with the same arguments and compares the answers; the console tests call it the same way.
Example, in the console:
load("sample")
t = readings(4) a table of 4 readings: values 10, 20, 30, 40
sum_values(t) 100
scale_values(t, 3) 4; t now holds 30, 60, 90, 120
As a service: the root service starts it on demand when a process with the grant to connect to it asks for sample. Its only grant is Serve.
The design: and.
See: Module contract: Tables and bytes as parameters, Module contract: Records and handles, as implemented
sample module with an entry
- Hash
6ec0b6879a174b2a963d0906909eea35b9ffe4d75b8ab683e5fbec031e55f2d5- Size
73728 bytes- Exports
14- Imports
0- Imported by
0- License
- MIT
A module of commands that take a record, a handle or a table, never text that the module must parse
| Fact | Value |
|---|---|
| Kind | Modules with no entry |
| Source | src/modules/demonstrations/sample |
| Boot image | yes: in the boot image, 73728 bytes in this build |
| Build settings | none: the defaults of the source |
| Imports | none |
| Imported by | none |
Neighbours
It imports no module, and no module imports it. The module graph shows it with the other modules that have no edge.
Exports
| Export | What it does |
|---|---|
area(rectangle: Rectangle) -> Integer | gives the width of a rectangle times its height |
grow(rectangle: Rectangle, by: Integer) -> Rectangle | gives a rectangle with a margin of by on each side |
fill(memory: Handle(Read, Write), value: Integer) -> Integer | writes a value in each byte of a memory object, and gives the count of bytes |
total(memory: Handle(Read)) -> Integer | adds the bytes of a memory object |
span_total(span: Span) -> Integer | adds the bytes of a part of a memory object |
spoil(memory: Handle(Read)) -> Integer | tries to write in a memory object that the caller lent to read |
readings(count: Integer) -> Table(Reading) | gives a table of count readings: sensor n, time n, value 10 * (n + 1) |
sum_values(readings: Table(Reading, Read)) -> Integer | gives the sum of the values of a table of readings |
scale_values(readings: Table(Reading, Change), factor: Integer) -> Integer | multiplies the value, the minimum and the maximum of each reading by a factor, in the memory of the caller (the direction Change), and gives the count of readings |
append_readings(count: Integer, first_value: Integer, readings: Table(Reading, Append)) -> Integer | appends count readings after those of the table (the direction Append): sensor n, time n, value first_value + n |
sum_words(words: Table(Word, Change), text: Bytes(Read)) -> Integer | writes in each word the sum of the bytes of the text that the word names, and gives the sum of all |
upper_case(text: Bytes(Change)) -> Integer | changes each letter of a range of bytes to upper case, in the memory of the caller (Bytes with the direction Change), and gives the count of letters that it changed |
scale_values_up_to(readings: Table(Reading, Change), factor: Integer, largest_value: Integer) -> Integer | multiplies the value, the minimum and the maximum of each reading by a factor, in order, in the memory of the caller (the direction Change), while each new value stays at most largest_value, and gives the count of readings |
weigh(first: Integer, second: Integer, third: Integer, fourth: Integer, fifth: Integer, sixth: Integer, seventh: Integer, eighth: Integer, label: Text) -> Integer | gives the sum of eight numbers, each times its place (1 to 8), plus the value of each byte of a label |
entry() | The same module as a process: a service that serves the exports above |
area
area(rectangle: ) ->
area gives the width of a rectangle times its height.
The rectangle is a record of four integers; the caller writes it as Rectangle{x = 0, y = 0, width = 3, height = 4}, and the result is 12. A negative width gives a negative area: the module checks nothing.
grow
grow(rectangle: , by: ) ->
grow gives a rectangle with a margin of by on each side.
The result is a record that the export writes in the memory of the caller (in an isolated call, in the call region). A negative by shrinks the rectangle.
rectangle:by:
src/modules/demonstrations/sample/sample.odin:68
fill
fill(memory: (Read, Write), value: ) ->
fill writes a value in each byte of a memory object, and gives the count of bytes. The caller lends the object to read and to write.
The handle is lent for this call only: the module cannot keep it. The value is taken modulo 256. When the kernel refuses the map, the result is -1 with the error record of the kernel for the argument memory.
memory: (Read, Write)value:
- When it fails
- a negative result, and the error record
src/modules/demonstrations/sample/sample.odin:112
total
total(memory: (Read)) ->
total adds the bytes of a memory object. The caller lends the object to read only.
When the kernel refuses the map, the result is -1 with the error record of the kernel for the argument memory.
memory: (Read)
- When it fails
- a negative result, and the error record
src/modules/demonstrations/sample/sample.odin:129
span_total
span_total(span: ) ->
span_total adds the bytes of a part of a memory object. The handle is a field of the record.
A span outside the object gives 0: the export checks first, count and their sum against the size. The handle in a record is lent as a handle parameter is: the rights of the field description, for the call only. When the kernel refuses the map, the result is -1 with the error record of the kernel for the argument span.
span:
- When it fails
- a negative result, and the error record
src/modules/demonstrations/sample/sample.odin:149
spoil
spoil(memory: (Read)) ->
spoil tries to write in a memory object that the caller lent to read. The result is the status of the kernel: 7 is "no right".
The handle has the right Read only, as the description says, so the kernel refuses the map with Write. The result 0 means that the lent handle had more rights than its description.
memory: (Read)
src/modules/demonstrations/sample/sample.odin:170
readings
readings(count: ) -> ()
readings gives a table of count readings: sensor n, time n, value 10 * (n + 1). The result is a Table of the readings that fit in the memory of the caller, and its count is count.
sum_values
sum_values(readings: (, Read)) ->
sum_values gives the sum of the values of a table of readings. The export reads the table: the direction Read.
readings: (, Read)
src/modules/demonstrations/sample/sample.odin:205
scale_values
scale_values(readings: (, Change), factor: ) ->
scale_values multiplies the value, the minimum and the maximum of each reading by a factor, in the memory of the caller (the direction Change), and gives the count of readings.
readings: (, Change)factor:
src/modules/demonstrations/sample/sample.odin:215
append_readings
append_readings(count: , first_value: , readings: (, Append)) ->
append_readings appends count readings after those of the table (the direction Append): sensor n, time n, value first_value + n. It writes those that fit before the capacity, and adds count to the count of the table: a count larger than the capacity says that they did not all fit. The result is the count of readings that it wrote.
count:first_value:readings: (, Append)
src/modules/demonstrations/sample/sample.odin:226
sum_words
sum_words(words: (, Change), text: (Read)) ->
sum_words writes in each word the sum of the bytes of the text that the word names, and gives the sum of all. A word that names bytes after the end of the text is an error, not a fault: the result is -1 with an error record of the kind Out_Of_Range (the end of the bytes of the word, the count of bytes of the text, and the number of the word).
words: (, Change)text: (Read)
- When it fails
- a negative result, and the error record
src/modules/demonstrations/sample/sample.odin:246
upper_case
upper_case(text: (Change)) ->
upper_case changes each letter of a range of bytes to upper case, in the memory of the caller (Bytes with the direction Change), and gives the count of letters that it changed.
text: (Change)
src/modules/demonstrations/sample/sample.odin:271
scale_values_up_to
scale_values_up_to(readings: (, Change), factor: , largest_value: ) ->
scale_values_up_to multiplies the value, the minimum and the maximum of each reading by a factor, in order, in the memory of the caller (the direction Change), while each new value stays at most largest_value, and gives the count of readings. At the first reading whose value would pass it, it stops with -1 and an error record Bad_Value for the factor, with the largest factor that this reading takes: the readings before it are already changed. A caller in another process receives none of these changes: a failed export gives back only its table of error details.
A factor under 1 fails at once with -1 and an error record Too_Small for the factor, with the limit 1; no reading changes.
readings: (, Change)factor:largest_value:
- When it fails
- a negative result, and the error record
src/modules/demonstrations/sample/sample.odin:287
weigh
weigh(first: , second: , third: , fourth: , fifth: , sixth: , seventh: , eighth: , label: ) ->
weigh gives the sum of eight numbers, each times its place (1 to 8), plus the value of each byte of a label. It takes nine parameters and an error record: ten arguments, four of them on the stack after the six registers. Each number has another weight, so a value in the wrong place gives another sum: weigh(1, 2, 3, 4, 5, 6, 7, 8, "A") gives 269.
A label of more than 16 bytes fails with -1 and an error record Too_Long for the label, argument 9.
See: Module contract: The call form
first:second:third:fourth:fifth:sixth:seventh:eighth:label:
- When it fails
- a negative result, and the error record
src/modules/demonstrations/sample/sample.odin:310
entry
entry()
The same module as a process: a service that serves the exports above. An export needs nothing from the entry.
The entry opens the service ring; with none it writes "sample: no service ring" and stops with Refused. It writes "sample: serves its exports", then waits for calls and runs them for ever. It has no window.
- Form
- The entry of the module: one parameter, an address, fixed by the contract.
src/modules/demonstrations/sample/sample.odin:331
Read from the source of the module, as the build reads it to describe the exports (tools/modustool/build/describe.odin).
Rectangle record of a module
A record of the module sample: 32 bytes, aligned on 8 bytes, 0 bytes of padding.
xi6408 bytesyi6488 byteswidthi64168 bytesheighti64248 bytes
Integer wire form
A wire form of the module contract: one word of a call. A signed integer of 64 bits, in a register.
Handle wire form
A wire form of the module contract: one word of a call. A handle that the caller lends for the time of the call, in a register.
Error_Record record of a module
A record of the module sample: 56 bytes, aligned on 8 bytes, 0 bytes of padding.
kindi6408 bytesuniti6488 bytesoperationi64168 bytesargumenti64248 bytesvaluei64328 byteslimiti64408 bytesfoundi64488 bytes
Span record of a module
A record of the module sample: 24 bytes, aligned on 8 bytes, 4 bytes of padding.
memoryHandle04 bytes- padding44 bytes
firsti6488 bytescounti64168 bytes
Table wire form
A wire form of the module contract: one word of a call. Records of one description that follow each other (a table).
Reading record of a module
A record of the module sample: 40 bytes, aligned on 8 bytes, 0 bytes of padding.
sensori6408 bytestimei6488 bytesvaluei64168 bytesminimumi64248 bytesmaximumi64328 bytes
Word record of a module
A record of the module sample: 16 bytes, aligned on 8 bytes, 0 bytes of padding.
startu3204 bytescountu3244 bytessumi6488 bytes
Bytes wire form
A wire form of the module contract: one word of a call. Bytes with no description.
Text wire form
A wire form of the module contract: one word of a call. The address of a slice record {address, count} of UTF-8 bytes.
