2016-06-24 11:14:22 -04:00
|
|
|
var Promise = require('promise');
|
|
|
|
|
2016-06-09 14:29:07 -04:00
|
|
|
function Scratch3OperatorsBlocks(runtime) {
|
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the block primitives implemented by this package.
|
|
|
|
* @return {Object.<string, Function>} Mapping of opcode to Function.
|
|
|
|
*/
|
|
|
|
Scratch3OperatorsBlocks.prototype.getPrimitives = function() {
|
|
|
|
return {
|
|
|
|
'math_number': this.number,
|
2016-06-10 10:36:05 -04:00
|
|
|
'text': this.text,
|
2016-06-14 18:08:41 -04:00
|
|
|
'operator_add': this.add,
|
2016-06-20 14:42:06 -04:00
|
|
|
'operator_subtract': this.subtract,
|
|
|
|
'operator_multiply': this.multiply,
|
|
|
|
'operator_divide': this.divide,
|
|
|
|
'operator_lt': this.lt,
|
2016-06-17 15:10:28 -04:00
|
|
|
'operator_equals': this.equals,
|
2016-06-20 14:42:06 -04:00
|
|
|
'operator_gt': this.gt,
|
|
|
|
'operator_and': this.and,
|
|
|
|
'operator_or': this.or,
|
2016-06-20 14:42:56 -04:00
|
|
|
'operator_not': this.not,
|
2016-06-17 15:10:28 -04:00
|
|
|
'operator_random': this.random
|
2016-06-09 14:29:07 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-06-10 10:36:05 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.number = function (args) {
|
2016-06-29 20:56:55 -04:00
|
|
|
var num = Number(args.NUM);
|
|
|
|
if (num !== num) {
|
|
|
|
// NaN
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return num;
|
2016-06-09 14:29:07 -04:00
|
|
|
};
|
|
|
|
|
2016-06-10 10:36:05 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.text = function (args) {
|
|
|
|
return String(args.TEXT);
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.add = function (args) {
|
2016-06-09 14:29:07 -04:00
|
|
|
return args.NUM1 + args.NUM2;
|
|
|
|
};
|
|
|
|
|
2016-06-20 14:42:06 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.subtract = function (args) {
|
|
|
|
return args.NUM1 - args.NUM2;
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.multiply = function (args) {
|
|
|
|
return args.NUM1 * args.NUM2;
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.divide = function (args) {
|
|
|
|
return args.NUM1 / args.NUM2;
|
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.lt = function (args) {
|
2016-06-24 10:52:49 -04:00
|
|
|
return Boolean(args.OPERAND1 < args.OPERAND2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
2016-06-10 10:36:05 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.equals = function (args) {
|
2016-06-24 10:52:49 -04:00
|
|
|
return Boolean(args.OPERAND1 == args.OPERAND2);
|
2016-06-10 10:36:05 -04:00
|
|
|
};
|
|
|
|
|
2016-06-20 14:42:06 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.gt = function (args) {
|
2016-06-24 10:52:49 -04:00
|
|
|
return Boolean(args.OPERAND1 > args.OPERAND2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.and = function (args) {
|
2016-06-24 10:52:49 -04:00
|
|
|
return Boolean(args.OPERAND1 && args.OPERAND2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.or = function (args) {
|
2016-06-24 10:52:49 -04:00
|
|
|
return Boolean(args.OPERAND1 || args.OPERAND2);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Scratch3OperatorsBlocks.prototype.not = function (args) {
|
2016-06-24 10:52:49 -04:00
|
|
|
return Boolean(!args.OPERAND);
|
2016-06-20 14:42:06 -04:00
|
|
|
};
|
|
|
|
|
2016-06-30 16:57:12 -04:00
|
|
|
Scratch3OperatorsBlocks.prototype.random = function (args) {
|
2016-06-17 15:10:28 -04:00
|
|
|
// As a demo, this implementation of random returns after 1 second of yield.
|
|
|
|
// @todo Match Scratch 2.0 implementation with int-truncation.
|
|
|
|
// See: http://bit.ly/1Qc0GzC
|
2016-06-24 11:14:22 -04:00
|
|
|
var examplePromise = new Promise(function(resolve) {
|
|
|
|
setTimeout(function() {
|
|
|
|
var res = (Math.random() * (args.TO - args.FROM)) + args.FROM;
|
|
|
|
resolve(res);
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
return examplePromise;
|
2016-06-17 15:10:28 -04:00
|
|
|
};
|
|
|
|
|
2016-06-09 14:29:07 -04:00
|
|
|
module.exports = Scratch3OperatorsBlocks;
|