码迷,mamicode.com
首页 > 其他好文 > 详细

Dart server side call dll

时间:2017-10-09 19:34:50      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:exception   为我   今天   调用本地   var   print   lib   告诉   except   

今天,查看文档时发现Dart运行在服务端下可以调用本地实现(C/C++ dll)。

我想应该有大用处

拿出来分享!

一 先做Dart库

//sse.dart

library sample_synchronous_extension;

import ‘dart-ext:sample_extension‘;

// The simplest way to call native code: top-level functions.
int systemRand() native "SystemRand";
bool systemSrand(int seed) native "SystemSrand";

二. 本地实现的C动态链接库

// sample_extension.c

#define DART_SHARED_LIB

/*#include <string.h>
#include <stdlib.h>
#include <stdio.h>*/

#include "C:\\tools\\dart-sdk\\include\\dart_native_api.h"

//#include <stdbool.h>
typedef enum{false = 0,true = 1} _bool;

#pragma comment(lib, "C:\\tools\\dart-sdk\\bin\\dart.lib")

// Forward declaration of ResolveName function.
Dart_NativeFunction ResolveName(Dart_Handle name, int argc, bool* auto_setup_scope);

// The name of the initialization function is the extension name followed
// by _Init.
DART_EXPORT Dart_Handle sample_extension_Init(Dart_Handle parent_library) {
  if (Dart_IsError(parent_library)) return parent_library;

  Dart_Handle result_code = Dart_SetNativeResolver(parent_library, ResolveName, NULL);
  if (Dart_IsError(result_code)) return result_code;

  return Dart_Null();
}

Dart_Handle HandleError(Dart_Handle handle) {
  if (Dart_IsError(handle)) Dart_PropagateError(handle);
  return handle;
}

// Native functions get their arguments in a Dart_NativeArguments structure
// and return their results with Dart_SetReturnValue.
void SystemRand(Dart_NativeArguments arguments) {
  Dart_Handle result = HandleError(Dart_NewInteger(rand()));
  Dart_SetReturnValue(arguments, result);
}

void SystemSrand(Dart_NativeArguments arguments) {
  bool success = false;
  Dart_Handle seed_object = HandleError(Dart_GetNativeArgument(arguments, 0));
  if (Dart_IsInteger(seed_object)) {
    bool fits;
    HandleError(Dart_IntegerFitsIntoInt64(seed_object, &fits));
    if (fits) {
      int64_t seed;
      HandleError(Dart_IntegerToInt64(seed_object, &seed));
      //srand(static_cast<unsigned>(seed));
      srand(seed);
      success = true;
    }
  }
  Dart_SetReturnValue(arguments, HandleError(Dart_NewBoolean(success)));
}

Dart_NativeFunction ResolveName(Dart_Handle name, int argc, bool* auto_setup_scope) {
  // If we fail, we return NULL, and Dart throws an exception.
  if (!Dart_IsString(name)) return NULL;
  Dart_NativeFunction result = NULL;
  const char* cname;
  HandleError(Dart_StringToCString(name, &cname));

  if (strcmp("SystemRand", cname) == 0) result = SystemRand;
  if (strcmp("SystemSrand", cname) == 0) result = SystemSrand;
  return result;
}

//生成库

cl -c sample_extension.c

link -dll sample_extension.obj

生成sample_extension.dll动态链接库

三 测试Dart库

//test.dart

import ‘sse.dart‘;

void main() {
  if (systemSrand(120)){
    var i = systemRand();
    print(‘rand number is $i‘);
    i = systemRand();
    print(‘rand number is $i‘);
  }
}

我们试试效果吧:

C:\Dart-pro\c-dll>dart test.dart
rand number is 430
rand number is 19576

Finally:

我抱着也许客户端(browser)也能用的心态,勇敢的去试了一下(尝试转成javascript),遗憾的是:报native关键字错误,即,不可以在客户端用,只能在服务器的命令行模式下用哦!

Google的经验告诉我们,大公司的设计都是走正经套路的,所以,他们总是不能跟随你的节拍,因为你的节拍太定向化

研究过程中,我觉得以技术角度似乎不应该不能在客户端使用,但是从web标准看,还真不该在browser中实现。

哈哈,你们自己去应用吧,我想没人会那Dart做服务器吧!

哎,从Google全心支持WebAssembly来看,估计,以后还是得走这条路,不过是,由厂商为我们实现想用的C/C++库而已。

Dart server side call dll

标签:exception   为我   今天   调用本地   var   print   lib   告诉   except   

原文地址:http://www.cnblogs.com/woodzcl/p/7642467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!