Skip to content

Commit

Permalink
Streamline append VM implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wycats committed Oct 31, 2024
1 parent 7f4f3f0 commit 4da86b9
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 262 deletions.
2 changes: 2 additions & 0 deletions packages/@glimmer/interfaces/lib/program.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export interface RuntimeConstants {
getArray<T>(handle: number): T[];
}

export type JitConstants = CompileTimeConstants & ResolutionTimeConstants & RuntimeConstants;

export interface CompileTimeArtifacts {
heap: CompileTimeHeap;
constants: CompileTimeConstants & ResolutionTimeConstants;
Expand Down
11 changes: 3 additions & 8 deletions packages/@glimmer/interfaces/lib/runtime/runtime.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import type {
ResolutionTimeConstants,
RuntimeConstants,
RuntimeHeap,
RuntimeOp,
} from '../program.js';
import type { JitConstants, RuntimeHeap, RuntimeOp } from '../program.js';
import type { RuntimeResolver } from '../serialize.js';
import type { Environment } from './environment.js';

Expand All @@ -21,13 +16,13 @@ export interface RuntimeContext {
}

export interface RuntimeProgram {
readonly constants: RuntimeConstants & ResolutionTimeConstants;
readonly constants: JitConstants;
readonly heap: RuntimeHeap;

opcode(offset: number): RuntimeOp;
}

export interface RuntimeArtifacts {
readonly constants: RuntimeConstants & ResolutionTimeConstants;
readonly constants: JitConstants;
readonly heap: RuntimeHeap;
}
5 changes: 2 additions & 3 deletions packages/@glimmer/program/lib/program.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
CompileTimeHeap,
ResolutionTimeConstants,
RuntimeConstants,
JitConstants,
RuntimeHeap,
RuntimeProgram,
SerializedHeap,
Expand Down Expand Up @@ -209,7 +208,7 @@ export class RuntimeProgramImpl implements RuntimeProgram {
private _opcode: RuntimeOpImpl;

constructor(
public constants: RuntimeConstants & ResolutionTimeConstants,
public constants: JitConstants,
public heap: RuntimeHeap
) {
this._opcode = new RuntimeOpImpl(this.heap);
Expand Down
3 changes: 2 additions & 1 deletion packages/@glimmer/runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export { on } from './lib/modifiers/on';
export { renderComponent, renderMain, renderSync } from './lib/render';
export { DynamicScopeImpl, PartialScopeImpl } from './lib/scope';
export type { SafeString } from './lib/upsert';
export { type InternalVM, VM as LowLevelVM, UpdatingVM } from './lib/vm';
export { UpdatingVM, type VM } from './lib/vm';
export {
createCapturedArgs,
EMPTY_ARGS,
Expand All @@ -62,6 +62,7 @@ export {
RemoteLiveBlock,
UpdatableBlockImpl,
} from './lib/vm/element-builder';
export { LowLevelVM } from './lib/vm/low-level';
export {
isSerializationFirstNode,
RehydrateBuilder,
Expand Down
56 changes: 27 additions & 29 deletions packages/@glimmer/runtime/lib/compiled/opcodes/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { $t0, $t1, CurriedTypes, InternalComponentCapabilities, Op } from '@glim

import type { CurriedValue } from '../../curried-value';
import type { UpdatingVM } from '../../vm';
import type { InternalVM } from '../../vm/append';
import type { VM } from '../../vm/append';
import type { BlockArgumentsImpl } from '../../vm/arguments';

import { ConcreteBounds } from '../../bounds';
Expand All @@ -63,7 +63,6 @@ import { isCurriedType, isCurriedValue, resolveCurriedValue } from '../../currie
import { getDebugName } from '../../debug-render-tree';
import { APPEND_OPCODES } from '../../opcodes';
import createClassListRef from '../../references/class-list';
import { ARGS, CONSTANTS } from '../../symbols';
import { EMPTY_ARGS, VMArgumentsImpl } from '../../vm/arguments';
import {
CheckArguments,
Expand Down Expand Up @@ -112,7 +111,7 @@ export interface PartialComponentDefinition {
}

APPEND_OPCODES.add(Op.PushComponentDefinition, (vm, { op1: handle }) => {
let definition = vm[CONSTANTS].getValue<ComponentDefinition>(handle);
let definition = vm.constants.getValue<ComponentDefinition>(handle);
assert(!!definition, `Missing component for ${handle}`);

let { manager, capabilities } = definition;
Expand All @@ -136,7 +135,7 @@ APPEND_OPCODES.add(Op.ResolveDynamicComponent, (vm, { op1: _isStrict }) => {
valueForRef(check(stack.pop(), CheckReference)),
CheckOr(CheckString, CheckCurriedComponentDefinition)
);
let constants = vm[CONSTANTS];
let constants = vm.constants;
let owner = vm.getOwner();
let isStrict = constants.getValue<boolean>(_isStrict);

Expand Down Expand Up @@ -167,7 +166,7 @@ APPEND_OPCODES.add(Op.ResolveCurriedComponent, (vm) => {
let stack = vm.stack;
let ref = check(stack.pop(), CheckReference);
let value = valueForRef(ref);
let constants = vm[CONSTANTS];
let constants = vm.constants;

let definition: CurriedValue | ComponentDefinition | null;

Expand Down Expand Up @@ -219,21 +218,20 @@ APPEND_OPCODES.add(Op.PushDynamicComponentInstance, (vm) => {

APPEND_OPCODES.add(Op.PushArgs, (vm, { op1: _names, op2: _blockNames, op3: flags }) => {
let stack = vm.stack;
let names = vm[CONSTANTS].getArray<string>(_names);
let names = vm.constants.getArray<string>(_names);

let positionalCount = flags >> 4;
let atNames = flags & 0b1000;
let blockNames =
flags & 0b0111 ? vm[CONSTANTS].getArray<string>(_blockNames) : EMPTY_STRING_ARRAY;
let blockNames = flags & 0b0111 ? vm.constants.getArray<string>(_blockNames) : EMPTY_STRING_ARRAY;

vm[ARGS].setup(stack, names, blockNames, positionalCount, !!atNames);
stack.push(vm[ARGS]);
vm.args.setup(stack, names, blockNames, positionalCount, !!atNames);
stack.push(vm.args);
});

APPEND_OPCODES.add(Op.PushEmptyArgs, (vm) => {
let { stack } = vm;

stack.push(vm[ARGS].empty(stack));
stack.push(vm.args.empty(stack));
});

APPEND_OPCODES.add(Op.CaptureArgs, (vm) => {
Expand All @@ -257,7 +255,7 @@ APPEND_OPCODES.add(Op.PrepareArgs, (vm, { op1: _state }) => {
"If the component definition was curried, we don't yet have a manager"
);

let constants = vm[CONSTANTS];
let constants = vm.constants;

let {
definition: resolvedDefinition,
Expand Down Expand Up @@ -430,10 +428,10 @@ APPEND_OPCODES.add(Op.PutComponentOperations, (vm) => {
});

APPEND_OPCODES.add(Op.ComponentAttr, (vm, { op1: _name, op2: _trusting, op3: _namespace }) => {
let name = vm[CONSTANTS].getValue<string>(_name);
let trusting = vm[CONSTANTS].getValue<boolean>(_trusting);
let name = vm.constants.getValue<string>(_name);
let trusting = vm.constants.getValue<boolean>(_trusting);
let reference = check(vm.stack.pop(), CheckReference);
let namespace = _namespace ? vm[CONSTANTS].getValue<string>(_namespace) : null;
let namespace = _namespace ? vm.constants.getValue<string>(_namespace) : null;

check(vm.fetchValue($t0), CheckInstanceof(ComponentElementOperations)).setAttribute(
name,
Expand All @@ -444,9 +442,9 @@ APPEND_OPCODES.add(Op.ComponentAttr, (vm, { op1: _name, op2: _trusting, op3: _na
});

APPEND_OPCODES.add(Op.StaticComponentAttr, (vm, { op1: _name, op2: _value, op3: _namespace }) => {
let name = vm[CONSTANTS].getValue<string>(_name);
let value = vm[CONSTANTS].getValue<string>(_value);
let namespace = _namespace ? vm[CONSTANTS].getValue<string>(_namespace) : null;
let name = vm.constants.getValue<string>(_name);
let value = vm.constants.getValue<string>(_value);
let namespace = _namespace ? vm.constants.getValue<string>(_namespace) : null;

check(vm.fetchValue($t0), CheckInstanceof(ComponentElementOperations)).setStaticAttribute(
name,
Expand Down Expand Up @@ -491,7 +489,7 @@ export class ComponentElementOperations implements ElementOperations {
this.attributes[name] = deferred;
}

addModifier(vm: InternalVM, modifier: ModifierInstance, capturedArgs: CapturedArguments): void {
addModifier(vm: VM, modifier: ModifierInstance, capturedArgs: CapturedArguments): void {
this.modifiers.push(modifier);

if (vm.env.debugRenderTree !== undefined) {
Expand Down Expand Up @@ -533,7 +531,7 @@ export class ComponentElementOperations implements ElementOperations {
}
}

flush(vm: InternalVM): ModifierInstance[] {
flush(vm: VM): ModifierInstance[] {
let type: DeferredAttribute | undefined;
let attributes = this.attributes;

Expand Down Expand Up @@ -578,7 +576,7 @@ function allStringClasses(classes: (string | Reference<unknown>)[]): classes is
}

function setDeferredAttr(
vm: InternalVM,
vm: VM,
name: string,
value: string | Reference<unknown>,
namespace: Nullable<string>,
Expand Down Expand Up @@ -621,12 +619,12 @@ APPEND_OPCODES.add(Op.GetComponentSelf, (vm, { op1: _state, op2: _names }) => {

let args: CapturedArguments;

if (vm.stack.peek() === vm[ARGS]) {
args = vm[ARGS].capture();
if (vm.stack.peek() === vm.args) {
args = vm.args.capture();
} else {
let names = vm[CONSTANTS].getArray<string>(_names);
vm[ARGS].setup(vm.stack, names, [], 0, true);
args = vm[ARGS].capture();
let names = vm.constants.getArray<string>(_names);
vm.args.setup(vm.stack, names, [], 0, true);
args = vm.args.capture();
}

let moduleName: string;
Expand Down Expand Up @@ -729,9 +727,9 @@ APPEND_OPCODES.add(Op.GetComponentLayout, (vm, { op1: _state }) => {

if (compilable === null) {
if (managerHasCapability(manager, capabilities, InternalComponentCapabilities.wrapped)) {
compilable = unwrapTemplate(vm[CONSTANTS].defaultTemplate).asWrappedLayout();
compilable = unwrapTemplate(vm.constants.defaultTemplate).asWrappedLayout();
} else {
compilable = unwrapTemplate(vm[CONSTANTS].defaultTemplate).asLayout();
compilable = unwrapTemplate(vm.constants.defaultTemplate).asLayout();
}
}
}
Expand Down Expand Up @@ -835,7 +833,7 @@ function bindBlock(
blockName: string,
state: ComponentInstance,
blocks: BlockArgumentsImpl,
vm: InternalVM
vm: VM
) {
let symbol = state.table.symbols.indexOf(symbolName);
let block = blocks.get(blockName);
Expand Down
5 changes: 2 additions & 3 deletions packages/@glimmer/runtime/lib/compiled/opcodes/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { decodeHandle, dict, unwrap } from '@glimmer/util';
import { Op } from '@glimmer/vm';

import { APPEND_OPCODES } from '../../opcodes';
import { CONSTANTS } from '../../symbols';

export type DebugGet = (path: string) => unknown;

Expand Down Expand Up @@ -72,8 +71,8 @@ class ScopeInspector {
}

APPEND_OPCODES.add(Op.Debugger, (vm, { op1: _symbols, op2: _debugInfo }) => {
let symbols = vm[CONSTANTS].getArray<string>(_symbols);
let debugInfo = vm[CONSTANTS].getArray<number>(decodeHandle(_debugInfo));
let symbols = vm.constants.getArray<string>(_symbols);
let debugInfo = vm.constants.getArray<number>(decodeHandle(_debugInfo));
let inspector = new ScopeInspector(vm.scope(), symbols, debugInfo);
callback(valueForRef(vm.getSelf()), (path) => valueForRef(inspector.get(path)));
});
21 changes: 10 additions & 11 deletions packages/@glimmer/runtime/lib/compiled/opcodes/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,20 @@ import type { DynamicAttribute } from '../../vm/attributes/dynamic';

import { isCurriedType, resolveCurriedValue } from '../../curried-value';
import { APPEND_OPCODES } from '../../opcodes';
import { CONSTANTS } from '../../symbols';
import { createCapturedArgs } from '../../vm/arguments';
import { CheckArguments, CheckOperations, CheckReference } from './-debug-strip';
import { Assert } from './vm';

APPEND_OPCODES.add(Op.Text, (vm, { op1: text }) => {
vm.elements().appendText(vm[CONSTANTS].getValue(text));
vm.elements().appendText(vm.constants.getValue(text));
});

APPEND_OPCODES.add(Op.Comment, (vm, { op1: text }) => {
vm.elements().appendComment(vm[CONSTANTS].getValue(text));
vm.elements().appendComment(vm.constants.getValue(text));
});

APPEND_OPCODES.add(Op.OpenElement, (vm, { op1: tag }) => {
vm.elements().openElement(vm[CONSTANTS].getValue(tag));
vm.elements().openElement(vm.constants.getValue(tag));
});

APPEND_OPCODES.add(Op.OpenDynamicElement, (vm) => {
Expand Down Expand Up @@ -138,7 +137,7 @@ APPEND_OPCODES.add(Op.Modifier, (vm, { op1: handle }) => {

let owner = vm.getOwner();
let args = check(vm.stack.pop(), CheckArguments);
let definition = vm[CONSTANTS].getValue<ModifierDefinition>(handle);
let definition = vm.constants.getValue<ModifierDefinition>(handle);

let { manager } = definition;

Expand Down Expand Up @@ -356,19 +355,19 @@ export class UpdateDynamicModifierOpcode implements UpdatingOpcode {
}

APPEND_OPCODES.add(Op.StaticAttr, (vm, { op1: _name, op2: _value, op3: _namespace }) => {
let name = vm[CONSTANTS].getValue<string>(_name);
let value = vm[CONSTANTS].getValue<string>(_value);
let namespace = _namespace ? vm[CONSTANTS].getValue<string>(_namespace) : null;
let name = vm.constants.getValue<string>(_name);
let value = vm.constants.getValue<string>(_value);
let namespace = _namespace ? vm.constants.getValue<string>(_namespace) : null;

vm.elements().setStaticAttribute(name, value, namespace);
});

APPEND_OPCODES.add(Op.DynamicAttr, (vm, { op1: _name, op2: _trusting, op3: _namespace }) => {
let name = vm[CONSTANTS].getValue<string>(_name);
let trusting = vm[CONSTANTS].getValue<boolean>(_trusting);
let name = vm.constants.getValue<string>(_name);
let trusting = vm.constants.getValue<boolean>(_trusting);
let reference = check(vm.stack.pop(), CheckReference);
let value = valueForRef(reference);
let namespace = _namespace ? vm[CONSTANTS].getValue<string>(_namespace) : null;
let namespace = _namespace ? vm.constants.getValue<string>(_namespace) : null;

let attribute = vm.elements().setDynamicAttribute(name, value, trusting, namespace);

Expand Down
9 changes: 4 additions & 5 deletions packages/@glimmer/runtime/lib/compiled/opcodes/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { $v0, CurriedTypes, Op } from '@glimmer/vm';
import { isCurriedType, resolveCurriedValue } from '../../curried-value';
import { APPEND_OPCODES } from '../../opcodes';
import createCurryRef from '../../references/curry-value';
import { CONSTANTS } from '../../symbols';
import { reifyPositional } from '../../vm/arguments';
import { createConcatRef } from '../expressions/concat';
import {
Expand Down Expand Up @@ -62,7 +61,7 @@ APPEND_OPCODES.add(Op.Curry, (vm, { op1: type, op2: _isStrict }) => {

if (import.meta.env.DEV) {
// strict check only happens in import.meta.env.DEV builds, no reason to load it otherwise
isStrict = vm[CONSTANTS].getValue<boolean>(decodeHandle(_isStrict));
isStrict = vm.constants.getValue<boolean>(decodeHandle(_isStrict));
}

vm.loadValue(
Expand Down Expand Up @@ -151,7 +150,7 @@ function resolveHelper(definition: HelperDefinitionState, ref: Reference): Helpe

APPEND_OPCODES.add(Op.Helper, (vm, { op1: handle }) => {
let stack = vm.stack;
let helper = check(vm[CONSTANTS].getValue(handle), CheckHelper);
let helper = check(vm.constants.getValue(handle), CheckHelper);
let args = check(stack.pop(), CheckArguments);
let value = helper(args.capture(), vm.getOwner(), vm.dynamicScope());

Expand Down Expand Up @@ -182,7 +181,7 @@ APPEND_OPCODES.add(Op.SetBlock, (vm, { op1: symbol }) => {
});

APPEND_OPCODES.add(Op.ResolveMaybeLocal, (vm, { op1: _name }) => {
let name = vm[CONSTANTS].getValue<string>(_name);
let name = vm.constants.getValue<string>(_name);
let locals = vm.scope().getPartialMap()!;

let ref = locals[name];
Expand All @@ -198,7 +197,7 @@ APPEND_OPCODES.add(Op.RootScope, (vm, { op1: symbols }) => {
});

APPEND_OPCODES.add(Op.GetProperty, (vm, { op1: _key }) => {
let key = vm[CONSTANTS].getValue<string>(_key);
let key = vm.constants.getValue<string>(_key);
let expr = check(vm.stack.pop(), CheckReference);
vm.stack.push(childRefFor(expr, key));
});
Expand Down
Loading

0 comments on commit 4da86b9

Please sign in to comment.