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

Unified Diff: sdk/lib/_internal/js_runtime/lib/interceptors.dart

Issue 1318043005: Support user generated custom native JS classes. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: ptal Created 5 years, 3 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:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/_internal/js_runtime/lib/interceptors.dart
diff --git a/sdk/lib/_internal/js_runtime/lib/interceptors.dart b/sdk/lib/_internal/js_runtime/lib/interceptors.dart
index 0247af4ab45094c9af83d02c647d4dfd7e6c0c48..620ff44a0ad53594135832f59c7bbdf899bad6db 100644
--- a/sdk/lib/_internal/js_runtime/lib/interceptors.dart
+++ b/sdk/lib/_internal/js_runtime/lib/interceptors.dart
@@ -22,6 +22,7 @@ import 'dart:_js_helper' show allMatchesInStringUnchecked,
checkString,
defineProperty,
diagnoseIndexError,
+ getIsolateAffinityTag,
getRuntimeType,
initNativeDispatch,
initNativeDispatchFlag,
@@ -40,6 +41,7 @@ import 'dart:_js_helper' show allMatchesInStringUnchecked,
StringMatch,
firstMatchAfter,
NoInline;
+
import 'dart:_foreign_helper' show
JS,
JS_EFFECT,
@@ -52,6 +54,9 @@ part 'js_array.dart';
part 'js_number.dart';
part 'js_string.dart';
+final String DART_CLOSURE_PROPERTY_NAME =
+ getIsolateAffinityTag(r'_$dart_dartClosure');
+
String _symbolToString(Symbol symbol) => _symbol_dev.Symbol.getName(symbol);
_symbolMapToStringMap(Map<Symbol, dynamic> map) {
@@ -170,6 +175,9 @@ getNativeInterceptor(object) {
// are 'plain' Objects. This test could be simplified and the dispatch path
// be faster if Object.prototype was pre-patched with a non-leaf dispatch
// record.
+ if (JS('bool', 'typeof # == "function"', object)) {
+ return JS_INTERCEPTOR_CONSTANT(JavaScriptFunction);
+ }
var proto = JS('', 'Object.getPrototypeOf(#)', object);
if (JS('bool', '# == null || # === Object.prototype', proto, proto)) {
return JS_INTERCEPTOR_CONSTANT(PlainJavaScriptObject);
@@ -394,13 +402,24 @@ abstract class JSObject {
* Interceptor base class for JavaScript objects not recognized as some more
* specific native type.
*/
-abstract class JavaScriptObject extends Interceptor implements JSObject {
+class JavaScriptObject extends Interceptor implements JSObject {
const JavaScriptObject();
// It would be impolite to stash a property on the object.
int get hashCode => 0;
Type get runtimeType => JSObject;
+
+ /**
+ * Returns the result of the JavaScript objects `toString` method.
+ */
+ String toString() {
+ try {
+ return JS('String', 'String(#)', this);
+ } catch(e) {
Siggi Cherem (dart-lang) 2015/09/18 20:34:10 under what circumstances do you expect an exceptio
Jacob 2015/10/01 00:47:33 Just matching the toString() method that was in da
+ return super.toString();
+ }
+ }
}
@@ -424,3 +443,18 @@ class UnknownJavaScriptObject extends JavaScriptObject {
String toString() => JS('String', 'String(#)', this);
}
+
+/**
+ * Interceptor for JavaScript function objects and Dart functions that have
+ * been converted to JavaScript functions.
+ * These interceptor methods are not always used as the JavaScript function
+ * object has also been mangled to support Dart function calling conventions.
+ */
+class JavaScriptFunction extends JavaScriptObject implements Function {
+ const JavaScriptFunction();
+
+ String toString() {
+ var dartClosure = JS('', '#.#', this, DART_CLOSURE_PROPERTY_NAME);
+ return dartClosure == null ? super.toString() : dartClosure.toString();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698