Skip to content
Merged
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
22 changes: 22 additions & 0 deletions TSLANG_ISSUES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3114,3 +3114,25 @@ this.objs) yield o.n`, 182's leftover) stays refused, with its error. USER RULIN
out of the array (which would empty it, unlike JavaScript).

Tests diagnostic-location `generator_override_throwing_object_through_yielding_finally_own.ts`.

186 (2026-11-24). Fixed:
- `arguments.length` of `h(1, undefined)` for `h(a, b?)` was 1 (JavaScript says 2, 114/183): the function can't tell
an `undefined` given from a parameter left out. A function declaration whose body reads `arguments` and has optional
parameters (not a method, not generic, not exported, no rest parameter) takes a parameter of its own after the
others, `.argc` (ARGUMENT_COUNT_NAME, mlirGenParameters), and a call by its name gives it the count of its
arguments (mlirGenCall); the length is that count, no more than the parameters (the arguments past them are not
passed). Called as a function value it is left out, and the length is worked out from the parameters as before.
- An optional value given to an `any` (`const x: any = b` of a `b?: string`): one holding nothing was boxed as the
type's value with no text, which crashed where it was read (`"" + x`); it is boxed as undefined now, and one holding
a value as that value (as an optional `any` was).
- A `yield*` over a generator whose type has throw() but not return(): the delegate was not held, so a throw() of the
generator was not handed to it. It is held for its throw(), and no return() is called that the type does not have.
- -mm=own: `yield* it` of a generator a parameter holds hands a throw() to the delegate's throw() as well (184 did a
delegate made there), for elements that own nothing.

Looked at, still open:
- `arguments` of a method, a generic function or one called as a function value: the length is worked out from the
parameters, as before (an `undefined` given counts as left out).

Tests `00arguments_length_given.ts` (cases added), `00generator_yield_star_throw_only.ts` and
`00generator_yield_star_throw_param.ts` (gc, rc, own).
1 change: 1 addition & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# What's new

- `arguments.length` counts an `undefined` given (`h(1, undefined)` is 2) in a call by name; an optional value given to an `any` that holds nothing is `undefined`; a `yield*` hands throw() to a delegate typed with `throw` but no `return`, and under -mm=own to one a parameter holds
- -mm=own: a method whose override throws an object, called through the base class in a `try` whose `finally` yields, is a compile error at the call, not a stop at run time
- A function with parameters of different types can be given to `(...args: A) => T` (`A` is inferred as a tuple, `[number, string]`); under -mm=own, throw() of a generator paused in a `yield*` of numbers is handed to the delegate too
- `arguments.length` counts the arguments given, not the parameters declared; a generic arrow can be given to a generic parameter (`wrap(<T>(x: T) => x)`), and a function to `(...args: A) => T`; throw() of a generator paused in a `yield*` is handed to the delegate's throw(), which may catch it and go on yielding
Expand Down
2 changes: 2 additions & 0 deletions tslang/include/TypeScript/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@
#define INFINITY_NAME "Infinity"
#define NAN_NAME "NaN"
#define THIS_NAME "this"
// the parameter a function that reads `arguments` is given the count of its arguments in (TSLANG 186)
#define ARGUMENT_COUNT_NAME ".argc"
#define SUPER_NAME "super"
#define STATIC_NAME "static"
#define STATIC_CONSTRUCTOR_NAME "static_constructor"
Expand Down
2 changes: 2 additions & 0 deletions tslang/include/TypeScript/MLIRLogic/MLIRGenContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ struct GenContext
// variable its result goes to, in place of the delegate's next(), and the flag that says next() is not called
const char *yieldStarResult = nullptr;
const char *yieldStarSkipNext = nullptr;
// ...and whether that delegate has `return` (one with `throw` only is held for its throw(), TSLANG 186)
bool yieldStarHasReturn = true;
// out-of-band cancellation signal; mutable so stop() stays callable through the const& threading
mutable bool stopProcess = false;
mlir::SmallVector<std::unique_ptr<mlir::Diagnostic>> *postponedMessages = nullptr;
Expand Down
29 changes: 29 additions & 0 deletions tslang/lib/TypeScript/MLIRGenAccessCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,35 @@ namespace mlirgen
// through its virtual table: the method of the class the call names)
auto *calleeOp = funcRefValue.getDefiningOp();
auto calleeName = calleeOp ? calleeOp->getAttrOfType<mlir::FlatSymbolRefAttr>("identifier") : mlir::FlatSymbolRefAttr();

// a function that reads `arguments` called by its name: given the count of the arguments, `undefined` for
// the parameters left out before it (mlirGenParameters, TSLANG 186). More arguments than parameters are put
// after it, refused as before.
if (auto symbolRefOp = mlir::dyn_cast_or_null<mlir_ts::SymbolRefOp>(calleeOp))
{
auto counting = functionsCountingArguments.find(symbolRefOp.getIdentifier());
if (counting != functionsCountingArguments.end())
{
auto countPosition = counting->second;
auto given = operands.size();
auto countValue = builder.create<mlir_ts::ConstantOp>(location, builder.getI32Type(),
builder.getI32IntegerAttr((int)given));
CAST_A(count, location, getNumberType(), countValue, genContext);
if (given <= countPosition)
{
while (operands.size() < countPosition)
{
operands.push_back(builder.create<mlir_ts::UndefOp>(location, getUndefinedType()));
}

operands.push_back(count);
}
else
{
operands.insert(operands.begin() + countPosition, count);
}
}
}
if (compileOptions.memoryModel == MemoryModelOwn && calleeName &&
mlir::isa<mlir_ts::SymbolRefOp, mlir_ts::ThisSymbolRefOp, mlir_ts::ThisVirtualSymbolRefOp,
mlir_ts::VirtualSymbolRefOp>(calleeOp))
Expand Down
14 changes: 11 additions & 3 deletions tslang/lib/TypeScript/MLIRGenCast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,16 +1083,24 @@ namespace mlirgen
}

// `any | undefined` (an optional `any` field, `{ baz?: any }`) is the `any` it holds, or undefined boxed:
// boxed whole it had no `typeof` name, and lowering made a box with no tag
// boxed whole it had no `typeof` name, and lowering made a box with no tag. So is any other optional
// (`b?: string` given to an `any`, TSLANG 186): the value it holds boxed, or undefined - boxed whole, one
// holding nothing was a string with no text, which crashed where it was read
if (isa<mlir_ts::AnyType>(type))
{
if (auto optType = dyn_cast<mlir_ts::OptionalType>(valueType); optType && isa<mlir_ts::AnyType>(optType.getElementType()))
if (auto optType = dyn_cast<mlir_ts::OptionalType>(valueType))
{
auto hasValue = builder.create<mlir_ts::HasValueOp>(location, getBooleanType(), value);
auto ifOp = builder.create<mlir_ts::IfOp>(location, type, hasValue, true);

builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
builder.create<mlir_ts::ResultOp>(location, mlir::ValueRange{builder.create<mlir_ts::ValueOp>(location, type, value)});
mlir::Value held = builder.create<mlir_ts::ValueOp>(location, optType.getElementType(), value);
if (!isa<mlir_ts::AnyType>(held.getType()))
{
CAST(held, location, type, held, genContext);
}

builder.create<mlir_ts::ResultOp>(location, mlir::ValueRange{held});

builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
auto undefinedValue = builder.create<mlir_ts::UndefOp>(location, getUndefinedType());
Expand Down
42 changes: 40 additions & 2 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4047,6 +4047,8 @@ namespace mlirgen
// `h(1)` of `h(a, b?)` gave one argument, not two (TSLANG 114, 183)
SmallVector<mlir::Value, 4> givenOptional;
auto anyOptional = false;
// the count a call by the function's name gives (ARGUMENT_COUNT_NAME, TSLANG 186)
mlir::Value givenCount;
for (auto &param : genContext.bodyFuncProto->getParams())
{
// a method's object is not one of its arguments
Expand All @@ -4055,6 +4057,14 @@ namespace mlirgen
continue;
}

if (param->getName() == ARGUMENT_COUNT_NAME)
{
auto result = mlirGen(location, param->getName(), genContext);
EXIT_IF_FAILED_OR_NO_VALUE(result)
givenCount = V(result);
continue;
}

// a destructured parameter is the argument under the name the compiler gave it; a rest parameter's
// elements are the arguments after the others (TSLANG 114)
auto result = mlirGen(location, param->getName(), genContext);
Expand Down Expand Up @@ -4127,8 +4137,8 @@ namespace mlirgen
mlirGenRetainCaptured(location, values);
argumentsArray = builder.create<mlir_ts::CreateArrayOp>(location, getArrayType(getAnyType()), values);

// as many as the caller gave: up to the last parameter given (an optional one given `undefined` is given,
// `h(1, undefined)`, as JavaScript counts it); the ones after it are left out
// as many as the caller gave: up to the last parameter given; the ones after it are left out (an optional
// one given `undefined` looks left out here: the count a call by name gives, below, counts it)
if (anyOptional)
{
auto i32Type = builder.getI32Type();
Expand All @@ -4153,6 +4163,34 @@ namespace mlirgen
count = ifOp.getResult(0);
}

// ...unless the caller said how many it gave (a call by the function's name): an `undefined` given
// is given (`h(1, undefined)` is 2), and more than the parameters are as many as they are
if (givenCount && isa<mlir_ts::OptionalType>(givenCount.getType()))
{
auto hasCount = builder.create<mlir_ts::HasValueOp>(location, getBooleanType(), givenCount);
auto ifOp = builder.create<mlir_ts::IfOp>(location, mlir::TypeRange{i32Type}, hasCount, true);
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
auto countNumber = builder.create<mlir_ts::ValueOp>(location, getNumberType(), givenCount);
auto parameterCount = builder.create<mlir_ts::ConstantOp>(
location, i32Type, builder.getI32IntegerAttr((int)givenOptional.size()));
CAST_A(parameterNumber, location, getNumberType(), parameterCount, genContext);
// (no more than the array holds: the arguments past the parameters are not passed)
auto fewer = mlirGenBinaryValues(location, SyntaxKind::LessThanToken, countNumber, parameterNumber, genContext);
EXIT_IF_FAILED_OR_NO_VALUE(fewer)
auto pickOp = builder.create<mlir_ts::IfOp>(location, mlir::TypeRange{getNumberType()}, V(fewer), true);
builder.setInsertionPointToStart(&pickOp.getThenRegion().front());
builder.create<mlir_ts::ResultOp>(location, mlir::ValueRange{countNumber});
builder.setInsertionPointToStart(&pickOp.getElseRegion().front());
builder.create<mlir_ts::ResultOp>(location, mlir::ValueRange{parameterNumber});
builder.setInsertionPointAfter(pickOp);
mlir::Value callerCount = builder.create<mlir_ts::CastOp>(location, i32Type, pickOp.getResult(0));
builder.create<mlir_ts::ResultOp>(location, mlir::ValueRange{callerCount});
builder.setInsertionPointToStart(&ifOp.getElseRegion().front());
builder.create<mlir_ts::ResultOp>(location, mlir::ValueRange{count});
builder.setInsertionPointAfter(ifOp);
count = ifOp.getResult(0);
}

// (set on the array's place: the local below, or a place of its own for a temporary)
argumentsCount = builder.create<mlir_ts::CastOp>(location, builder.getIndexType(), count);
}
Expand Down
Loading
Loading