fix!: export extension support types

This commit is contained in:
Christopher Willis-Ford 2024-03-11 10:05:50 -07:00
parent 48e1543ff9
commit 4ebcd1f894
2 changed files with 32 additions and 33 deletions

View file

@ -1,3 +1,10 @@
const VirtualMachine = require('./virtual-machine'); import VirtualMachine from './virtual-machine.js';
module.exports = VirtualMachine; import ArgumentType from './extension-support/argument-type.js';
import BlockType from './extension-support/block-type.js';
export default VirtualMachine;
export {
ArgumentType,
BlockType
};

View file

@ -1,28 +1,30 @@
import EventEmitter from 'events';
import JSZip from 'jszip';
import {Buffer} from 'buffer';
import centralDispatch from './dispatch/central-dispatch.js';
import ExtensionManager from './extension-support/extension-manager.js';
import log from './util/log.js';
import MathUtil from './util/math-util.js';
import Runtime from './engine/runtime.js';
import StringUtil from './util/string-util.js';
import * as formatMessage from 'format-message';
import Variable from './engine/variable.js';
import newBlockIds from './util/new-block-ids.js';
import {loadCostume} from './import/load-costume.js';
import {loadSound} from './import/load-sound.js';
import {serializeSounds, serializeCostumes} from './serialization/serialize-assets.js';
import 'canvas-toBlob';
import validate from 'scratch-parser';
import * as scratchSb1Converter from 'scratch-sb1-converter';
import sb3 from './serialization/sb3.js';
import sb2 from './serialization/sb2.js';
let _TextEncoder; let _TextEncoder;
if (typeof TextEncoder === 'undefined') { if (typeof TextEncoder === 'undefined') {
_TextEncoder = require('text-encoding').TextEncoder; _TextEncoder = require('text-encoding').TextEncoder;
} else { } else {
_TextEncoder = TextEncoder; _TextEncoder = TextEncoder;
} }
const EventEmitter = require('events');
const JSZip = require('jszip');
const Buffer = require('buffer').Buffer;
const centralDispatch = require('./dispatch/central-dispatch');
const ExtensionManager = require('./extension-support/extension-manager');
const log = require('./util/log');
const MathUtil = require('./util/math-util');
const Runtime = require('./engine/runtime');
const StringUtil = require('./util/string-util');
const formatMessage = require('format-message');
const Variable = require('./engine/variable');
const newBlockIds = require('./util/new-block-ids');
const {loadCostume} = require('./import/load-costume.js');
const {loadSound} = require('./import/load-sound.js');
const {serializeSounds, serializeCostumes} = require('./serialization/serialize-assets');
require('canvas-toBlob');
const RESERVED_NAMES = ['_mouse_', '_stage_', '_edge_', '_myself_', '_random_']; const RESERVED_NAMES = ['_mouse_', '_stage_', '_edge_', '_myself_', '_random_'];
@ -317,7 +319,6 @@ class VirtualMachine extends EventEmitter {
} }
const validationPromise = new Promise((resolve, reject) => { const validationPromise = new Promise((resolve, reject) => {
const validate = require('scratch-parser');
// The second argument of false below indicates to the validator that the // The second argument of false below indicates to the validator that the
// input should be parsed/validated as an entire project (and not a single sprite) // input should be parsed/validated as an entire project (and not a single sprite)
validate(input, false, (error, res) => { validate(input, false, (error, res) => {
@ -326,7 +327,7 @@ class VirtualMachine extends EventEmitter {
}); });
}) })
.catch(error => { .catch(error => {
const {SB1File, ValidationError} = require('scratch-sb1-converter'); const {SB1File, ValidationError} = scratchSb1Converter;
try { try {
const sb1 = new SB1File(input); const sb1 = new SB1File(input);
@ -462,7 +463,6 @@ class VirtualMachine extends EventEmitter {
* @return {string} Serialized state of the runtime. * @return {string} Serialized state of the runtime.
*/ */
toJSON (optTargetId) { toJSON (optTargetId) {
const sb3 = require('./serialization/sb3');
return StringUtil.stringify(sb3.serialize(this.runtime, optTargetId)); return StringUtil.stringify(sb3.serialize(this.runtime, optTargetId));
} }
@ -495,11 +495,9 @@ class VirtualMachine extends EventEmitter {
const deserializePromise = function () { const deserializePromise = function () {
const projectVersion = projectJSON.projectVersion; const projectVersion = projectJSON.projectVersion;
if (projectVersion === 2) { if (projectVersion === 2) {
const sb2 = require('./serialization/sb2');
return sb2.deserialize(projectJSON, runtime, false, zip); return sb2.deserialize(projectJSON, runtime, false, zip);
} }
if (projectVersion === 3) { if (projectVersion === 3) {
const sb3 = require('./serialization/sb3');
return sb3.deserialize(projectJSON, runtime, zip); return sb3.deserialize(projectJSON, runtime, zip);
} }
// TODO: reject with an Error (possible breaking API change!) // TODO: reject with an Error (possible breaking API change!)
@ -589,7 +587,6 @@ class VirtualMachine extends EventEmitter {
} }
const validationPromise = new Promise((resolve, reject) => { const validationPromise = new Promise((resolve, reject) => {
const validate = require('scratch-parser');
// The second argument of true below indicates to the parser/validator // The second argument of true below indicates to the parser/validator
// that the given input should be treated as a single sprite and not // that the given input should be treated as a single sprite and not
// an entire project // an entire project
@ -632,8 +629,6 @@ class VirtualMachine extends EventEmitter {
*/ */
_addSprite2 (sprite, zip) { _addSprite2 (sprite, zip) {
// Validate & parse // Validate & parse
const sb2 = require('./serialization/sb2');
return sb2.deserialize(sprite, this.runtime, true, zip) return sb2.deserialize(sprite, this.runtime, true, zip)
.then(({targets, extensions}) => .then(({targets, extensions}) =>
this.installTargets(targets, extensions, false)); this.installTargets(targets, extensions, false));
@ -647,7 +642,6 @@ class VirtualMachine extends EventEmitter {
*/ */
_addSprite3 (sprite, zip) { _addSprite3 (sprite, zip) {
// Validate & parse // Validate & parse
const sb3 = require('./serialization/sb3');
return sb3 return sb3
.deserialize(sprite, this.runtime, zip, true) .deserialize(sprite, this.runtime, zip, true)
.then(({targets, extensions}) => this.installTargets(targets, extensions, false)); .then(({targets, extensions}) => this.installTargets(targets, extensions, false));
@ -1247,8 +1241,6 @@ class VirtualMachine extends EventEmitter {
* @return {!Promise} Promise that resolves when the extensions and blocks have been added. * @return {!Promise} Promise that resolves when the extensions and blocks have been added.
*/ */
shareBlocksToTarget (blocks, targetId, optFromTargetId) { shareBlocksToTarget (blocks, targetId, optFromTargetId) {
const sb3 = require('./serialization/sb3');
const copiedBlocks = JSON.parse(JSON.stringify(blocks)); const copiedBlocks = JSON.parse(JSON.stringify(blocks));
newBlockIds(copiedBlocks); newBlockIds(copiedBlocks);
const target = this.runtime.getTargetById(targetId); const target = this.runtime.getTargetById(targetId);
@ -1583,4 +1575,4 @@ class VirtualMachine extends EventEmitter {
} }
} }
module.exports = VirtualMachine; export default VirtualMachine;