Skip to content

Commit

Permalink
dart jni part
Browse files Browse the repository at this point in the history
  • Loading branch information
dhcdht committed Sep 30, 2024
1 parent 3f2b60e commit 1201890
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Lib/dart/dart.swg
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ SWIGINTERN jint SWIG_dartIntFromSize_t(size_t size) {
%typemap(jtype) signed char, const signed char & "byte"
%typemap(jtype) unsigned char, const unsigned char & "short"
%typemap(jtype) short, const short & "short"
%typemap(jtype) unsigned short, const unsigned short & "int"
%typemap(jtype) int, const int & "int"
%typemap(jtype) unsigned short, const unsigned short & "Int"
%typemap(jtype) int, const int & "Int"
%typemap(jtype) unsigned int, const unsigned int & "long"
%typemap(jtype) long, const long & "int"
%typemap(jtype) long, const long & "Int"
%typemap(jtype) unsigned long, const unsigned long & "long"
%typemap(jtype) long long, const long long & "long"
%typemap(jtype) unsigned long long, const unsigned long long & "dart.math.BigInteger"
%typemap(jtype) float, const float & "float"
%typemap(jtype) double, const double & "double"
%typemap(jtype) void "void"
%typemap(jtype) double, const double & "Double"
%typemap(jtype) void "Void"

%typemap(jstype) bool, const bool & "boolean"
%typemap(jstype) char, const char & "char"
Expand Down
30 changes: 25 additions & 5 deletions Source/Modules/dart.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "cparse.h"
#include <limits.h> // for INT_MAX
#include <ctype.h>
#include <string>
// #include "dartdoc.h"

/* Hash type used for upcalls from C/C++ */
Expand Down Expand Up @@ -516,7 +517,7 @@ class DART:public Language {
}
// Generate the intermediary class
{
String *filen = NewStringf("%s%s.dart", outputDirectory(imclass_package), imclass_name);
String *filen = NewStringf("%s%sJNI.dart", outputDirectory(imclass_package), imclass_name);
File *f_im = NewFile(filen, "w", SWIG_output_files());
if (!f_im) {
FileErrorDisplay(filen);
Expand Down Expand Up @@ -552,7 +553,24 @@ class DART:public Language {
// }
// Printf(f_im, "{\n");

Printf(f_im, "import 'dart:ffi';\nimport 'dart:io';\nimport 'package:ffi/ffi.dart';\n\n");
String *imbind = NewString("");
const char* imbindStr =
"\n\
import 'dart:ffi';\n\
import 'dart:io';\n\
import 'package:ffi/ffi.dart';\n\
\n\
late final DynamicLibrary _dylib;\n \
void $imclassname_setDylib(DynamicLibrary dylib) {\n\
_dylib = dylib;\n\
return;\n\
}\n\
\n\
";
Printf(imbind, imbindStr);
Replaceall(imbind, "$module", module_class_name);
Replaceall(imbind, "$imclassname", imclass_name);
Printv(f_im, imbind, NIL);

// Add the intermediary class methods
Replaceall(imclass_class_code, "$module", module_class_name);
Expand Down Expand Up @@ -959,11 +977,12 @@ class DART:public Language {
}

// Printf(imclass_class_code, " public final static native %s %s(", im_return_type, overloaded_name);
String *im_return_type_lower = Swig_string_lower(im_return_type);
Printf(imclass_class_code, "late final %s_%s = ptr_%s_%s.asFunction<%s Function(",
module_class_name, overloaded_name,
module_class_name, overloaded_name, im_return_type
module_class_name, overloaded_name, im_return_type_lower
);
Printf(imclass_class_lookup_code, "late final ptr_%s_%s = _lookup<NativeFunction<%s Function(",
Printf(imclass_class_lookup_code, "late final ptr_%s_%s = _dylib.lookup<NativeFunction<%s Function(",
module_class_name, overloaded_name, im_return_type
);

Expand Down Expand Up @@ -1004,7 +1023,8 @@ class DART:public Language {
Printf(imclass_class_lookup_code, ", ");
}
// Printf(imclass_class_code, "%s %s", im_param_type, arg);
Printf(imclass_class_code, "%s", im_param_type);
String *im_param_type_lower = Swig_string_lower(im_param_type);
Printf(imclass_class_code, "%s", im_param_type_lower);
Printf(imclass_class_lookup_code, "%s", im_param_type);

// Add parameter to C function
Expand Down

0 comments on commit 1201890

Please sign in to comment.