[vm/bytecode] Correct scripts for members not from the script of enclosing class/library

This change ensures that fields and functions have correct script if their
script doesn't match script of enclosing class/library.

Change-Id: Icd598707fd8a3d8227b425ecbe3a9f6806c73d7a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/107060
Reviewed-by: RĂ©gis Crelier <regis@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
diff --git a/pkg/vm/lib/bytecode/gen_bytecode.dart b/pkg/vm/lib/bytecode/gen_bytecode.dart
index 74f7ded..894471a 100644
--- a/pkg/vm/lib/bytecode/gen_bytecode.dart
+++ b/pkg/vm/lib/bytecode/gen_bytecode.dart
@@ -452,16 +452,20 @@
         flags |= FieldDeclaration.hasPragmaFlag;
       }
     }
-    if (field.fileUri != (field.parent as dynamic).fileUri) {
-      // TODO(alexmarkov): support custom scripts
-      // flags |= FieldDeclaration.hasCustomScriptFlag;
+    ObjectHandle script;
+    if (field.fileUri != null &&
+        field.fileUri != (field.parent as FileUriNode).fileUri) {
+      final isInAnonymousMixin =
+          enclosingClass != null && enclosingClass.isAnonymousMixin;
+      script = getScript(field.fileUri, !isInAnonymousMixin);
+      flags |= FieldDeclaration.hasCustomScriptFlag;
     }
     return new FieldDeclaration(
         flags,
         name,
         objectTable.getHandle(field.type),
         objectTable.getHandle(value),
-        null, // TODO(alexmarkov): script
+        script,
         position,
         endPosition,
         getterName,
@@ -556,9 +560,15 @@
         flags |= FunctionDeclaration.hasPragmaFlag;
       }
     }
-    if (member.fileUri != (member.parent as dynamic).fileUri) {
-      // TODO(alexmarkov): support custom scripts
-      // flags |= FunctionDeclaration.hasCustomScriptFlag;
+    ObjectHandle script;
+    if (member.fileUri != null &&
+        member.fileUri != (member.parent as FileUriNode).fileUri) {
+      final isInAnonymousMixin =
+          enclosingClass != null && enclosingClass.isAnonymousMixin;
+      final isSynthetic = member is Procedure &&
+          (member.isNoSuchMethodForwarder || member.isSyntheticForwarder);
+      script = getScript(member.fileUri, !isInAnonymousMixin && !isSynthetic);
+      flags |= FunctionDeclaration.hasCustomScriptFlag;
     }
 
     final name = objectTable.getNameHandle(member.name.library,
@@ -572,7 +582,7 @@
     return new FunctionDeclaration(
         flags,
         name,
-        null, // TODO(alexmarkov): script
+        script,
         position,
         endPosition,
         typeParameters,
diff --git a/pkg/vm/lib/bytecode/object_table.dart b/pkg/vm/lib/bytecode/object_table.dart
index be1a7ed..6b418e0 100644
--- a/pkg/vm/lib/bytecode/object_table.dart
+++ b/pkg/vm/lib/bytecode/object_table.dart
@@ -1543,6 +1543,11 @@
 
   set source(SourceFile sourceFile) {
     _source = sourceFile;
+    if (_source != null) {
+      _flags |= flagHasSourceFile;
+    } else {
+      _flags &= ~flagHasSourceFile;
+    }
   }
 
   @override
@@ -1571,7 +1576,8 @@
   bool operator ==(other) => other is _ScriptHandle && this.uri == other.uri;
 
   @override
-  String toString() => "$uri";
+  String toString() =>
+      "$uri${source != null ? '(source ${source.importUri})' : ''}";
 }
 
 class ObjectTable implements ObjectWriter, ObjectReader {