You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reproduce browser elements with this compiler #231
I'm thinking about project that contains browser environment as library. Window, document etc, classes are implemented. Not every single API, but the most common one. The API that is not implemented would use native browser API. The idea is to load lightweight custom browser environment, compile the javascript code into executable and run. This allows for advanced scraping with high speed.
What code could this compiler compile and what it would not?
Yes, I see, at this moment only simple "import" is working with file name. This kind of import is not implemented "import { hostSetInnerHTML } from "./API";" But strangly on windows it is not crashing
one more thing: "export declare" is not allowed. "declare" should be in .d.ts included file and .d.ts can be included using "/// <reference=file.d.ts />". Check default library with example how to include declarations
Thanks for the sources. They found several real compiler bugs. I compiled both zips with your flags (--di --opt_level=0 --emit=obj --export=all) against current main and fixed what crashed.
The segfault in addClassDeclarationToExport from your July 15 stack trace: exporting a declaration that referred to an unregistered class dereferenced a null pointer. It is guarded now.
Fixed in open PRs. These caused the segfaults in the first version (Event.ts, and every file that imports it). They crashed on Windows too, not only on Linux.
With both PRs applied, no file in either zip crashes. What is left are ordinary compile errors.
First version
if (this.head === null) return; does not narrow this.head for the code after it yet (Event.ts:85). Put the rest of the function in an else, or use if (this.head !== null) { … }.
Array.prototype.splice is not implemented yet (Node.ts:47).
Document.ts:52 (this.searchIdRecursive(current.childNodes[i], id)) fails with failed to legalize operation 'ts.SymbolCallInternal'. That is a compiler bug, and I'll look at it separately.
The circular import Node.ts ↔ TextNode.ts prints "can't resolve name: Node" while the compiler retries. Files that finish still produce their .o.
Second version (native.d.ts + /// <reference>)
No crashes. The failures are mostly in the sources:
Storage.ts and Timers.ts are missing their /// <reference path="…" />.
readNativeHandles comes from a NativeBridge.ts that isn't in the zip.
Crypto.ts does import "./Timer", but only Timer.d.ts exists.
Window.ts uses EventTarget without importing it.
JSON and String.fromCharCode are not supported yet.
EventTarget.ts: "can't find type by name: EventListenerCallback" inside lib.generics.ts, from a type alias used as a Map value type. That one may be a compiler bug too.
To your original question: code like the sources in tslang/test/tester/tests and in TypeScriptCompilerDefaultLib compiles. The first things you will run into are:
no JSON;
no splice;
narrowing only inside if / else / ?: / && blocks, not after an early return;
declarations go in a .d.ts pulled in with /// <reference path="…" />.
Activity
ASDAlexander77 commentedon Jul 14, 2026
It defenetly can compile code which present in tests folder and in the project https://github.com/ASDAlexander77/TypeScriptCompilerDefaultLib. There new examples of "fetch" command
Sinfolke commentedon Jul 14, 2026
I've made some initial objects but it seg faults:
Sinfolke commentedon Jul 14, 2026
BrowserLib.zip
You may review if needed. Hope this helps improve compiler
ASDAlexander77 commentedon Jul 14, 2026
Yes, it is good I will have a look, or better to say claude code will do the fix :)
ASDAlexander77 commentedon Jul 14, 2026
Yes, I see, at this moment only simple "import" is working with file name. This kind of import is not implemented "import { hostSetInnerHTML } from "./API";" But strangly on windows it is not crashing
ASDAlexander77 commentedon Jul 14, 2026
one more thing: "export declare" is not allowed. "declare" should be in .d.ts included file and .d.ts can be included using "/// <reference=file.d.ts />". Check default library with example how to include declarations
Sinfolke commentedon Jul 15, 2026
It breaks for declare function in .d.ts files ... on linux
BrowserLib.zip
Fix llvm_unreachable aborts resolving unknown field/method names in g…
ASDAlexander77 commentedon Sep 27, 2026
Thanks for the sources. They found several real compiler bugs. I compiled both zips with your flags (
--di --opt_level=0 --emit=obj --export=all) against currentmainand fixed what crashed.Already fixed on
mainimport { x } from "./API"andimport "./x"now work (Generate a referenced or imported file once; references beside their file; import aliases #371).export declare functionin a.tsfile compiles.addClassDeclarationToExportfrom your July 15 stack trace: exporting a declaration that referred to an unregistered class dereferenced a null pointer. It is guarded now.--difailing with "Failed to emit LLVM IR" (Give a catch copy thunk a debug scope of its own #361).Fixed in open PRs. These caused the segfaults in the first version (Event.ts, and every file that imports it). They crashed on Windows too, not only on Linux.
elsebranch leaked out of the branch. Afterif (this.head === null) { … } else { … }, a later read ofthis.headused freed memory.nullorundefinedwhen the object has a union type (if (h.next !== null)withh: Node | null) crashed.With both PRs applied, no file in either zip crashes. What is left are ordinary compile errors.
First version
if (this.head === null) return;does not narrowthis.headfor the code after it yet (Event.ts:85). Put the rest of the function in anelse, or useif (this.head !== null) { … }.Array.prototype.spliceis not implemented yet (Node.ts:47).this.searchIdRecursive(current.childNodes[i], id)) fails withfailed to legalize operation 'ts.SymbolCallInternal'. That is a compiler bug, and I'll look at it separately..o.Second version (native.d.ts +
/// <reference>)No crashes. The failures are mostly in the sources:
/// <reference path="…" />.readNativeHandlescomes from a NativeBridge.ts that isn't in the zip.import "./Timer", but onlyTimer.d.tsexists.EventTargetwithout importing it.JSONandString.fromCharCodeare not supported yet.lib.generics.ts, from a type alias used as aMapvalue type. That one may be a compiler bug too.To your original question: code like the sources in
tslang/test/tester/testsand in TypeScriptCompilerDefaultLib compiles. The first things you will run into are:JSON;splice;if/else/?:/&&blocks, not after an earlyreturn;.d.tspulled in with/// <reference path="…" />.Comparing a union's field with null or undefined no longer crashes (#376
Narrowing in an else branch ends with the branch
Narrowing in an else branch ends with the branch (#377)
A generic specialization made during an import's discovery is regener…
new of a generic class takes its type arguments from the receiver (#382)
Narrowing after an early exit; a narrowed field is read from the field (
An import's errors are the importer's to report
A field of a union of same-layout object types is read from the base …
An import's errors are the importer's to report (#381)
A field of a union of same-layout object types is read from the base …