red-langred-system

How to append data to block from R/S?


I am trying to append data to block from Red/System.

Red []

my-red-block: ["some text"] ; some already existen data in block

foo: routine [
    blk
]
[
    block/rs-append as red-block! blk as red-value! unicode/load-utf8 "new text" size? "new text"
]

foo my-red-block

print my-red-block

I decided to pass block to routine end modify it there.

I am getting error:

*** Runtime Error 1: access violation
*** at: 630EB4DFh

Solution

  • The reason for that is because load-utf8 returns a node! that references an external string buffer, not the string! value itself.

    node! is essentially a pointer, and it doesn't match the structure of high-level Red values; however, since it's a pointer, it can be casted to a pointer of another type, like e.g. red-value! or any other struct. The crash happens when you try to access that malformed value slot.

    As for the original question, this should answer it.