Commit 5c353046

5c35304691baMerged

Ori Pekelman <[email protected]> · 5 months ago · 7 files · +2 added · ~5 modified

Description

Ractor Milestone 4: user-object / struct messages

Deep-copy user objects across the Ractor boundary. cls_id is a stable class-table index shared by every Ractor, so it travels on the wire; each ivar is serialized recursively through the value codec and rebuilt in the receiver's heap. Nested objects, and objects as Ractor.new spawn args, work.

The value codec (sp_runtime.h) keeps the buffer/recursion; codegen emits four per-class hooks it drives -- sp_ractor_obj_{nivars,getivar,alloc,setivar} -- as switches over the class table, installed at startup beside the value codec. getivar boxes each ivar (emit_boxed_text), setivar unboxes it (emit_unbox_text), alloc makes a zeroed instance with the class scan fn; the rebuilt object is GC-rooted across the ivar loop.

A class is deep-copyable only when every ivar is a copyable type (scalar, String, Symbol, nested object, Array, Hash). Classes with by-value-struct ivars (Class/Range/Time/...) or non-copyable references (Proc/Fiber/IO/Method/ StringIO/Regexp/...), and exception subclasses / reopened builtins, report nivars -1 and raise Ractor::Error if sent -- never miscompile.

Full suite 931 pass / 0 fail / 0 error; object demo (incl. nested + spawn-arg) GC-verify- and valgrind memcheck-clean; sending a Proc raises Ractor::Error. Adds test/ractor_object.rb.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
5c35304691ba
Parent
1df8520ea126

lib/sp_ractor.cmodifiedView file

@@ -15,6 +15,10 @@
1515 /* Codec hooks, installed by the generated TU at startup. */
1616 sp_RactorBlob (*sp_ractor_serialize_hook)(sp_RbVal v) = NULL;
1717 sp_RbVal (*sp_ractor_deserialize_hook)(sp_RactorBlob b) = NULL;
18+int (*sp_ractor_obj_nivars_hook)(int cls_id) = NULL;
19+sp_RbVal (*sp_ractor_obj_getivar_hook)(void *obj, int cls_id, int i) = NULL;
20+void * (*sp_ractor_obj_alloc_hook)(int cls_id) = NULL;
21+void (*sp_ractor_obj_setivar_hook)(void *obj, int cls_id, int i, sp_RbVal v) = NULL;
1822
1923 /* A blocking single-ended FIFO of serialized message blobs guarded by a
2024 mutex+condvar. Each Ractor has two: `inbox` (messages sent to it; drained by