21. Recipes


21. Recipes

Step-by-step procedures for the changes made most often: a built-in function, a native method, a Rexx method in the prelude, an R-code instruction and a library routine. Each one lists every file that has to change in both hosts, what to regenerate, which version constant to bump and what to run. They assume the workflow and conventions of 20-maintaining.md; the mechanisms behind each step are in the chapters linked from it. Paths are relative to bin/modules/rvm/ unless they start with bin/.

Every recipe was checked by carrying it out on a scratch copy of the tree: the symptoms quoted for a missed step are what the RVM then does.

Adding a built-in function

A built-in function exists in three places: the Rexx Parser (which decides at translation that a call is a BIF), the ooRexx host and the JS host (11).

  1. The parser: name and signature. Add a line BIFARGS.NAME = "rANY oWHOLE>=0 ..." to LoadBIFInfo in bin/parser/BIFs.cls (one word per argument: r/o plus a type from the table in 11). This builds the entry of .Parser.BIF (the set the parser's second pass uses to mark the call BUILTIN, so the compiler emits FUNCB/CALLB) and of .Parser.BIFInfo (the signature CheckArgs reads). This is a change to the Rexx Parser, not to the RVM: it is made and proposed as the parser rules of 20 say, and the RVM side is made against a parser that has it. Without it the call compiles to FUNCX and is an external call (43.1).
  2. The ooRexx host: the routine. In ../RVM.BIFs.cls add ::Routine BIF.NAME with Use Strict Arg name, args, context, starting with a = CheckArgs( name, args, context ) (pass a checklist as fourth argument only when the signature depends on the arguments). Errors go through BifError with a 40.x code; a new message text goes into RVM.BifMessages~activate. Use only the byte layer for strings (20).
  3. The ooRexx host: the name list. Add NAME to the hand-written names string of RVM.BIFTable~activate (end of the same file). The table is built from that list, not from the routines: without the name, every call raises 43.1 "Could not find routine" although BIF.NAME exists.
  4. The JS host. register("NAME", (name, args, context) => { ... }) in the js/bifs*.mjs module of its kind (string functions in ../js/bifs.mjs, numeric ones in bifs-number.mjs, state and system ones in bifs-system.mjs, streams in bifs-stream.mjs), starting with checkArgs(name, args, context); errors with bifError, a new text in MESSAGES of ../js/bifcore.mjs. register is the whole registration (a new module must be imported by bifs.mjs).
  5. Regenerate js/rvm-data.mjs, whose BIF_INFO is the JS copy of the parser's signatures: (cd js && REXX_PATH=$(cd ../../.. && pwd) rexx gendata.rex > rvm-data.mjs). Without it the JS host crashes on the first call (TypeError: Cannot read properties of undefined (reading 'max') in checkArgs).
  6. If it is also a String method (native has a method of the same name), add it to the StringMethods spec in ../RVM.Objects.cls ("NAME position min max": the argument position the receiver takes) and to stringMethods in ../js/objects.mjs; its argument checks follow native's method order (RVM.StringArgs / STRING_ARGS), and an error the method must report as 93.9xx is attached with BifMethodError / bifMethodError (11). A function that ooRexx runs as a method of String goes into RVM.BIFTable's viaMethod list and VIA_METHOD in ../js/bifs.mjs (the receiver's position and how the function itself checks each argument), so that its errors are ooRexx's.
  7. Versions and the prelude. A name that becomes a BIF changes the code generated for calls to it (FUNCX → FUNCB): the parser's change to its BIF table raises its frontier stamp, which invalidates .rcode caches; regenerate both preludes' .rcode and .mjs (10).
  8. Tests. A Classic case tests/e2e/cases/e2e-NNN-name.rex (results, omitted arguments, each error), run with tests/e2e/e2e.sh both 'e2e-NNN*'; its errors must be native's too (the probe cases e2e-223…e2e-231 show how to cover them). A string function also goes into the names list of tests/fuzz/fuzz-strbif-gen.rex (then fuzz-strbif-run.sh rvm and js with a new seed). Run the parser's tests/RunTests.rex (step 1 changed the parser), the smoke test and the quick check.
  9. Documentation. The function's row in 11; a divergence, if any, in 17.

Adding a native method to a class

A native method is a method of a built-in class implemented in the host language (08). Nothing is regenerated and no version changes: the R-code and the prelude are not involved.

  1. Declare it. In RVM.World~init (../RVM.Objects.cls) find the class's Call Methods <class>, "I", "..." (instance methods; "C" for class methods) and add an entry NAME min max KEY [argName]; in the World constructor of ../js/objects.mjs add the same entry to the matching defineMethods(<class>, "I", "..."). min and max are the argument counts (−1: no maximum), checked before the native runs (CheckMethodArgs: 93.902 for too many; 93.903, or 88.901 naming argName, for a missing required one). A name with blanks or symbols is quoted ('[]=').
  2. Implement it. KEY is FAMILY.WHAT; the family selects the code (V Array, D Directory, S String, O Object... in CallNative / callNative, some families in their own *Native routine, e.g. JS arrayNative). Add When "WHAT" Then ... there in the ooRexx host and case "WHAT": in JS. The receiver's host value is receiver~native / receiver.native. A key with no implementation raises 93.900 "RVM: native method KEY is missing".
  3. If it must run Rexx code (call a method of the receiver or of an argument), the native does not call it: it returns a delegation (RVM.Delegate, 08) and the machine runs the send. If the whole method is better written in Rexx, use the next recipe instead.
  4. Tests. An OO case tests/oo/cases/FAMILY-name.rex that prints every result and traps the errors (show the condition object), run with tests/oo/oo-e2e.sh FAMILY- (the family prefix, e.g. arr-); for a collection, its OO fuzzer (tests/fuzz/oo/fuzz-array-run.sh SEED N, 16); then the quick check with -p FAMILY.
  5. Documentation. The class's section of 10 (or of the chapter of that class).

Adding a Rexx method in the prelude

Methods that ooRexx implements natively but that must call Rexx code (an Array sort that sends COMPARE, for example) are written in Rexx in ../prelude-rvm.orx and copied into their class at bootstrap (10).

  1. Write it. Add a ::Method to a class of prelude-rvm.orx (the existing one is RVM.ArraySort; a method for another class may go into a new ::Class "RVM.Something" there). The class is only a container: the method runs with the receiver of the class it is copied into.
  2. Copy it into its class, in both hosts: World~bootstrap (../RVM.Objects.cls) and bootstrap (../js/objects.mjs). Following the existing code: take the method from classes["RVM.ARRAYSORT"]~native~imethods, CopyMethod(m, NAME, targetClass) / copyMethod, set compiled to true (one "Compiled method" traceback line, no trace, no guard lock, as a native method), and store it in the target class's native~imethods[NAME] (the target class variable must be in the method's Expose list). Without this step the method exists only in the container class and the target class does not understand the message.
  3. Rebuild prelude.orx. makeprelude.rex makes it from ooRexx's CoreClasses.orx, which is not in the repository: export it from ooRexx's Subversion repository at the revision of the native ooRexx in use (rexx -v), keeping its name, and run rexx makeprelude.rex .../CoreClasses.orx > prelude.orx. Without the ooRexx file, rebuild it from itself: before editing prelude-rvm.orx compute n = lines of prelude.orx − lines of prelude-rvm.orx; after editing, prelude.orx is its first n lines followed by the new prelude-rvm.orx. Both ways give the same file; the commands are in 10.
  4. Regenerate its R-code and module: ./rvm -l prelude.orx > prelude.orx.rcode (about 10 s), then (cd js && rexx genprelude.rex ../prelude.orx.rcode > rvm-prelude.mjs). The streams prelude is not affected. No version changes. Skipping this step leaves the JS host on the old prelude with no error at load time (10).
  5. Tests. The OO family of the class (tests/oo/oo-e2e.sh arr-), its fuzzer, the smoke test, the quick check. An error raised inside the method is reported at a REXX line after the last line of CoreClasses.orx, which native does not have: a case showing such an error needs care (17 if it is a difference).

Adding an R-code instruction

An instruction is a contract between the compiler and two machines (04). The R-code writer and readers are generic (operands by class), so only the table, the compiler, the two machines and the versions change, unless the instruction needs a new operand class or flow letter.

  1. The table, twice. Add "OP operands pops pushes flow" to the spec of RVM.ISA~activate (../RVM.Machine.cls) and the identical string to SPEC in ../js/rcode.mjs (04). The verifier uses it at once on every program. A new operand class also needs OperandOK / operandOK and its relocation in appendFragment (04); a new flow letter, the verifier's path walk, in both hosts.
  2. Bump the ISA version: ::Constant VERSION of RVM.ISA and ISA_VERSION in rcode.mjs, to the same value (04).
  3. The compiler. Emit it with unit~emit( "OP", operand, ... ) in the rcompile method of the construct in ../RVM.Compiler.cls, and bump RVM.Compiler~VERSION (the generated code changes).
  4. The machines. Add When "OP" Then ... to the dispatch Select Case ins[1] of RVM.Machine~execute, and case "OP": to the switch (ins[0]) of Machine.loop in ../js/machine.mjs, next to an instruction of the same kind; it must leave the stack as the table says and produce the trace lines native writes (04). Search both hosts for an existing opcode of the same kind (grep -n '"CATB"') to find any other place that names opcodes (the operator text of a message send for an operator; PauseDrop for a pause).
  5. Regenerate both preludes (.rcode and .mjs of prelude.orx and prelude-streams.orx, 10), delete the .rcode caches written with -w, and rebuild the browser library (14). With a stale rvm-prelude.mjs the JS host fails on the first object use with 93.900 "RVM: R-code for instruction set ... compile the program again"; with a stale prelude.orx.rcode the ooRexx host recompiles the prelude on every run (about 10 s each).
  6. Tests. Cases that make the compiler generate it (Classic e2e or OO, both hosts), and a program that uses it in smoke/cases/ with its expected files (rexx smoke/smoke.rex --make PATTERN), since the smoke test must cover every instruction (16); then everything the change can reach (20), including the self-hosting check.
  7. Documentation. The instruction's entry in 04.

Adding a library routine

REXXUTIL routines are external routines found by the routine search, not BIFs (12): the parser, the compiler and the generated files are not involved, and no version changes.

  1. The ooRexx host's table. Add an entry to the list of RexxUtilRoutines in ../RVM.BIFs.cls: "SYSNAME min max KEY sig;". With a signature (types rSTR, oPOSW..., - for none), the arguments are converted and checked as native's typed routines are; without one, CheckRoutineArgs checks only the counts (12). RXFUNCQUERY answers 0 for it from then on.
  2. The JS host's table. The same entry in ROUTINES of ../js/rexxutil.mjs (in the second loop when it has a signature; there an empty signature is written by leaving it out, not -).
  3. Implement it. When "KEY" Then ... in RexxUtilNative (../RVM.RexxUtil.cls) and case "KEY": in rexxUtilNative (rexxutil.mjs). The arguments arrive converted (JS: whole numbers as BigInt); errors are native's 88.9xx (Raise Syntax 88.905 Array(...) / new RexxError("88.905", [...])); the result is a string. The ooRexx host may delegate to the host's REXXUTIL after validating the arguments itself (12).
  4. If it was one of the routines not provided, remove it from the list in 12, and from 17 or the gap in 18.
  5. Tests. An OO case tests/oo/cases/rexxutil-name.rex (results and the 88.9xx errors), tests/oo/oo-e2e.sh rexxutil-, and the ooTest census groups that use it (tests/check.sh quick -p rexxutil -t PATTERN).

A routine of another library of the RVM follows the same steps with that library's table and dispatcher: rxmath (RxMathRoutines, RxMathNative, ../RVM.RxMath.cls, ../js/rxmath.mjs), rxsock (RxSockRoutines, RxSockNative, js/rxsock.mjs); the registry of libraries is RVMLibrary / ../js/libraries.mjs (12).