Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDC - DataTransferItem.getAsEntry returns 'NativeJavaScriptObject' instead of 'Entry' #35510

Closed
Loonacy opened this issue Dec 28, 2018 · 9 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-html

Comments

@Loonacy
Copy link

Loonacy commented Dec 28, 2018

  • Dart SDK Version 2.1.0
  • Windows
  • Chrome

When compiled with web serve, it throws an exception that says "Type 'NativeJavaScriptObject' is not a subtype of expected type 'Entry'."
When compiled with dart2js it works as expected.

Here's a sample of the code i'm using:

void main() {
	fileDivElement.onDrop.listen(_onFilesDrop);
}

void _onFilesDrop(MouseEvent event) async {
	event.stopPropagation();
	event.preventDefault();
	List<Entry> items = List<Entry>();
	for(int i=0; i<event.dataTransfer.items.length; i++) {
// ------------------- This is where it dies --------------------- //
		items.add(event.dataTransfer.items[i].getAsEntry()); 
	}
	_processFiles(items);
}
@natebosch natebosch changed the title webdev serve, DataTransferItem.getAsEntry returns 'NativeJavaScriptObject' instead of 'Entry' DDC - DataTransferItem.getAsEntry returns 'NativeJavaScriptObject' instead of 'Entry' Dec 28, 2018
@natebosch
Copy link
Member

cc @jmesserly - related to JS interop difference between dart2js and ddc?

@jmesserly
Copy link

related to JS interop difference between dart2js and ddc?

yeah, DDC does stricter checks right now, my proposal is to relax them to match dart2js (and relax both compilers when it comes to generics).

@vsmenon vsmenon added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-html labels Jan 3, 2019
@vsmenon
Copy link
Member

vsmenon commented Jan 3, 2019

I think when we hit this, @terrylucas typically adds a call to registerExtension on the unrecognized type (in dart:html) to map it to the Dart type.

@terrylucas
Copy link
Contributor

Yep Looks like known problem that I've fixed in the past with registerExtension. In this case type DataTransferItem needs registerExtension.

@terrylucas terrylucas self-assigned this Jan 3, 2019
@terrylucas
Copy link
Contributor

Okay I've tried to repro this and I'm unable to. What version of Chrome were you using? I'm using Version 71.0.3578.98 (Official Build) (64-bit) on Linux.

I've tried drag/drop of resources (e.g., images, etc) as well as files and directories being dropped to my dropArea. All work, if directory get a DirectoryReader to iterate over all files in the directory. Look at my _onFilesDrop function. I'm able to get an Entry, File, FileEntry, and DirectoryEntry all seem to work.

        Entry entry = item.getAsEntry();
        File file = item.getAsFile();
        FileEntry fileEntry;
        DirectoryEntry directoryEntry;
        if (entry.isFile) 
          fileEntry = entry;
        else if (entry.isDirectory)
          directoryEntry = entry;
        Entry parentEntry = await entry.getParent();

I'm wondering if Chrome has changed what it returns?
What did you try to drop a file or directory (all seem to work for me) or something else?

Here's my _onFilesDrop event listener:

void _onFilesDrop(MouseEvent event) async {
    print("ENTER _onFilesDrop");
    event.stopPropagation();
    event.preventDefault();
    List<Entry> items = List<Entry>();
    for(int i=0; i<event.dataTransfer.items.length; i++) {
      // ------------------- This is where it dies --------------------- //
      var item = event.dataTransfer.items[i];
      if (item.kind == 'file') {
        Entry entry = item.getAsEntry();
        File file = item.getAsFile();

        FileEntry fileEntry;
        DirectoryEntry directoryEntry;
        if (entry.isFile) 
          fileEntry = entry;
        else if (entry.isDirectory)
          directoryEntry = entry;

        print(">>>> Entry = ${entry.name}");
        Entry parentEntry = await entry.getParent();
        print(">>>> Entry parentEntry = $parentEntry");
        print(">>>> File = ${file.name} - ${file.lastModifiedDate}");
        if (fileEntry != null)
          print(">>>> fileEntry name = ${fileEntry.name}");
        if (directoryEntry != null) {
          print(">>>> directoryEntry fullPath = ${directoryEntry.fullPath}");
          DirectoryReader directoryReader = directoryEntry.createReader();
          List<Entry> directoryEntries = await directoryReader.readEntries();
          directoryEntries.forEach((entry) {
            print(">>>> directoryEntries entry = ${entry.name}");
          });
        }

        items.add(entry); 
      } else {
        var value = event.dataTransfer.getData("text");
        print("Unhandled dataTransfer kind = ${item.kind} : getData('text') = $value");
      }
    }
    print("items length = ${items.length}");
    print("EXIT _onFilesDrop");
}

I've zipped up my *.html and *.dart file for testing drag/drop.

data_transfer.tar.gz

@Loonacy
Copy link
Author

Loonacy commented Jan 8, 2019

I just tried again with a bare project and got the same error. I also tried with terrylucas' code with the same error.
More info on my environment:
Windows 10 Pro 1809 (OS Build 17763.253)
IntelliJ IDEA 2018.3.2 (Community edition)
Dart SDK 2.1.0
Chrome 71.0.3578.98 64-bit

I've attached the files I tested with.
My console log:

dart_sdk.js:18260 Got file drop
dart_sdk.js:18260 Pre-entry
dart_sdk.js:4858 Uncaught _js_helper.TypeErrorImpl.new {message: "Type 'NativeJavaScriptObject' is not a subtype of expected type 'Entry'.", Symbol(_error): Error
    at Object.dart.throw (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:48…}
dart.throw @ dart_sdk.js:4858
async._rethrow @ dart_sdk.js:27742
async._schedulePriorityAsyncCallback.dart.fn @ dart_sdk.js:27737
async._microtaskLoop @ dart_sdk.js:24434
async._startMicrotaskLoop @ dart_sdk.js:24440
internalCallback @ dart_sdk.js:24510
childList (async)
dart.fn.callback @ dart_sdk.js:24520
_scheduleImmediate @ dart_sdk.js:24496
async._scheduleAsyncCallback @ dart_sdk.js:24454
async._rootScheduleMicrotask @ dart_sdk.js:27793
scheduleMicrotask @ dart_sdk.js:27983
[_asyncCompleteError] @ dart_sdk.js:24124
async._asyncCompleteWithErrorCallback @ dart_sdk.js:23581
runBody @ dart_sdk.js:21875
async.async @ dart_sdk.js:21880
main._onFilesDrop @ main.dart:3
dart._checkAndCall @ dart_sdk.js:4668
dart.dcall @ dart_sdk.js:4673
(anonymous function).html$._wrapZone.dart.fn.e @ dart_sdk.js:96928

Log from terrylucas' code:

dart_sdk.js:18260 ENTER _onDragOver
dart_sdk.js:18260 EXIT _onDragOver
dart_sdk.js:18260 ENTER _onFilesDrop
dart_sdk.js:4858 Uncaught _js_helper.TypeErrorImpl.new {message: "Type 'NativeJavaScriptObject' is not a subtype of expected type 'FileEntry'.", Symbol(_error): Error
    at Object.dart.throw (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:48…}message: "Type 'NativeJavaScriptObject' is not a subtype of expected type 'FileEntry'."Symbol(_error): Error
    at Object.dart.throw (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:4857:29)
    at Object.dart.castError (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:4474:15)
    at Object.dart.as (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:4773:17)
    at Function.check_C [as _check] (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:4000:19)
    at _onFilesDrop (http://localhost:53322/main.ddc.js:50:40)
    at _onFilesDrop.next (<anonymous>)
    at runBody (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:21853:34)
    at Object.async.async (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:21880:7)
    at main._onFilesDrop (http://localhost:53322/main.ddc.js:37:18)
    at async._RootZone.new.runUnaryGuarded (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:27908:11)
    at _BroadcastSubscription.new.[_sendData] (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:22422:22)
    at _BroadcastSubscription.new.[_add] (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:22369:26)
    at _SyncBroadcastStreamController.new.[_sendData] (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:23054:29)
    at _SyncBroadcastStreamController.new.add (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:22864:24)
    at Object.dart._checkAndCall (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:4668:16)
    at Object.dart.dcall (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:4673:17)
    at HTMLDivElement.(anonymous function).html$._wrapZone.dart.fn.e (http://localhost:53322/packages/$sdk/dev_compiler/amd/dart_sdk.js:96928:96)hashCode: (...)runtimeType: (...)stackTrace: (...)Symbol(dartx.hashCode): (...)Symbol(dartx.runtimeType): (...)Symbol(dartx.stackTrace): (...)__proto__: Error
dart.throw @ dart_sdk.js:4858
async._rethrow @ dart_sdk.js:27742
async._schedulePriorityAsyncCallback.dart.fn @ dart_sdk.js:27737
async._microtaskLoop @ dart_sdk.js:24434
async._startMicrotaskLoop @ dart_sdk.js:24440
internalCallback @ dart_sdk.js:24510
childList (async)
dart.fn.callback @ dart_sdk.js:24520
_scheduleImmediate @ dart_sdk.js:24496
async._scheduleAsyncCallback @ dart_sdk.js:24454
async._rootScheduleMicrotask @ dart_sdk.js:27793
scheduleMicrotask @ dart_sdk.js:27983
[_asyncCompleteError] @ dart_sdk.js:24124
async._asyncCompleteWithErrorCallback @ dart_sdk.js:23581
runBody @ dart_sdk.js:21875
async.async @ dart_sdk.js:21880
main._onFilesDrop @ main.dart:13
runUnaryGuarded @ dart_sdk.js:27908
[_sendData] @ dart_sdk.js:22422
[_add] @ dart_sdk.js:22369
[_sendData] @ dart_sdk.js:23054
add @ dart_sdk.js:22864
dart._checkAndCall @ dart_sdk.js:4668
dart.dcall @ dart_sdk.js:4673
(anonymous function).html$._wrapZone.dart.fn.e @ dart_sdk.js:96928

EntryTest.zip

@Loonacy
Copy link
Author

Loonacy commented Jan 8, 2019

Forgot to add, I tried dropping a file and a folder, neither works.

@terrylucas
Copy link
Contributor

Okay, I'm finally able to repro. My DDC wasn't building and running.

@terrylucas
Copy link
Contributor

Have a fix CL pending - a bunch of applyExtensions (more than I thought). But works now in both dart2js and DDC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-html
Projects
None yet
Development

No branches or pull requests

5 participants