Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@ember/-internals/glimmer/lib/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const BUILTIN_KEYWORD_HELPERS: Record<string, object> = {
mut,
readonly,
unbound,
fn,
'-hash': hash,
'-each-in': eachIn,
'-normalize-class': normalizeClassHelper,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,32 @@ class DynamicStrictModeTest extends RenderTest {
this.assertStableRerender();
}

@test
'calling a method on a class component works'() {
const Bar = defineComponent(
{ on },
'<output>{{this.data.count}}</output><button {{on "click" this.increment}}>inc</button>',
{
definition: class extends GlimmerishComponent {
data = trackedObj({ count: 0 }) as { count: number };

increment() {
this.data.count++;
}
},
}
);

this.renderComponent(Bar);
this.assertHTML('<output>0</output><button>inc</button>');

castToBrowser(this.element, 'div').querySelector('button')!.click();
this.rerender();

this.assertHTML('<output>1</output><button>inc</button>');
this.assertStableRerender();
}

@test
'Can use a dynamic modifier with a changing definition'(assert: Assert) {
const modifier1 = defineSimpleModifier((element: Element) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/@glimmer/constants/lib/syscall-ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import type {
VmJumpUnless,
VmLoad,
VmLog,
VmInvokableReference,
VmMain,
VmModifier,
VmNot,
Expand Down Expand Up @@ -187,7 +188,8 @@ export const VM_IF_INLINE_OP = 109 satisfies VmIfInline;
export const VM_NOT_OP = 110 satisfies VmNot;
export const VM_GET_DYNAMIC_VAR_OP = 111 satisfies VmGetDynamicVar;
export const VM_LOG_OP = 112 satisfies VmLog;
export const VM_SYSCALL_SIZE = 113 satisfies VmSize;
export const VM_INVOKABLE_REFERENCE_OP = 113 satisfies VmInvokableReference;
export const VM_SYSCALL_SIZE = 114 satisfies VmSize;

export function isOp(value: number): value is VmOp {
return value >= 16;
Expand Down
7 changes: 7 additions & 0 deletions packages/@glimmer/debug/lib/opcode-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
VM_HAS_BLOCK_PARAMS_OP,
VM_HELPER_OP,
VM_IF_INLINE_OP,
VM_INVOKABLE_REFERENCE_OP,
VM_INVOKE_COMPONENT_LAYOUT_OP,
VM_INVOKE_STATIC_OP,
VM_INVOKE_VIRTUAL_OP,
Expand Down Expand Up @@ -291,6 +292,12 @@ if (LOCAL_DEBUG) {
stackChange: 0,
};

METADATA[VM_INVOKABLE_REFERENCE_OP] = {
name: 'InvokableReference',
mnemonic: 'invref',
stackChange: 0,
};

METADATA[VM_REIFY_U32_OP] = {
name: 'ReifyU32',
mnemonic: 'reifyload',
Expand Down
8 changes: 5 additions & 3 deletions packages/@glimmer/interfaces/lib/vm-opcodes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export type VmIfInline = 109;
export type VmNot = 110;
export type VmGetDynamicVar = 111;
export type VmLog = 112;
export type VmSize = 113;
export type VmInvokableReference = 113;

export type VmOp =
| VmHelper
Expand Down Expand Up @@ -198,14 +198,16 @@ export type VmOp =
| VmDidCreateElement
| VmDidRenderLayout
| VmDebugger
| VmSize
| VmStaticComponentAttr
| VmDynamicContentType
| VmDynamicHelper
| VmDynamicModifier
| VmIfInline
| VmNot
| VmGetDynamicVar
| VmLog;
| VmLog
| VmInvokableReference;

export type SomeVmOp = VmOp | VmMachineOp;

export type VmSize = 114;
2 changes: 2 additions & 0 deletions packages/@glimmer/opcode-compiler/lib/syntax/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
VM_HAS_BLOCK_OP,
VM_HAS_BLOCK_PARAMS_OP,
VM_IF_INLINE_OP,
VM_INVOKABLE_REFERENCE_OP,
VM_LOG_OP,
VM_NOT_OP,
VM_POP_FRAME_OP,
Expand Down Expand Up @@ -45,6 +46,7 @@ EXPRESSIONS.add(SexpOpcodes.Call, (op, [, expression, positional, named]) => {
});
} else {
expr(op, expression);
op(VM_INVOKABLE_REFERENCE_OP);
CallDynamic(op, positional, named);
}
});
Expand Down
103 changes: 64 additions & 39 deletions packages/@glimmer/reference/lib/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,11 @@ export function isInvokableRef(ref: Reference) {
}

export function createInvokableRef(inner: Reference): Reference {
const ref = createComputeRef(
() => valueForRef(inner),
(value) => updateRef(inner, value)
);
ref.debugLabel = inner.debugLabel;
ref[REFERENCE] = INVOKABLE;
const ref = new ReferenceImpl(INVOKABLE);

ref.compute = () => valueForRef(inner);
ref.update = (value) => updateRef(inner, value);
ref.debugLabel = inner.debugLabel === false ? undefined : inner.debugLabel;

return ref;
}
Expand Down Expand Up @@ -193,6 +192,61 @@ export function updateRef(_ref: Reference, value: unknown) {
update(value);
}

function bindPrototypeMethod(parent: object, path: string, value: unknown): unknown {
if (
typeof value === 'function' &&
!Object.prototype.hasOwnProperty.call(parent, path) &&
Object.keys(value).length === 0
) {
return value.bind(parent);
}

return value;
}

function readChildValue(
parent: unknown,
path: string,
getter: (obj: object, path: string) => unknown
): unknown {
if (!isDict(parent)) return undefined;

const value = getter(parent, path);
return bindPrototypeMethod(parent, path, value);
}

function createUnboundChildRef(parentRef: ReferenceImpl, path: string): Reference {
const parent = valueForRef(parentRef);

if (!isDict(parent)) return UNDEFINED_REFERENCE;

const value = readChildValue(parent, path, (obj, key) => (obj as Record<string, unknown>)[key]);

return createUnboundRef(value, DEBUG && `${parentRef.debugLabel}.${path}`);
}

function createComputeChildRef(parentRef: ReferenceImpl, path: string): Reference {
const child = createComputeRef(
() => {
const parent = valueForRef(parentRef);
return readChildValue(parent, path, getProp);
},
(val) => {
const parent = valueForRef(parentRef);

if (isDict(parent)) {
return setProp(parent, path, val);
}
}
);

if (DEBUG) {
child.debugLabel = `${parentRef.debugLabel}.${path}`;
}

return child;
}

export function childRefFor(_parentRef: Reference, path: string): Reference {
const parentRef = _parentRef as ReferenceImpl;

Expand All @@ -209,39 +263,10 @@ export function childRefFor(_parentRef: Reference, path: string): Reference {
if (next) return next;
}

if (type === UNBOUND) {
const parent = valueForRef(parentRef);

if (isDict(parent)) {
child = createUnboundRef(
(parent as Record<string, unknown>)[path],
DEBUG && `${parentRef.debugLabel}.${path}`
);
} else {
child = UNDEFINED_REFERENCE;
}
} else {
child = createComputeRef(
() => {
const parent = valueForRef(parentRef);

if (isDict(parent)) {
return getProp(parent, path);
}
},
(val) => {
const parent = valueForRef(parentRef);

if (isDict(parent)) {
return setProp(parent, path, val);
}
}
);

if (DEBUG) {
child.debugLabel = `${parentRef.debugLabel}.${path}`;
}
}
child =
type === UNBOUND
? createUnboundChildRef(parentRef, path)
: createComputeChildRef(parentRef, path);

children.set(path, child);

Expand Down
8 changes: 8 additions & 0 deletions packages/@glimmer/runtime/lib/compiled/opcodes/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
VM_JUMP_IF_OP,
VM_JUMP_UNLESS_OP,
VM_LOAD_OP,
VM_INVOKABLE_REFERENCE_OP,
VM_POP_DYNAMIC_SCOPE_OP,
VM_POP_OP,
VM_POP_SCOPE_OP,
Expand All @@ -46,6 +47,7 @@ import { toBool } from '@glimmer/global-context';
import {
createComputeRef,
createConstRef,
createInvokableRef,
createPrimitiveRef,
FALSE_REFERENCE,
isConstRef,
Expand Down Expand Up @@ -120,6 +122,12 @@ APPEND_OPCODES.add(VM_PRIMITIVE_REFERENCE_OP, (vm) => {
stack.push(ref);
});

APPEND_OPCODES.add(VM_INVOKABLE_REFERENCE_OP, (vm) => {
let stack = vm.stack;
let ref = check(stack.pop(), CheckReference);
stack.push(createInvokableRef(ref));
});

APPEND_OPCODES.add(VM_DUP_OP, (vm, { op1: register, op2: offset }) => {
let position = check(vm.fetchValue(check(register, CheckRegister)), CheckNumber) - offset;
vm.stack.dup(position);
Expand Down
2 changes: 1 addition & 1 deletion smoke-tests/scenarios/basic-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function basicTest(scenarios: Scenarios, appName: string) {
})
}

louder = () => {
louder() {
this.message = this.message + '!';
}

Expand Down
Loading