boots-edu / textbook

The textbook for the Boots curriculum
0 stars 0 forks source link

Rewrite typescript code runner #2

Closed acbart closed 1 week ago

acbart commented 3 weeks ago

Currently, the ts_runner.js file is an abomination of hacked-together snippets. That worked fine for the Tome, but we can do much better. We should create some TypeScript files that get packed together in the deploy workflow. This will make everything considerably easier, since we can get more native support for the TypeScript execution rather than loading some shady library service directly.

acbart commented 1 week ago

The basic version of the new execution engine is now live on the site, thanks to c3589779c5b065bce5ad517686083c71e886ac41

When the page loads, we find all the highlighted TypeScript code blocks, and attach a pencil button in the topright corner. Clicking the button replaces the code block with a CodeMirror instance that can be freely edited. Below the editor is a Run button and, secretly, an iframe. Clicking the button will:

  1. Compile the code locally from TypeScript to JavaScript, while also:
    1. Removing any export keywords
    2. Collecting some general information about the class
  2. Assemble the code with some header that provides a basic implementation of test, describe, and expect, and generally wraps the needed infrastructure to safely sandbox and run the code.
  3. Sends the code to the iframe using postMessage
  4. Executes the code in a Web Worker
  5. The web worker posts messages back to the iframe, which proxies them back to the main window, where they are captured by the appropriate ExecutionEngine
  6. The results are interpreted and logged to a ConTodo console below the code editor

Eventually, I plan to get Webz working in here too (#7), and also show students the values and types of any variables they declared (#5). But this commit does have the basic support for unit tests (#6).