如何通过 LLVM C++ 调用 Objective-C 块API?
举例来说,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恐怕这不是一个简单的答案。前端将块降低到对块运行时的调用。对于 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.
在 C 中,我只是将块分配给的 var 视为函数指针。以您的代码为例,在将函数的结果分配给“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:
and it should work.
Warning, this is untested in C++, but I see no reason why it wouldn't work.