Skip to content

feat(stable-api): Add RB_FL_ABLE to check if object can have flags #674

@ianks

Description

@ianks

Summary

Add stable API method to check if a Ruby object can have flags set on it, equivalent to RB_FL_ABLE.

Motivation

Before manipulating object flags, you need to verify the object can actually have flags. Special constants like Qnil, Qtrue, Qfalse, and small integers (Fixnums) cannot have flags because they're immediate values without backing storage.

This is a prerequisite for safe flag operations and should be exposed before implementing FL_SET/FL_TEST etc.

Ruby Source Reference

// include/ruby/internal/fl_type.h

static bool
RB_FL_ABLE(VALUE obj)
{
    if (RB_SPECIAL_CONST_P(obj)) {
        return false;
    }
    else {
        RBIMPL_ASSERT_OR_ASSUME(!RB_TYPE_P(obj, RUBY_T_NODE));
        return true;
    }
}

Proposed API

/// Check if an object can have flags (akin to `RB_FL_ABLE`).
///
/// Returns false for immediate values (nil, true, false, Fixnum, Symbol, Flonum)
/// which don't have flag storage. Returns true for heap-allocated objects.
///
/// This should be called before any flag manipulation operations.
fn fl_able(&self, obj: VALUE) -> bool;

Implementation Notes

  • Essentially the inverse of SPECIAL_CONST_P
  • Does not need unsafe - just checks if value is an immediate
  • Foundation for other flag operations
  • Reference: include/ruby/internal/fl_type.h:439-449

Checklist

  • Add method to StableApiDefinition trait
  • Implement for each Ruby version
  • Add C fallback in compiled.c
  • Add public macro wrapper in macros.rs
  • Add tests for all immediate types

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