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

dart:js JSONP error / discrepancy in running between SDK and dart2js #15065

Closed
DartBot opened this issue Nov 14, 2013 · 14 comments
Closed

dart:js JSONP error / discrepancy in running between SDK and dart2js #15065

DartBot opened this issue Nov 14, 2013 · 14 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-obsolete Closed as the reported issue is no longer relevant library-js type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@DartBot
Copy link

DartBot commented Nov 14, 2013

This issue was originally filed by aznaum...@gmail.com


 
Attempting to run this example: https://code.google.com/p/dart/source/browse/branches/bleeding_edge/dart/samples/jsonp/web/index.dart

A code sample of fetching JSON data. Though I am using Tumblr as a JSON source instead -- Tumblr API v1.0.

Had to modify the document.body.children.add(script) statement as it causes an error.

But interesting, while the code will work in the SDK editor -- it successfully pulls the JSON from the web URL and invokes the callback function -- it will not work with the dart2js output in Chrome.

(Using build 30107 of dart2js)

Here is the error: http://pbs.twimg.com/media/BZAJk26CYAE9w8Z.jpg

@DartBot
Copy link
Author

DartBot commented Nov 14, 2013

This comment was originally written by aznaum...@gmail.com


I downloaded 1.0 SDK, and example (with different JSON URL input, using Tumblr API) and same behavior as I previously reported -- Dart SDK editor successfully runs but generated compiled javascript (did not try dart2js), just used the GUI generate javascript and then attempted to load page in Chrome browser.

Here is the code:

import 'dart:html';
import 'dart:js';

void main() {
  // Create a jsObject to handle the response.
  context['processData'] = (response) {
    print(response['tumblelog']['title']);
    querySelector('#pageboard').innerHtml = response['tumblelog']['title'];
  };

  ScriptElement script = new Element.tag("script");
  script.src = "http://azspot.net/api/read/json/?callback=processData";
  document.body.children.add(script);
}

//-----

In the SDK runs, but generated javascript produces this error:

Uncaught ReferenceError: DartObject is not defined
dart-sdk/lib/js/dart2js/js_dart2js.dart

// converts a Dart object to a reference to a native JS object
// which might be a DartObject JS->Dart proxy
Object _convertToDart(o) {
  if (JS('bool', '# == null', o) ||
      JS('bool', 'typeof # == "string"', o) ||
      JS('bool', 'typeof # == "number"', o) ||
      JS('bool', 'typeof # == "boolean"', o)) {
    return o;
  } else if (_isLocalObject(o)
      && (o is Blob || o is Event || o is KeyRange || o is ImageData
      || o is Node || o is TypedData || o is Window)) {
    // long line: dart2js doesn't allow string concatenation in the JS() form
    return JS('Blob|Event|KeyRange|ImageData|Node|TypedData|Window', '#', o);
  } else if (JS('bool', '# instanceof Date', o)) {
    var ms = JS('num', '#.getMilliseconds()', o);
    return new DateTime.fromMillisecondsSinceEpoch(ms);

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//!!!!!!! Uncaught reference Error: DartObject is not defined
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  } else if (JS('bool', '#.constructor === DartObject', o)) {
    return JS('', '#.o', o);
  } else {
    return _wrapToDart(o);
  }
}

@anders-sandholm
Copy link
Contributor

Not clear if dart2js or dart:js issue.


Added Library-JS, Area-Dart2JS, Triaged labels.

@peter-ahe-google
Copy link
Contributor

I think I recognize this code from a recent CL from Justin.


Set owner to @justinfagnani.
Removed Area-Dart2JS label.
Added Area-Library label.

@skybrian
Copy link

I'm getting "DartObject is undefined" as well. I'm using the "dart:js" library directly.

There's a another bug to remove DartObject and I wonder if this was done incompletely:
https://code.google.com/p/dart/issues/detail?id=14627

Looking at the JavaScript output, there are references to DartObject in two functions:
_convertToDart
_convertToJS_closure0

But there's no definition of DartObject that I can see.

It seems like there should be some end-to-end tests for JavaScript interop with dart2js?

@DartBot
Copy link
Author

DartBot commented Dec 14, 2013

This comment was originally written by @financecoding


Also hitting this error with.

Dart Editor version 1.0.3_r30939 (DEV)
Dart SDK version 1.0.3.0_r30939

@DartBot
Copy link
Author

DartBot commented Dec 23, 2013

This comment was originally written by youyou3...@gmail.com


Also getting this error using "google_maps" via pub.
Any news ?

@DartBot
Copy link
Author

DartBot commented Jan 4, 2014

This comment was originally written by Burak.Emir...@gmail.com


FWIW: I had some previously working code (pre-M4) that I am trying to get up to date again and then ran exactly into this error message. Then I checked the documentation and realized I have to include another script:

<script src="packages/browser/interop.js"></script>

That solved it for me.

@DartBot
Copy link
Author

DartBot commented Jan 12, 2014

This comment was originally written by douglas.damo...@gmail.com


Also receiving this error with 'dart:js' library.

Dart VM version: 1.0.0.10_r30798 (Mon Dec 2 10:26:47 2013) on "macos_x64"

@DartBot
Copy link
Author

DartBot commented Jan 13, 2014

This comment was originally written by youyou333...@gmail.com


<script src="packages/browser/interop.js"></script> has solved the "DartObject is undefined" for me.
thank you "skybr...@google.com"

@DartBot
Copy link
Author

DartBot commented Jan 13, 2014

This comment was originally written by @financecoding


fwiw, when I hit "DartObject is undefined" I noticed it might of been caused by the order the js scripts was included. Make sure interop.js is the first script. Example:

<script src="packages/browser/interop.js"></script>
<script type="application/dart">export 'package:polymer/init.dart';</script>
<script src="packages/browser/dart.js"></script>

@DartBot
Copy link
Author

DartBot commented Jan 14, 2014

This comment was originally written by douglas.da...@gmail.com


May I plus +1 Adam's tip here? It resolved my problem. To clarify, I iinterop'd from dart to an existing javascript library. I included <script src="packages/browser/interop.js"></script> after <script src="packages/browser/dart.js"></script> and the dart2js code didn't work until I reversed the order.

@DartBot
Copy link
Author

DartBot commented Jan 14, 2014

This comment was originally written by @financecoding


Agreed, this is one of those odd cases that is hard to remember or know what is the root cause of the issue. Maybe this should be posted on stack overflow and additional logging that could reference potential issue?

@DartBot
Copy link
Author

DartBot commented Feb 1, 2014

This comment was originally written by jirkad...@gmail.com


I posted it on SO as http://stackoverflow.com/questions/21492385/referenceerror-dartobject-is-not-defined

@DartBot
Copy link
Author

DartBot commented May 11, 2014

This comment was originally written by ernest.mi...@gmail.com


I noticed dat this solution (adding interop.js) is no longer working for 1.4.0-dev.6.2.
Refering to this script from HTML reports:

    packages/browser/interop.js is no longer needed for dart:js. See http://pub.dartlang.org/packages/browser.

What does solve it (again) is putting dart.js loading BEFORE loading my projects' dart file.

@DartBot DartBot added Type-Defect library-js area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels May 11, 2014
@kevmoo kevmoo added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed Type-Defect labels Mar 1, 2016
@matanlurey matanlurey added the closed-obsolete Closed as the reported issue is no longer relevant label Jun 19, 2018
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. closed-obsolete Closed as the reported issue is no longer relevant library-js type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

7 participants