src/virtual-machine.js
/**
     * Load a project from a Scratch JSON representation.
     * @param {string} json JSON string representing a project.
     * @returns {Promise} Promise that resolves after the project has loaded
     */
Load a project from a Scratch JSON representation.
@param {string} json JSON string representing a project.
@returns {Promise} Promise that resolves after the project has loaded
Load a project from a Scratch JSON representation . @param { string } json JSON string representing a project . @returns { Promise } Promise that resolves after the project has loaded
fromJSON (json) {
        this.clear();
        if (typeof json !== 'string' && typeof json !== 'object') {
            throw new Error('Failed to parse project. Invalid type supplied to fromJSON.');
        }
        let deserializer;
        let validatedProject;
        const possibleSb3 = typeof json === 'string' ? JSON.parse(json) : json;
        if ((typeof possibleSb3.meta !== 'undefined') && (typeof possibleSb3.meta.semver !== 'undefined')) {
            deserializer = sb3;
            validatedProject = possibleSb3;
        } else {
            const possibleSb2 = typeof json === 'object' ? JSON.stringify(json) : json;
            validate(possibleSb2, (err, project) => {
                if (err) {
                    throw new Error(
                        `The given project could not be validated, parsing failed with error: ${JSON.stringify(err)}`);
                } else {
                    deserializer = sb2;
                    validatedProject = project;
                }
            });
        }
        return deserializer.deserialize(validatedProject, this.runtime)
            .then(({targets, extensions}) =>
                this.installTargets(targets, extensions, true));
    }
fromJSON ( json ) { this . clear ( ) ; if ( typeof json !== 'string' && typeof json !== 'object' ) { throw new Error ( 'Failed to parse project. Invalid type supplied to fromJSON.' ) ; } let deserializer ; let validatedProject ; const possibleSb3 = typeof json === 'string' ? JSON . parse ( json ) : json ; if ( ( typeof possibleSb3 . meta !== 'undefined' ) && ( typeof possibleSb3 . meta . semver !== 'undefined' ) ) { deserializer = sb3 ; validatedProject = possibleSb3 ; } else { const possibleSb2 = typeof json === 'object' ? JSON . stringify ( json ) : json ; validate ( possibleSb2 , ( err , project ) => { if ( err ) { throw new Error ( ` ${ JSON . stringify ( err ) } ` ) ; } else { deserializer = sb2 ; validatedProject = project ; } } ) ; } return deserializer . deserialize ( validatedProject , this . runtime ) . then ( ( { targets , extensions } ) => this . installTargets ( targets , extensions , true ) ) ; }
Load a project from a Scratch JSON representation.
Load a project from a Scratch JSON representation .
// Clear the current runtime // Validate & parse // Establish version, deserialize, and load into runtime // @todo Support Scratch 1.4 // @todo This is an extremely naïve / dangerous way of determining version. //       See `scratch-parser` for a more sophisticated validation //       methodology that should be adapted for use here // scratch-parser expects a json string or a buffer