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

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

Issue 1182653002: Refactor runtime into libraries, better type reps (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Add missing file 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
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 a set of general javascript utilities for us
Jennifer Messerly 2015/06/12 20:12:49 btw, I just noticed this library could be more tig
6 * by the Dart runtime.
7 */
8
9 var js_utils;
10 (function (js_utils) {
11 'use strict';
12
13 const defineProperty = Object.defineProperty;
14 js_utils.defineProperty = defineProperty;
Jennifer Messerly 2015/06/12 17:32:25 hmm, not sure this is worth it. We can get all of
Leaf 2015/06/12 19:52:14 I was having a hard time remembering which of the
Jennifer Messerly 2015/06/12 20:12:49 yeah I don't feel super strongly. Seems like it's
Leaf 2015/06/12 20:34:25 My problem was in remembering where to import them
15 const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
16 js_utils.getOwnPropertyDescriptor = getOwnPropertyDescriptor;
17 const getOwnPropertyNames = Object.getOwnPropertyNames;
18 js_utils.getOwnPropertyNames = getOwnPropertyNames;
19 const getOwnPropertySymbols = Object.getOwnPropertySymbols;
20 js_utils.getOwnPropertySymbols = getOwnPropertySymbols;
21 const hasOwnProperty = Object.prototype.hasOwnProperty;
22 js_utils.hasOwnProperty = hasOwnProperty;
23 const slice = [].slice;
24 js_utils.slice = [].slice;
25
26 /* This error indicates a bug in the runtime or the compiler.
Jennifer Messerly 2015/06/12 20:12:49 /** ?
Leaf 2015/06/12 20:34:25 Already fixed in last update.
27 */
28 function throwRuntimeError(message) {
29 throw Error(message);
30 }
31 js_utils.throwRuntimeError = throwRuntimeError;
32
33 function assert(condition) {
34 if (!condition) throwRuntimeError("The compiler is broken: failed assert");
35 }
36 js_utils.assert = assert;
37
38 function getOwnNamesAndSymbols(obj) {
39 return getOwnPropertyNames(obj).concat(getOwnPropertySymbols(obj));
40 }
41 js_utils.getOwnNamesAndSymbols = getOwnNamesAndSymbols;
42
43 function safeGetOwnProperty(obj, name) {
44 let desc = getOwnPropertyDescriptor(obj, name);
45 if (desc) return desc.value;
46 }
47 js_utils.safeGetOwnProperty = safeGetOwnProperty;
48
49 /**
50 * Defines a lazy property.
51 * After initial get or set, it will replace itself with a value property.
52 */
53 // TODO(jmesserly): is this the best implementation for JS engines?
54 // TODO(jmesserly): reusing descriptor objects has been shown to improve
55 // performance in other projects (e.g. webcomponents.js ShadowDOM polyfill).
56 function defineLazyProperty(to, name, desc) {
57 let init = desc.get;
58 let writable = !!desc.set;
59 function lazySetter(value) {
60 defineProperty(to, name, { value: value, writable: writable });
61 }
62 function lazyGetter() {
63 // Clear the init function to detect circular initialization.
64 let f = init;
65 if (f === null) throwRuntimeError('circular initialization for field ' + n ame);
Jennifer Messerly 2015/06/12 17:32:25 long line
Leaf 2015/06/12 19:52:14 Done.
66 init = null;
67
68 // Compute and store the value.
69 let value = f();
70 lazySetter(value);
71 return value;
72 }
73 desc.get = lazyGetter;
74 desc.configurable = true;
75 if (writable) desc.set = lazySetter;
76 defineProperty(to, name, desc);
77 }
78 js_utils.defineLazyProperty = defineLazyProperty;
79
80 function defineLazy(to, from) {
81 for (let name of getOwnNamesAndSymbols(from)) {
82 defineLazyProperty(to, name, getOwnPropertyDescriptor(from, name));
83 }
84 }
85 js_utils.defineLazy = defineLazy;
86
87 function defineMemoizedGetter(obj, name, get) {
88 let cache = null;
89 function getter() {
90 if (cache != null) return cache;
91 cache = get();
92 get = null;
93 return cache;
94 }
95 defineProperty(obj, name, {get: getter, configurable: true});
96 }
97 js_utils.defineMemoizedGetter = defineMemoizedGetter;
98
99 function copyTheseProperties(to, from, names) {
100 for (let name of names) {
101 defineProperty(to, name, getOwnPropertyDescriptor(from, name));
102 }
103 return to;
104 }
105 js_utils.copyTheseProperties = copyTheseProperties;
106
107 /**
108 * Copy properties from source to destination object.
109 * This operation is commonly called `mixin` in JS.
110 */
111 function copyProperties(to, from) {
112 return copyTheseProperties(to, from, getOwnNamesAndSymbols(from));
113 }
114 js_utils.copyProperties = copyProperties;
115
116 })(js_utils || (js_utils = {}));
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698