美文网首页
FFI functions_callbacks

FFI functions_callbacks

作者: CentForever | 来源:发表于2020-12-30 09:29 被阅读0次

    https://github.com/dart-lang/sdk/blob/2.12.0-21.0.dev/samples/ffi/sample_ffi_functions_callbacks.dart

    C++

    typedef intptr_t (*IntptrBinOp)(intptr_t a, intptr_t b);
    // Returns a closure.
    // Note this closure is not properly marked as DART_EXPORT or extern "C".
    // Used for testing passing a pointer to a closure to Dart.
    // TODO(dacoharkes): is this a supported use case?
    DART_EXPORT IntptrBinOp IntptrAdditionClosure() {
      std::cout << "IntptrAdditionClosure()\n";
      IntptrBinOp retval = [](intptr_t a, intptr_t b) { return a + b; };
      std::cout << "returning " << &retval << "\n";
      return retval;
    }
    

    Dart

    typedef NativeIntptrBinOp = IntPtr Function(IntPtr, IntPtr);
    typedef NativeIntptrBinOpLookup = Pointer<NativeFunction<NativeIntptrBinOp>>
        Function();
    
    

    调用

     {
        // Return a c pointer to a c function from a c function.
        Pointer<NativeFunction<NativeIntptrBinOpLookup>> p14 =
            ffiTestFunctions.lookup("IntptrAdditionClosure");
        NativeIntptrBinOpLookup intptrAdditionClosure = p14.asFunction();
    
        Pointer<NativeFunction<NativeIntptrBinOp>> intptrAdditionPointer =
            intptrAdditionClosure();
        BinaryOp intptrAddition = intptrAdditionPointer.asFunction();
        print(intptrAddition(10, 27));
      }
    

    相关文章

      网友评论

          本文标题:FFI functions_callbacks

          本文链接:https://www.haomeiwen.com/subject/dnmtoktx.html