Chromium Code Reviews| Index: lib/runtime/dart_runtime.js |
| diff --git a/lib/runtime/dart_runtime.js b/lib/runtime/dart_runtime.js |
| index 01791fdc06a5c9cc1c9e6bc0251e7edd0d6cdf44..de5675bf480957a0914f9fa8d8469a808c4046e5 100644 |
| --- a/lib/runtime/dart_runtime.js |
| +++ b/lib/runtime/dart_runtime.js |
| @@ -96,7 +96,7 @@ var dart, _js_helper; |
| function dindex(obj, index) { |
| // TODO(jmesserly): remove this special case once Array extensions are |
| // hooked up. |
| - if (obj instanceof Array && getRuntimeType(index) == core.int) { |
| + if (obj instanceof Array && realRuntimeType(index) == core.int) { |
| return obj[index]; |
| } |
| return checkAndCall(obj.get, obj, [index], '[]'); |
| @@ -122,7 +122,7 @@ var dart, _js_helper; |
| function cast(obj, type) { |
| // TODO(vsm): handle non-nullable types |
| if (obj == null) return obj; |
| - let actual = getRuntimeType(obj); |
| + let actual = realRuntimeType(obj); |
| if (isSubtype(actual, type)) return obj; |
| throw new _js_helper.CastErrorImplementation(actual, type); |
| } |
| @@ -130,15 +130,9 @@ var dart, _js_helper; |
| // TODO(vsm): How should we encode the runtime type? |
| - let runtimeType = Symbol('runtimeType'); |
| + let _runtimeType = Symbol('_runtimeType'); |
| - /** |
| - * Returns the runtime type of obj. This is the same as `obj.runtimeType` |
| - * but will not call an overridden getter. |
| - * |
| - * Currently this will return null for non-Dart objects. |
| - */ |
| - function getRuntimeType(obj) { |
| + function checkPrimitiveType(obj) { |
| switch (typeof obj) { |
| case "undefined": |
| return core.Null; |
| @@ -154,9 +148,21 @@ var dart, _js_helper; |
| // Undefined is handled above. For historical reasons, |
| // typeof null == "object" in JS. |
| if (obj === null) return core.Null; |
| + return null; |
| + } |
| + |
| + /** |
| + * Returns the runtime type of obj. This is the same as `obj.realRuntimeType` |
| + * but will not call an overridden getter. |
| + * |
| + * Currently this will return null for non-Dart objects. |
| + */ |
| + function realRuntimeType(obj) { |
| + var result = checkPrimitiveType(obj); |
| + if (result !== null) return result; |
| // TODO(vsm): Should we treat Dart and JS objects differently here? |
| // E.g., we can check if obj instanceof core.Object to differentiate. |
| - var result = obj[runtimeType]; |
| + result = obj[_runtimeType]; |
| if (result) return result; |
| result = obj.constructor; |
| if (result == Function) { |
| @@ -164,10 +170,10 @@ var dart, _js_helper; |
| } |
| return result; |
| } |
| - dart.getRuntimeType = getRuntimeType; |
| + dart.realRuntimeType = realRuntimeType; |
| function instanceOf(obj, type) { |
| - return isSubtype(getRuntimeType(obj), type); |
| + return isSubtype(realRuntimeType(obj), type); |
| } |
| dart.is = instanceOf; |
| @@ -832,7 +838,7 @@ var dart, _js_helper; |
| * - nested values of the object are themselves already canonicalized. |
| */ |
| function constant(obj) { |
| - let objectKey = [getRuntimeType(obj)]; |
| + let objectKey = [realRuntimeType(obj)]; |
| // There's no guarantee in JS that names/symbols are returned in the same |
| // order. We could probably get the same order if we're judicious about |
| // initializing them, but easier to not depend on that. |
| @@ -847,10 +853,61 @@ var dart, _js_helper; |
| /** Sets the runtime type of `obj` to be `type` */ |
| function setType(obj, type) { |
| - obj[runtimeType] = type; |
| + obj[_runtimeType] = type; |
| } |
| dart.setType = setType; |
| + // The following are helpers for Object methods when the receiver |
| + // may be null or primitive. These should only be generated by |
| + // the compiler. |
| + function hashCode(obj) { |
| + if (obj == null) { |
| + return 0; |
| + } |
| + // TODO(vsm): What should we do for primitives and non-Dart objects? |
| + switch (typeof obj) { |
| + case "number": |
| + case "boolean": |
| + return obj & 0x1FFFFFFF; |
| + case "string": |
| + // TODO(vsm): Call the JSString hashCode? |
| + return obj.length; |
| + } |
| + return obj.hashCode; |
| + } |
| + dart.hashCode = hashCode; |
| + |
| + function runtimeType(obj) { |
| + var result = checkPrimitiveType(obj); |
| + if (result !== null) return result; |
| + return obj.runtimeType; |
| + } |
| + dart.runtimeType = runtimeType; |
| + |
| + function toString(obj) { |
| + if (obj == null) { |
| + return "null"; |
| + } |
| + return obj.toString(); |
| + } |
| + dart.toString = toString; |
| + |
| + function noSuchMethod(obj, invocation) { |
| + if (obj == null) { |
| + throw new core.NoSuchMethodError(obj, invocation.memberName, |
|
Jennifer Messerly
2015/04/23 21:09:30
could this be like:
return core.Object.noSuch
vsm
2015/04/23 21:23:29
This feels mildly dangerous ... depending on how O
|
| + invocation.positionalArguments, invocation.namedArguments); |
| + } |
| + switch (typeof obj) { |
| + case "number": |
| + case "boolean": |
| + case "string": |
| + throw new core.NoSuchMethodError(obj, invocation.memberName, |
|
Jennifer Messerly
2015/04/23 21:09:30
this too
|
| + invocation.positionalArguments, invocation.namedArguments); |
| + } |
| + return obj.noSuchMethod(invocation); |
| + } |
| + dart.noSuchMethod = noSuchMethod; |
| + |
| // TODO(jmesserly): right now this is a sentinel. It should be a type object |
| // of some sort, assuming we keep around `dynamic` at runtime. |
| dart.dynamic = { toString() { return 'dynamic'; } }; |