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
105 changes: 105 additions & 0 deletions scripts/check_declarations.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Checks that every function the declarations (src\lib.d.ts and what it references) give the library's
# users has the signature src\lib.ts compiles to. A program is compiled against the declarations, and
# calls a function by its declared type; lib.ts is compiled on its own, and defines it with its own.
# Where the two differ the call passes or reads the wrong things: `Number.toString` returned nothing
# while lib.d.ts said string, and a program printing its result crashed.
# .\scripts\check_declarations.ps1 the compiler of the sibling build tree, or $Env:TOOL_PATH
# Exit code 1 lists each function whose declared and compiled signatures differ.
param([string]$Tool)

$ErrorActionPreference = "Stop"

$Root = Split-Path $PSScriptRoot -Parent
if (-not $Tool) {
$ToolPath = if ($Env:TOOL_PATH) { $Env:TOOL_PATH } else { "$Root\..\TypeScriptCompiler\__build\tslang\windows-msbuild-2026-release\bin" }
$Tool = Join-Path $ToolPath "tslang.exe"
}

if (-not (Test-Path $Tool)) {
Write-Error "tslang.exe not found at '$Tool': give -Tool or set TOOL_PATH"
}

# the declarations as a default library: a folder whose `defaultlib` is src
$Work = Join-Path ([System.IO.Path]::GetTempPath()) ("tslang-check-declarations-" + [System.Guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Path $Work | Out-Null
$Junction = Join-Path $Work "defaultlib"
New-Item -ItemType Junction -Path $Junction -Target (Join-Path $Root "src") | Out-Null

try {
$Program = Join-Path $Work "program.ts"
Set-Content -Path $Program -Value "function main() {`n}"

# the paths the environment may point at an installed library would win over the folder given
$Env:TSLANG_LIB_PATH = $null
$Env:DEFAULT_LIB_PATH = $null

$DeclaredMlir = Join-Path $Work "declared.mlir"
$CompiledMlir = Join-Path $Work "compiled.mlir"
& $Tool --emit=mlir --nowarn --default-lib-path="$Work" $Program 2> $DeclaredMlir
if ($LASTEXITCODE -ne 0) {
Get-Content $DeclaredMlir | Select-Object -First 20
Write-Error "the declarations do not compile"
}

# as build_core.bat compiles it
& $Tool --emit=mlir --export=none --nowarn --no-default-lib (Join-Path $Root "src\lib.ts") 2> $CompiledMlir
if ($LASTEXITCODE -ne 0) {
Get-Content $CompiledMlir | Select-Object -First 20
Write-Error "src\lib.ts does not compile"
}

function Get-Signatures([string]$mlir) {
$signatures = @{}
foreach ($line in [System.IO.File]::ReadLines($mlir)) {
if ($line -match '^\s*ts\.Func (?:private )?@(\S+) (!ts\.func<.*>)\s*(\{|attributes|$)') {
if (-not $signatures.ContainsKey($Matches[1])) {
$signatures[$Matches[1]] = $Matches[2].Trim()
}
}
}

return $signatures
}

# spellings of one signature: `int` and `s32` (both a 32-bit integer), a `void` result and none, and
# `T | null` of a class or an opaque pointer and T (a pointer that may be null either way)
function Get-Normalized([string]$signature) {
$signature = $signature -replace '\bsi(\d+)\b', 'i$1'
$signature = $signature -replace ', !ts\.void, (true|false)>$', ', , $1>'
$signature = $signature -replace '!ts\.union<(!ts\.class<@[^,>]+, !ts\.class_storage<@[^>]+>>|!ts\.opaque), !ts\.null>', '$1'
return $signature
}

$declared = Get-Signatures $DeclaredMlir
$compiled = Get-Signatures $CompiledMlir

$checked = 0
$mismatches = @()
foreach ($name in ($declared.Keys | Sort-Object)) {
if (-not $compiled.ContainsKey($name)) {
continue
}

$checked++
if ((Get-Normalized $declared[$name]) -ne (Get-Normalized $compiled[$name])) {
$mismatches += "$name`n declared: $($declared[$name])`n compiled: $($compiled[$name])"
}
}

Write-Host "checked $checked functions declared and compiled"
if ($mismatches.Count -gt 0) {
$mismatches | ForEach-Object { Write-Host $_ }
Write-Host "$($mismatches.Count) declaration(s) differ from what lib.ts compiles to"
exit 1
}

Write-Host "every declaration matches"
}
finally {
# the junction only, not what it points at
if (Test-Path $Junction) {
[System.IO.Directory]::Delete($Junction, $false)
}

Remove-Item -Recurse -Force $Work
}
4 changes: 2 additions & 2 deletions src/lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ declare class MatchResults

private lastIndex: index;

constructor(match?: Opaque | null, lastIndex?: index);
constructor(match?: Opaque | null, hasIndices?: boolean, lastIndex?: index);

[index: number]: string;

Expand Down Expand Up @@ -422,7 +422,7 @@ declare class String {

substring(this: string, indexStart: int, indexEnd?: int): string;

startsWith(this: string, searchString: string, position?: int);
startsWith(this: string, searchString: string, position?: int): boolean;

toLocaleLowerCase(this: string, locale?: string): string;

Expand Down
7 changes: 7 additions & 0 deletions tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ if ($Model) {
Write-Host "Memory model: $Model"
}

# what a program is compiled against has the signatures lib.ts compiles to (one memory model is enough:
# the signatures do not depend on it)
pwsh -NoProfile -File "$PSScriptRoot\scripts\check_declarations.ps1"
if ($LASTEXITCODE -ne 0) {
$allFailedTests += "declarations: scripts\check_declarations.ps1"
}

if ($Env:TSLANG_ARCH -eq "x86") {
# The JIT (jit.cpp) refuses x86 targets by design; only the two compile passes apply.
Write-Host "jit is host-only; skipped for x86" -ForegroundColor Yellow
Expand Down
11 changes: 11 additions & 0 deletions tests/string_object_starts_with.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// A String object's startsWith gives a boolean: lib.d.ts declared it with no result type, so a call
// had no value (`const r = s.startsWith("he")` left `r` unresolved) while lib.ts returned one.
// scripts\check_declarations.ps1 found it, comparing each declaration with what lib.ts compiles to.
function main() {
const s = new String("hello");
const r = s.startsWith("he");
assert(r, "a prefix");
assert(!s.startsWith("lo"), "not a prefix");
assert(s.startsWith("lo", 3), "from a position");
console.log("ALL DONE");
}