我一直在尝试从 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.
程序运行,但我没有看到回调的输出。
我也尝试过传递简单的回调。
其实我已经明白了。
" @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