À quoi ressemble la sortie de Dart?

Dart, le nouveau langage Web de Google, dit qu'il prend en charge la sortie en JavaScript.

À quoi ressemble une simple conversion?

32
demandé sur Daniel A. White 2011-10-10 20:00:37

2 réponses

main() {
      print('Hello, Dart!');
}

Lorsqu'il est compilé avec dart2js (à partir de 2013-04-26) (voir la note En bas), il est converti en:

// Generated by dart2js, the Dart to JavaScript compiler.
// The code supports the following hooks:
// dartPrint(message)   - if this function is defined it is called
//                        instead of the Dart [print] method.
// dartMainRunner(main) - if this function is defined, the Dart [main]
//                        method will not be invoked directly.
//                        Instead, a closure that will invoke [main] is
//                        passed to [dartMainRunner].
function Isolate() {}
init();

var $ = Isolate.$isolateProperties;
// Bound closures
$.Primitives_printString = function(string) {
  if (typeof dartPrint == "function") {
    dartPrint(string);
    return;
  }
  if (typeof window == "object") {
    if (typeof console == "object")
      console.log(string);
    return;
  }
  if (typeof print == "function") {
    print(string);
    return;
  }
  throw "Unable to print message: " + String(string);
};

$.main = function() {
  $.Primitives_printString("Hello, Dart!");
};

$.String = {builtin$cls: "String"};

var $ = null;
Isolate = Isolate.$finishIsolateConstructor(Isolate);
var $ = new Isolate();
// BEGIN invoke [main].
if (typeof document !== "undefined" && document.readyState !== "complete") {
  document.addEventListener("readystatechange", function () {
    if (document.readyState == "complete") {
      if (typeof dartMainRunner === "function") {
        dartMainRunner(function() { $.main(); });
      } else {
        $.main();
      }
    }
  }, false);
} else {
  if (typeof dartMainRunner === "function") {
    dartMainRunner(function() { $.main(); });
  } else {
    $.main();
  }
}
// END invoke [main].
function init() {
  Isolate.$isolateProperties = {};
  Isolate.$finishIsolateConstructor = function(oldIsolate) {
    var isolateProperties = oldIsolate.$isolateProperties;
    isolateProperties.$currentScript = typeof document == "object" ? document.currentScript || document.scripts[document.scripts.length - 1] : null;
    var isolatePrototype = oldIsolate.prototype;
    var str = "{\n";
    str += "var properties = Isolate.$isolateProperties;\n";
    var hasOwnProperty = Object.prototype.hasOwnProperty;
    for (var staticName in isolateProperties) {
      if (hasOwnProperty.call(isolateProperties, staticName)) {
        str += "this." + staticName + "= properties." + staticName + ";\n";
      }
    }
    str += "}\n";
    var newIsolate = new Function(str);
    newIsolate.prototype = isolatePrototype;
    isolatePrototype.constructor = newIsolate;
    newIsolate.$isolateProperties = isolateProperties;
    return newIsolate;
  };
}
//@ sourceMappingURL=out.js.map

Note pour la postérité: la réponse originale à cette question a été modifiée pour refléter l'état actuel des choses.

Le 2012-05-12 la sortie dart pour Hello World était de 18 718 caractères.

Sur 2012-08-29 la sortie était 1531 caractères.

Sur 2013-04-26, la sortie était 2642 caractères.

Dart2js peut réduire le code. Voici un exemple (au 26-04-2013)

// Generated by dart2js, the Dart to JavaScript compiler.
function I(){}
init()
var $=I.p
$.ib=function(a){if(typeof dartPrint=="function"){dartPrint(a)
return}if(typeof window=="object"){if(typeof console=="object")console.log(a)
return}if(typeof print=="function"){print(a)
return}throw "Unable to print message: " + String(a)}
$.E2=function(){$.ib("Hello, Dart!")}
$.qU={builtin$cls:"qU"}

var $=null
I = I.$finishIsolateConstructor(I)
var $=new I()
if (typeof document !== "undefined" && document.readyState !== "complete") {
  document.addEventListener("readystatechange", function () {
    if (document.readyState == "complete") {
      if (typeof dartMainRunner === "function") {
        dartMainRunner(function() { $.E2(); });
      } else {
        $.E2();
      }
    }
  }, false);
} else {
  if (typeof dartMainRunner === "function") {
    dartMainRunner(function() { $.E2(); });
  } else {
    $.E2();
  }
}
function init(){I.p={}
I.$finishIsolateConstructor=function(a){var z=a.p
z.$currentScript=typeof document=="object"?document.currentScript||document.scripts[document.scripts.length-1]:null
var y=a.prototype
var x="{\n"
x+="var properties = I.p;\n"
var w=Object.prototype.hasOwnProperty
for(var v in z){if(w.call(z,v)){x+="this."+v+"= properties."+v+";\n"}}x+="}\n"
var u=new Function(x)
u.prototype=y
y.constructor=u
u.p=z
return u}}//@ sourceMappingURL=out.js.map

Sur 2013-04-26, le code minifié était de 1386 caractères.

46
répondu Simon Sarris 2013-04-27 03:43:10

La sortie du compilateur Dart - > JavaScript est une cible mobile. La première version (technical preview) n'a pas fait beaucoup de Tree shaking et était donc assez grosse. Le nouveau compilateur (expérimental) frog est bien meilleur à cet égard (Le blog de David Chandler), mais je m'attends à ce que DartC s'améliore considérablement aussi.

10
répondu Florian Loitsch 2011-11-05 02:00:31