GNUstep Objective-C Runtime (libobjc2)

repository·master·Indexed 19 days ago

https://github.com/gnustep/libobjc2

A high-performance, drop-in replacement for the GCC Objective-C runtime. It supports modern Objective-C features including Blocks, ARC, and type-safe method dispatch. The runtime provides three ABIs: Old GCC, GNUstep non-fragile (v1.9), and Modern (v2). Key features include support for APIs introduced in OS X 10.5 through 10.7, synthesized property accessors, and @synchronized().

Tokens
822
Snippets
3
Records
5
Agent score
18%

What's inside libobjc2

  1. Features supported by GNUstep runtime

    master

    Regardless of the ABI selected, the GNUstep runtime provides the following features that exceed the standard GCC runtime capabilities:

    • Modern Objective-C APIs: Support for APIs introduced with OS X 10.5.
    • Blocks: Support for Objective-C closures.
    • Synthesised property accessors.
    • @synchronized(): Efficient implementation of synchronization.
    • Type-dependent dispatch: Prevents stack corruption from mismatched selectors.
    • Associated Reference APIs: Support for APIs introduced with Mac OS X 10.6.
    • Automatic Reference Counting (ARC): Support for APIs introduced with Mac OS X 10.7.
  2. How Type-Dependent Dispatch works

    master

    Traditional Objective-C method lookup relies solely on the method name (selector). If a sender and receiver disagree on the argument types (e.g., one expects an int and the other a float), it can lead to incorrect register usage or stack corruption.

    GNUstep's Type-Dependent Dispatch mitigates this:

    1. Typed Selectors: Sending a message with a typed selector ensures that only a method with matching types is invoked.
    2. Untyped Selectors: Sending a message with an untyped selector will invoke any method with a matching name. However, the lookup function returns the method's types, allowing the caller to verify them and construct a valid call frame.
    3. Error Handling: If a typed selector lookup matches a method with incorrect types, the runtime calls a handler. By default, this handler prints an error message and exits. If using LanguageKit, an alternative handler can dynamically generate a new method to perform the necessary boxing to call the original method safely.
  3. Select an Objective-C ABI

    master

    The GNUstep Objective-C runtime supports three distinct Application Binary Interfaces (ABIs). You can select the desired ABI using Clang flags during compilation:

    • Old GCC ABI: Provides support for Objective-C 1.0 features. Use -fobjc-runtime=gcc in Clang or compile with GCC.
    • GNUstep non-fragile ABI (v1.9): Intended for compatibility with the GCC ABI while supporting modern Objective-C features. Use -fobjc-runtime=gnustep-1.9 in Clang.
    • Modern (v2) ABI: Provides richer reflection metadata, smaller binaries, and reduced memory usage. Use -fobjc-runtime=gnustep-2.0 in Clang 7.0 or later.
    # Example: Compiling with the modern v2 ABI using Clang
    clang -fobjc-runtime=gnustep-2.0 main.m
  4. Optimize the runtime binary size

    master

    To reduce the binary size of the runtime, you can disable support for older ABIs. This prevents the runtime from linking against code that uses the legacy GCC or v1.9 ABIs. This is achieved by setting the OLDABI_COMPAT flag to OFF in your CMake configuration.

    set(OLDABI_COMPAT OFF)
  5. Build the libobjc2 runtime on Windows

    master

    To build the GNUstep Objective-C runtime on Windows, you must use the Ninja build system and CMake. You will need to provide paths to your Clang compilers for both C and C++ during the CMake configuration step.

    > mkdir build
    > cd build
    > cmake .. -G Ninja -DCMAKE_C_COMPILER=path/to/clang.exe -DCMAKE_CXX_COMPILER=path/to/clang.exe
    > ninja