2

我正在尝试const Duration从 Dart 本机扩展调用构造函数。我将如何使用Dart_NewC 函数来调用此构造函数?

我试过像任何其他Dart_New调用一样调用它,但我得到一个Incorrect number of arguments错误。

4

1 回答 1

1

以下代码片段来自伟大的oracle.dart库,用于与 Oracle 数据库交互。它从结果集中读取 Oracle 日期并创建一个DateTime具有 6 个构造函数参数的新 Dart 对象。

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();
}
于 2015-08-15T02:52:45.910 回答