UNPKG

13.4 kBTypeScriptView Raw
1import { Dict, Nullable, Stack, SimpleElement, InsertPosition, Namespace, SimpleNode, Maybe, Present, PresentArray, SimpleDocument, ErrHandle, HandleResult, OkHandle, Template, TemplateOk } from "@glimmer/interfaces";
2declare const EMPTY_ARRAY: readonly unknown[];
3declare function emptyArray<T>(): T[];
4declare const EMPTY_STRING_ARRAY: string[];
5declare const EMPTY_NUMBER_ARRAY: number[];
6/**
7 * This function returns `true` if the input array is the special empty array sentinel,
8 * which is sometimes used for optimizations.
9 */
10declare function isEmptyArray(input: unknown[] | readonly unknown[]): boolean;
11declare function reverse<T>(input: T[]): IterableIterator<T>;
12declare function enumerate<T>(input: Iterable<T>): IterableIterator<[
13 number,
14 T
15]>;
16// let alreadyWarned = false;
17declare function debugAssert(test: any, msg: string): asserts test;
18declare function deprecate(desc: string): void;
19declare function dict<T = unknown>(): Dict<T>;
20declare function isDict<T>(u: T): u is Dict & T;
21declare function isObject<T>(u: T): u is object & T;
22declare class StackImpl<T> implements Stack<T> {
23 private stack;
24 current: Nullable<T>;
25 constructor(values?: T[]);
26 get size(): number;
27 push(item: T): void;
28 pop(): Nullable<T>;
29 nth(from: number): Nullable<T>;
30 isEmpty(): boolean;
31 toArray(): T[];
32}
33declare let beginTestSteps: (() => void) | undefined;
34declare let endTestSteps: (() => void) | undefined;
35declare let verifySteps: ((type: string, steps: unknown[] | ((steps: unknown[]) => void), message?: string) => void) | undefined;
36declare let logStep: ((type: string, steps: unknown) => void) | undefined;
37declare let debugToString: undefined | ((value: unknown) => string);
38declare function clearElement(parent: SimpleElement): void;
39declare const RAW_NODE = -1;
40declare const ELEMENT_NODE = 1;
41declare const TEXT_NODE = 3;
42declare const COMMENT_NODE = 8;
43declare const DOCUMENT_NODE = 9;
44declare const DOCUMENT_TYPE_NODE = 10;
45declare const DOCUMENT_FRAGMENT_NODE = 11;
46declare const NS_HTML: Namespace.HTML;
47declare const NS_MATHML: Namespace.MathML;
48declare const NS_SVG: Namespace.SVG;
49declare const NS_XLINK: Namespace.XLink;
50declare const NS_XML: Namespace.XML;
51declare const NS_XMLNS: Namespace.XMLNS;
52declare const INSERT_BEFORE_BEGIN: InsertPosition.beforebegin;
53declare const INSERT_AFTER_BEGIN: InsertPosition.afterbegin;
54declare const INSERT_BEFORE_END: InsertPosition.beforeend;
55declare const INSERT_AFTER_END: InsertPosition.afterend;
56/*
57Encoding notes
58
59We use 30 bit integers for encoding, so that we don't ever encode a non-SMI
60integer to push on the stack.
61
62Handles are >= 0
63Immediates are < 0
64
65True, False, Undefined and Null are pushed as handles into the symbol table,
66with well known handles (0, 1, 2, 3)
67
68The negative space is divided into positives and negatives. Positives are
69higher numbers (-1, -2, -3, etc), negatives are lower.
70
71We only encode immediates for two reasons:
72
731. To transfer over the wire, so they're smaller in general
742. When pushing values onto the stack from the low level/inner VM, which may
75be converted into WASM one day.
76
77This allows the low-level VM to always use SMIs, and to minimize using JS
78values via handles for things like the stack pointer and frame pointer.
79Externally, most code pushes values as JS values, except when being pulled
80from the append byte code where it was already encoded.
81
82Logically, this is because the low level VM doesn't really care about these
83higher level values. For instance, the result of a userland helper may be a
84number, or a boolean, or undefined/null, but it's extra work to figure that
85out and push it correctly, vs. just pushing the value as a JS value with a
86handle.
87
88Note: The details could change here in the future, this is just the current
89strategy.
90*/
91declare enum ImmediateConstants {
92 MAX_SMI = 1073741823,
93 MIN_SMI = -1073741824,
94 SIGN_BIT = -536870913,
95 MAX_INT = 536870911,
96 MIN_INT = -536870912,
97 FALSE_HANDLE = 0,
98 TRUE_HANDLE = 1,
99 NULL_HANDLE = 2,
100 UNDEFINED_HANDLE = 3,
101 ENCODED_FALSE_HANDLE = 0,
102 ENCODED_TRUE_HANDLE = 1,
103 ENCODED_NULL_HANDLE = 2,
104 ENCODED_UNDEFINED_HANDLE = 3
105}
106declare function isHandle(value: number): boolean;
107declare function isNonPrimitiveHandle(value: number): boolean;
108declare function constants(...values: unknown[]): unknown[];
109declare function isSmallInt(value: number): boolean;
110declare function encodeNegative(num: number): number;
111declare function decodeNegative(num: number): number;
112declare function encodePositive(num: number): number;
113declare function decodePositive(num: number): number;
114declare function encodeHandle(num: number): number;
115declare function decodeHandle(num: number): number;
116declare function encodeImmediate(num: number): number;
117declare function decodeImmediate(num: number): number;
118/**
119 Strongly hint runtimes to intern the provided string.
120
121 When do I need to use this function?
122
123 For the most part, never. Pre-mature optimization is bad, and often the
124 runtime does exactly what you need it to, and more often the trade-off isn't
125 worth it.
126
127 Why?
128
129 Runtimes store strings in at least 2 different representations:
130 Ropes and Symbols (interned strings). The Rope provides a memory efficient
131 data-structure for strings created from concatenation or some other string
132 manipulation like splitting.
133
134 Unfortunately checking equality of different ropes can be quite costly as
135 runtimes must resort to clever string comparison algorithms. These
136 algorithms typically cost in proportion to the length of the string.
137 Luckily, this is where the Symbols (interned strings) shine. As Symbols are
138 unique by their string content, equality checks can be done by pointer
139 comparison.
140
141 How do I know if my string is a rope or symbol?
142
143 Typically (warning general sweeping statement, but truthy in runtimes at
144 present) static strings created as part of the JS source are interned.
145 Strings often used for comparisons can be interned at runtime if some
146 criteria are met. One of these criteria can be the size of the entire rope.
147 For example, in chrome 38 a rope longer then 12 characters will not
148 intern, nor will segments of that rope.
149
150 Some numbers: http://jsperf.com/eval-vs-keys/8
151
152 Known Trick™
153
154 @private
155 @return {String} interned version of the provided string
156 */
157declare function intern(str: string): string;
158declare const SERIALIZATION_FIRST_NODE_STRING = "%+b:0%";
159declare function isSerializationFirstNode(node: SimpleNode): boolean;
160declare let assign: {
161 <T extends {}, U>(target: T, source: U): T & U;
162 <T_1 extends {}, U_1, V>(target: T_1, source1: U_1, source2: V): T_1 & U_1 & V;
163 <T_2 extends {}, U_2, V_1, W>(target: T_2, source1: U_2, source2: V_1, source3: W): T_2 & U_2 & V_1 & W;
164 (target: object, ...sources: any[]): any;
165};
166declare function values<T>(obj: {
167 [s: string]: T;
168}): T[];
169type ObjectEntry<D extends object> = {
170 [P in keyof D]: [
171 P,
172 D[P]
173 ];
174}[keyof D];
175declare function entries<D extends object>(dict: D): ObjectEntry<D>[];
176type Factory<T> = new (...args: unknown[]) => T;
177declare function keys<T extends object>(obj: T): Array<keyof T>;
178declare function unwrap<T>(val: Maybe<T>): T;
179declare function expect<T>(val: T, message: string): Present<T>;
180declare function unreachable(message?: string): Error;
181declare function exhausted(value: never): never;
182type Lit = string | number | boolean | undefined | null | void | {};
183declare const tuple: <T extends Lit[]>(...args: T) => T;
184declare function isPresent<T>(value: T): value is Present<T>;
185declare function assertPresent<T extends string>(value: T): asserts value is Present<T>;
186declare function assertPresent<T>(value: T, message: string): asserts value is Present<T>;
187declare function isPresentArray<T>(list: readonly T[]): list is PresentArray<T>;
188declare function ifPresent<T, U, V>(list: T[], ifPresent: (input: PresentArray<T>) => U, otherwise: () => V): U | V;
189declare function arrayToOption<T>(list: T[]): Nullable<PresentArray<T>>;
190declare function assertPresentArray<T>(list: T[], message?: string): asserts list is PresentArray<T>;
191declare function asPresentArray<T>(list: T[], message?: string): PresentArray<T>;
192declare function getLast<T>(list: PresentArray<T>): T;
193declare function getLast<T>(list: T[]): T | undefined;
194declare function getFirst<T>(list: PresentArray<T>): T;
195declare function getFirst<T>(list: T[]): T | undefined;
196declare function mapPresentArray<T, U>(list: PresentArray<T>, mapper: (input: T) => U): PresentArray<U>;
197declare function mapPresentArray<T, U>(list: PresentArray<T> | null, mapper: (input: T) => U): PresentArray<U> | null;
198interface GenericElementTags {
199 HTML: HTMLElement;
200 SVG: SVGElement;
201 ELEMENT: HTMLElement | SVGElement;
202}
203interface GenericNodeTags {
204 NODE: Node;
205}
206type GenericNodeTag = keyof GenericNodeTags;
207interface BrowserElementTags extends HTMLElementTagNameMap, GenericElementTags {
208}
209type BrowserElementTag = keyof BrowserElementTags;
210interface BrowserTags extends BrowserElementTags, GenericNodeTags {
211}
212type BrowserTag = keyof BrowserTags;
213type NodeCheck<N extends Node> = (node: Node) => node is N;
214type SugaryNodeCheck<K extends BrowserTag = BrowserTag> = NodeCheck<BrowserTags[K]> | K | K[];
215type NodeForSugaryCheck<S extends SugaryNodeCheck<BrowserTag>> = S extends NodeCheck<infer N> ? N : S extends keyof BrowserTags ? BrowserTags[S] : S extends (keyof BrowserTags)[] ? BrowserTags[S[number]] : never;
216type BrowserNode = Element | Document | DocumentFragment | Text | Comment | Node;
217declare function castToSimple(doc: Document | SimpleDocument): SimpleDocument;
218declare function castToSimple(elem: Element | SimpleElement): SimpleElement;
219declare function castToSimple(node: Node | SimpleNode): SimpleNode;
220// If passed a document, verify we're in the browser and return it as a Document
221declare function castToBrowser(doc: Document | SimpleDocument): Document;
222// If we don't know what this is, but the check requires it to be an element,
223// the cast will mandate that it's a browser element
224declare function castToBrowser<S extends SugaryNodeCheck<BrowserElementTag>>(node: BrowserNode | SimpleNode, check: S): NodeForSugaryCheck<S>;
225// Finally, if it's a more generic check, the cast will mandate that it's a
226// browser node and return a BrowserNodeUtils corresponding to the check
227declare function castToBrowser<S extends SugaryNodeCheck<GenericNodeTag>>(element: BrowserNode | SimpleNode, check: S): NodeForSugaryCheck<S>;
228declare function castToBrowser<K extends keyof HTMLElementTagNameMap>(element: SimpleElement | Element, check: K): HTMLElementTagNameMap[K];
229declare function isSimpleElement(node: Maybe<SimpleNode | Node>): node is SimpleElement;
230declare function isElement(node: Maybe<Node | SimpleNode>): node is Element;
231declare function checkBrowserNode<S extends SugaryNodeCheck>(node: Node | SimpleNode | null, check: S): NodeForSugaryCheck<S>;
232declare function strip(strings: TemplateStringsArray, ...args: unknown[]): string;
233declare function unwrapHandle(handle: HandleResult): number;
234declare function unwrapTemplate(template: Template): TemplateOk;
235declare function extractHandle(handle: HandleResult): number;
236declare function isOkHandle(handle: HandleResult): handle is OkHandle;
237declare function isErrHandle(handle: HandleResult): handle is ErrHandle;
238declare function buildUntouchableThis(source: string): null | object;
239type FIXME<T, S extends string> = (T & S) | T;
240/**
241 * This constant exists to make it easier to differentiate normal logs from
242 * errant console.logs. LOCAL_LOGGER should only be used inside a
243 * LOCAL_SHOULD_LOG check.
244 *
245 * It does not alleviate the need to check LOCAL_SHOULD_LOG, which is used
246 * for stripping.
247 */
248declare const LOCAL_LOGGER: Console;
249/**
250 * This constant exists to make it easier to differentiate normal logs from
251 * errant console.logs. LOGGER can be used outside of LOCAL_SHOULD_LOG checks,
252 * and is meant to be used in the rare situation where a console.* call is
253 * actually appropriate.
254 */
255declare const LOGGER: Console;
256declare function assertNever(value: never, desc?: string): never;
257export { EMPTY_ARRAY, emptyArray, EMPTY_STRING_ARRAY, EMPTY_NUMBER_ARRAY, isEmptyArray, reverse, enumerate, debugAssert as assert, deprecate, dict, isDict, isObject, StackImpl as Stack, beginTestSteps, endTestSteps, logStep, verifySteps, debugToString, clearElement, RAW_NODE, ELEMENT_NODE, TEXT_NODE, COMMENT_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, DOCUMENT_FRAGMENT_NODE, NS_HTML, NS_MATHML, NS_SVG, NS_XLINK, NS_XML, NS_XMLNS, INSERT_BEFORE_BEGIN, INSERT_AFTER_BEGIN, INSERT_BEFORE_END, INSERT_AFTER_END, ImmediateConstants, isHandle, isNonPrimitiveHandle, constants, isSmallInt, encodeNegative, decodeNegative, encodePositive, decodePositive, encodeHandle, decodeHandle, encodeImmediate, decodeImmediate, intern, isSerializationFirstNode, SERIALIZATION_FIRST_NODE_STRING, assign, entries, values, Factory, keys, unwrap, expect, unreachable, exhausted, Lit, tuple, isPresent, assertPresent, isPresentArray, ifPresent, arrayToOption, assertPresentArray, asPresentArray, getLast, getFirst, mapPresentArray, castToBrowser, castToSimple, checkBrowserNode as checkNode, isElement, isSimpleElement, strip, unwrapHandle, unwrapTemplate, extractHandle, isOkHandle, isErrHandle, buildUntouchableThis, FIXME, LOCAL_LOGGER, LOGGER, assertNever };
258//# sourceMappingURL=index.d.ts.map
\No newline at end of file