Skip to content

Commit

Permalink
Substantial clarity improvements
Browse files Browse the repository at this point in the history
Improvements to naming and documentation as well as significant
consolidation of concepts that no longer need to be separated.

The consolidation is largely driven by the removal of concepts that were
originally introduced for the (long removed) AOT mode.
  • Loading branch information
wycats committed Nov 6, 2024
1 parent 5fc90f8 commit b652468
Show file tree
Hide file tree
Showing 41 changed files with 346 additions and 454 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
InternalModifierManager,
ModifierDefinitionState,
ResolvedComponentDefinition,
SimpleDocument,
SimpleElement,
} from '@glimmer/interfaces';
import {
Expand All @@ -13,11 +14,13 @@ import {
setInternalHelperManager,
setInternalModifierManager,
} from '@glimmer/manager';
import { programCompilationContext } from '@glimmer/opcode-compiler';
import { ProgramContextImpl } from '@glimmer/opcode-compiler';
import { artifacts, RuntimeOpImpl } from '@glimmer/program';
import { runtimeContext } from '@glimmer/runtime';

import type { UpdateBenchmark } from '../interfaces';

import createEnvDelegate from './create-env-delegate';
import renderBenchmark from './render-benchmark';

export interface Registry {
Expand Down Expand Up @@ -77,9 +80,23 @@ export default function createRegistry(): Registry {
setInternalModifierManager(manager, modifier);
modifiers.set(name, modifier);
},
render: (entry, args, element, isIteractive) => {
render: (entry, args, element, isInteractive) => {
const sharedArtifacts = artifacts();
const context = programCompilationContext(

const document = element.ownerDocument as SimpleDocument;
const envDelegate = createEnvDelegate(isInteractive ?? true);
const runtime = runtimeContext(
{
document,
},
envDelegate,
sharedArtifacts,
{
lookupComponent: () => null,
}
);

const context = new ProgramContextImpl(
sharedArtifacts,
{
lookupHelper: (name) => helpers.get(name) ?? null,
Expand All @@ -89,24 +106,15 @@ export default function createRegistry(): Registry {
lookupBuiltInHelper: () => null,
lookupBuiltInModifier: () => null,
},
(heap) => new RuntimeOpImpl(heap)
(heap) => new RuntimeOpImpl(heap),
runtime
);
const component = components.get(entry);
if (!component) {
throw new Error(`missing ${entry} component`);
}

return renderBenchmark(
sharedArtifacts,
context,
{
lookupComponent: () => null,
},
component,
args,
element as SimpleElement,
isIteractive
);
return renderBenchmark(sharedArtifacts, context, component, args, element as SimpleElement);
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,32 @@ import type {
ProgramContext,
ResolvedComponentDefinition,
RuntimeArtifacts,
RuntimeResolver,
SimpleElement,
} from '@glimmer/interfaces';
import { NewElementBuilder, renderComponent, renderSync, runtimeContext } from '@glimmer/runtime';
import { NewElementBuilder, renderComponent, renderSync } from '@glimmer/runtime';

import type { UpdateBenchmark } from '../interfaces';

import createEnvDelegate, { registerResult } from './create-env-delegate';
import { registerResult } from './create-env-delegate';
import { measureRender } from './util';

export default async function renderBenchmark(
artifacts: RuntimeArtifacts,
context: ProgramContext,
runtimeResolver: RuntimeResolver,
component: ResolvedComponentDefinition,
args: Dict,
element: SimpleElement,
isInteractive = true
element: SimpleElement
): Promise<UpdateBenchmark> {
let resolveRender: (() => void) | undefined;

await measureRender('render', 'renderStart', 'renderEnd', () => {
const document = element.ownerDocument;
const envDelegate = createEnvDelegate(isInteractive);
const runtime = runtimeContext(
{
document,
},
envDelegate,
artifacts,
runtimeResolver
);
const env = runtime.env;
const env = context.env;
const cursor = { element, nextSibling: null };
const treeBuilder = NewElementBuilder.forInitialRender(env, cursor);

const result = renderSync(
env,
renderComponent(runtime, treeBuilder, context, {}, component.state, args)
renderComponent(context, treeBuilder, {}, component.state, args)
);

registerResult(result, () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {
CompileTimeResolver,
ClassicResolver,
HelperDefinitionState,
ModifierDefinitionState,
Nullable,
Expand All @@ -8,7 +8,7 @@ import type {

import type { TestJitRuntimeResolver } from './resolver';

export default class JitCompileTimeLookup implements CompileTimeResolver {
export default class JitCompileTimeLookup implements ClassicResolver {
constructor(private resolver: TestJitRuntimeResolver) {}

lookupHelper(name: string): Nullable<HelperDefinitionState> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import type {
Environment,
HandleResult,
Helper,
Nullable,
ProgramContext,
Nullable,
RenderResult,
RuntimeContext,
SimpleDocument,
SimpleDocumentFragment,
SimpleElement,
Expand All @@ -21,7 +20,7 @@ import type { Reference } from '@glimmer/reference';
import type { CurriedValue, EnvironmentDelegate } from '@glimmer/runtime';
import type { ASTPluginBuilder, PrecompileOptions } from '@glimmer/syntax';
import { castToBrowser, castToSimple, expect, unwrapTemplate } from '@glimmer/debug-util';
import { programCompilationContext } from '@glimmer/opcode-compiler';
import { ProgramContextImpl } from '@glimmer/opcode-compiler';
import { artifacts, RuntimeOpImpl } from '@glimmer/program';
import { createConstRef } from '@glimmer/reference';
import {
Expand Down Expand Up @@ -58,24 +57,19 @@ import { TestJitRegistry } from './registry';
import { renderTemplate } from './render';
import { TestJitRuntimeResolver } from './resolver';

export interface JitTestDelegateContext {
runtime: RuntimeContext;
program: ProgramContext;
}

export function JitDelegateContext(
doc: SimpleDocument,
resolver: TestJitRuntimeResolver,
env: EnvironmentDelegate
): JitTestDelegateContext {
): ProgramContext {
let sharedArtifacts = artifacts();
let context = programCompilationContext(
let runtime = runtimeContext({ document: doc }, env, sharedArtifacts, resolver);
return new ProgramContextImpl(
sharedArtifacts,
new JitCompileTimeLookup(resolver),
(heap) => new RuntimeOpImpl(heap)
(heap) => new RuntimeOpImpl(heap),
runtime
);
let runtime = runtimeContext({ document: doc }, env, sharedArtifacts, resolver);
return { runtime, program: context };
}

export class JitRenderDelegate implements RenderDelegate {
Expand All @@ -86,7 +80,7 @@ export class JitRenderDelegate implements RenderDelegate {
protected resolver: TestJitRuntimeResolver;

private plugins: ASTPluginBuilder[] = [];
private _context: JitTestDelegateContext | null = null;
private _context: ProgramContext | null = null;
private self: Nullable<Reference> = null;
private doc: SimpleDocument;
private env: EnvironmentDelegate;
Expand All @@ -108,7 +102,7 @@ export class JitRenderDelegate implements RenderDelegate {
this.registry.register('helper', 'concat', concat);
}

get context(): JitTestDelegateContext {
get context(): ProgramContext {
if (this._context === null) {
this._context = JitDelegateContext(this.doc, this.resolver, this.env);
}
Expand All @@ -118,7 +112,7 @@ export class JitRenderDelegate implements RenderDelegate {

getCapturedRenderTree(): CapturedRenderNode[] {
return expect(
this.context.runtime.env.debugRenderTree,
this.context.env.debugRenderTree,
'Attempted to capture the DebugRenderTree during tests, but it was not created. Did you enable it in the environment?'
).capture();
}
Expand Down Expand Up @@ -206,13 +200,13 @@ export class JitRenderDelegate implements RenderDelegate {
compileTemplate(template: string): HandleResult {
let compiled = preprocess(template, this.precompileOptions);

return unwrapTemplate(compiled).asLayout().compile(this.context.program);
return unwrapTemplate(compiled).asLayout().compile(this.context);
}

renderTemplate(template: string, context: Dict<unknown>, element: SimpleElement): RenderResult {
let cursor = { element, nextSibling: null };

let { env } = this.context.runtime;
let { env } = this.context;

return renderTemplate(
template,
Expand All @@ -230,11 +224,12 @@ export class JitRenderDelegate implements RenderDelegate {
dynamicScope?: DynamicScope
): RenderResult {
let cursor = { element, nextSibling: null };
let { program, runtime } = this.context;
let builder = this.getElementBuilder(runtime.env, cursor);
let iterator = renderComponent(runtime, builder, program, {}, component, args, dynamicScope);
let { context } = this;

let builder = this.getElementBuilder(context.env, cursor);
let iterator = renderComponent(this.context, builder, {}, component, args, dynamicScope);

return renderSync(runtime.env, iterator);
return renderSync(context.env, iterator);
}

private get precompileOptions(): PrecompileOptions {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import type { ElementBuilder, RenderResult } from '@glimmer/interfaces';
import type { ElementBuilder, ProgramContext, RenderResult } from '@glimmer/interfaces';
import type { Reference } from '@glimmer/reference';
import type { PrecompileOptions } from '@glimmer/syntax';
import { unwrapTemplate } from '@glimmer/debug-util';
import { renderMain, renderSync } from '@glimmer/runtime';

import type { JitTestDelegateContext } from './delegate';

import { preprocess } from '../../compile';

export function renderTemplate(
src: string,
{ runtime, program }: JitTestDelegateContext,
context: ProgramContext,
self: Reference,
builder: ElementBuilder,
options?: PrecompileOptions
): RenderResult {
let template = preprocess(src, options);

let iterator = renderMain(
runtime,
program,
{},
self,
builder,
unwrapTemplate(template).asLayout()
);
return renderSync(runtime.env, iterator);
let iterator = renderMain(context, {}, self, builder, unwrapTemplate(template).asLayout());
return renderSync(context.env, iterator);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type {
HelperDefinitionState,
ClassicResolver,
ModifierDefinitionState,
Nullable,
ResolvedComponentDefinition,
RuntimeResolver,
} from '@glimmer/interfaces';

import type { TestJitRegistry } from './registry';

export class TestJitRuntimeResolver implements RuntimeResolver {
export class TestJitRuntimeResolver implements ClassicResolver {
constructor(private registry: TestJitRegistry) {}

lookupHelper(name: string): Nullable<HelperDefinitionState> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ElementNamespace,
Environment,
Helper,
ProgramContext,
Nullable,
RenderResult,
SimpleDocument,
Expand All @@ -26,7 +27,6 @@ import type { UserHelper } from '../../helpers';
import type { TestModifierConstructor } from '../../modifiers';
import type RenderDelegate from '../../render-delegate';
import type { RenderDelegateOptions } from '../../render-delegate';
import type { JitTestDelegateContext } from '../jit/delegate';
import type { DebugRehydrationBuilder } from './builder';

import { BaseEnv } from '../../base-env';
Expand All @@ -53,8 +53,8 @@ export class RehydrationDelegate implements RenderDelegate {

private plugins: ASTPluginBuilder[] = [];

public clientEnv: JitTestDelegateContext;
public serverEnv: JitTestDelegateContext;
public clientContext: ProgramContext;
public serverContext: ProgramContext;

private clientResolver: TestJitRuntimeResolver;
private serverResolver: TestJitRuntimeResolver;
Expand All @@ -75,12 +75,12 @@ export class RehydrationDelegate implements RenderDelegate {
this.clientDoc = castToSimple(document);
this.clientRegistry = new TestJitRegistry();
this.clientResolver = new TestJitRuntimeResolver(this.clientRegistry);
this.clientEnv = JitDelegateContext(this.clientDoc, this.clientResolver, delegate);
this.clientContext = JitDelegateContext(this.clientDoc, this.clientResolver, delegate);

this.serverDoc = createHTMLDocument();
this.serverRegistry = new TestJitRegistry();
this.serverResolver = new TestJitRuntimeResolver(this.serverRegistry);
this.serverEnv = JitDelegateContext(this.serverDoc, this.serverResolver, delegate);
this.serverContext = JitDelegateContext(this.serverDoc, this.serverResolver, delegate);
}

getInitialElement(): SimpleElement {
Expand Down Expand Up @@ -119,12 +119,12 @@ export class RehydrationDelegate implements RenderDelegate {
): string {
element = element || this.serverDoc.createElement('div');
let cursor = { element, nextSibling: null };
let { env } = this.serverEnv.runtime;
let { env } = this.serverContext;

// Emulate server-side render
renderTemplate(
template,
this.serverEnv,
this.serverContext,
this.getSelf(env, context),
this.getElementBuilder(env, cursor),
this.precompileOptions
Expand All @@ -147,15 +147,15 @@ export class RehydrationDelegate implements RenderDelegate {
}

renderClientSide(template: string, context: Dict<unknown>, element: SimpleElement): RenderResult {
let env = this.clientEnv.runtime.env;
let env = this.clientContext.env;
this.self = null;

// Client-side rehydration
let cursor = { element, nextSibling: null };
let builder = this.getElementBuilder(env, cursor) as DebugRehydrationBuilder;
let result = renderTemplate(
template,
this.clientEnv,
this.clientContext,
this.getSelf(env, context),
builder,
this.precompileOptions
Expand Down
Loading

0 comments on commit b652468

Please sign in to comment.