mirror of
https://github.com/dart-lang/sdk
synced 2024-09-18 21:51:18 +00:00
7c7246fd2e
R=vsm@google.com BUG= TEST= Review URL: https://chromiumcodereview.appspot.com//10700101 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@9387 260f80e4-7a28-3924-810f-c04153c831b5
120 lines
3.5 KiB
JavaScript
120 lines
3.5 KiB
JavaScript
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
// Bootstrap support for Dart scripts on the page as this script.
|
|
if (navigator.webkitStartDart) {
|
|
if (!navigator.webkitStartDart()) {
|
|
document.body.innerHTML = 'This build has expired. Please download a new Dartium at http://www.dartlang.org/dartium/index.html';
|
|
}
|
|
} else {
|
|
// TODO:
|
|
// - Support in-browser compilation.
|
|
// - Handle inline Dart scripts.
|
|
window.addEventListener("DOMContentLoaded", function (e) {
|
|
// Fall back to compiled JS. Run through all the scripts and
|
|
// replace them if they have a type that indicate that they source
|
|
// in Dart code.
|
|
//
|
|
// <script type="application/dart" src="..."></script>
|
|
//
|
|
var scripts = document.getElementsByTagName("script");
|
|
var length = scripts.length;
|
|
for (var i = 0; i < length; ++i) {
|
|
if (scripts[i].type == "application/dart") {
|
|
// Remap foo.dart to foo.dart.js.
|
|
if (scripts[i].src && scripts[i].src != '') {
|
|
var script = document.createElement('script');
|
|
script.src = scripts[i].src + '.js';
|
|
var parent = scripts[i].parentNode;
|
|
parent.replaceChild(script, scripts[i]);
|
|
}
|
|
}
|
|
}
|
|
}, false);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Experimental support for JS interoperability
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function SendPortSync(receivePort) {
|
|
this.receivePort = receivePort;
|
|
}
|
|
|
|
function ReceivePortSync() {
|
|
this.id = ReceivePortSync.id++;
|
|
ReceivePortSync.map[this.id] = this;
|
|
}
|
|
|
|
ReceivePortSync.id = 0;
|
|
ReceivePortSync.map = {};
|
|
|
|
ReceivePortSync.prototype.receive = function(callback) {
|
|
this.callback = callback;
|
|
};
|
|
|
|
ReceivePortSync.prototype.toSendPort = function() {
|
|
return new SendPortSync(this);
|
|
};
|
|
|
|
ReceivePortSync.prototype.close = function() {
|
|
delete ReceivePortSync.map[this.id];
|
|
};
|
|
|
|
// TODO(kasperl): Hide these serialization methods so they
|
|
// do not clutter up the global scope.
|
|
function _serialize(message) {
|
|
if (message == null ||
|
|
typeof(message) == 'string' ||
|
|
typeof(message) == 'number' ||
|
|
typeof(message) == 'boolean') {
|
|
return message;
|
|
} else if (message instanceof SendPortSync) {
|
|
return [ 'sendport', message.receivePort.id ];
|
|
} else {
|
|
var id = 0;
|
|
var keys = Object.getOwnPropertyNames(message);
|
|
var values = new Array(keys.length);
|
|
for (var i = 0; i < keys.length; i++) {
|
|
values[i] = message[keys[i]];
|
|
}
|
|
return [ 'map', id, keys, values ];
|
|
}
|
|
}
|
|
|
|
function _deserialize(message) {
|
|
return _deserializeHelper(message);
|
|
}
|
|
|
|
function _deserializeHelper(x) {
|
|
if (x == null ||
|
|
typeof(x) == 'string' ||
|
|
typeof(x) == 'number' ||
|
|
typeof(x) == 'boolean') {
|
|
return x;
|
|
}
|
|
switch (x[0]) {
|
|
case 'map': return _deserializeMap(x);
|
|
default: throw 'unimplemented';
|
|
}
|
|
}
|
|
|
|
function _deserializeMap(x) {
|
|
var result = { };
|
|
var id = x[1];
|
|
var keys = x[2];
|
|
var values = x[3];
|
|
for (var i = 0, length = keys.length; i < length; i++) {
|
|
var key = _deserializeHelper(keys[i]);
|
|
var value = _deserializeHelper(values[i]);
|
|
result[key] = value;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
function registerPort(name, port) {
|
|
var stringified = JSON.stringify(_serialize(port));
|
|
window.localStorage['dart-port:' + name] = stringified;
|
|
}
|