如何在 Objective-C 的环境下实现 defer

关注仓库,及时得到更新:iOS-Source-Code-Analyzehtml

Follow: Draveness · Githubgit

这篇文章会对 libextobjc 中的一小部分代码进行分析,也是如何扩展 Objective-C 语言系列文章的第一篇,笔者会从 libextobjc 中选择一些黑魔法进行介绍。github

对 Swift 稍有了解的人都知道,defer 在 Swift 语言中是一个关键字;在 defer 代码块中的代码,会在做用域结束时执行。在这里,咱们会使用一些神奇的方法在 Objective-C 中实现 defer编程

若是你已经很是了解 defer 的做用,你能够跳过第一部分的内容,直接看 Variable Attributes安全

关于 defer

defer 是 Swift 在 2.0 时代加入的一个关键字,它提供了一种很是安全而且简单的方法声明一个在做用域结束时执行的代码块。app

若是你在 Swift Playground 中输入如下代码:框架

func hello() {
    defer {
        print("4")
    }
    if true {
        defer {
            print("2")
        }
        defer {
            print("1")
        }
    }
    print("3")
}

hello()

控制台的输出会是这样的:jsp

1
2
3
4

你能够仔细思考一下为何会有这样的输出,并在 Playground 使用 defer 写一些简单的代码,相信你能够很快理解它是如何工做的。函数

若是对 defer 的做用仍然不是很是了解,能够看 guard & defer 这篇文章的后半部分。学习

Variable Attributes

libextobjc 实现的 defer 并无基于 Objective-C 的动态特性,甚至也没有调用已有的任何方法,而是使用了 Variable Attributes 这一特性。

一样在 GCC 中也存在用于修饰函数的 Function Attributes

Variable Attributes 实际上是 GCC 中用于描述变量的一种修饰符。咱们可使用 __attribute__ 来修饰一些变量来参与静态分析等编译过程;而在 Cocoa Touch 中不少的宏其实都是经过 __attribute__ 来实现的,例如:

#define NS_ROOT_CLASS __attribute__((objc_root_class))

cleanup 就是在这里会使用的变量属性:

The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.

GCC 文档中对 cleanup 属性的介绍告诉咱们,在 cleanup 中必须传入只有一个参数的函数而且这个参数须要与变量的类型兼容

若是上面这句比较绕口的话很难理解,能够经过一个简单的例子理解其使用方法:

void cleanup_block(int *a) {
    printf("%d\n", *a);
}

int variable __attribute__((cleanup(cleanup_block))) = 2;

variable 这个变量离开做用域以后,就会自动将这个变量的指针传入 cleanup_block 中,调用 cleanup_block 方法来进行『清理』工做。

实现 defer

到目前为止已经有了实现 defer 须要的所有知识,咱们能够开始分析 libextobjc 是怎么作的。

在 libextobjc 中并无使用 defer 这个名字,而是使用了 onExit(表示代码是在退出做用域时执行)

为了使 onExit 在使用时更加明显,libextobjc 经过一些其它的手段使得咱们在每次使用 onExit 时都须要添加一个 @ 符号。

{
    @onExit {
        NSLog("Log when out of scope.");
    };
    NSLog("Log before out of scope.");
}

onExit 其实只是一个精心设计的宏:

#define onExit \
    ext_keywordify \
    __strong ext_cleanupBlock_t metamacro_concat(ext_exitBlock_, __LINE__) __attribute__((cleanup(ext_executeCleanupBlock), unused)) = ^

既然它只是一个宏,那么上面的代码实际上是能够展开的:

autoreleasepool {}
__strong ext_cleanupBlock_t ext_exitBlock_19 __attribute__((cleanup(ext_executeCleanupBlock), unused)) = ^ {
    NSLog("Log when out of scope.");
};

这里,咱们分几个部分来分析上面的代码片断是如何实现 defer 的功能的:

  1. ext_keywordify 也是一个宏定义,它经过添加在宏以前添加 autoreleasepool {} 强迫 onExit 前必须加上 @ 符号。

    #define ext_keywordify autoreleasepool {}
  2. ext_cleanupBlock_t 是一个类型:

    typedef void (^ext_cleanupBlock_t)();
  3. metamacro_concat(ext_exitBlock_, __LINE__) 会将 ext_exitBlock 和当前行号拼接成一个临时的的变量名,例如:ext_exitBlock_19

  4. __attribute__((cleanup(ext_executeCleanupBlock), unused))cleanup 函数设置为 ext_executeCleanupBlock;并将当前变量 ext_exitBlock_19 标记为 unused 来抑制 Unused variable 警告。

  5. 变量 ext_exitBlock_19 的值为 ^{ NSLog("Log when out of scope."); },是一个类型为 ext_cleanupBlock_t 的 block。

  6. 在这个变量离开做用域时,会把上面的 block 的指针传入 cleanup 函数,也就是 ext_executeCleanupBlock

    void ext_executeCleanupBlock (__strong ext_cleanupBlock_t *block) {
        (*block)();
    }

    这个函数的做用只是简单的执行传入的 block,它知足了 GCC 文档中对 cleanup 函数的几个要求:

    1. 只能包含一个参数

    2. 参数的类型是一个指向变量类型的指针

    3. 函数的返回值是 void

总结

这是分析 libextobjc 框架的第一篇文章,也是比较简短的一篇,由于咱们在平常开发中基本上用不到这个框架提供的 API,可是它依然会为咱们展现了不少编程上的黑魔法。

libextobjc 将 cleanup 这一变量属性,很好地包装成了 @onExit,它的实现也是比较有意思的,也激起了笔者学习 GCC 编译命令而且阅读一些文档的想法。

关注仓库,及时得到更新:iOS-Source-Code-Analyze

Follow: Draveness · Github

原文连接: https://draveness.me/defer

相关文章
相关标签/搜索