Is there a guide to Dart_Handle somewhere?

114 views
Skip to first unread message

davids...@gmail.com

unread,
May 30, 2018, 6:38:24 PM5/30/18
to Dart Misc
I've been trying to figure out how to use the native extensions system for a few days now, and I'm having a couple issues...
1) Is there no way to import these C++ structs/classes?
struct SimpleStruct {
    int f1;
    int f2;
    SimpleStruct(int ff1, int ff2) : f1(ff1), f2(ff2) {}
    int max();
};

class SimpleClass {
    int f1;
    int f2;

public:
    SimpleClass();
    SimpleClass(SimpleClass const &foo);
    SimpleClass(int ff1, int ff2);
    int max();
};
I tried saying putting this in my Dart library with the rest of my native function calls:
class SimpleStruct native 'SimpleStruct';
but that doesn't work ("Native clause can only be used in the SDK and code that is loaded through native extensions.", which I thought I was doing, but apparently not).

2) Assuming that I really do need to manually create Dart types for all the C/C++ types I'm using, when I create a Dart version of my C struct:
class SimpleStruct {
  int f1;
  int f2;
  SimpleStruct(this.f1, this.f2) {}
  
  int max() native 'SimpleStruct::max';
}
How do I get at the implicit `this` argument for instance methods such as `max()`? Right now, I redirect the function like this:
  static int __max(SimpleStruct) native 'SimpleStruct::max';
  int max() => SimpleStruct.__max(this);
but it seems like their ought to be a way to get at the `this`.

3) On the C++ side of things, I keep butting up against the `Dart_Handle` type in C/C++. As far as I can tell, it's an opaque pointer to "any type, instance, error, or concept that might possibly exist in Dart"... which is fine for things that the VM passes around for you, but I can't figure out how to get to the fields of passed-in Dart objects (for instance). At the moment, my non-working C++ code looks like this:
void _SimpleStruct_max(Dart_NativeArguments arguments) {
    Dart_Handle DartObj_This = Dart_GetNativeArgument(arguments, 0);
    Dart_Handle fieldHandles[2];
    fieldHandles[0] = Dart_GetNativeInstanceField(DartObj_This, 0, nullptr);
    fieldHandles[1] = Dart_GetNativeInstanceField(DartObj_This, 1, nullptr);
    int64_t fields[2];
    Dart_IntegerToInt64(fieldHandles[0], &(fields[0]));
    Dart_IntegerToInt64(fieldHandles[1], &(fields[1]));
    SimpleStruct local_this = SimpleStruct((int)fields[0], (int)fields[1]);
    Dart_SetReturnValue(arguments, HandleError(Dart_NewInteger(local_this.max())));
}
I say "non-working" because the output of this in Dart:
  var foo = new SimpleStruct(1,2);
  print(foo.max());
is "101974081" instead of "2".

I suspect at least part of the problem is the way that I'm getting the `Dart_Handle` instances for the two fields... seems odd to be passing `nullptr` into them, but that's the only function I could find that looked like it'd extract fields from a Dart object, and there aren't any `intptr_t` variables laying around to pass in there.

Is there a resource online that goes into the native extensions system a bit more thoroughly than "https://www.dartlang.org/articles/dart-vm/native-extensions"? Especially something that explains how to get at all the `Dart_Handle` variables?

Thanks,
- Dave Sweeris

Tobe Osakwe

unread,
May 30, 2018, 7:18:21 PM5/30/18
to mi...@dartlang.org
For one thing, if you’re passing a struct into Dart, you’re probably better of using the ByteData, ByteBuffer, Uint8List, etc. classes in dart:typed_data IMO. You can have a simple wrapper class that modifies an instance of ByteData, and effectively mimic a struct in Dart.

However, when it comes to the native extension API, to get a field, you’re far better off using Dart_GetField instead of Dart_GetNativeInstanceField.

To be fair, there really is not much documentation about native extensions for Dart, so if you have further questions I’d be more than happy to answer.
--
For more ways to connect visit https://www.dartlang.org/community
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/misc/3d6c21b4-b232-4a2e-bd0b-3b3640d51f62%40dartlang.org.
Reply all
Reply to author
Forward
0 new messages