diff --git a/tslang/lib/TypeScript/MLIRGenVariables.cpp b/tslang/lib/TypeScript/MLIRGenVariables.cpp index c1c2cd5f0..9c14b8fda 100644 --- a/tslang/lib/TypeScript/MLIRGenVariables.cpp +++ b/tslang/lib/TypeScript/MLIRGenVariables.cpp @@ -878,17 +878,19 @@ namespace mlirgen { // the resolved symbol's storage is a single boxed // pointer (ObjectType lowers to `ptr`) to the real - // data, not the data inline - load the pointer first, - // then load through it, instead of reading - // sizeof(fieldType) bytes directly at the symbol's own - // address (which would read past a lone 8-byte slot). - // See DeclarationPrinter.cpp's @boxed emission. + // data, not the data inline - load the pointer, + // instead of reading sizeof(fieldType) bytes directly + // at the symbol's own address (which would read past a + // lone 8-byte slot). The variable is that pointer, as + // the exporter's is: a copy of the data it points to + // was a snapshot, which the exporter's object never saw + // a write to, nor a method call, and a cast to an + // interface copied again. See DeclarationPrinter.cpp's + // @boxed emission. auto boxedType = getObjectType(fieldType); auto refToBoxed = cast(location, mlir_ts::RefType::get(boxedType), referenceToStaticFieldOpaque, genContext); auto boxedPtr = V(builder.create(location, boxedType, refToBoxed)); - auto refToTyped = V(cast(location, mlir_ts::RefType::get(fieldType), boxedPtr, genContext)); - auto valueOfField = builder.create(location, fieldType, refToTyped); - return std::make_tuple(valueOfField.getType(), V(valueOfField), TypeProvided::Yes); + return std::make_tuple(boxedPtr.getType(), boxedPtr, TypeProvided::Yes); } auto refToTyped = cast(location, mlir_ts::RefType::get(fieldType), referenceToStaticFieldOpaque, genContext); diff --git a/tslang/test/tester/tests/export_object_literal_untyped.ts b/tslang/test/tester/tests/export_object_literal_untyped.ts index baf610b04..2121ed8f2 100644 --- a/tslang/test/tester/tests/export_object_literal_untyped.ts +++ b/tslang/test/tester/tests/export_object_literal_untyped.ts @@ -14,4 +14,7 @@ namespace A { // `count: 0.0`: an integer literal is an s32, which the interface's `number` field would widen, // and a cast of an object whose method writes that field is an error (castObjectToInterface) export var counterObj = { count: 0.0, inc() { this.count = this.count + 1; } }; + + // the exporter's own view of the object, after the importer changed it + export function countOf() { return counterObj.count; } } diff --git a/tslang/test/tester/tests/import_object_literal_untyped.ts b/tslang/test/tester/tests/import_object_literal_untyped.ts index b62204d1b..dfef3e999 100644 --- a/tslang/test/tester/tests/import_object_literal_untyped.ts +++ b/tslang/test/tester/tests/import_object_literal_untyped.ts @@ -16,5 +16,9 @@ var c: A.Counter = A.counterObj; c.inc(); print(c.count); assert(c.count == 3); +// the interface and the import are one object, the exporter's: the importer's variable is its +// boxed pointer, not a copy of what it points to +assert(A.counterObj.count == 3); +assert(A.countOf() == 3); print("done.");