如何在 Gnu Smalltalk 中将回调传递给 C 函数?

问题描述 投票:0回答:1

我一直在尝试从 Gnu Smalltalk 程序中绑定到 C 函数。我想出了如何做到这一点,但我不知道如何传递回调。

这是代码

CObject subclass: XFrames [
    XFrames class >> init: a b: bb c: cc onInit: onInitCb [
        <cCall: 'init' returning: #void args: #(#string #string #string #smalltalk)>
    ]

    XFrames class >> test [
        Transcript show: 'Callback called with no arguments'; nl.
    ]
]

callback3 := [ XFrames test ].

callbackDescriptor3 := CCallbackDescriptor for: callback3
    returning: #void
    withArgs: #().

XFrames init: './assets' b: '{}' c: '{}' onInit: callbackDescriptor3.

程序运行,但我没有看到回调的输出。

我也尝试过传递简单的回调。

smalltalk gnu-smalltalk
1个回答
0
投票

其实我已经明白了。


" @see https://www.gnu.org/software/smalltalk/manual/html_node/C-data-types.html "
CObject subclass: XFrames [
    XFrames class >> init: a fontsDefs: fd themeDef: td onInit: onInitCb onTextChanged: onTextChangedCb onComboChanged: onComboChangedCb onNumericValueChanged: onNumericValueChangedCb onBooleanValueChanged: onBooleanValueChangedCb onMultipleNumericValuesChanged: onMultipleNumericValuesChangedCb onClick: onClickCb [
        <cCall: 'init' returning: #void args: #(#string #string #string #unknown #unknown #unknown #unknown #unknown #unknown #unknown)>
    ]

    XFrames class >> setElement: elementJson [
        <cCall: 'setElement' returning: #void args: #(#string)>
    ]

    XFrames class >> setChildren: parentId childrenIds: childrenIdsJson [
        <cCall: 'setChildren' returning: #void args: #(#int #string)>
    ]
]

回调示例

onTextChangedClosureBlock := [ :arg1 :arg2 |
    Transcript show: 'Text value changed: ', arg1 printString, ' : ', arg2 printString; nl.
].

onTextChangedCb := CCallbackDescriptor for: onTextChangedClosureBlock
    returning: #void
    withArgs: #(#int #string).

加载库并调用

init

DLD addLibrary: 'libxframesshared.so'.
(CFunctionDescriptor isFunction: 'init') displayNl.
 

XFrames init: './assets' fontsDefs: fontDefsJson themeDef: '{}' onInit: onInitCb onTextChanged: onTextChangedCb onComboChanged: onComboChangedCbs onNumericValueChanged: onNumericValueChangedCb onBooleanValueChanged: onBooleanValueChangedCb onMultipleNumericValuesChanged: onInitCb onClick: onClickCb.

此处提供工作代码:https://github.com/xframes-project/xframes-gnu-smalltalk/tree/main

© www.soinside.com 2019 - 2024. All rights reserved.