How do you convert a Dart_Handle List to a C++ array?

291 Views Asked by At

I'm trying to port over a node.js native abstraction to dart as a native extension. The node.js nan also happens to be using the v8 namespace, which I'm not terribly familiar with.

The [incomplete] code snippet in particular that I'm having trouble with would be this:

NAN_METHOD(TestFunction) {
    NanScope();

    String::Utf8Value file(args[0]->ToString());

    int i = 0;
    Local<Array> argv_ = Local<Array>::Cast(args[1]);
    int argc = argv_->Length();
    int argl = argc + 1 + 1;
    char **argv = new char*[argl];
    argv[0] = strdup(*file);
    argv[argl-1] = NULL;
    for (; i < argc; i++) {
        String::Utf8Value arg(argv_->Get(NanNew<Integer>(i))->ToString());
    argv[i+1] = strdup(*arg);

Strings and Integers are fairly straightforward, but the equivalent functions in dart_api.h for Arrays don't seem to exist so I'm a bit stuck. This is what I have so far:

void TestFunction(Dart_NativeArguments args) {
    Dart_EnterScope();

    uint8_t* file;
    intptr_t f_length = 0;
    Dart_StringToUTF8(Dart_GetNativeArgument(args, 0), &file, &f_length);

    int i = 0;
    intptr_t a_length;
    Dart_Handle a_list;
    Dart_handle a_handle;

    a_list = Dart_GetNativeArgument(args, 1);
    a_handle = Dart_ListLength(list, &a_length);
1

There are 1 best solutions below

0
On BEST ANSWER

v8::Array::Get is Dart_ListGetAt.

You would want to write something like this:

intptr_t argc = 0;
DART_CHECK_VALID(Dart_ListLength(list, &argc));
for (intptr_t i = 0; i < argc; i++) {
  Dart_Handle element = Dart_ListGetAt(list, i);
  DART_CHECK_VALID(element);

  const char* element_str = NULL;
  DART_CHECK_VALID(Dart_StringToCString(element, &element_str));

  argv[i + 1] = strdup(element_str);
}