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

Side by Side Diff: lib/runtime/_rtti.js

Issue 1182653002: Refactor runtime into libraries, better type reps (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Address comments 2 Created 5 years, 6 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 unified diff | Download patch
« no previous file with comments | « lib/runtime/_operations.js ('k') | lib/runtime/_types.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 /* This library defines the association between runtime objects and
6 * runtime types.
7 */
8
9 dart_library.library('dart_runtime/_rtti', null, /* Imports */[
10 ], /* Lazy Imports */[
11 'dart/core',
12 'dart_runtime/_types'
13 ], function(exports, core, types) {
14 'use strict';
15
16 const defineLazyProperty = dart_utils.defineLazyProperty;
17
18 const defineProperty = Object.defineProperty;
19
20 const slice = [].slice;
21
22 /**
23 *Tag a closure with a type, using one of three forms:
24 * dart.fn(cls) marks cls has having no optional or named
25 * parameters, with all argument and return types as dynamic
26 * dart.fn(cls, func) marks cls with the lazily computed
27 * runtime type as computed by func()
28 * dart.fn(cls, rType, argsT, extras) marks cls as having the
29 * runtime type dart.functionType(rType, argsT, extras)
30 */
31 function fn(closure/* ...args*/) {
32 // Closure and a lazy type constructor
33 if (arguments.length == 2) {
34 defineLazyProperty(closure, _runtimeType, {get : arguments[1]});
35 return closure;
36 }
37 let t;
38 if (arguments.length == 1) {
39 // No type arguments, it's all dynamic
40 let len = closure.length;
41 let build = () => {
42 let args = Array.apply(null, new Array(len)).map(() => core.Object);
43 return types.functionType(core.Object, args);
44 };
45 // We could be called before Object is defined.
46 if (core.Object === void 0) return fn(closure, build);
47 t = build();
48 } else {
49 // We're passed the piecewise components of the function type,
50 // construct it.
51 let args = slice.call(arguments, 1);
52 t = types.functionType.apply(null, args);
53 }
54 tag(closure, t);
55 return closure;
56 }
57 exports.fn = fn;
58
59 // TODO(vsm): How should we encode the runtime type?
60 const _runtimeType = Symbol('_runtimeType');
61
62 function checkPrimitiveType(obj) {
63 switch (typeof obj) {
64 case "undefined":
65 return core.Null;
66 case "number":
67 return Math.floor(obj) == obj ? core.int : core.double;
68 case "boolean":
69 return core.bool;
70 case "string":
71 return core.String;
72 case "symbol":
73 return Symbol;
74 }
75 // Undefined is handled above. For historical reasons,
76 // typeof null == "object" in JS.
77 if (obj === null) return core.Null;
78 return null;
79 }
80
81 function runtimeType(obj) {
82 let result = checkPrimitiveType(obj);
83 if (result !== null) return result;
84 return obj.runtimeType;
85 }
86 exports.runtimeType = runtimeType;
87
88 function getFunctionType(obj) {
89 // TODO(vsm): Encode this properly on the function for Dart-generated code.
90 let args = Array.apply(null, new Array(obj.length)).map(() => core.Object);
91 return types.functionType(types.bottom, args);
92 }
93
94 /**
95 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType`
96 * but will not call an overridden getter.
97 *
98 * Currently this will return null for non-Dart objects.
99 */
100 function realRuntimeType(obj) {
101 let result = checkPrimitiveType(obj);
102 if (result !== null) return result;
103 // TODO(vsm): Should we treat Dart and JS objects differently here?
104 // E.g., we can check if obj instanceof core.Object to differentiate.
105 result = obj[_runtimeType];
106 if (result) return result;
107 result = obj.constructor;
108 if (result == Function) {
109 return getFunctionType(obj);
110 }
111 return result;
112 }
113 exports.realRuntimeType = realRuntimeType;
114
115 function LazyTagged(infoFn) {
116 class _Tagged {
117 get [_runtimeType]() {return infoFn();}
118 }
119 return _Tagged;
120 }
121 exports.LazyTagged = LazyTagged;
122
123 function read(value) {
124 return value[_runtimeType];
125 }
126 exports.read = read;
127
128 function tag(value, info) {
129 value[_runtimeType] = info;
130 }
131 exports.tag = tag;
132
133 function tagComputed(value, compute) {
134 defineProperty(value, _runtimeType, { get: compute });
135 }
136 exports.tagComputed = tagComputed;
137
138 function tagMemoized(value, compute) {
139 let cache = null;
140 function getter() {
141 if (compute == null) return cache;
142 cache = compute();
143 compute = null;
144 return cache;
145 }
146 tagComputed(value, getter);
147 }
148 exports.tagMemoized = tagMemoized;
149 });
OLDNEW
« no previous file with comments | « lib/runtime/_operations.js ('k') | lib/runtime/_types.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698