Skip to content

JS extension: initialize runtime.extensionRuntimeOptions and guard unsandbox flag to prevent TypeError #15

@coderabbitai

Description

@coderabbitai

Summary
When loading the JavaScript/SPjavascriptV2 extension from PR TurboWarp#159, VM logs:
TypeError: can't access property "javascriptUnsandboxed", this.runtime.extensionRuntimeOptions is undefined

This happens as soon as the extension tries to read runtime.extensionRuntimeOptions.javascriptUnsandboxed.

Backlinks

Steps to reproduce

  1. Use scratch-gui (PR More CONDITIONAL and LOOP stuff TurboWarp/scratch-vm#159) with the JS extension enabled.
  2. Open the extension library and add the JavaScript/SPjavascriptV2 extension.
  3. Observe VM console warnings/errors referencing javascriptUnsandboxed and undefined extensionRuntimeOptions.

Actual behavior

  • Accessing this.runtime.extensionRuntimeOptions.javascriptUnsandboxed throws because extensionRuntimeOptions is undefined.

Root cause

  • In our VM fork, extensionRuntimeOptions is never initialized before extensions load.
  • Some extension code assumes it exists and directly reads javascriptUnsandboxed.

Fix (Option A: initialize in Runtime constructor)
File: src/engine/runtime.js (constructor)

 class Runtime {
   constructor() {
+    // Ensure extensions can safely read runtime options bag.
+    this.extensionRuntimeOptions = this.extensionRuntimeOptions || {};
     // ... existing constructor code ...
   }
 }

Fix (Option B: initialize in VirtualMachine after Runtime creation, before loading extensions)
File: src/virtual-machine.js (constructor/init path)

- this.runtime = new Runtime();
+ this.runtime = new Runtime();
+ // Guard: ensure the options bag exists before any extension reads it.
+ this.runtime.extensionRuntimeOptions = this.runtime.extensionRuntimeOptions || {};

Fix (defensive read in the JS extension; belongs in the extension repo, but documenting here)

- const unsandboxed = this.runtime.extensionRuntimeOptions.javascriptUnsandboxed;
+ const unsandboxed = this.runtime.extensionRuntimeOptions?.javascriptUnsandboxed === true;

Acceptance criteria

  • No VM warnings/errors about extensionRuntimeOptions being undefined when the JS extension loads.
  • Reading javascriptUnsandboxed never throws, regardless of default config.
  • Existing extensions keep working.

Notes

  • This brings behavior in line with PenguinMod/TurboWarp forks that initialize Runtime.extensionRuntimeOptions to an object.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions