Skip to content

Commit 4cdb3f2

Browse files
iinozemtsevCommit Queue
authored andcommitted
Shims generation improvement
- When return type is not directly supported and just a Dart_Handle, automatically create persistent handles - Change `Dart_LoadLibrary` to `Dart_LookupLibrary` - switch from iostream to cstdio, I have no idea why, but it doesn't work for me in emulators and it's not a big difference. TEST=ci,manual Change-Id: I9b6539c7f16e81af9e8a9ab3995d6040bac64a1d Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/429962 Reviewed-by: Tess Strickland <[email protected]> Commit-Queue: Ivan Inozemtsev <[email protected]>
1 parent 2b492a8 commit 4cdb3f2

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

pkg/vm/lib/embedder/shims.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ void _convertDartTypeToC(
2323
}
2424
}
2525

26+
String _getCReturnType(CoreTypes? coreTypes, DartType type) {
27+
if (coreTypes == null) {
28+
return 'Dart_Handle';
29+
} else if (type is VoidType) {
30+
return 'void';
31+
} else if (type == coreTypes.intNonNullableRawType) {
32+
return 'int64_t';
33+
} else if (type == coreTypes.doubleNonNullableRawType) {
34+
return 'double';
35+
} else {
36+
return 'Dart_Handle';
37+
}
38+
}
39+
2640
void _convertCValueToDart(
2741
StringBuffer buffer,
2842
CoreTypes coreTypes,
@@ -653,10 +667,18 @@ class EntryPointCallShim extends EntryPointFunctionShim {
653667

654668
@override
655669
void _writeReturnBody(StringBuffer buffer) {
670+
var isDartHandle = _getCReturnType(coreTypes, returnType) == 'Dart_Handle';
671+
672+
if (isDartHandle) {
673+
buffer.write('Dart_NewPersistentHandle(');
674+
}
656675
buffer.write('Dart_Invoke(');
657676
_writeTarget(buffer);
658677
buffer.write(', Dart_NewStringFromCString("$baseName"), ');
659678
_writeArgumentsListCountAndPointer(buffer);
660679
buffer.write(')');
680+
if (isDartHandle) {
681+
buffer.write(')');
682+
}
661683
}
662684
}

pkg/vm/lib/embedder/writer.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class EntryPointShimWriter {
5858
definitions.write('''
5959
#include "${path.basename(_headerPath)}"
6060
61-
#include <iostream>
61+
#include <cstdio>
62+
#include <cstdlib>
6263
#include <string>
6364
#include <string_view>
6465
#include <unordered_map>
@@ -68,9 +69,9 @@ namespace {
6869
6970
Dart_Handle CheckError(Dart_Handle handle, std::string_view context = "") {
7071
if (Dart_IsError(handle)) {
71-
std::cerr << "Error " << context << ": " << Dart_GetError(handle)
72-
<< std::endl;
73-
std::exit(1);
72+
std::string context_str(context);
73+
fprintf(stderr, "Error %s: %s\\n", context_str.c_str(), Dart_GetError(handle));
74+
exit(1);
7475
}
7576
return handle;
7677
}
@@ -79,8 +80,8 @@ int64_t IntFromHandle(Dart_Handle handle) {
7980
CheckError(handle, "IntFromHandle received an error");
8081
8182
if (!Dart_IsInteger(handle)) {
82-
std::cerr << "IntFromHandle handle is not an int" << std::endl;
83-
std::exit(1);
83+
fprintf(stderr, "IntFromHandle handle is not an int\\n");
84+
exit(1);
8485
}
8586
8687
int64_t result;
@@ -93,8 +94,8 @@ double DoubleFromHandle(Dart_Handle handle) {
9394
CheckError(handle, "DoubleFromHandle received an error");
9495
9596
if (!Dart_IsDouble(handle)) {
96-
std::cerr << "DoubleFromHandle handle is not an double" << std::endl;
97-
std::exit(1);
97+
fprintf(stderr, "DoubleFromHandle handle is not a double\\n");
98+
exit(1);
9899
}
99100
100101
double result;
@@ -119,7 +120,7 @@ class PackageState {
119120
definitions.write('Dart_RootLibrary()');
120121
} else {
121122
definitions.write(
122-
'Dart_LoadLibrary(Dart_NewStringFromCString(kLibraryUri))',
123+
'Dart_LookupLibrary(Dart_NewStringFromCString(kLibraryUri))',
123124
);
124125
}
125126
definitions.writeln(''';

0 commit comments

Comments
 (0)