diff --git a/TSLANG_ISSUES.md b/TSLANG_ISSUES.md index 6c0f0807b..ebe3b8cdc 100644 --- a/TSLANG_ISSUES.md +++ b/TSLANG_ISSUES.md @@ -2166,12 +2166,24 @@ Tests: `00object_dictionary.ts`, `00own_param_exclusive_moves.ts` (compile, JIT, undefined or any) is an error at the `return`: next() gives `{ value: T }`, and `return "end"` in a `Generator` was cast to a number without a word (it read 0). +- next(value) of a generator typed `Generator`: its next() takes `value?: TNext`, and the + `yield` it resumes evaluates to the value, a TNext (TNext's default - 0, null - when next() is given none: the + first one, a for...of). It was undefined. +- An object-literal method given as an interface's function member of another arity - fewer parameters (`next()` + for `next: (value?: number) => ...`, the rest ignored, as JavaScript does) or more optional ones (`next(v?)` for + `next: () => ...`): "can't assign '() => ...' to '(p1: number | undefined) => ...': it takes 0 argument(s), 2 + expected". The vtable's slot holds a thunk giving the method as many arguments as it takes + (isArityAdaptableObjectMethod, generateSynthMethodToCallField), as for a field holding a closure. So a generator + whose next() takes no value is a Generator whose next takes one, and the other way round. The library's + `Generator` declares `next: (value?: TNext)` once a compiler with this is released (a released one without it + can't give a plain generator as one). + Still open, looked at: -- next(value) / TNext: next() would take `value?: TNext` and the resumed `yield` evaluate to it - tried, and it works - for a generator cast to an interface declaring `next(value?)` - but a generator's next() is then cast to - interfaces declaring `next: () => ...` (a program's own, the library's Iterable), and an object's method of - another arity has no adapter in the interface's vtable (castObjectToInterface; a class has them, 150). Needs that - adapter, then `Generator`'s next in the library. return(v) / throw(e) likewise. +- return(v) / throw(e) of a generator. +- -mm=own: next(value) of a generator sending it an object (`Generator`) is refused ("'this value' + is a parameter, kept here, but -mm=own cannot make the callers of this function expression give it up"): next() + keeps what it is given, and a method used other than by a call can't make its callers give it up. A number or a + string works. - -mm=own: a generator typed as an interface other than the library-instantiated Iterator - a program's own, or Iterable - is "a value takes a second reference; -mm=own cannot prove a move or a borrow here yet" at its `for...of` (before these changes too), and so is an array given as an Iterable. @@ -2182,4 +2194,5 @@ Still open, looked at: `[Symbol.asyncIterator]` of an async generator. Tests: `00iterables.ts`, `00generator_typed_yield.ts` (compile, JIT, rc), -diagnostic-location `generator_return_of_other_type.ts`. +diagnostic-location `generator_return_of_other_type.ts`; `00generator_next_value.ts`, `00object_method_arity.ts` +(compile, JIT, rc, own). diff --git a/tslang/include/TypeScript/Defines.h b/tslang/include/TypeScript/Defines.h index 2bb7e1067..5185d8f08 100644 --- a/tslang/include/TypeScript/Defines.h +++ b/tslang/include/TypeScript/Defines.h @@ -562,6 +562,9 @@ // `{ [k]: v }` with a key known only at run time). Outside the class, `d.name` and `d["name"]` are its // entries, never its members (ClassInfo::isDictionary). #define DICTIONARY_DECORATOR "dictionary" +// next(value) of a generator typed Generator: the value, the parameter of its next(), is what +// the `yield` it resumes evaluates to +#define GENERATOR_SENT_NAME ".sent" #define OBJECT_DICTIONARY_CLASS_NAME "ObjectDictionary" // the default library's function a spread of a dictionary into a new one calls #define OBJECT_DICTIONARY_ASSIGN_NAME "objectDictionaryAssign" diff --git a/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h b/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h index 5df8556cc..3696c9593 100644 --- a/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h +++ b/tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h @@ -1370,6 +1370,39 @@ class MLIRTypeHelper return false; } + // An object-literal method (`this` first) an interface's function member can call through a thunk that gives + // it as many arguments as it takes: it takes fewer parameters than the member (`next()` for `next(value?)`, + // the rest ignored, as JavaScript does), or more, each of them optional (`next(value?)` for `next()`). The + // parameters both have, and the results, match. A generator's `next` is given as an iterator's this way. + bool isArityAdaptableObjectMethod(mlir::Location location, mlir::Type methodType, mlir::Type memberType) + { + auto srcType = dyn_cast(methodType); + auto destType = dyn_cast(memberType); + if (!srcType || !destType || srcType == destType || isObjectFieldFunctionWithoutThis(srcType) + || destType.getNumInputs() == 0 || srcType.isVarArg() || destType.isVarArg() + || srcType.getNumInputs() == destType.getNumInputs()) + { + return false; + } + + auto common = std::min(srcType.getNumInputs(), destType.getNumInputs()); + for (auto extra : srcType.getInputs().drop_front(common)) + { + auto unionType = dyn_cast(extra); + if (!isa(extra) + && !(unionType && llvm::any_of(unionType.getTypes(), [](mlir::Type t) { return isa(t); }))) + { + return false; + } + } + + // the method as if it took the member's parameters past the common ones: then it has to match + SmallVector inputs(srcType.getInputs().take_front(common)); + inputs.append(destType.getInputs().begin() + common, destType.getInputs().end()); + auto aligned = mlir_ts::FunctionType::get(context, inputs, srcType.getResults(), false); + return TestFunctionTypesMatchWithObjectMethods(location, aligned, destType).result == MatchResultType::Match; + } + MatchResult TestFunctionTypesMatchWithObjectMethods(mlir::Location location, mlir::Type inFuncType, mlir::Type resFuncType, unsigned startParamIn = 0, unsigned startParamRes = 0) { @@ -1682,6 +1715,13 @@ class MLIRTypeHelper } } + // a method taking fewer parameters, or more optional ones, is called through a thunk too + // (isArityAdaptableObjectMethod) + if (isArityAdaptableObjectMethod(location, foundField.type, fieldType)) + { + return {mlir_ts::FieldInfo{id, fieldType, false, mlir_ts::AccessLevel::Public}, mlir::success()}; + } + auto test = isa(foundField.type) && isa(fieldType) ? TestFunctionTypesMatchWithObjectMethods(location, foundField.type, fieldType).result == MatchResultType::Match : stripLiteralType(fieldType) == stripLiteralType(foundField.type); diff --git a/tslang/lib/TypeScript/MLIRGenExpressions.cpp b/tslang/lib/TypeScript/MLIRGenExpressions.cpp index b8bf9cbf9..09dc464a0 100644 --- a/tslang/lib/TypeScript/MLIRGenExpressions.cpp +++ b/tslang/lib/TypeScript/MLIRGenExpressions.cpp @@ -513,8 +513,22 @@ namespace mlirgen label << GENERATOR_STATELABELPREFIX << state; builder.create(location, label.str()); - // TODO: the value passed to next(value); next() takes no argument yet, so a yield evaluates to undefined. - // It is made after the label: the turn that resumes here is the one that uses it. + // The value passed to next(value) - next()'s parameter, which a generator typed Generator has - or undefined. It is read after the label: the turn that resumes here is the one that uses it. + if (auto sent = resolveIdentifier(location, GENERATOR_SENT_NAME, genContext)) + { + // a TNext, as TypeScript types the `yield`: the parameter is optional (the first next(), a for...of, + // give none), and a turn given none reads TNext's default (0 for a number, null for a class) + if (auto optType = dyn_cast(sent.getType())) + { + auto valueOrDefault = builder.create(location, optType.getElementType(), sent); + valueOrDefault->setAttr(EXPLICIT_CAST_ATTR_NAME, builder.getUnitAttr()); + return V(valueOrDefault); + } + + return sent; + } + return V(builder.create(location, getUndefinedType())); } diff --git a/tslang/lib/TypeScript/MLIRGenFunctions.cpp b/tslang/lib/TypeScript/MLIRGenFunctions.cpp index 8d849f358..ed99b8da4 100644 --- a/tslang/lib/TypeScript/MLIRGenFunctions.cpp +++ b/tslang/lib/TypeScript/MLIRGenFunctions.cpp @@ -1269,6 +1269,7 @@ namespace mlirgen // s32 the literal is), as TypeScript types it; without it the yields' own type was the element's, and // `yield 1` could not be a Generator ("can't assign '() => {value:s32, ...}'") TypeNode nextResultType = undefined; + NodeArray nextParameters; if (auto declaredType = functionLikeDeclarationBaseAST->type; declaredType && declaredType == SyntaxKind::TypeReference && !hasModifier(functionLikeDeclarationBaseAST, SyntaxKind::AsyncKeyword)) { @@ -1285,6 +1286,14 @@ namespace mlirgen members.push_back(nf.createPropertySignature(undefined, nf.createIdentifier(S("done")), undefined, nf.createToken(SyntaxKind::BooleanKeyword).as())); nextResultType = nf.createTypeLiteralNode(members); + + // Generator: next(value?: TNext), the value what the resumed `yield` is + if (typeName == "Generator" && typeReference->typeArguments.size() >= 3) + { + nextParameters.push_back(nf.createParameterDeclaration( + undefined, undefined, nf.createIdentifier(S(GENERATOR_SENT_NAME)), + nf.createToken(SyntaxKind::QuestionToken), typeReference->typeArguments[2])); + } } } } @@ -1292,7 +1301,7 @@ namespace mlirgen // create method next in object auto nextMethodDecl = nf.createMethodDeclaration(undefined, undefined, nf.createIdentifier(S(ITERATOR_NEXT)), undefined, - undefined, undefined, nextResultType, nextBody); + undefined, nextParameters, nextResultType, nextBody); nextMethodDecl->internalFlags |= InternalFlags::VarsInObjectContext | InternalFlags::GeneratorNext; // copy location info, to fix issue with names of anonymous functions diff --git a/tslang/lib/TypeScript/MLIRGenImpl.h b/tslang/lib/TypeScript/MLIRGenImpl.h index e4227342e..f909ee997 100644 --- a/tslang/lib/TypeScript/MLIRGenImpl.h +++ b/tslang/lib/TypeScript/MLIRGenImpl.h @@ -17110,7 +17110,8 @@ class MLIRGenImpl // mlirGenClassVirtualTableDefinitionForInterface, mlirGenObjectVirtualTableDefinitionForInterface) mlir::LogicalResult generateSynthMethodToCallField(mlir::Location location, mlir::Type thisType, StringRef fieldName, StringRef thunkName, - mlir_ts::FunctionType funcType, const GenContext &genContext) + mlir_ts::FunctionType funcType, const GenContext &genContext, + unsigned maxArguments = ~0u) { auto retType = mth.getReturnTypeFromFuncRef(funcType); @@ -17136,7 +17137,9 @@ class MLIRGenImpl NodeFactory nf(NodeFactoryFlags::None); NodeArray argumentsArray; - for (unsigned index = 1; index < funcType.getNumInputs(); index++) + // as many as the field's function takes (maxArguments): a method with fewer parameters than the + // interface's member ignores the rest + for (unsigned index = 1; index < funcType.getNumInputs() && index - 1 < maxArguments; index++) { std::string paramName("p"); paramName += std::to_string(index - 1); diff --git a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp index 0b6c502b5..77786862b 100644 --- a/tslang/lib/TypeScript/MLIRGenInterfaces.cpp +++ b/tslang/lib/TypeScript/MLIRGenInterfaces.cpp @@ -316,7 +316,8 @@ namespace mlirgen // a field the interface calls through a thunk has the thunk in its slot already auto fieldInfo = mth.getFieldInfoByIndex(storeType, index); if (lookupObjectLiteralMethodSymbol(fieldInfo.type, fieldId).empty() - && !mth.isObjectFieldFunctionWithoutThis(fieldInfo.type)) + && !mth.isObjectFieldFunctionWithoutThis(fieldInfo.type) + && !mth.isArityAdaptableObjectMethod(location, fieldInfo.type, method.funcType)) { methodsNeedingPatch.push_back({methodPtr, vtableOffset}); } @@ -860,7 +861,9 @@ namespace mlirgen } auto storageFieldType = tupleStorageType.getFieldInfo(storageIndex).type; - if (storageFieldType == funcType || !mth.isObjectFieldFunctionWithoutThis(storageFieldType)) + // a method of another arity (isArityAdaptableObjectMethod) is given as many arguments as it takes + auto arityAdapted = mth.isArityAdaptableObjectMethod(location, storageFieldType, funcType); + if (storageFieldType == funcType || !mth.isObjectFieldFunctionWithoutThis(storageFieldType) && !arityAdapted) { continue; } @@ -872,8 +875,9 @@ namespace mlirgen } auto thunkName = concat(fullClassInterfaceVTableFieldName, fieldName.getValue(), "thunk"); + auto maxArguments = arityAdapted ? mlir::cast(storageFieldType).getNumInputs() - 1 : ~0u; if (mlir::failed(generateSynthMethodToCallField(location, objectType, fieldName.getValue(), thunkName, - funcType, genContext))) + funcType, genContext, maxArguments))) { return mlir::failure(); } diff --git a/tslang/test/tester/CMakeLists.txt b/tslang/test/tester/CMakeLists.txt index c462862ca..b32ad8535 100644 --- a/tslang/test/tester/CMakeLists.txt +++ b/tslang/test/tester/CMakeLists.txt @@ -468,6 +468,8 @@ tslang_add_test(NAME test-compile-00-own-param-exclusive-moves COMMAND test-runn tslang_add_test(NAME test-compile-00-object-dictionary COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_dictionary.ts") tslang_add_test(NAME test-compile-00-iterables COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00iterables.ts") tslang_add_test(NAME test-compile-00-generator-typed-yield COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_typed_yield.ts") +tslang_add_test(NAME test-compile-00-object-method-arity COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts") +tslang_add_test(NAME test-compile-00-generator-next-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts") tslang_add_test(NAME test-compile-00-constructor-type-value COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-compile-00-string-concat-chain COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00string_concat_chain.ts") tslang_add_test(NAME test-compile-00-arrays COMMAND test-runner "${PROJECT_SOURCE_DIR}/test/tester/tests/00array.ts") @@ -1300,6 +1302,12 @@ tslang_add_test(NAME test-jit-00-iterables COMMAND test-runner -jit "${PROJECT_S tslang_add_test(NAME test-jit-rc-00-iterables COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00iterables.ts") tslang_add_test(NAME test-jit-00-generator-typed-yield COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_typed_yield.ts") tslang_add_test(NAME test-jit-rc-00-generator-typed-yield COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_typed_yield.ts") +tslang_add_test(NAME test-jit-00-object-method-arity COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts") +tslang_add_test(NAME test-jit-rc-00-object-method-arity COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts") +tslang_add_test(NAME test-jit-own-00-object-method-arity COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00object_method_arity.ts") +tslang_add_test(NAME test-jit-00-generator-next-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts") +tslang_add_test(NAME test-jit-rc-00-generator-next-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts") +tslang_add_test(NAME test-jit-own-00-generator-next-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00generator_next_value.ts") tslang_add_test(NAME test-jit-00-constructor-type-value COMMAND test-runner -jit "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-jit-rc-00-constructor-type-value COMMAND test-runner -jit -mm=rc "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") tslang_add_test(NAME test-jit-own-00-constructor-type-value COMMAND test-runner -jit -mm=own "${PROJECT_SOURCE_DIR}/test/tester/tests/00constructor_type_value.ts") diff --git a/tslang/test/tester/tests/00generator_next_value.ts b/tslang/test/tester/tests/00generator_next_value.ts new file mode 100644 index 000000000..c7470c7b5 --- /dev/null +++ b/tslang/test/tester/tests/00generator_next_value.ts @@ -0,0 +1,34 @@ +// next(value) of a generator typed Generator: the value is what the yield it resumes +// evaluates to, a TNext (its default when next() is given none). A generator whose next() takes no value is +// a Generator too, and a for...of calls next() with none. +type IterateResult = { value: T, done: boolean }; +interface Generator { + next: (value?: TNext) => IterateResult; +} + +function* echo(): Generator { + let last = "start"; + while (true) { + const got = yield last; + last = "got " + got; + } +} + +function* counter(): Generator { + for (let i = 0; i < 3; i++) yield i; +} + +function main() { + const e = echo(); + print(e.next().value); + print(e.next("a").value); + print(e.next("b").value); + + for (const v of counter()) print(v); + + const c = counter(); + print(c.next(123).value); + + print("done."); +} + diff --git a/tslang/test/tester/tests/00object_method_arity.ts b/tslang/test/tester/tests/00object_method_arity.ts new file mode 100644 index 000000000..301cbe6ec --- /dev/null +++ b/tslang/test/tester/tests/00object_method_arity.ts @@ -0,0 +1,23 @@ +// An object-literal method given as an interface's function member of another arity: next() for a member +// next(value?) (the value ignored, as JavaScript does), next(v?) for next() (v undefined). Was +// "can't assign '() => ...' to '(p1: number | undefined) => ...': it takes 0 argument(s), 2 expected". +type R = { value: number, done: boolean }; +interface WithArgField { + next: (value?: number) => R; +} +interface WithArgMethod { + next(value?: number): R; +} +interface NoArgField { + next: () => R; +} + +function main() { + const a: WithArgField = { next() { return { value: 1, done: false }; } }; + print(a.next(3).value); + const b: WithArgMethod = { next() { return { value: 2, done: false }; } }; + print(b.next(3).value); + const c: NoArgField = { next(v?: number) { return { value: v === undefined ? 30 : v, done: false }; } }; + print(c.next().value); + print("done."); +}