Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. Expanding and explaning the cherry-picked lines of Red/System code referenced above:
  2.  
  3. return old-gradient-pen DC cmds start tail cmd sym catch?
  4. OS-draw-brush-pattern DC size crop-1 crop-2 word as red-block! cmd sym = fill-pen
  5. _read opfd hash alg-digest-size type
  6.  
  7. Let's take the first line (coming from the low-level implementation of our Draw DSL), when a few surrounding lines are pulled with it, it looks like this:
  8.  
  9. either grad? [ ;-- gradient pen
  10. count: 0
  11. stops: cmd + 1
  12. if TYPE_OF(stops) = TYPE_PAIR [
  13. return old-gradient-pen DC cmds start tail cmd sym catch?
  14. ]
  15. loop 2 [ ;-- at least two stops required
  16. DRAW_FETCH_VALUE_2(TYPE_TUPLE TYPE_WORD)
  17. DRAW_FETCH_OPT_VALUE(TYPE_FLOAT)
  18. count: count + 1
  19. ]
  20. _start: cmd
  21. while [ cmd < tail ][ ;-- optional more stops
  22.  
  23. `old-gradient-pen` is a function call (defined in the same file), taking 7 arguments. The naming of that function is not complying with our coding guidelines, as we require function names to start with a verb, so this could have been made a little more readable, that's granted. But look at that, 3 comments there! (they start with a ; character)... so much for the "with no comments"...
  24.  
  25. For the second line, the surrounding code is:
  26.  
  27. case [
  28. mode = _pattern [
  29. DRAW_FETCH_VALUE(TYPE_PAIR)
  30. size: as red-pair! cmd
  31. word: null
  32. crop-1: null
  33. crop-2: null
  34. DRAW_FETCH_OPT_VALUE(TYPE_PAIR)
  35. if cmd = pos [ crop-1: as red-pair! cmd ]
  36. DRAW_FETCH_OPT_VALUE(TYPE_PAIR)
  37. if cmd = pos [ crop-2: as red-pair! cmd ]
  38. DRAW_FETCH_OPT_VALUE(TYPE_WORD)
  39. if pos = cmd [
  40. word: as red-word! cmd
  41. type: symbol/resolve word/symbol
  42. unless any [
  43. type = tile
  44. type = flip-x
  45. type = flip-y
  46. type = flip-xy
  47. type = clamp
  48. ][ cmd: cmd - 1 word: null ]
  49. ]
  50. DRAW_FETCH_VALUE(TYPE_BLOCK)
  51. OS-draw-brush-pattern DC size crop-1 crop-2 word as red-block! cmd sym = fill-pen
  52. ]
  53.  
  54. This code is coming from our Draw DSL interpreter. The `DRAW_FETCH_*` macros are there to help process the DSL commands and the code organization around them is consistent across all the file. This particular snippet is from the `check-pen` function, so the `pen` command in `pattern` mode as implied by the `mode = _pattern`. The syntax of that command is documented here [8]. Using it, reading this code snippet becomes straightforward for any Red/System developer.
  55.  
  56. Now about the specific line you have pointed out, `OS-draw-brush-pattern` is a function call, taking 7 arguments too. Those functions are cross-platform wrappers over 2D-vector drawing API, that are notoriously heavy on parameters. The `= fill-pen` part is adding an inline comparison (`=` is a comparison operator, while `:` denotes assignement in Red and Red/System) with the result of the left function call, and returning the resulting logic value to the calling expression. An intermediary variable could have been used, or parens wrapping the left function call, but that's up to the maintainer of that code part to decide. This is nit-picking anyway, as all the Red contributors working with Red/System will have no issue reading that code on first look.
  57.  
  58. Now let's look at the last line you've cherry-picked (with some context):
  59.  
  60. fd: socket AF_ALG SOCK_SEQPACKET 0
  61. sock-bind fd sa 88
  62. opfd: accept fd null null
  63. _write opfd as c-string! data len
  64. _read opfd hash alg-digest-size type
  65. _close opfd
  66. _close fd
  67.  
  68. It is coming from the Windows platform-specific `get-digest` function in `runtime/crypto.reds` file. It is a very low-level function compared to the rest of the runtime stack. `_read` is just an OS API import with 3 arguments. `alg-digest-size` is a function call taking 1 argument, and with a bad naming again. It should have started with a verb, or at least use a tail `?` character to denote a property retrieval. Also `opfd` is not very well named... So this is not the best example of nicely crafted code in the Red runtime library, I agree with that. Though, using that to mislead readers into thinking that the whole source code is unreadable is, once again, dishonest.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement