如何通过 LLVM C++ 调用 Objective-C 块API?

发布于 2024-10-10 00:14:37 字数 613 浏览 0 评论 0原文

举例来说,我有一个 Objective-C 编译模块,其中包含如下内容:

typedef bool (^BoolBlock)(void);
BoolBlock returnABlock(void)
{
    return Block_copy(^bool(void){
        printf("Block executing.\n");
        return YES;
    });
}

...然后,使用 LLVM C++ API,加载该模块并创建一个 CallInst 来调用 returnABlock() 函数:

Function *returnABlockFunction = returnABlockModule->getFunction(std::string("returnABlock"));
CallInst *returnABlockCall = CallInst::Create(returnABlockFunction, "returnABlockCall", entryBlock);

如何调用通过 returnABlockCall 对象返回的块?

Say, for example, I have an Objective-C compiled Module that contains something like the following:

typedef bool (^BoolBlock)(void);
BoolBlock returnABlock(void)
{
    return Block_copy(^bool(void){
        printf("Block executing.\n");
        return YES;
    });
}

...then, using the LLVM C++ API, I load that Module and create a CallInst to call the returnABlock() function:

Function *returnABlockFunction = returnABlockModule->getFunction(std::string("returnABlock"));
CallInst *returnABlockCall = CallInst::Create(returnABlockFunction, "returnABlockCall", entryBlock);

How can I then invoke the Block returned via the returnABlockCall object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2024-10-17 00:14:37

恐怕这不是一个简单的答案。前端将块降低到对块运行时的调用。对于 clang,相关代码位于 clang/lib/CodeGen/CGBlocks.[h|cpp]

值得在 cfe-dev 列表上询问是否有办法将此代码分解出来以便在其他前端中重用。

Not an easy answer here, I'm afraid. Blocks are lowered by the front-end into calls into the blocks runtime. In the case of clang, the relevant code is at clang/lib/CodeGen/CGBlocks.[h|cpp].

It would be worth asking on the cfe-dev list if there's a way to factor this code out for reuse in other front-ends.

沦落红尘 2024-10-17 00:14:37

在 C 中,我只是将块分配给的 var 视为函数指针。以您的代码为例,在将函数的结果分配给“returnABlockCall”之后,您可以只写:

returnABlockCall();

并且它应该可以工作。

警告,这在 C++ 中未经测试,但我认为没有理由它不起作用。

In C, I just act as if the var I assigned the block to was a function pointer. Using your code as an example, after you assign the result of the function to "returnABlockCall", you could just write:

returnABlockCall();

and it should work.

Warning, this is untested in C++, but I see no reason why it wouldn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文