Skip to content

feat(stable-api): Add FL_TEST, FL_SET, FL_UNSET for flag manipulation #664

Open

Description

@ianks

Summary

Add stable API methods for testing and manipulating Ruby object flags, equivalent to the C macros FL_TEST, FL_SET, FL_UNSET, and related flag operations.

Motivation

Object flags are used extensively in Ruby internals for tracking object state (frozen, tainted, embedded, etc.). Direct flag manipulation is needed for:

  • Implementing custom Ruby objects with proper GC semantics
  • Performance-critical code that checks object properties
  • Advanced use cases like implementing custom containers

Proposed API

/// Test if specific flags are set on an object (akin to `FL_TEST`).
///
/// # Safety
/// This function is unsafe because it dereferences a raw pointer to get
/// access to underlying RBasic struct. The caller must ensure that the
/// VALUE is a valid pointer to a heap object.
unsafe fn fl_test(&self, obj: VALUE, flags: VALUE) -> VALUE;

/// Test if specific flags are set (returns bool) (akin to `FL_TEST_RAW`).
///
/// # Safety
/// Same as fl_test.
unsafe fn fl_test_raw(&self, obj: VALUE, flags: VALUE) -> bool;

/// Set specific flags on an object (akin to `FL_SET`).
///
/// # Safety
/// This function is unsafe because it modifies internal Ruby object state.
/// The caller must ensure proper synchronization and that the flags being
/// set are appropriate for the object type.
unsafe fn fl_set(&self, obj: VALUE, flags: VALUE);

/// Unset specific flags on an object (akin to `FL_UNSET`).
///
/// # Safety
/// Same as fl_set.
unsafe fn fl_unset(&self, obj: VALUE, flags: VALUE);

Implementation Notes

  • Flags are stored in RBasic.flags field
  • Some flags have special meaning and should not be modified directly
  • Reference: include/ruby/internal/fl_type.h
  • Common flags: FL_FREEZE, FL_TAINT (deprecated), FL_USER*

Checklist

  • Add methods to StableApiDefinition trait
  • Implement for each Ruby version (2.7, 3.0, 3.1, 3.2, 3.3, 3.4, 4.0)
  • Add C fallback in compiled.c
  • Add public macro wrappers in macros.rs
  • Add comprehensive tests
  • Document which flags are safe to use

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions