Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Unified Diff: sdk/lib/html/dartium/html_dartium.dart

Side-by-side diff isn't available for this file because of its large size.
Issue 25931003: Make List.shuffle take an optional Random object to use. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comment. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
Download patch
« no previous file with comments | « sdk/lib/html/dart2js/html_dart2js.dart ('k') | sdk/lib/typed_data/typed_data.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/html/dartium/html_dartium.dart
diff --git a/sdk/lib/html/dartium/html_dartium.dart b/sdk/lib/html/dartium/html_dartium.dart
index 35f6bcc4ed278d4bcb38f8f722f6dc1a2e15a19c..b5bc2b39d2de3043986b4754bccd3802968ba627 100644
--- a/sdk/lib/html/dartium/html_dartium.dart
+++ b/sdk/lib/html/dartium/html_dartium.dart
@@ -8722,7 +8722,7 @@ class _ChildrenElementList extends ListBase<Element> {
throw new UnsupportedError('Cannot sort element lists');
}
- void shuffle() {
+ void shuffle([Random random]) {
throw new UnsupportedError('Cannot shuffle element lists');
}
@@ -9171,7 +9171,7 @@ class _FrozenElementList<T extends Element> extends ListBase<T> implements Eleme
throw new UnsupportedError('Cannot sort list');
}
- void shuffle() {
+ void shuffle([Random random]) {
throw new UnsupportedError('Cannot shuffle list');
}
@@ -19235,7 +19235,7 @@ class _ChildNodeListLazy extends ListBase<Node> {
throw new UnsupportedError("Cannot sort Node list");
}
- void shuffle() {
+ void shuffle([Random random]) {
throw new UnsupportedError("Cannot shuffle Node list");
}
@@ -31872,7 +31872,7 @@ abstract class ImmutableListMixin<E> implements List<E> {
throw new UnsupportedError("Cannot sort immutable List.");
}
- void shuffle() {
+ void shuffle([Random random]) {
throw new UnsupportedError("Cannot shuffle immutable List.");
}
@@ -33377,7 +33377,7 @@ abstract class _MicrotaskScheduler {
* Scheduler which uses window.postMessage to schedule events.
*/
class _PostMessageScheduler extends _MicrotaskScheduler {
- const _MICROTASK_MESSAGE = "DART-MICROTASK";
+ final _MICROTASK_MESSAGE = "DART-MICROTASK";
_PostMessageScheduler(_MicrotaskCallback callback): super(callback) {
// Messages from other windows do not cause a security risk as
@@ -35038,7 +35038,6 @@ class _Utils {
static window() native "Utils_window";
static forwardingPrint(String message) native "Utils_forwardingPrint";
- static void spawnDomFunction(Function f, int replyTo) native "Utils_spawnDomFunction";
static int _getNewIsolateId() native "Utils_getNewIsolateId";
// The following methods were added for debugger integration to make working
@@ -35072,6 +35071,15 @@ class _Utils {
}
static _ConsoleVariables _consoleTempVariables = new _ConsoleVariables();
+
+ /**
+ * Header passed in from the Dartium Developer Tools when an expression is
+ * evaluated in the console as opposed to the watch window or another context
+ * that does not expect REPL support.
+ */
+ static const _CONSOLE_API_SUPPORT_HEADER =
+ 'with ((this && this.console && this.console._commandLineAPI) || {}) {\n';
+
/**
* Takes an [expression] and a list of [local] variable and returns an
* expression for a closure with a body matching the original expression
@@ -35082,11 +35090,22 @@ class _Utils {
* with the list of arguments passed to this method.
*
* For example:
- * <code>wrapExpressionAsClosure("foo + bar", ["bar", 40, "foo", 2])</code>
+ * <code>
+ * _consoleTempVariables = {'a' : someValue, 'b': someOtherValue}
+ * wrapExpressionAsClosure("${_CONSOLE_API_SUPPORT_HEADER}foo + bar + a",
+ * ["bar", 40, "foo", 2])
+ * </code>
* will return:
- * <code>["(final $var, final bar, final foo) => foo + bar", [40, 2]]</code>
+ * <code>
+ * ["""(final $consoleVariables, final bar, final foo, final a, final b) =>
+ * (foo + bar + a
+ * )""",
+ * [_consoleTempVariables, 40, 2, someValue, someOtherValue]]
+ * </code>
*/
static List wrapExpressionAsClosure(String expression, List locals) {
+ // FIXME: dartbug.com/10434 find a less fragile way to determine whether
+ // we need to strip off console API support added by InjectedScript.
var args = {};
var sb = new StringBuffer("(");
addArg(arg, value) {
@@ -35105,16 +35124,111 @@ class _Utils {
args[arg] = value;
}
- addArg("\$var", _consoleTempVariables);
-
- for (int i = 0; i < locals.length; i+= 2) {
- addArg(locals[i], locals[i+1]);
+ if (expression.indexOf(_CONSOLE_API_SUPPORT_HEADER) == 0) {
+ expression = expression.substring(expression.indexOf('\n') + 1);
+ expression = expression.substring(0, expression.lastIndexOf('\n'));
+
+ addArg("\$consoleVariables", _consoleTempVariables);
+
+ // FIXME: use a real Dart tokenizer. The following regular expressions
+ // only allow setting variables at the immediate start of the expression
+ // to limit the number of edge cases we have to handle.
+
+ // Match expressions that start with "var x"
+ final _VARIABLE_DECLARATION = new RegExp("^(\\s*)var\\s+(\\w+)");
+ // Match expressions that start with "someExistingConsoleVar ="
+ final _SET_VARIABLE = new RegExp("^(\\s*)(\\w+)(\\s*=)");
+ // Match trailing semicolons.
+ final _ENDING_SEMICOLONS = new RegExp("(;\\s*)*\$");
+ expression = expression.replaceAllMapped(_VARIABLE_DECLARATION,
+ (match) {
+ var variableName = match[2];
+ // Set the console variable if it isn't already set.
+ if (!_consoleTempVariables._data.containsKey(variableName)) {
+ _consoleTempVariables._data[variableName] = null;
+ }
+ return "${match[1]}\$consoleVariables.${variableName}";
+ });
+
+ expression = expression.replaceAllMapped(_SET_VARIABLE,
+ (match) {
+ var variableName = match[2];
+ // Only rewrite if the name matches an existing console variable.
+ if (_consoleTempVariables._data.containsKey(variableName)) {
+ return "${match[1]}\$consoleVariables.${variableName}${match[3]}";
+ } else {
+ return match[0];
+ }
+ });
+
+ // We only allow dart expressions not Dart statements. Silently remove
+ // trailing semicolons the user might have added by accident to reduce the
+ // number of spurious compile errors.
+ expression = expression.replaceFirst(_ENDING_SEMICOLONS, "");
+ }
+
+ if (locals != null) {
+ for (int i = 0; i < locals.length; i+= 2) {
+ addArg(locals[i], locals[i+1]);
+ }
}
- sb..write(')=>\n$expression');
+ // Inject all the already defined console variables.
+ _consoleTempVariables._data.forEach(addArg);
+
+ // TODO(jacobr): remove the parentheses around the expresson once
+ // dartbug.com/13723 is fixed. Currently we wrap expression in parentheses
+ // to ensure only valid Dart expressions are allowed. Otherwise the DartVM
+ // quietly ignores trailing Dart statements resulting in user confusion
+ // when part of an invalid expression they entered is ignored.
+ sb..write(') => (\n$expression\n)');
return [sb.toString(), args.values.toList(growable: false)];
}
/**
+ * TODO(jacobr): this is a big hack to get around the fact that we are still
+ * passing some JS expression to the evaluate method even when in a Dart
+ * context.
+ */
+ static bool isJsExpression(String expression) =>
+ expression.startsWith("(function getCompletions");
+
+ /**
+ * Returns a list of completions to use if the receiver is o.
+ */
+ static List<String> getCompletions(o) {
+ MirrorSystem system = currentMirrorSystem();
+ var completions = new Set<String>();
+ addAll(Map<Symbol, dynamic> map, bool isStatic) {
+ map.forEach((symbol, mirror) {
+ if (mirror.isStatic == isStatic && !mirror.isPrivate) {
+ var name = MirrorSystem.getName(symbol);
+ if (mirror is MethodMirror && mirror.isSetter)
+ name = name.substring(0, name.length - 1);
+ completions.add(name);
+ }
+ });
+ }
+
+ addForClass(ClassMirror mirror, bool isStatic) {
+ if (mirror == null)
+ return;
+ addAll(mirror.members, isStatic);
+ if (mirror.superclass != null)
+ addForClass(mirror.superclass, isStatic);
+ for (var interface in mirror.superinterfaces) {
+ addForClass(interface, isStatic);
+ }
+ }
+
+ if (o is Type) {
+ addForClass(reflectClass(o), true);
+ } else {
+ addForClass(reflect(o).type, false);
+ }
+ return completions.toList(growable: false);
+ }
+
+ /**
* Convenience helper to get the keys of a [Map] as a [List].
*/
static List getMapKeyList(Map map) => map.keys.toList();
@@ -35297,79 +35411,10 @@ class _DOMStringMap extends NativeFieldWrapperClass1 implements Map<String, Stri
bool get isNotEmpty => Maps.isNotEmpty(this);
}
-// TODO(vsm): Remove DOM isolate code. This is only used to support
-// printing and timers in background isolates. Background isolates
-// should just forward to the main DOM isolate instead of requiring a
-// special DOM isolate.
-
-_makeSendPortFuture(spawnRequest) {
- final completer = new Completer<SendPort>.sync();
- final port = new ReceivePort();
- port.receive((result, _) {
- completer.complete(result);
- port.close();
- });
- // TODO: SendPort.hashCode is ugly way to access port id.
- spawnRequest(port.toSendPort().hashCode);
- return completer.future;
-}
-
-Future<SendPort> _spawnDomFunction(Function f) =>
- _makeSendPortFuture((portId) { _Utils.spawnDomFunction(f, portId); });
-
-final Future<SendPort> __HELPER_ISOLATE_PORT =
- _spawnDomFunction(_helperIsolateMain);
-
-// Tricky part.
-// Once __HELPER_ISOLATE_PORT gets resolved, it will still delay in .then
-// and to delay Timer.run is used. However, Timer.run will try to register
-// another Timer and here we got stuck: event cannot be posted as then
-// callback is not executed because it's delayed with timer.
-// Therefore once future is resolved, it's unsafe to call .then on it
-// in Timer code.
-SendPort __SEND_PORT;
-
-_sendToHelperIsolate(msg, SendPort replyTo) {
- if (__SEND_PORT != null) {
- __SEND_PORT.send(msg, replyTo);
- } else {
- __HELPER_ISOLATE_PORT.then((port) {
- __SEND_PORT = port;
- __SEND_PORT.send(msg, replyTo);
- });
- }
-}
-
-final _TIMER_REGISTRY = new Map<SendPort, Timer>();
-
-const _NEW_TIMER = 'NEW_TIMER';
-const _CANCEL_TIMER = 'CANCEL_TIMER';
-const _TIMER_PING = 'TIMER_PING';
-const _PRINT = 'PRINT';
-
-_helperIsolateMain() {
- port.receive((msg, replyTo) {
- final cmd = msg[0];
- if (cmd == _NEW_TIMER) {
- final duration = new Duration(milliseconds: msg[1]);
- bool periodic = msg[2];
- ping() { replyTo.send(_TIMER_PING); };
- _TIMER_REGISTRY[replyTo] = periodic ?
- new Timer.periodic(duration, (_) { ping(); }) :
- new Timer(duration, ping);
- } else if (cmd == _CANCEL_TIMER) {
- _TIMER_REGISTRY.remove(replyTo).cancel();
- } else if (cmd == _PRINT) {
- final message = msg[1];
- // TODO(antonm): we need somehow identify those isolates.
- print('[From isolate] $message');
- }
- });
-}
-
final _printClosure = window.console.log;
final _pureIsolatePrintClosure = (s) {
- _sendToHelperIsolate([_PRINT, s], null);
+ throw new UnimplementedError("Printing from a background isolate "
+ "is not supported in the browser");
};
final _forwardingPrintClosure = _Utils.forwardingPrint;
@@ -35408,46 +35453,10 @@ get _timerFactoryClosure =>
return new _Timer(milliSeconds, callback, repeating);
};
-
-class _PureIsolateTimer implements Timer {
- bool _isActive = true;
- final ReceivePort _port = new ReceivePort();
- SendPort _sendPort; // Effectively final.
-
- static SendPort _SEND_PORT;
-
- _PureIsolateTimer(int milliSeconds, callback, repeating) {
- _sendPort = _port.toSendPort();
- _port.receive((msg, replyTo) {
- assert(msg == _TIMER_PING);
- _isActive = repeating;
- callback(this);
- if (!repeating) _cancel();
- });
-
- _send([_NEW_TIMER, milliSeconds, repeating]);
- }
-
- void cancel() {
- _cancel();
- _send([_CANCEL_TIMER]);
- }
-
- void _cancel() {
- _isActive = false;
- _port.close();
- }
-
- _send(msg) {
- _sendToHelperIsolate(msg, _sendPort);
- }
-
- bool get isActive => _isActive;
-}
-
get _pureIsolateTimerFactoryClosure =>
((int milliSeconds, void callback(Timer time), bool repeating) =>
- new _PureIsolateTimer(milliSeconds, callback, repeating));
+ throw new UnimplementedError("Timers on background isolates "
+ "are not supported in the browser"));
void _initializeCustomElement(Element e) {
_Utils.initializeCustomElement(e);
« no previous file with comments | « sdk/lib/html/dart2js/html_dart2js.dart ('k') | sdk/lib/typed_data/typed_data.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698