Skip to content

Commit

Permalink
🔧 chore: set global Scratch object while loading unsandboxed extension
Browse files Browse the repository at this point in the history
Signed-off-by: SimonShiki <sinangentoo@gmail.com>
  • Loading branch information
SimonShiki committed Oct 30, 2023
1 parent 58035eb commit 49faa77
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference path="node_modules/@turbowarp/types/index.d.ts" />
// <reference path="./loader/loader" />
// <reference path="./loader/make-ctx" />

declare interface Window {
Blockly?: Partial<ScratchBlocks>;
Expand All @@ -17,4 +18,5 @@ declare interface Window {
>;
openFrontend(): void;
};
Scratch?: Context;
}
21 changes: 7 additions & 14 deletions src/loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ class ChibiLoader {
*/
private nextExtensionWorker = 0;

/**
* Whether Scratch object should be passed inline.
* @type {boolean}
*/
private inlinedCtx = false;

/**
* FIFO queue of extensions which have been requested but not yet loaded in a worker,
* along with promise resolution functions to call once the worker is ready or failed.
Expand All @@ -71,14 +65,6 @@ class ChibiLoader {

constructor (vm: VM) {
this.vm = vm;
this.inlinedCtx = typeof window.Scratch === 'object';
if (!this.inlinedCtx) {
window.Scratch = makeCtx(this.vm);
} else {
warn(
'A Scratch instance already exists in the current environment, so it will be passed inline for unsandboxed extension.'
);
}
dispatch.setService('loader', this).catch((e: Error) => {
error(`ChibiLoader was unable to register extension service: ${JSON.stringify(e)}`);
});
Expand Down Expand Up @@ -108,6 +94,13 @@ class ChibiLoader {
const originalScript = await response.text();
const closureFunc = new Function('Scratch', originalScript);
const ctx = makeCtx(this.vm);
// Provide compatibility support for some extensions
// Nevertheless, it is a very bad behavior for Chibi to directly read the Scratch
// object on the window, because the extension should not depend on unknown external
// environment.
if (!('Scratch' in window)) {
window.Scratch = ctx;
}
ctx.extensions.register = (extensionObj: ExtensionClass) => {
const extensionInfo = extensionObj.getInfo();
this._registerExtensionInfo(extensionObj, extensionInfo, ext);
Expand Down

0 comments on commit 49faa77

Please sign in to comment.