mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-25 07:22:33 -05:00
67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
/**
|
|
* @fileoverview
|
|
* Object representing a Scratch variable.
|
|
*/
|
|
|
|
const uid = require('../util/uid');
|
|
|
|
class Variable {
|
|
/**
|
|
* @param {string} id Id of the variable.
|
|
* @param {string} name Name of the variable.
|
|
* @param {string} type Type of the variable, one of '' or 'list'
|
|
* @param {boolean} isCloud Whether the variable is stored in the cloud.
|
|
* @constructor
|
|
*/
|
|
constructor (id, name, type, isCloud) {
|
|
this.id = id || uid();
|
|
this.name = name;
|
|
this.type = type;
|
|
this.isCloud = isCloud;
|
|
switch (this.type) {
|
|
case Variable.SCALAR_TYPE:
|
|
this.value = 0;
|
|
break;
|
|
case Variable.LIST_TYPE:
|
|
this.value = [];
|
|
break;
|
|
case Variable.BROADCAST_MESSAGE_TYPE:
|
|
this.value = this.name;
|
|
break;
|
|
default:
|
|
throw new Error(`Invalid variable type: ${this.type}`);
|
|
}
|
|
}
|
|
|
|
toXML () {
|
|
return `<variable type="${this.type}" id="${this.id}">${this.name}</variable>`;
|
|
}
|
|
|
|
/**
|
|
* Type representation for scalar variables.
|
|
* This is currently represented as ''
|
|
* for compatibility with blockly.
|
|
* @const {string}
|
|
*/
|
|
static get SCALAR_TYPE () {
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Type representation for list variables.
|
|
* @const {string}
|
|
*/
|
|
static get LIST_TYPE () {
|
|
return 'list';
|
|
}
|
|
|
|
/**
|
|
* Type representation for list variables.
|
|
* @const {string}
|
|
*/
|
|
static get BROADCAST_MESSAGE_TYPE () {
|
|
return 'broadcast_msg';
|
|
}
|
|
}
|
|
|
|
module.exports = Variable;
|