-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathstdlib.ts
More file actions
152 lines (132 loc) · 4.32 KB
/
stdlib.ts
File metadata and controls
152 lines (132 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { BlockMetadata, BuilderOp, EvaluationContext, HighLevelOp } from '@glimmer/interfaces';
import {
VM_APPEND_DOCUMENT_FRAGMENT_OP,
VM_APPEND_HTML_OP,
VM_APPEND_NODE_OP,
VM_APPEND_SAFE_HTML_OP,
VM_APPEND_TEXT_OP,
VM_ASSERT_SAME_OP,
VM_CONTENT_TYPE_OP,
VM_MAIN_OP,
VM_PUSH_DYNAMIC_COMPONENT_INSTANCE_OP,
VM_RESOLVE_CURRIED_COMPONENT_OP,
} from '@glimmer/constants/lib/syscall-ops';
import { VM_INVOKE_STATIC_OP } from '@glimmer/constants/lib/vm-ops';
import { $s0 } from '@glimmer/vm/lib/registers';
import { ContentType } from '@glimmer/vm/lib/content';
import type { HighLevelStatementOp, PushStatementOp } from '../../syntax/compilers';
import { encodeOp, EncoderImpl } from '../encoder';
import { StdLib } from '../stdlib';
import { InvokeBareComponent, invokePreparedComponent } from './components';
import { SwitchCases } from './conditional';
import { CallDynamic } from './vm';
export function main(op: PushStatementOp): void {
op(VM_MAIN_OP, $s0);
invokePreparedComponent(op, false, false, true);
}
/**
* Append content to the DOM. This standard function triages content and does the
* right thing based upon whether it's a string, safe string, component, fragment
* or node.
*
* @param trusting whether to interpolate a string as raw HTML (corresponds to
* triple curlies)
*/
export function StdAppend(
op: PushStatementOp,
trusting: boolean,
nonDynamicAppend: number | null
): void {
SwitchCases(
op,
() => op(VM_CONTENT_TYPE_OP),
(when) => {
when(ContentType.String, () => {
if (trusting) {
op(VM_ASSERT_SAME_OP);
op(VM_APPEND_HTML_OP);
} else {
op(VM_APPEND_TEXT_OP);
}
});
if (typeof nonDynamicAppend === 'number') {
when(ContentType.Component, () => {
op(VM_ASSERT_SAME_OP);
op(VM_RESOLVE_CURRIED_COMPONENT_OP);
op(VM_PUSH_DYNAMIC_COMPONENT_INSTANCE_OP);
InvokeBareComponent(op);
});
when(ContentType.Helper, () => {
CallDynamic(op, null, null, () => {
op(VM_INVOKE_STATIC_OP, nonDynamicAppend);
});
});
} else {
// when non-dynamic, we can no longer call the value (potentially because we've already called it)
// this prevents infinite loops. We instead coerce the value, whatever it is, into the DOM.
when(ContentType.Component, () => {
op(VM_APPEND_TEXT_OP);
});
when(ContentType.Helper, () => {
op(VM_APPEND_TEXT_OP);
});
}
when(ContentType.SafeString, () => {
op(VM_ASSERT_SAME_OP);
op(VM_APPEND_SAFE_HTML_OP);
});
when(ContentType.Fragment, () => {
op(VM_ASSERT_SAME_OP);
op(VM_APPEND_DOCUMENT_FRAGMENT_OP);
});
when(ContentType.Node, () => {
op(VM_ASSERT_SAME_OP);
op(VM_APPEND_NODE_OP);
});
}
);
}
export function compileStd(context: EvaluationContext): StdLib {
let mainHandle = build(context, (op) => main(op));
let trustingGuardedNonDynamicAppend = build(context, (op) => StdAppend(op, true, null));
let cautiousGuardedNonDynamicAppend = build(context, (op) => StdAppend(op, false, null));
let trustingGuardedDynamicAppend = build(context, (op) =>
StdAppend(op, true, trustingGuardedNonDynamicAppend)
);
let cautiousGuardedDynamicAppend = build(context, (op) =>
StdAppend(op, false, cautiousGuardedNonDynamicAppend)
);
return new StdLib(
mainHandle,
trustingGuardedDynamicAppend,
cautiousGuardedDynamicAppend,
trustingGuardedNonDynamicAppend,
cautiousGuardedNonDynamicAppend
);
}
export const STDLIB_META: BlockMetadata = {
symbols: {
locals: null,
upvars: null,
},
moduleName: 'stdlib',
// TODO: ??
scopeValues: null,
isStrictMode: true,
owner: null,
size: 0,
};
function build(evaluation: EvaluationContext, builder: (op: PushStatementOp) => void): number {
let encoder = new EncoderImpl(evaluation.program.heap, STDLIB_META);
function pushOp(...op: BuilderOp | HighLevelOp | HighLevelStatementOp) {
encodeOp(encoder, evaluation, STDLIB_META, op as BuilderOp | HighLevelOp);
}
builder(pushOp);
let result = encoder.commit(0);
if (typeof result !== 'number') {
// This shouldn't be possible
throw new Error(`Unexpected errors compiling std`);
} else {
return result;
}
}