Skip to content

Reproduce browser elements with this compiler #231

Closed

Description

@Sinfolke

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?

Activity

  1. ASDAlexander77 commented on Jul 14, 2026

    @ASDAlexander77
    Owner

    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

  2. Sinfolke commented on Jul 14, 2026

    @Sinfolke
    Author

    I've made some initial objects but it seg faults:

    [1/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/HTMLElement.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/HTMLElement.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/HTMLElement.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/HTMLElement.ts
    Segmentation fault (core dumped)
    [2/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/CssStyleDeclaration.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/CssStyleDeclaration.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/CssStyleDeclaration.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/CssStyleDeclaration.ts
    Segmentation fault (core dumped)
    [3/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/Element.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/Element.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/Element.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/Element.ts
    Segmentation fault (core dumped)
    [4/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/Event.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/Event.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/Event.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/Event.ts
    Segmentation fault (core dumped)
    [5/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/Node.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/Node.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/Node.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/Node.ts
    Segmentation fault (core dumped)
    [6/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/Document.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/Document.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/Document.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/Document.ts
    Segmentation fault (core dumped)
    
  3. Sinfolke commented on Jul 14, 2026

    @Sinfolke
    Author

    BrowserLib.zip

    You may review if needed. Hope this helps improve compiler

  4. ASDAlexander77 commented on Jul 14, 2026

    @ASDAlexander77
    Owner

    BrowserLib.zip

    You may review if needed. Hope this helps improve compiler

    Yes, it is good I will have a look, or better to say claude code will do the fix :)

  5. ASDAlexander77 commented on Jul 14, 2026

    @ASDAlexander77
    Owner

    I've made some initial objects but it seg faults:

    [1/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/HTMLElement.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/HTMLElement.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/HTMLElement.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/HTMLElement.ts
    Segmentation fault (core dumped)
    [2/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/CssStyleDeclaration.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/CssStyleDeclaration.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/CssStyleDeclaration.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/CssStyleDeclaration.ts
    Segmentation fault (core dumped)
    [3/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/Element.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/Element.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/Element.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/Element.ts
    Segmentation fault (core dumped)
    [4/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/Event.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/Event.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/Event.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/Event.ts
    Segmentation fault (core dumped)
    [5/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/Node.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/Node.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/Node.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/Node.ts
    Segmentation fault (core dumped)
    [6/14] Building TSLANG object CMakeFiles/BrowserLib.dir/runtime/object/Window/Document.ts.o
    FAILED: [code=139] CMakeFiles/BrowserLib.dir/runtime/object/Window/Document.ts.o 
    /opt/tslang/tslang --di --opt_level=0 --default-lib-path=/opt/tslang --emit=obj --export=all -o=CMakeFiles/BrowserLib.dir/runtime/object/Window/Document.ts.o /home/ura/Programming/BrowserLib/runtime/object/Window/Document.ts
    Segmentation fault (core dumped)
    

    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

  6. ASDAlexander77 commented on Jul 14, 2026

    @ASDAlexander77
    Owner

    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

  7. Sinfolke commented on Jul 15, 2026

    @Sinfolke
    Author

    It breaks for declare function in .d.ts files ... on linux

     ()
    (gdb) bt
    #0  0x0000555557886323 in (anonymous namespace)::MLIRGenImpl::addClassDeclarationToExport(std::shared_ptr<ClassInfo>) ()
    #1  0x0000555557886bca in (anonymous namespace)::MLIRGenImpl::addTypeDeclarationToExportNoCheck(mlir::Type) [clone .isra.0] ()
    #2  0x00005555578862b1 in (anonymous namespace)::MLIRGenImpl::addDependancyTypesToExport(mlir::Type) ()
    #3  0x00005555578f4ceb in (anonymous namespace)::MLIRGenImpl::mlirGen(ptr_t<ts::data::Statement>, (anonymous namespace)::GenContext const&) ()
    #4  0x00005555578f6087 in (anonymous namespace)::MLIRGenImpl::mlirGen(ts::data::NodeArray<ptr_t<ts::data::Statement> >, (anonymous namespace)::GenContext const&) ()
    #5  0x0000555557927159 in (anonymous namespace)::MLIRGenImpl::mlirCodeGenModule(ptr_t<ts::data::SourceFile>, std::vector<ptr_t<ts::data::SourceFile>, std::allocator<ptr_t<ts::data::SourceFile> > >, bool, bool) ()
    #6  0x0000555557929c4a in (anonymous namespace)::MLIRGenImpl::mlirGenSourceFile(ptr_t<ts::data::SourceFile>, std::vector<ptr_t<ts::data::SourceFile>, std::allocator<ptr_t<ts::data::SourceFile> > >) ()
    #7  0x0000555557929f92 in typescript::mlirGenFromMainSource(mlir::MLIRContext const&, llvm::StringRef const&, llvm::SourceMgr const&, CompileOptions&) ()
    #8  0x00005555563a9df0 in compileTypeScriptFileIntoMLIR(mlir::MLIRContext&, llvm::StringRef, llvm::SourceMgr&, mlir::OwningOpRef<mlir::ModuleOp>&, CompileOptions&) ()
    #9  0x0000555555e4e3a8 in main ()
    

    BrowserLib.zip

  8. ASDAlexander77 commented on Sep 27, 2026

    @ASDAlexander77
    Owner

    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.

    Already fixed on main

    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="…" />.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions