Skip to content

An imported boxed object is the exporter's object, not a copy - #726

Merged
ASDAlexander77 merged 3 commits into
mainfrom
cross-module-interface-copy
Oct 29, 2026
Merged

ASDAlexander77 merged 3 commits into
mainfrom
cross-module-interface-copy

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

An untyped object literal with methods is exported boxed: the exporter's global holds a pointer to the object, and the declarations say @boxed let counterObj : {count:number, inc(): void}. The importer loaded that pointer and then copied the whole object into its own variable (MLIRGenVariables.cpp, the isBoxed path). So A.counterObj in the importer was a snapshot:

  • its method calls and field writes never reached the exporter's object (A.countOf(), the exporter's own read, stayed at 0);
  • a cast to an interface copied the snapshot again: after c.inc(), c.count was 3 and A.counterObj.count was 2.

The importer's variable is now the pointer itself (an object type), the same as the exporter's. Field reads, method calls and interface casts all reach the one object.

import_object_literal_untyped now asserts the count three ways after the interface's call: through the interface, through the import, and through a function of the exporter. It runs as compile and JIT, under gc, rc and none.

Validated on Windows: ctest 6210/6210; DefaultLib release passes under gc (175), rc and none (174 + 1 gc-only skip), own (164 + 11 not-own skips).

Not changed: a variable declared with a type literal (const t: { count: number, inc(): void } = ...) is a value in this language. Assigning it, passing it, storing it in an array and casting it to an interface all copy it, by design.

🤖 Generated with Claude Code

ASDAlexander77 and others added 3 commits October 28, 2026 23:55
… an error

`{ count: 0, inc() { this.count = this.count + 1; } }` cast to an
interface with `count: number` was given a clone with `count` widened to
f64, while `inc` kept writing the s32 layout into it: `c.count` read
4.94066e-324 after one call, in every model.

The cast of an object with methods to an interface now reports an error
when a field it would convert is one the object's members name through
`this` (or they use `this` otherwise), naming the field and both types and
suggesting `0.0` or `<number>0`. MLIRGen records those names for each
object literal's storage (recordObjectLiteralThisFields); an object from
another module has no record, so any method of it counts. Objects whose
methods never name the field (00interface_object5) and objects of fields
only are still cloned, with the warning.

export_object_literal_untyped now gives `count: 0.0`, and its importer
asserts the count read through the interface.

Tests: 00interface_method_object_field_type,
interface-cast/method_object_narrow_field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An untyped object literal with methods is exported boxed: its global holds
a pointer to the object (@boxed in the declarations). The importer loaded
that pointer and then copied the whole object into its own variable, so
`A.counterObj` in the importer was a snapshot: its method calls and writes
never reached the exporter's object, and a cast to an interface copied the
snapshot again (`c.inc()` left `A.counterObj.count` behind).

The importer's variable is now the pointer itself, an object type, as the
exporter's is.

import_object_literal_untyped asserts the count read through the import
and through a function of the exporter after the interface's call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e-copy

# Conflicts:
#	tslang/test/tester/tests/export_object_literal_untyped.ts
#	tslang/test/tester/tests/import_object_literal_untyped.ts
@ASDAlexander77
ASDAlexander77 merged commit e2bd960 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the cross-module-interface-copy branch October 29, 2026 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant