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

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

Issue 1530563003: Generate all runtime files from dart. (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: simplify diff in js_codegen.dart Created 4 years, 11 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
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/_rtti', null, /* Imports */[
10 ], /* Lazy Imports */[
11 'dart/_utils',
12 'dart/core',
13 'dart/_types'
14 ], function(exports, dart_utils, core, types) {
15 'use strict';
16
17 const defineLazyProperty = dart_utils.defineLazyProperty;
18
19 const defineProperty = Object.defineProperty;
20
21 /**
22 * Runtime type information. This module defines the mapping from
23 * runtime objects to their runtime type information. See the types
24 * module for the definition of how type information is represented.
25 *
26 * Runtime objects fall into four main categories:
27 *
28 * - Things represented by javascript primitives, such as
29 * null, numbers, booleans, strings, and symbols. For these
30 * we map directly from the javascript type (given by typeof)
31 * to the appropriate class type from core, which serves as their
32 * rtti.
33 *
34 * - Functions, which are represented by javascript functions.
35 * Representations of Dart functions always have a
36 * _runtimeType property attached to them with the appropriate
37 * rtti.
38 *
39 * - Objects (instances) which are represented by instances of
40 * javascript (ES6) classes. Their types are given by their
41 * classes, and the rtti is accessed by projecting out their
42 * constructor field.
43 *
44 * - Types objects, which are represented as described in the types
45 * module. Types always have a _runtimeType property attached to
46 * them with the appropriate rtti. The rtti for these is always
47 * core.Type. TODO(leafp): consider the possibility that we can
48 * reliably recognize type objects and map directly to core.Type
49 * rather than attaching this property everywhere.
50 *
51 */
52
53 /**
54 *Tag a closure with a type, using one of three forms:
55 * dart.fn(cls) marks cls has having no optional or named
56 * parameters, with all argument and return types as dynamic
57 * dart.fn(cls, func) marks cls with the lazily computed
58 * runtime type as computed by func()
59 * dart.fn(cls, rType, argsT, extras) marks cls as having the
60 * runtime type dart.functionType(rType, argsT, extras)
61 *
62 * Note that since we are producing a type for a concrete function,
63 * it is sound to use the definite arrow type.
64 */
65 function fn(closure, ...args) {
66 // Closure and a lazy type constructor
67 if (args.length == 1) {
68 defineLazyProperty(closure, _runtimeType, {get : args[0]});
69 return closure;
70 }
71 let t;
72 if (args.length == 0) {
73 // No type arguments, it's all dynamic
74 t = types.definiteFunctionType(
75 types.dynamic, Array(closure.length).fill(types.dynamic));
76 } else {
77 // We're passed the piecewise components of the function type,
78 // construct it.
79 t = types.definiteFunctionType.apply(null, args);
80 }
81 tag(closure, t);
82 return closure;
83 }
84 exports.fn = fn;
85
86 // TODO(vsm): How should we encode the runtime type?
87 const _runtimeType = Symbol('_runtimeType');
88
89 function checkPrimitiveType(obj) {
90 switch (typeof obj) {
91 case "undefined":
92 return core.Null;
93 case "number":
94 return Math.floor(obj) == obj ? core.int : core.double;
95 case "boolean":
96 return core.bool;
97 case "string":
98 return core.String;
99 case "symbol":
100 return Symbol;
101 }
102 // Undefined is handled above. For historical reasons,
103 // typeof null == "object" in JS.
104 if (obj === null) return core.Null;
105 return null;
106 }
107
108 function runtimeType(obj) {
109 let result = checkPrimitiveType(obj);
110 if (result !== null) return result;
111 return obj.runtimeType;
112 }
113 exports.runtimeType = runtimeType;
114
115 function getFunctionType(obj) {
116 // TODO(vsm): Encode this properly on the function for Dart-generated code.
117 let args = Array(obj.length).fill(types.dynamic);
118 return types.definiteFunctionType(types.bottom, args);
119 }
120
121 /**
122 * Returns the runtime type of obj. This is the same as `obj.realRuntimeType`
123 * but will not call an overridden getter.
124 *
125 * Currently this will return null for non-Dart objects.
126 */
127 function realRuntimeType(obj) {
128 let result = checkPrimitiveType(obj);
129 if (result !== null) return result;
130 // TODO(vsm): Should we treat Dart and JS objects differently here?
131 // E.g., we can check if obj instanceof core.Object to differentiate.
132 result = obj[_runtimeType];
133 if (result) return result;
134 result = obj.constructor;
135 if (result == Function) {
136 // An undecorated Function should have come from
137 // JavaScript. Treat as untyped.
138 return types.jsobject;
139 }
140 return result;
141 }
142 exports.realRuntimeType = realRuntimeType;
143
144 function LazyTagged(infoFn) {
145 class _Tagged {
146 get [_runtimeType]() {return infoFn();}
147 }
148 return _Tagged;
149 }
150 exports.LazyTagged = LazyTagged;
151
152 function read(value) {
153 return value[_runtimeType];
154 }
155 exports.read = read;
156
157 function tag(value, info) {
158 value[_runtimeType] = info;
159 }
160 exports.tag = tag;
161
162 function tagComputed(value, compute) {
163 defineProperty(value, _runtimeType, { get: compute });
164 }
165 exports.tagComputed = tagComputed;
166
167 function tagMemoized(value, compute) {
168 let cache = null;
169 function getter() {
170 if (compute == null) return cache;
171 cache = compute();
172 compute = null;
173 return cache;
174 }
175 tagComputed(value, getter);
176 }
177 exports.tagMemoized = tagMemoized;
178 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698