Revert "Enhance dart:js interop in a backwards compatible manner."

This reverts commit ac32e37ab1300cb1ce1cd081f050ff1d04e54cbe.

BUG=

Review URL: https://codereview.chromium.org//1209093005.
This commit is contained in:
Jacob Richman 2015-06-26 10:29:25 -07:00
parent 3fdeb669ee
commit 7e9e4d89d3
6 changed files with 30 additions and 1181 deletions

View file

@ -120,9 +120,9 @@ final htmlBlinkMap = {
'_DOMWindowCrossFrame': () => _DOMWindowCrossFrame,
// FIXME: Move these to better locations.
'DateTime': () => DateTime,
'JsObject': () => js.JsObjectImpl,
'JsFunction': () => js.JsFunctionImpl,
'JsArray': () => js.JsArrayImpl,
'JsObject': () => js.JsObject,
'JsFunction': () => js.JsFunction,
'JsArray': () => js.JsArray,
'AbstractWorker': () => AbstractWorker,
'Animation': () => Animation,
'AnimationEffect': () => AnimationEffect,

View file

@ -89,386 +89,11 @@ library dart.js;
import 'dart:collection' show ListMixin;
import 'dart:nativewrappers';
import 'dart:math' as math;
import 'dart:mirrors' as mirrors;
// Pretend we are always in checked mode as we aren't interested in users
// running Dartium code outside of checked mode.
final bool CHECK_JS_INVOCATIONS = true;
final _allowedMethods = new Map<Symbol, _DeclarationSet>();
final _allowedGetters = new Map<Symbol, _DeclarationSet>();
final _allowedSetters = new Map<Symbol, _DeclarationSet>();
final _jsInterfaceTypes = new Set<Type>();
Iterable<Type> get jsInterfaceTypes => _jsInterfaceTypes;
/// A collection of methods where all methods have the same name.
/// This class is intended to optimize whether a specific invocation is
/// appropritate for at least some of the methods in the collection.
class _DeclarationSet {
_DeclarationSet() : _members = <mirrors.DeclarationMirror>[];
static bool _checkType(obj, mirrors.TypeMirror type) {
if (obj == null) return true;
return mirrors.reflectType(obj.runtimeType).isSubtypeOf(type);
}
/// Returns whether the return [value] has a type is consistent with the
/// return type from at least one of the members matching the DeclarationSet.
bool _checkReturnType(value) {
if (value == null) return true;
var valueMirror = mirrors.reflectType(value.runtimeType);
for (var member in _members) {
if (member is mirrors.VariableMirror || member.isGetter) {
// TODO(jacobr): actually check return types for getters that return
// function types.
return true;
} else {
if (valueMirror.isSubtypeOf(member.returnType)) return true;
}
}
return false;
}
/**
* Check whether the [invocation] is consistent with the [member] mirror.
*/
bool _checkDeclaration(
Invocation invocation, mirrors.DeclarationMirror member) {
if (member is mirrors.VariableMirror || member.isGetter) {
// TODO(jacobr): actually check method types against the function type
// returned by the getter or field.
return true;
}
var parameters = member.parameters;
var positionalArguments = invocation.positionalArguments;
// Too many arguments
if (parameters.length < positionalArguments.length) return false;
// Too few required arguments.
if (parameters.length > positionalArguments.length &&
!parameters[positionalArguments.length].isOptional) return false;
for (var i = 0; i < positionalArguments.length; i++) {
if (parameters[i].isNamed) {
// Not enough positional arguments.
return false;
}
if (!_checkType(
invocation.positionalArguments[i], parameters[i].type)) return false;
}
if (invocation.namedArguments.isNotEmpty) {
var startNamed;
for (startNamed = parameters.length - 1; startNamed >= 0; startNamed--) {
if (!parameters[startNamed].isNamed) break;
}
startNamed++;
// TODO(jacobr): we are unneccessarily using an O(n^2) algorithm here.
// If we have JS APIs with a lange number of named parameters we should
// optimize this. Either use a HashSet or invert this, walking over
// parameters, querying invocation, and making sure we match
//invocation.namedArguments.size keys.
for (var name in invocation.namedArguments.keys) {
bool match = false;
for (var j = startNamed; j < parameters.length; j++) {
var p = parameters[j];
if (p.simpleName == name) {
if (!_checkType(invocation.namedArguments[name],
parameters[j].type)) return false;
match = true;
break;
}
}
if (match == false) return false;
}
}
return true;
}
bool checkInvocation(Invocation invocation) {
for (var member in _members) {
if (_checkDeclaration(invocation, member)) return true;
}
return false;
}
void add(mirrors.DeclarationMirror mirror) {
_members.add(mirror);
}
final List<mirrors.DeclarationMirror> _members;
}
/**
* Temporary method that we hope to remove at some point. This method should
* generally only be called by machine generated code.
*/
void registerJsInterfaces(List<Type> classes) {
if (_finalized == true) {
throw 'JSInterop class registration already finalized';
}
for (Type type in classes) {
if (!_jsInterfaceTypes.add(type)) continue; // Already registered.
mirrors.ClassMirror typeMirror = mirrors.reflectType(type);
typeMirror.declarations.forEach((symbol, declaration) {
if (declaration is mirrors.MethodMirror ||
declaration is mirrors.VariableMirror && !declaration.isStatic) {
bool treatAsGetter = false;
bool treatAsSetter = false;
if (declaration is mirrors.VariableMirror) {
treatAsGetter = true;
if (!declaration.isConst && !declaration.isFinal) {
treatAsSetter = true;
}
} else {
if (declaration.isGetter) {
treatAsGetter = true;
} else if (declaration.isSetter) {
treatAsSetter = true;
} else if (!declaration.isConstructor) {
_allowedMethods
.putIfAbsent(symbol, () => new _DeclarationSet())
.add(declaration);
}
}
if (treatAsGetter) {
_allowedGetters
.putIfAbsent(symbol, () => new _DeclarationSet())
.add(declaration);
_allowedMethods
.putIfAbsent(symbol, () => new _DeclarationSet())
.add(declaration);
}
if (treatAsSetter) {
_allowedSetters
.putIfAbsent(symbol, () => new _DeclarationSet())
.add(declaration);
}
}
});
}
}
_finalizeJsInterfaces() native "Js_finalizeJsInterfaces";
/**
* Generates a part file defining source code for JsObjectImpl and related
* classes. This calass is needed so that type checks for all registered JavaScript
* interop classes pass.
*/
String _generateJsObjectImplPart() {
Iterable<Type> types = jsInterfaceTypes;
var libraryPrefixes = new Map<mirrors.LibraryMirror, String>();
var prefixNames = new Set<String>();
var sb = new StringBuffer();
var implements = <String>[];
for (var type in types) {
mirrors.ClassMirror typeMirror = mirrors.reflectType(type);
mirrors.LibraryMirror libraryMirror = typeMirror.owner;
var prefixName;
if (libraryPrefixes.containsKey(libraryMirror)) {
prefixName = libraryPrefixes[libraryMirror];
} else {
var basePrefixName =
mirrors.MirrorSystem.getName(libraryMirror.simpleName);
basePrefixName = basePrefixName.replaceAll('.', '_');
if (basePrefixName.isEmpty) basePrefixName = "lib";
prefixName = basePrefixName;
var i = 1;
while (prefixNames.contains(prefixName)) {
prefixName = '$basePrefixName$i';
i++;
}
prefixNames.add(prefixName);
libraryPrefixes[libraryMirror] = prefixName;
}
implements.add(
'${prefixName}.${mirrors.MirrorSystem.getName(typeMirror.simpleName)}');
}
libraryPrefixes.forEach((libraryMirror, prefix) {
sb.writeln('import "${libraryMirror.uri}" as $prefix;');
});
var implementsClause =
implements.isEmpty ? "" : "implements ${implements.join(', ')}";
// TODO(jacobr): only certain classes need to be implemented by
// Function and Array.
sb.write('''
class JsObjectImpl extends JsObject $implementsClause {
JsObjectImpl.internal() : super.internal();
}
class JsFunctionImpl extends JsFunction $implementsClause {
JsFunctionImpl.internal() : super.internal();
}
class JsArrayImpl<E> extends JsArray<E> $implementsClause {
JsArrayImpl.internal() : super.internal();
}
''');
return sb.toString();
}
// Start of block of helper methods facilitating emulating JavaScript Array
// methods on Dart List objects passed to JavaScript via JS interop.
// TODO(jacobr): match JS more closely.
String _toStringJs(obj) => '$obj';
// TODO(jacobr): this might not exactly match JS semantics but should be
// adequate for now.
int _toIntJs(obj) {
if (obj is int) return obj;
if (obj is num) return obj.toInt();
return num.parse('$obj'.trim(), (_) => 0).toInt();
}
// TODO(jacobr): this might not exactly match JS semantics but should be
// adequate for now.
num _toNumJs(obj) {
return obj is num ? obj : num.parse('$obj'.trim(), (_) => 0);
}
/// Match the behavior of setting List length in JavaScript with the exception
/// that Dart does not distinguish undefined and null.
_setListLength(List list, rawlen) {
num len = _toNumJs(rawlen);
if (len is! int || len < 0) {
throw new RangeError("Invalid array length");
}
if (len > list.length) {
_arrayExtend(list, len);
} else if (len < list.length) {
list.removeRange(len, list.length);
}
return rawlen;
}
// TODO(jacobr): should we really bother with this method instead of just
// shallow copying to a JS array and calling the JavaScript join method?
String _arrayJoin(List list, sep) {
if (sep == null) {
sep = ",";
}
return list.map((e) => e == null ? "" : e.toString()).join(sep.toString());
}
// TODO(jacobr): should we really bother with this method instead of just
// shallow copying to a JS array and using the toString method?
String _arrayToString(List list) => _arrayJoin(list, ",");
int _arrayPush(List list, e) {
list.add(e);
return list.length;
}
_arrayPop(List list) {
if (list.length > 0) return list.removeLast();
}
// TODO(jacobr): would it be better to just copy input to a JS List
// and call Array.concat?
List _arrayConcat(List input, List args) {
var ret = new List.from(input);
for (var e in args) {
// TODO(jacobr): technically in ES6 we should use
// Symbol.isConcatSpreadable to determine whether call addAll. Once v8
// supports it, we can make all Dart classes implementing Iterable
// specify isConcatSpreadable and tweak this behavior to allow Iterable.
if (e is List) {
ret.addAll(e);
} else {
ret.add(e);
}
}
return ret;
}
List _arraySplice(List input, List args) {
int start = 0;
if (args.length > 0) {
var rawStart = _toIntJs(args[0]);
if (rawStart < 0) {
start = math.max(0, input.length - rawStart);
} else {
start = math.min(input.length, rawStart);
}
}
var end = start;
if (args.length > 1) {
var rawDeleteCount = _toIntJs(args[1]);
if (rawDeleteCount < 0) rawDeleteCount = 0;
end = math.min(input.length, start + rawDeleteCount);
}
var replacement = [];
var removedElements = input.getRange(start, end).toList();
if (args.length > 2) {
replacement = args.getRange(2, args.length);
}
input.replaceRange(start, end, replacement);
return removedElements;
}
List _arrayReverse(List l) {
for (var i = 0, j = l.length - 1; i < j; i++, j--) {
var tmp = l[i];
l[i] = l[j];
l[j] = tmp;
}
return l;
}
_arrayShift(List l) {
if (l.isEmpty) return null; // Technically we should return undefined.
return l.removeAt(0);
}
int _arrayUnshift(List l, List args) {
l.insertAll(0, args);
return l.length;
}
_arrayExtend(List l, int newLength) {
for (var i = l.length; i < newLength; i++) {
// TODO(jacobr): we'd really like to add undefined to better match
// JavaScript semantics.
l.add(null);
}
}
List _arraySort(List l, rawCompare) {
// TODO(jacobr): alternately we could just copy the Array to JavaScript,
// invoke the JS sort method and then copy the result back to Dart.
Comparator compare;
if (rawCompare == null) {
compare = (a, b) => _toStringJs(a).compareTo(_toStringJs(b));
} else if (rawCompare is JsFunction) {
compare = (a, b) => rawCompare.apply([a, b]);
} else {
compare = rawCompare;
}
l.sort(compare);
return l;
}
// End of block of helper methods to emulate JavaScript Array methods on Dart List.
/**
* Can be called to provide a predictable point where no more JS interfaces can
* be added. Creating an instance of JsObject will also automatically trigger
* all JsObjects to be finalized.
*/
void finalizeJsInterfaces() {
if (_finalized == true) {
throw 'JSInterop class registration already finalized';
}
_finalizeJsInterfaces();
}
JsObject _cachedContext;
JsObject get _context native "Js_context_Callback";
bool get _finalized native "Js_interfacesFinalized_Callback";
JsObject get context {
if (_cachedContext == null) {
_cachedContext = _context;
@ -489,24 +114,9 @@ class JsObject extends NativeFieldWrapperClass2 {
* Constructs a new JavaScript object from [constructor] and returns a proxy
* to it.
*/
factory JsObject(JsFunction constructor, [List arguments]) =>
_create(constructor, arguments);
factory JsObject(JsFunction constructor, [List arguments]) => _create(constructor, arguments);
static JsObject _create(
JsFunction constructor, arguments) native "JsObject_constructorCallback";
_buildArgs(Invocation invocation) {
if (invocation.namedArguments.isEmpty) {
return invocation.positionalArguments;
} else {
var varArgs = new Map<String, Object>();
invocation.namedArguments.forEach((symbol, val) {
varArgs[mirrors.MirrorSystem.getName(symbol)] = val;
});
return invocation.positionalArguments.toList()
..add(new JsObject.jsify(varArgs));
}
}
static JsObject _create(JsFunction constructor, arguments) native "JsObject_constructorCallback";
/**
* Constructs a [JsObject] that proxies a native Dart object; _for expert use
@ -521,7 +131,8 @@ class JsObject extends NativeFieldWrapperClass2 {
*/
factory JsObject.fromBrowserObject(object) {
if (object is num || object is String || object is bool || object == null) {
throw new ArgumentError("object cannot be a num, string, bool, or null");
throw new ArgumentError(
"object cannot be a num, string, bool, or null");
}
return _fromBrowserObject(object);
}
@ -544,8 +155,7 @@ class JsObject extends NativeFieldWrapperClass2 {
static JsObject _jsify(object) native "JsObject_jsify";
static JsObject _fromBrowserObject(
object) native "JsObject_fromBrowserObject";
static JsObject _fromBrowserObject(object) native "JsObject_fromBrowserObject";
/**
* Returns the value associated with [property] from the proxied JavaScript
@ -553,7 +163,7 @@ class JsObject extends NativeFieldWrapperClass2 {
*
* The type of [property] must be either [String] or [num].
*/
operator [](property) native "JsObject_[]";
operator[](property) native "JsObject_[]";
/**
* Sets the value associated with [property] on the proxied JavaScript
@ -561,14 +171,13 @@ class JsObject extends NativeFieldWrapperClass2 {
*
* The type of [property] must be either [String] or [num].
*/
operator []=(property, value) native "JsObject_[]=";
operator[]=(property, value) native "JsObject_[]=";
int get hashCode native "JsObject_hashCode";
operator ==(other) => other is JsObject && _identityEquality(this, other);
operator==(other) => other is JsObject && _identityEquality(this, other);
static bool _identityEquality(
JsObject a, JsObject b) native "JsObject_identityEquality";
static bool _identityEquality(JsObject a, JsObject b) native "JsObject_identityEquality";
/**
* Returns `true` if the JavaScript object contains the specified property
@ -598,7 +207,7 @@ class JsObject extends NativeFieldWrapperClass2 {
String toString() {
try {
return _toString();
} catch (e) {
} catch(e) {
return super.toString();
}
}
@ -614,7 +223,7 @@ class JsObject extends NativeFieldWrapperClass2 {
callMethod(String method, [List args]) {
try {
return _callMethod(method, args);
} catch (e) {
} catch(e) {
if (hasProperty(method)) {
rethrow;
} else {
@ -623,63 +232,13 @@ class JsObject extends NativeFieldWrapperClass2 {
}
}
noSuchMethod(Invocation invocation) {
throwError() {
throw new NoSuchMethodError(this, invocation.memberName,
invocation.positionalArguments, invocation.namedArguments);
}
String name = mirrors.MirrorSystem.getName(invocation.memberName);
if (invocation.isGetter) {
if (CHECK_JS_INVOCATIONS) {
var matches = _allowedGetters[invocation.memberName];
if (matches == null &&
!_allowedMethods.containsKey(invocation.memberName)) {
throwError();
}
var ret = this[name];
if (matches != null && matches._checkReturnType(ret)) return ret;
if (ret is Function ||
(ret is JsFunction /* shouldn't be needed in the future*/) &&
_allowedMethods.containsKey(
invocation.memberName)) return ret; // Warning: we have not bound "this"... we could type check on the Function but that is of little value in Dart.
throwError();
} else {
// TODO(jacobr): should we throw if the JavaScript object doesn't have the property?
return this[name];
}
} else if (invocation.isSetter) {
if (CHECK_JS_INVOCATIONS) {
var matches = _allowedSetters[invocation.memberName];
if (matches == null ||
!matches.checkInvocation(invocation)) throwError();
}
assert(name.endsWith("="));
name = name.substring(0, name.length - 1);
return this[name] = invocation.positionalArguments.first;
} else {
// TODO(jacobr): also allow calling getters that look like functions.
var matches;
if (CHECK_JS_INVOCATIONS) {
matches = _allowedMethods[invocation.memberName];
if (matches == null ||
!matches.checkInvocation(invocation)) throwError();
}
var ret = this.callMethod(name, _buildArgs(invocation));
if (CHECK_JS_INVOCATIONS) {
if (!matches._checkReturnType(ret)) throwError();
}
return ret;
}
}
_callMethod(String name, List args) native "JsObject_callMethod";
}
/**
* Proxies a JavaScript Function object.
*/
class JsFunction extends JsObject implements Function {
class JsFunction extends JsObject {
JsFunction.internal() : super.internal();
/**
@ -694,21 +253,13 @@ class JsFunction extends JsObject implements Function {
*/
dynamic apply(List args, {thisArg}) native "JsFunction_apply";
noSuchMethod(Invocation invocation) {
if (invocation.isMethod && invocation.memberName == #call) {
return apply(_buildArgs(invocation));
}
return super.noSuchMethod(invocation);
}
/**
* Internal only version of apply which uses debugger proxies of Dart objects
* rather than opaque handles. This method is private because it cannot be
* efficiently implemented in Dart2Js so should only be used by internal
* tools.
*/
_applyDebuggerOnly(List args,
{thisArg}) native "JsFunction_applyDebuggerOnly";
_applyDebuggerOnly(List args, {thisArg}) native "JsFunction_applyDebuggerOnly";
static JsFunction _withThis(Function f) native "JsFunction_withThis";
}
@ -717,17 +268,14 @@ class JsFunction extends JsObject implements Function {
* A [List] proxying a JavaScript Array.
*/
class JsArray<E> extends JsObject with ListMixin<E> {
JsArray.internal() : super.internal();
factory JsArray() => _newJsArray();
static JsArray _newJsArray() native "JsArray_newJsArray";
factory JsArray.from(Iterable<E> other) =>
_newJsArrayFromSafeList(new List.from(other));
factory JsArray.from(Iterable<E> other) => _newJsArrayFromSafeList(new List.from(other));
static JsArray _newJsArrayFromSafeList(
List list) native "JsArray_newJsArrayFromSafeList";
static JsArray _newJsArrayFromSafeList(List list) native "JsArray_newJsArrayFromSafeList";
_checkIndex(int index, {bool insert: false}) {
int length = insert ? this.length + 1 : this.length;
@ -756,7 +304,7 @@ class JsArray<E> extends JsObject with ListMixin<E> {
}
void operator []=(index, E value) {
if (index is int) {
if(index is int) {
_checkIndex(index);
}
super[index] = value;
@ -764,9 +312,7 @@ class JsArray<E> extends JsObject with ListMixin<E> {
int get length native "JsArray_length";
void set length(int length) {
super['length'] = length;
}
void set length(int length) { super['length'] = length; }
// Methods overriden for better performance
@ -780,7 +326,7 @@ class JsArray<E> extends JsObject with ListMixin<E> {
}
void insert(int index, E element) {
_checkIndex(index, insert: true);
_checkIndex(index, insert:true);
callMethod('splice', [index, 0, element]);
}
@ -819,7 +365,7 @@ class JsArray<E> extends JsObject with ListMixin<E> {
*/
const _UNDEFINED = const Object();
// TODO(jacobr): this method is a hack to work around the lack of proper dart
// FIXME(jacobr): this method is a hack to work around the lack of proper dart
// support for varargs methods.
List _stripUndefinedArgs(List args) =>
args.takeWhile((i) => i != _UNDEFINED).toList();
@ -829,7 +375,8 @@ List _stripUndefinedArgs(List args) =>
* than 11) of arguments without violating Dart type checks.
*/
Function _wrapAsDebuggerVarArgsFunction(JsFunction jsFunction) =>
([a1 = _UNDEFINED, a2 = _UNDEFINED, a3 = _UNDEFINED, a4 = _UNDEFINED,
a5 = _UNDEFINED, a6 = _UNDEFINED, a7 = _UNDEFINED, a8 = _UNDEFINED,
a9 = _UNDEFINED, a10 = _UNDEFINED]) => jsFunction._applyDebuggerOnly(
_stripUndefinedArgs([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]));
([a1=_UNDEFINED, a2=_UNDEFINED, a3=_UNDEFINED, a4=_UNDEFINED,
a5=_UNDEFINED, a6=_UNDEFINED, a7=_UNDEFINED, a8=_UNDEFINED,
a9=_UNDEFINED, a10=_UNDEFINED]) =>
jsFunction._applyDebuggerOnly(_stripUndefinedArgs(
[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10]));

View file

@ -6,9 +6,6 @@ interactive_test: Skip # Must be run manually.
dromaeo_smoke_test: Skip # Issue 14521, 8257
cross_frame_test: Skip # Test reloads itself. Issue 18558
js_array_test: Skip # Issue 23676, 23677
js_typed_interop_test: Skip # Issue 23676, 23677
[ $compiler == none && ($runtime == drt || $runtime == dartium || $runtime == ContentShellOnAndroid) ]
custom/attribute_changed_callback_test/unsupported_on_polyfill: Fail # Issue 18931 (Disabled for Chrome 35 roll)
form_data_test/functional: Skip # Issue 19726

View file

@ -1,556 +0,0 @@
// Copyright (c) 2015, 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.
library jsArrayTest;
import 'dart:html';
import 'dart:js';
import 'package:unittest/unittest.dart';
import 'package:unittest/html_config.dart';
_injectJs() {
document.body.append(new ScriptElement()
..type = 'text/javascript'
..innerHtml = r"""
function callJsMethod(jsObj, jsMethodName, args) {
return jsObj[jsMethodName].apply(jsObj, args);
}
function jsEnumerateIndices(obj) {
var ret = [];
for(var i in obj) {
ret.push(i);
}
return ret;
}
function setValue(obj, index, value) {
return obj[index] = value;
}
function getValue(obj, index) {
return obj[index];
}
function checkIsArray(obj) {
return Array.isArray(obj);
}
function concatValues(obj) {
return obj.concat("a", "b", ["c", "d"], 42, {foo: 10});
}
function concatOntoArray(obj) {
return [1,2,3].concat(obj, "foo");
}
function repeatedConcatOntoArray(obj) {
return [1,2,3].concat(obj, obj);
}
function everyGreaterThanZero(obj) {
return obj.every(function(currentValue, index, array) {
return currentValue > 0;
});
}
function everyGreaterThanZeroCheckThisArg(obj) {
var j = 0;
return obj.every(function(currentValue, index, array) {
if (j != index) {
throw "Unxpected index";
}
j++;
if (array !== obj) {
throw "Array argument doesn't match obj";
}
return currentValue > 0;
});
}
function filterGreater42(obj) {
return obj.filter(function(currentValue, index, array) {
return currentValue > 42;
});
}
function forEachCollectResult(array, callback) {
var result = [];
array.forEach(function(currentValue) {
result.push(currentValue * 2);
});
return result;
}
function someEqual42(array, callback) {
return array.some(function(currentValue) {
return currentValue == 42;
});
}
function sortNumbersBackwards(array) {
return array.sort(function(a, b) {
return b - a;
});
}
function spliceDummyItems(array) {
return array.splice(1, 2, "quick" ,"brown", "fox");
}
function spliceTestStringArgs(array) {
return array.splice("1.2", "2.01", "quick" ,"brown", "fox");
}
function splicePastEnd(array) {
return array.splice(1, 5332, "quick" ,"brown", "fox");
}
function callJsToString(array) {
return array.toString();
}
function mapAddIndexToEachElement(array) {
return array.map(function(currentValue, index) {
return currentValue + index;
});
}
function reduceSumDoubledElements(array) {
return array.reduce(function(previousValue, currentValue) {
return previousValue + currentValue*2;
},
0);
}
// TODO(jacobr): add a test that distinguishes reduce from reduceRight.
function reduceRightSumDoubledElements(array) {
return array.reduceRight(
function(previousValue, currentValue) {
return previousValue + currentValue*2;
},
0);
}
function identical(o1, o2) {
return o1 === o2;
}
function getOwnPropertyDescriptorJson(array, property) {
return JSON.stringify(Object.getOwnPropertyDescriptor(array, property));
}
function setLength(array, len) {
return array.length = len;
}
""");
}
class Foo {}
callJsMethod(List array, String methodName, List args) =>
context.callMethod("callJsMethod", [array, methodName, args]);
callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]);
callLastIndexOf(List array, value) =>
callJsMethod(array, "lastIndexOf", [value]);
callPop(List array) => callJsMethod(array, "pop", []);
callPush(List array, element) => callJsMethod(array, "push", [element]);
callShift(List array) => callJsMethod(array, "shift", []);
callReverse(List array) => callJsMethod(array, "reverse", []);
callSetLength(List array, length) =>
context.callMethod("setLength", [array, length]);
main() {
_injectJs();
useHtmlConfiguration();
group('indexOf', () {
var div = new DivElement();
var list = [3, 42, "foo", 42, div];
test('found', () {
expect(callIndexOf(list, 3), equals(0));
expect(callIndexOf(list, 42), equals(1));
expect(callIndexOf(list, "foo"), equals(2));
expect(callIndexOf(list, div), equals(4));
});
test('missing', () {
expect(callIndexOf(list, 31), equals(-1));
expect(callIndexOf(list, "42"), equals(-1));
expect(callIndexOf(list, null), equals(-1));
});
});
group('set length', () {
test('larger', () {
var list = ["a", "b", "c", "d"];
expect(callSetLength(list, 10), equals(10));
expect(list.length, equals(10));
expect(list.last, equals(null));
expect(list[3], equals("d"));
});
test('smaller', () {
var list = ["a", "b", "c", "d"];
expect(callSetLength(list, 2), equals(2));
expect(list.first, equals("a"));
expect(list.last, equals("b"));
expect(list.length, equals(2));
expect(callSetLength(list, 0), equals(0));
expect(list.length, equals(0));
expect(callSetLength(list, 2), equals(2));
expect(list.first, equals(null));
});
test('invalid', () {
var list = ["a", "b", "c", "d"];
expect(() => callSetLength(list, 2.3), throws);
expect(list.length, equals(4));
expect(() => callSetLength(list, -1), throws);
expect(list.length, equals(4));
// Make sure we are coercing to a JS number.
expect(callSetLength(list, "2"), equals("2"));
expect(list.length, equals(2));
});
});
group('join', () {
var list = [3, 42, "foo"];
var listWithDartClasses = [3, new Foo(), 42, "foo", new Object()];
test('default', () {
expect(callJsMethod(list, "join", []), equals("3,42,foo"));
expect(callJsMethod(listWithDartClasses, "join", []),
equals("3,Instance of 'Foo',42,foo,Instance of 'Object'"));
});
test('custom separator', () {
expect(callJsMethod(list, "join", ["##"]), equals("3##42##foo"));
});
});
group('lastIndexOf', () {
var list = [3, 42, "foo", 42];
test('found', () {
expect(callLastIndexOf(list, 3), equals(0));
expect(callLastIndexOf(list, 42), equals(3));
expect(callLastIndexOf(list, "foo"), equals(2));
});
test('missing', () {
expect(callLastIndexOf(list, 31), equals(-1));
expect(callLastIndexOf(list, "42"), equals(-1));
expect(callLastIndexOf(list, null), equals(-1));
});
});
group('pop', () {
test('all', () {
var foo = new Foo();
var div = new DivElement();
var list = [3, 42, "foo", foo, div];
expect(callPop(list), equals(div));
expect(list.length, equals(4));
expect(callPop(list), equals(foo));
expect(list.length, equals(3));
expect(callPop(list), equals("foo"));
expect(list.length, equals(2));
expect(callPop(list), equals(42));
expect(list.length, equals(1));
expect(callPop(list), equals(3));
expect(list.length, equals(0));
expect(callPop(list), equals(null));
expect(list.length, equals(0));
});
});
group('push', () {
test('strings', () {
var list = [];
var div = new DivElement();
expect(callPush(list, "foo"), equals(1));
expect(callPush(list, "bar"), equals(2));
expect(callPush(list, "baz"), equals(3));
expect(callPush(list, div), equals(4));
expect(list, equals(["foo", "bar", "baz", div]));
});
});
group('shift', () {
test('all', () {
var foo = new Foo();
var div = new DivElement();
var list = [3, 42, "foo", foo, div];
expect(callShift(list), equals(3));
expect(list.length, equals(4));
expect(callShift(list), equals(42));
expect(list.length, equals(3));
expect(callShift(list), equals("foo"));
expect(list.length, equals(2));
expect(callShift(list), equals(foo));
expect(list.length, equals(1));
expect(callShift(list), equals(div));
expect(list.length, equals(0));
expect(callShift(list), equals(null));
expect(list.length, equals(0));
});
});
group('reverse', () {
test('simple', () {
var foo = new Foo();
var div = new DivElement();
var list = [div, 42, foo];
callReverse(list);
expect(list, equals([foo, 42, div]));
list = [3, 42];
callReverse(list);
expect(list, equals([42, 3]));
});
});
group('slice', () {
test('copy', () {
var foo = new Foo();
var div = new DivElement();
var list = [3, 42, "foo", foo, div];
var copy = callJsMethod(list, "slice", []);
expect(identical(list, copy), isFalse);
expect(copy.length, equals(list.length));
for (var i = 0; i < list.length; i++) {
expect(list[i], equals(copy[i]));
}
expect(identical(list[3], copy[3]), isTrue);
expect(identical(list[4], copy[4]), isTrue);
copy.add("dummy");
expect(list.length + 1, equals(copy.length));
});
test('specify start', () {
var list = [3, 42, "foo"];
var copy = callJsMethod(list, "slice", [1]);
expect(copy.first, equals(42));
});
test('specify start and end', () {
var list = [3, 42, 92, "foo"];
var copy = callJsMethod(list, "slice", [1, 3]);
expect(copy.first, equals(42));
expect(copy.last, equals(92));
});
test('from end', () {
var list = [3, 42, 92, "foo"];
expect(callJsMethod(list, "slice", [-2]), equals([92, "foo"]));
// Past the end of the front of the array.
expect(callJsMethod(list, "slice", [-2, 3]), equals([92]));
// Past the end of the front of the array.
expect(callJsMethod(list, "slice", [-10, 2]), equals([3, 42]));
});
});
group("js snippet tests", () {
test("enumerate indices", () {
var list = ["a", "b", "c", "d"];
var indices = context.callMethod('jsEnumerateIndices', [list]);
expect(indices.length, equals(4));
for (int i = 0; i < 4; i++) {
expect(indices[i], equals('$i'));
}
});
test("set element", () {
var list = ["a", "b", "c", "d"];
context.callMethod('setValue', [list, 0, 42]);
expect(list[0], equals(42));
context.callMethod('setValue', [list, 1, 84]);
expect(list[1], equals(84));
context.callMethod(
'setValue', [list, 6, 100]); // Off the end of the list.
expect(list.length, equals(7));
expect(list[4], equals(null));
expect(list[6], equals(100));
// These tests have to be commented out because we don't persist
// JS proxies for Dart objects like we could/should.
// context.callMethod('setValue', [list, -1, "foo"]); // Not a valid index
// expect(context.callMethod('getValue', [list, -1]), equals("foo"));
// expect(context.callMethod('getValue', [list, "-1"]), equals("foo"));
});
test("get element", () {
var list = ["a", "b", "c", "d"];
expect(context.callMethod('getValue', [list, 0]), equals("a"));
expect(context.callMethod('getValue', [list, 1]), equals("b"));
expect(context.callMethod('getValue', [list, 6]), equals(null));
expect(context.callMethod('getValue', [list, -1]), equals(null));
expect(context.callMethod('getValue', [list, "0"]), equals("a"));
expect(context.callMethod('getValue', [list, "1"]), equals("b"));
});
test("is array", () {
var list = ["a", "b"];
expect(context.callMethod("checkIsArray", [list]), isTrue);
});
test("property descriptors", () {
// This test matters to make behavior consistent with JS native arrays
// and to make devtools integration work well.
var list = ["a", "b"];
expect(context.callMethod("getOwnPropertyDescriptorJson", [list, 0]),
equals('{"value":"a",'
'"writable":true,'
'"enumerable":true,'
'"configurable":true}'));
expect(
context.callMethod("getOwnPropertyDescriptorJson", [list, "length"]),
equals('{"value":2,'
'"writable":true,'
'"enumerable":false,'
'"configurable":false}'));
});
test("concat js arrays", () {
var list = ["1", "2"];
// Tests that calling the concat method from JS will flatten out JS arrays
// We concat the array with "a", "b", ["c", "d"], 42, {foo: 10}
// which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}]
var ret = context.callMethod("concatValues", [list]);
expect(list.length, equals(2));
expect(ret.length, equals(8));
expect(ret[0], equals("1"));
expect(ret[3], equals("b"));
expect(ret[5], equals("d"));
expect(ret[6], equals(42));
expect(ret[7]['foo'], equals(10));
});
test("concat onto arrays", () {
// This test only passes if we have monkey patched the core Array object
// prototype to handle Dart Lists.
var list = ["a", "b"];
var ret = context.callMethod("concatOntoArray", [list]);
expect(list.length, equals(2));
expect(ret, equals([1, 2, 3, "a", "b", "foo"]));
});
test("dart arrays on dart arrays", () {
// This test only passes if we have monkey patched the core Array object
// prototype to handle Dart Lists.
var list = ["a", "b"];
var ret = callJsMethod(list, "concat", [["c", "d"], "e", ["f", "g"]]);
expect(list.length, equals(2));
expect(ret, equals(["a", "b", "c", "d", "e", "f", "g"]));
});
test("every greater than zero", () {
expect(context.callMethod("everyGreaterThanZero", [[1, 5]]), isTrue);
expect(context.callMethod("everyGreaterThanZeroCheckThisArg", [[1, 5]]),
isTrue);
expect(context.callMethod("everyGreaterThanZero", [[1, 0]]), isFalse);
expect(context.callMethod("everyGreaterThanZero", [[]]), isTrue);
});
test("filter greater than 42", () {
expect(context.callMethod("filterGreater42", [[1, 5]]), equals([]));
expect(context.callMethod("filterGreater42", [[43, 5, 49]]),
equals([43, 49]));
expect(context.callMethod("filterGreater42", [["43", "5", "49"]]),
equals(["43", "49"]));
});
test("for each collect result", () {
expect(context.callMethod("forEachCollectResult", [[1, 5, 7]]),
equals([2, 10, 14]));
});
test("some", () {
expect(context.callMethod("someEqual42", [[1, 5, 9]]), isFalse);
expect(context.callMethod("someEqual42", [[1, 42, 9]]), isTrue);
});
test("sort backwards", () {
var arr = [1, 5, 9];
var ret = context.callMethod("sortNumbersBackwards", [arr]);
expect(identical(arr, ret), isTrue);
expect(ret, equals([9, 5, 1]));
});
test("splice dummy items", () {
var list = [1, 2, 3, 4];
var removed = context.callMethod("spliceDummyItems", [list]);
expect(removed.length, equals(2));
expect(removed[0], equals(2));
expect(removed[1], equals(3));
expect(list.first, equals(1));
expect(list[1], equals("quick"));
expect(list[2], equals("brown"));
expect(list[3], equals("fox"));
expect(list.last, equals(4));
});
test("splice string args", () {
var list = [1, 2, 3, 4];
var removed = context.callMethod("spliceTestStringArgs", [list]);
expect(removed.length, equals(2));
expect(removed[0], equals(2));
expect(removed[1], equals(3));
expect(list.first, equals(1));
expect(list[1], equals("quick"));
expect(list[2], equals("brown"));
expect(list[3], equals("fox"));
expect(list.last, equals(4));
});
test("splice pastEndOfArray", () {
var list = [1, 2, 3, 4];
var removed = context.callMethod("splicePastEnd", [list]);
expect(removed.length, equals(3));
expect(list.first, equals(1));
expect(list.length, equals(4));
expect(list[1], equals("quick"));
expect(list[2], equals("brown"));
expect(list[3], equals("fox"));
});
test("splice both bounds past end of array", () {
var list = [1];
var removed = context.callMethod("splicePastEnd", [list]);
expect(removed.length, equals(0));
expect(list.first, equals(1));
expect(list.length, equals(4));
expect(list[1], equals("quick"));
expect(list[2], equals("brown"));
expect(list[3], equals("fox"));
});
});
// This test group is disabled until we figure out an efficient way to
// distinguish between "array" Dart List types and non-array Dart list types.
/*
group('Non-array Lists', () {
test('opaque proxy', () {
// Dartium could easily support making LinkedList and all other classes
// implementing List behave like a JavaScript array but that would
// be challenging to implement in dart2js until browsers support ES6.
var list = ["a", "b", "c", "d"];
var listView = new UnmodifiableListView(list.getRange(1,3));
expect(listView is List, isTrue);
expect(listView.length, equals(2));
expect(context.callMethod("checkIsArray", [listView]), isFalse);
expect(context.callMethod("checkIsArray", [listView.toList()]), isTrue);
expect(context.callMethod("getOwnPropertyDescriptorJson",
[listView, "length"]), equals("null"));
});
});
*/
}

View file

@ -1,139 +0,0 @@
// Copyright (c) 2015, 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.
library jsArrayTest;
import 'dart:html';
import 'dart:js';
import 'package:unittest/unittest.dart';
import 'package:unittest/html_config.dart';
_injectJs() {
document.body.append(new ScriptElement()
..type = 'text/javascript'
..innerHtml = r"""
var foo = {
x: 3,
z: 40, // Not specified in typed Dart API so should fail in checked mode.
multiplyByX: function(arg) { return arg * this.x; },
// This function can be torn off without having to bind this.
multiplyBy2: function(arg) { return arg * 2; }
};
var foob = {
x: 8,
y: "why",
multiplyByX: function(arg) { return arg * this.x; }
};
var bar = {
x: "foo",
multiplyByX: true
};
var selection = ["a", "b", "c", foo, bar];
selection.doubleLength = function() { return this.length * 2; };
""");
}
abstract class Foo {
int get x;
set x(int v);
num multiplyByX(num y);
num multiplyBy2(num y);
}
abstract class Foob extends Foo {
final String y;
}
abstract class Bar {
String get x;
bool get multiplyByX;
}
class Baz {}
// This class shows the pattern used by APIs such as jQuery that add methods
// to Arrays.
abstract class Selection implements List {
num doubleLength();
}
Foo get foo => context['foo'];
Foob get foob => context['foob'];
Bar get bar => context['bar'];
Selection get selection => context['selection'];
main() {
// Call experimental API to register Dart interfaces implemented by
// JavaScript classes.
registerJsInterfaces([Foo, Foob, Bar, Selection]);
_injectJs();
useHtmlConfiguration();
group('property', () {
test('get', () {
expect(foo.x, equals(3));
expect(foob.x, equals(8));
expect(foob.y, equals("why"));
// Exists in JS but not in API.
expect(() => foo.z, throws);
expect(bar.multiplyByX, isTrue);
});
test('set', () {
foo.x = 42;
expect(foo.x, equals(42));
// Property tagged as read only in typed API.
expect(() => foob.y = "bla", throws);
expect(() => foo.unknownName = 42, throws);
});
});
group('method', () {
test('call', () {
foo.x = 100;
expect(foo.multiplyByX(4), equals(400));
foob.x = 10;
expect(foob.multiplyByX(4), equals(40));
});
test('tearoff', () {
foo.x = 10;
// TODO(jacobr): should we automatically bind "this" for tearoffs of JS
// objects?
JsFunction multiplyBy2 = foo.multiplyBy2;
expect(multiplyBy2(5), equals(10));
});
});
group('type check', () {
test('js interfaces', () {
expect(foo is JsObject, isTrue);
// Cross-casts are allowed.
expect(foo is Bar, isTrue);
expect(selection is JsArray, isTrue);
// We do know at runtime whether something is a JsArray or not.
expect(foo is JsArray, isFalse);
});
test('dart interfaces', () {
expect(foo is Function, isFalse);
expect(selection is List, isTrue);
});
});
group("registration", () {
test('repeated fails', () {
// The experimental registerJsInterfaces API has already been called so
// it cannot be called a second time.
expect(() => registerJsInterfaces([Baz]), throws);
});
});
}

View file

@ -146,9 +146,9 @@ final htmlBlinkMap = {
'_DOMWindowCrossFrame': () => _DOMWindowCrossFrame,
// FIXME: Move these to better locations.
'DateTime': () => DateTime,
'JsObject': () => js.JsObjectImpl,
'JsFunction': () => js.JsFunctionImpl,
'JsArray': () => js.JsArrayImpl,
'JsObject': () => js.JsObject,
'JsFunction': () => js.JsFunction,
'JsArray': () => js.JsArray,
$!TYPE_MAP
};