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

Unified Diff: lib/runtime/dart_runtime.js

Issue 961513002: Flesh out dynamic invocation code (Closed) Base URL: https://github.com/dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 10 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
« no previous file with comments | « no previous file | lib/src/codegen/js_codegen.dart » ('j') | lib/src/codegen/js_codegen.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/runtime/dart_runtime.js
diff --git a/lib/runtime/dart_runtime.js b/lib/runtime/dart_runtime.js
index 58641ea5be693f7d87622a5a3823c8770b3b197a..2aa1253cc3130a2d7ba658452ae027511cf984ad 100644
--- a/lib/runtime/dart_runtime.js
+++ b/lib/runtime/dart_runtime.js
@@ -10,6 +10,7 @@ var dart;
// Adapted from Angular.js
var FN_ARGS = /^function\s*[^\(]*\(\s*([^\)]*)\)/m;
+ var METHOD_ARGS = /^[^\(]*\(\s*([^\)]*)\)/m
var FN_ARG_SPLIT = /,/;
var FN_ARG = /^\s*(_?)(\S+?)\1\s*$/;
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
@@ -19,6 +20,11 @@ var dart;
var args=[];
fnText = fn.toString().replace(STRIP_COMMENTS, '');
argDecl = fnText.match(FN_ARGS);
+ if (!argDecl) {
+ // TODO(vsm): This appears to match current V8. I don't see this spec'ed.
+ // ES 6 methods have a different syntax.
Jennifer Messerly 2015/02/26 20:53:16 fyi, this is spec'd in Function.prototype.toString
vsm 2015/03/03 21:06:12 Thanks! I made pointer to the spec and filed an i
+ argDecl = fnText.match(METHOD_ARGS);
+ }
var r = argDecl[1].split(FN_ARG_SPLIT);
for(var a in r){
@@ -30,6 +36,12 @@ var dart;
return args;
}
+ function isDartFunction(fn) {
+ // TODO(vsm): How do we differentiate? For now, just reject native methods.
Jennifer Messerly 2015/02/26 20:53:16 Assuming we still have the target object, "obj ins
vsm 2015/03/03 21:06:12 Thanks, added a pointer to the issue.
+ // TODO(vsm): Make this more robust - e.g., a regexp.
+ return fn.toString().indexOf('[native code]') == -1;
+ }
+
function dload(obj, field) {
if (!(field in obj)) {
throw new core.NoSuchMethodError(obj, field);
@@ -39,12 +51,72 @@ var dart;
dart.dload = dload;
// TODO(jmesserly): this should call noSuchMethod, not throw.
+ function dput(obj, field, value) {
+ if (!(field in obj)) {
+ throw new core.NoSuchMethodError(obj, field);
+ }
+ // TODO(vsm): Type check required.
+ obj[field] = value;
+ }
+ dart.dput = dput;
+
function throwNoSuchMethod(obj, name, args, opt_func) {
if (obj === void 0) obj = opt_func;
throw new core.NoSuchMethodError(obj, name, args);
}
+ function _numberInvoke(f, obj, args, name) {
+ var checkArg = function () {
+ var arg = args[0];
+ if (typeof(arg) != 'number') {
+ throw 'invalid operation';
+ }
+ return arg;
+ }
+
+ // TODO(vsm): Implement all Dart int and double instance methods.
+ switch (name) {
+ case '+':
vsm 2015/02/26 00:34:00 Not sure which way we ought to go with these. Are
Jennifer Messerly 2015/02/26 20:53:16 probably not anymore, given the generalize type pr
+ return (args.length == 0) ? obj : obj + checkArg();
+ case '-':
+ return (args.length == 0) ? - obj : obj - checkArg();
+ case '*':
+ return obj * checkArg();
+ case '/':
+ return obj / checkArg();
+ case '~/':
+ return (obj / checkArg()).truncate();
+ case '<':
+ return obj < checkArg();
+ }
+ throw new core.UnimplementedError();
+ }
+
+ function _booleanInvoke(f, obj, args, name) {
+ // TODO(vsm): Implement all Dart bool instance methods.
+ throw new core.UnimplementedError();
+ }
+
+ function _stringInvoke(f, obj, args, name) {
+ // TODO(vsm): Implement all Dart string instance methods.
+ throw new core.UnimplementedError();
+ }
+
function checkAndCall(f, obj, args, name) {
+ if (obj != null && !(obj instanceof dart.Object)) {
+ // Check for primitive types.
Jennifer Messerly 2015/02/26 20:53:16 to build on previous comments ... Ideally, this wo
+ if (typeof(obj) == 'number') {
+ return _numberInvoke(f, obj, args, name);
+ } else if (typeof(obj) == 'boolean') {
+ return _booleanInvoke(f, obj, args, name);
+ } else if (typeof(obj) == 'string') {
+ return _stringInvoke(f, obj, args, name);
+ } else {
+ // obj is a JavaScript or native object. Just call.
+ return f.apply(obj, args);
+ }
+ }
+
if (!(f instanceof Function)) {
// Grab the `call` method if it's not a function.
if (f !== null) f = f.call;
@@ -52,6 +124,12 @@ var dart;
throwNoSuchMethod(obj, method, args);
}
}
+
+ if (!isDartFunction(f)) {
Jennifer Messerly 2015/02/26 20:53:16 we could also just see if it has our type signatur
vsm 2015/03/03 21:06:12 Right, thinking isDartFunction would do something
+ // A JS or native function. Just call.
+ return f.apply(obj, args);
+ }
+
var formals = formalParameterList(f);
// TODO(vsm): Type check args! We need to encode sufficient type info on f.
if (formals.length < args.length) {
« no previous file with comments | « no previous file | lib/src/codegen/js_codegen.dart » ('j') | lib/src/codegen/js_codegen.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698