| | const Cast = require('../util/cast'); |
| | class Scratch3ProcedureBlocks { |
| | constructor(runtime) { |
| | |
| | |
| | |
| | |
| | this.runtime = runtime; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | getPrimitives() { |
| | return { |
| | procedures_definition: this.definition, |
| | procedures_call: this.call, |
| | procedures_set: this.set, |
| | argument_reporter_string_number: this.argumentReporterStringNumber, |
| | argument_reporter_boolean: this.argumentReporterBoolean, |
| | argument_reporter_command: this.argumentReporterCommand |
| | }; |
| | } |
| |
|
| | definition() { |
| | |
| | } |
| |
|
| | call (args, util) { |
| | if (!util.stackFrame.executed) { |
| | const procedureCode = args.mutation.proccode; |
| | const paramNamesIdsAndDefaults = util.getProcedureParamNamesIdsAndDefaults(procedureCode); |
| |
|
| | |
| | |
| | |
| | if (paramNamesIdsAndDefaults === null) { |
| | return; |
| | } |
| |
|
| | const [paramNames, paramIds, paramDefaults] = paramNamesIdsAndDefaults; |
| |
|
| | |
| | |
| | |
| | util.initParams(); |
| | for (let i = 0; i < paramIds.length; i++) { |
| | if (args.hasOwnProperty(paramIds[i])) { |
| | util.pushParam(paramNames[i], args[paramIds[i]]); |
| | } else { |
| | util.pushParam(paramNames[i], paramDefaults[i]); |
| | } |
| | } |
| |
|
| | const addonBlock = util.runtime.getAddonBlock(procedureCode); |
| | if (addonBlock) { |
| | const result = addonBlock.callback(util.thread.getAllparams(), util); |
| | if (util.thread.status === 1 ) { |
| | |
| | |
| | util.stackFrame.executed = true; |
| | } |
| | return result; |
| | } |
| |
|
| | util.stackFrame.executed = true; |
| |
|
| | util.startProcedure(procedureCode); |
| | } |
| | } |
| |
|
| | set(args, util) { |
| | const contain = util.thread.blockContainer; |
| | const block = contain.getBlock(util.thread.isCompiled ? util.thread.peekStack() : util.thread.peekStackFrame().op.id); |
| | if (!block) return; |
| | const thread = util.thread; |
| | const param = contain.getBlock(block.inputs.PARAM?.block); |
| | if (param) { |
| | try { |
| | const curParams = thread.stackFrames[0].params; |
| | if (curParams !== null) thread.stackFrames[0].params[param.fields.VALUE.value] = args.VALUE; |
| | else thread.stackFrames[0].params = { [param.fields.VALUE.value]: args.VALUE } |
| | } catch { } |
| | } |
| | } |
| |
|
| | argumentReporterStringNumber(args, util) { |
| | const value = util.getParam(args.VALUE); |
| | if (value === null) { |
| | |
| | |
| | return 0; |
| | } |
| | return value; |
| | } |
| |
|
| | argumentReporterBoolean(args, util) { |
| | const value = util.getParam(args.VALUE); |
| | if (value === null) { |
| | |
| | |
| | return 0; |
| | } |
| | return value; |
| | } |
| |
|
| | argumentReporterCommand(args, util) { |
| | const branchInfo = util.getParam(args.VALUE) || {}; |
| | if (branchInfo.entry === null) return; |
| | const [branchId, target] = util.getBranchAndTarget( |
| | branchInfo.callerId, |
| | branchInfo.entry |
| | ) || []; |
| | if (branchId) { |
| | |
| | util.thread.pushStack(branchId, target); |
| | } else { |
| | util.thread.pushStack(null); |
| | } |
| | } |
| | } |
| |
|
| | module.exports = Scratch3ProcedureBlocks; |
| |
|