Skip to content

Commit

Permalink
[vm] Remove support for old native "<name>" syntax in VM
Browse files Browse the repository at this point in the history
The old `native "<name>"` syntax was lowered to `@ExternalName()`
annotations. Those have been deprecated in favor of
`@pragma('vm:external-name')`. Users have now been migrated and we can
therefore remove the VM support for `@ExternalName`.

Issue #28791

TEST=ci

Change-Id: I69febe49f59627659c540dd50ad0fbf704b6c3a7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/266387
Reviewed-by: Slava Egorov <vegorov@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
  • Loading branch information
mkustermann authored and Commit Queue committed Nov 1, 2022
1 parent 53a0047 commit 251303f
Show file tree
Hide file tree
Showing 10 changed files with 276 additions and 305 deletions.
17 changes: 1 addition & 16 deletions pkg/kernel/lib/external_name.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ String? getExternalName(CoreTypes coreTypes, Member procedure) {
// @pragma("vm:external-name", "<name-of-native>")
// external Object foo(arg0, ...);
//
// Previously the following encoding was used, which is still supported
// until all users are migrated away from it:
//
// import 'dart:_internal' as internal;
//
// @internal.ExternalName("<name-of-native>")
// external Object foo(arg0, ...);
//

if (!procedure.isExternal) {
return null;
}
Expand All @@ -40,9 +31,7 @@ String? _getExternalNameValue(CoreTypes coreTypes, Expression annotation) {
if (annotation is ConstantExpression) {
final Constant constant = annotation.constant;
if (constant is InstanceConstant) {
if (_isExternalName(constant.classNode)) {
return (constant.fieldValues.values.single as StringConstant).value;
} else if (_isPragma(constant.classNode)) {
if (_isPragma(constant.classNode)) {
final String pragmaName =
(constant.fieldValues[coreTypes.pragmaName.fieldReference]
as StringConstant)
Expand All @@ -61,10 +50,6 @@ String? _getExternalNameValue(CoreTypes coreTypes, Expression annotation) {
return null;
}

bool _isExternalName(Class klass) =>
klass.name == 'ExternalName' &&
klass.enclosingLibrary.importUri.toString() == 'dart:_internal';

bool _isPragma(Class klass) =>
klass.name == 'pragma' &&
klass.enclosingLibrary.importUri.toString() == 'dart:core';
Expand Down
11 changes: 4 additions & 7 deletions pkg/vm/lib/transformations/type_flow/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,16 @@ class MoveFieldInitializers {
c.initializers.last is RedirectingInitializer;
}

// Pass which removes all annotations except @ExternalName and @pragma
// on variables, members, classes and libraries.
// Pass which removes all annotations except @pragma on variables, members,
// classes and libraries.
// May also keep @TagNumber which is used by protobuf handler.
class CleanupAnnotations extends RecursiveVisitor {
final Class externalNameClass;
final Class pragmaClass;
final ProtobufHandler? protobufHandler;

CleanupAnnotations(
CoreTypes coreTypes, LibraryIndex index, this.protobufHandler)
: externalNameClass = index.getClass('dart:_internal', 'ExternalName'),
pragmaClass = coreTypes.pragmaClass;
: pragmaClass = coreTypes.pragmaClass;

@override
defaultNode(Node node) {
Expand All @@ -262,8 +260,7 @@ class CleanupAnnotations extends RecursiveVisitor {
final constant = annotation.constant;
if (constant is InstanceConstant) {
final cls = constant.classNode;
return (cls == externalNameClass) ||
(cls == pragmaClass) ||
return (cls == pragmaClass) ||
(protobufHandler != null &&
protobufHandler!.usesAnnotationClass(cls));
}
Expand Down
77 changes: 40 additions & 37 deletions runtime/vm/benchmark_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,43 +345,46 @@ static Dart_NativeFunction StackFrameNativeResolver(Dart_Handle name,
// Unit test case to verify stack frame iteration.
BENCHMARK(FrameLookup) {
const char* kScriptChars =
"class StackFrame {"
" static int accessFrame() native \"StackFrame_accessFrame\";"
"} "
"class First {"
" First() { }"
" int method1(int param) {"
" if (param == 1) {"
" param = method2(200);"
" } else {"
" param = method2(100);"
" }"
" return param;"
" }"
" int method2(int param) {"
" if (param == 200) {"
" return First.staticmethod(this, param);"
" } else {"
" return First.staticmethod(this, 10);"
" }"
" }"
" static int staticmethod(First obj, int param) {"
" if (param == 10) {"
" return obj.method3(10);"
" } else {"
" return obj.method3(200);"
" }"
" }"
" int method3(int param) {"
" return StackFrame.accessFrame();"
" }"
"}"
"class StackFrameTest {"
" static int testMain() {"
" First obj = new First();"
" return obj.method1(1);"
" }"
"}";
R"(
class StackFrame {
@pragma('vm:external-name', 'StackFrame_accessFrame')
external static int accessFrame();
}
class First {
First() { }
int method1(int param) {
if (param == 1) {
param = method2(200);
} else {
param = method2(100);
}
return param;
}
int method2(int param) {
if (param == 200) {
return First.staticmethod(this, param);
} else {
return First.staticmethod(this, 10);
}
}
static int staticmethod(First obj, int param) {
if (param == 10) {
return obj.method3(10);
} else {
return obj.method3(200);
}
}
int method3(int param) {
return StackFrame.accessFrame();
}
}
class StackFrameTest {
static int testMain() {
First obj = new First();
return obj.method1(1);
}
}
)";
Dart_Handle lib =
TestCase::LoadTestScript(kScriptChars, StackFrameNativeResolver);
Dart_Handle cls = Dart_GetClass(lib, NewString("StackFrameTest"));
Expand Down
103 changes: 53 additions & 50 deletions runtime/vm/custom_isolate_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,59 @@ static Dart_NativeFunction NativeLookup(Dart_Handle name,
bool* auto_setup_scope);

static const char* kCustomIsolateScriptChars =
"import 'dart:isolate';\n"
"\n"
"final RawReceivePort mainPort = new RawReceivePort();\n"
"final SendPort mainSendPort = mainPort.sendPort;\n"
"\n"
"echo(arg) native \"native_echo\";\n"
"\n"
"class CustomIsolateImpl implements CustomIsolate {\n"
" CustomIsolateImpl(String entry) : _entry = entry{\n"
" echo('Constructing isolate');\n"
" }\n"
"\n"
" SendPort spawn() {\n"
" return _start(_entry);\n"
" }\n"
"\n"
" static SendPort _start(entry)\n"
" native \"CustomIsolateImpl_start\";\n"
"\n"
" String _entry;\n"
"}\n"
"\n"
"abstract class CustomIsolate {\n"
" factory CustomIsolate(String entry) = CustomIsolateImpl;\n"
"\n"
" SendPort spawn();\n"
"}\n"
"\n"
"isolateMain() {\n"
" echo('Running isolateMain');\n"
" mainPort.handler = (message) {\n"
" var data = message[0];\n"
" var replyTo = message[1];\n"
" echo('Received: $data');\n"
" replyTo.send(data + 1);\n"
" mainPort.close();\n"
" };\n"
"}\n"
"\n"
"main() {\n"
" var isolate = new CustomIsolate(\"isolateMain\");\n"
" var receivePort = new RawReceivePort();\n"
" SendPort port = isolate.spawn();\n"
" port.send([42, receivePort.sendPort]);\n"
" receivePort.handler = (message) {\n"
" receivePort.close();\n"
" echo('Received: $message');\n"
" };\n"
" return 'success';\n"
"}\n";
R"(
import 'dart:isolate';
final RawReceivePort mainPort = new RawReceivePort();
final SendPort mainSendPort = mainPort.sendPort;
@pragma('vm:external-name', 'native_echo')
external void echo(arg);
class CustomIsolateImpl implements CustomIsolate {
CustomIsolateImpl(String entry) : _entry = entry {
echo('Constructing isolate');
}
SendPort spawn() {
return _start(_entry);
}
@pragma('vm:external-name', 'CustomIsolateImpl_start')
external static SendPort _start(entry);
String _entry;
}
abstract class CustomIsolate {
factory CustomIsolate(String entry) = CustomIsolateImpl;
SendPort spawn();
}
isolateMain() {
echo('Running isolateMain');
mainPort.handler = (message) {
var data = message[0];
var replyTo = message[1];
echo('Received: $data');
replyTo.send(data + 1);
mainPort.close();
};
}
main() {
var isolate = new CustomIsolate("isolateMain");
var receivePort = new RawReceivePort();
SendPort port = isolate.spawn();
port.send([42, receivePort.sendPort]);
receivePort.handler = (message) {
receivePort.close();
echo('Received: $message');
};
return 'success';
}
)";

// An entry in our event queue.
class Event {
Expand Down
Loading

0 comments on commit 251303f

Please sign in to comment.