Austin Cummings
Austin Cummings

Reputation: 860

Invoke constructor with named arguments via dart_api.h calls

I am trying to invoke the const Duration constructor from a Dart native extension. How would I use the Dart_New C function to invoke this constructor?

I've tried just invoking it like any other Dart_New call but I get an Incorrect number of arguments error.

Upvotes: 3

Views: 183

Answers (1)

loveridge
loveridge

Reputation: 23

The following code fragment is from the great oracle.dart library for interfacing with Oracle databases. It reads an Oracle date from the result set and creates a new Dart DateTime object with 6 constructor arguments.

void OracleResultSet_getDate(Dart_NativeArguments args) {
  Dart_EnterScope();

  auto rs = getThis<occi::ResultSet>(args);
  int64_t index = getDartArg<int64_t>(args, 1);

  occi::Date date;
  try {
    date = rs->getDate(index);
  } CATCH_SQL_EXCEPTION

  int year;
  unsigned int month;
  unsigned int day;
  unsigned int hour;
  unsigned int min;
  unsigned int seconds;
  date.getDate(year, month, day, hour, min, seconds);

  std::vector<Dart_Handle> dargs;
  dargs.push_back(Dart_NewInteger(year));
  dargs.push_back(Dart_NewInteger(month));
  dargs.push_back(Dart_NewInteger(day));
  dargs.push_back(Dart_NewInteger(hour));
  dargs.push_back(Dart_NewInteger(min));
  dargs.push_back(Dart_NewInteger(seconds));

  Dart_Handle lib = GetDartLibrary("dart:core");
  Dart_Handle type = HandleError(Dart_GetType(lib, Dart_NewStringFromCString("DateTime"), 0, NULL));
  Dart_Handle obj = HandleError(Dart_New(type, Dart_Null(), 6, &dargs[0]));

  Dart_SetReturnValue(args, obj);
  Dart_ExitScope();
}

Upvotes: 1

Related Questions