Tensegritics / Tensegritics/ClojureDart

How do you use Dart type annotations to allow a method to be called on an object that must be a specific type?

Open
#91 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Clojure
Stars
1.6k
Forks
119
PR merge metrics
No merged PRs in 30d

Description

I am trying to use dart:ffi, and the examples use type annotations heavily. I tried to call a C function using dart:ffi from Clojure without type annotations, but it seems that some steps need them. Here is the code I used, after requiring "dart:ffi" with ":as ffi":

(defn main []
(let [lib (ffi.DynamicLibrary/open "/usr/lib/libc.so.6")
printf (-> lib (.lookup "printf") .asFunction)]
(printf "This is an FFI call.")))

This is based on the dart:ffi example named hello_world at https://dart.dev/guides/libraries/c-interop, except I tried to call printf using the Linux C standard library instead of writing my own C library. Compiling this code succeeds, but I get an error when I try to run the executable generated by "dart compile exe". It seems like I need to convert the Pointer<NativeType> object returned by the lookup method to a Pointer<NativeFunction> type. Here is the stack trace:

Unhandled exception:
NoSuchMethodError: Class 'Pointer<NativeType>' has no instance method 'asFunction'.
Receiver: Pointer<NativeType>: address=0x7fbba4f37850
Tried calling: asFunction()
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:38)
#1      _objectNoSuchMethod (dart:core-patch/object_patch.dart:85)
#2      main (file:///home/elijah/circuit-simulator/lib/cljd-out/circuit-simulator/dart.dart:11)
#3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297)
#4      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the dart:ffi hello_world example and the generated lib/cljd-out/circuit-simulator/dart.dart entry point shown in the stack trace. Compare the lookup and asFunction call with the reported Pointer error, then verify the result by running the compiled executable and confirming that it invokes libc printf successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, clojure, dart
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.