|
|
| 第1行: |
第1行: |
| Scratch虚拟机(英文:Scratch Virtual Machine,简称Scratch VM或scratch-vm)是[[Scratch(全球少儿图形化编程语言)|Scratch]] 3.0的核心执行引擎与运行时库,由麻省理工学院媒体实验室(MIT Media Lab)终身幼儿园组(Lifelong Kindergarten Group)开发,现由Scratch基金会(Scratch Foundation)维护<ref name=":0">[https://github.com/scratchfoundation/scratch-vm Scratch VM - GitHub]</ref>。该库以[[JavaScript]]实现,负责将用户通过拖拽积木构建的图形化程序解析为抽象语法树(AST),并管理程序状态的表示、运行与维护<ref name=":1">[https://github.com/scratchfoundation/scratch-editor/ scratch-editor - GitHub]</ref>。作为Scratch 3.0技术架构的中间层,Scratch虚拟机向上与Scratch Blocks对接以接收积木变更事件,向下调用Scratch Render等模块完成舞台渲染,同时通过扩展系统支持硬件与第三方服务的接入<ref name=":2">[https://www.cnblogs.com/chuaWeb/p/13681999.html Scratch3的结构 - 博客园]</ref>。
| | <div class="cael-box"> |
| | | {{#if:{{{标题|{{{title|}}}}}}| |
| {| class="infobox" style="border:1px solid #ccc; background:#f8f9fa; width:24em; float:right; margin:0 0 1em 1em; border-spacing:5px;" | | <div class="cael-box-header"> |
| |- | | <span>{{{图标|{{{icon|📦}}}}}}</span> |
| ! colspan="2" style="text-align:center;" | '''Scratch虚拟机'''
| | <span>{{{标题|{{{title}}}}}}</span> |
| |-
| | </div> |
| | 中文名 || '''Scratch虚拟机'''
| | }} |
| |-
| | <div class="cael-box-body"> |
| | 英文名 || '''Scratch Virtual Machine'''
| | {{{内容|{{{1}}}}}} |
| |-
| | </div> |
| | 缩写 || '''Scratch VM / scratch-vm'''
| | {{#if:{{{更多|{{{more|}}}}}}| |
| |-
| | <div class="cael-box-more">[[{{{更多|{{{more}}}}}}|{{{更多文字|更多 »}}}]]</div> |
| | 开发者 || '''MIT Media Lab 终身幼儿园组'''
| | }} |
| |-
| | </div> |
| | 维护者 || '''Scratch Foundation'''
| |
| |-
| |
| | 初始发布 || '''2016年'''
| |
| |- | |
| | 编程语言 || '''JavaScript'''
| |
| |-
| |
| | 运行环境 || '''浏览器、Node.js、桌面端'''
| |
| |-
| |
| | 许可证 || '''BSD-3-Clause'''
| |
| |-
| |
| | 代码仓库 || '''scratch-editor(mono-repo)'''
| |
| |-
| |
| | 上游模块 || '''scratch-blocks'''
| |
| |-
| |
| | 下游模块 || '''scratch-render、scratch-audio'''
| |
| |}
| |
| | |
| == 概述 ==
| |
| | |
| Scratch虚拟机是[[Scratch(全球少儿图形化编程语言)|Scratch]] 3.0生态系统的底层引擎,其核心职责包括项目文件的加载与序列化、积木逻辑的解析与执行、多角色(Sprite)并发线程的调度,以及扩展功能的管理<ref name=":3">[https://gitee.com/songlu-cube/scratch-dev-note/blob/master/0%E9%A1%B9%E7%9B%AE%E4%BB%8B%E7%BB%8D.md Scratch开发笔记 - Gitee]</ref>。与Scratch 2.0基于Adobe Flash的架构不同,Scratch 3.0完全基于[[HTML]]5、[[CSS]]与[[JavaScript]]构建,虚拟机作为纯[[JavaScript]]库可在浏览器、[[Node.js]]及桌面端等多种环境中运行<ref name=":4">[https://www.i-programmer.info/news/98-languages/12423-scratch-30-released-to-mixed-reaction.html Scratch 3.0 Released To Mixed Reaction - I Programmer]</ref>。
| |
| | |
| == 架构设计 ==
| |
| | |
| === 核心组件 ===
| |
| | |
| Scratch虚拟机的架构围绕两个关键类构建:<code>VirtualMachine</code>与<code>Runtime</code>。<code>VirtualMachine</code>类位于<code>src/virtual-machine.js</code>,作为外部交互的主要接口,负责协调舞台、积木与扩展之间的连接;其内部包含一个<code>runtime</code>实例,来自<code>src/engine/runtime.js</code>,是实际管理程序执行的核心引擎<ref name=":5">[https://blog.csdn.net/gitblog_01166/article/details/153949711 深度解析scratch-vm架构 - CSDN]</ref>。Runtime存储所有关键运行时数据,包括目标对象(角色与舞台)、积木定义与执行逻辑、I/O设备状态(键盘、鼠标、视频等)以及线程调度队列。
| |
| | |
| === 抽象语法树 ===
| |
| | |
| 虚拟机通过<code>blockListener</code>监听scratch-blocks工作区发出的事件,据此构建并维护抽象语法树(AST)<ref name=":6">[https://github.com/mitmedialab/prg-raise-playground-scratch-vm scratch-vm - MIT Media Lab]</ref>。每个可执行目标(如角色)均保有独立的AST,可在运行时通过<code>vm.runtime.targets[...].blocks</code>查看其状态。AST中的每个积木节点包含标识符、操作码(opcode)、输入参数、字段值及执行链接等完整信息,为后续线程执行提供结构化依据。
| |
| | |
| === 线程与执行模型 ===
| |
| | |
| Scratch采用协作式多任务模型。Runtime维护一个线程队列,每个线程对应一条积木脚本链。虚拟机通过周期性的"步进"(step)机制调度线程执行:在每个帧周期内,线程依次执行其积木链中的指令,直至遇到 yield 点(如等待、重绘请求)或脚本结束<ref name=":7">[https://github.com/CodeClubLuxembourg/scratch-vm scratch-vm - CodeClubLuxembourg]</ref>。该设计保证了单线程JavaScript环境下的并发表现,同时避免了传统抢占式调度带来的复杂性。
| |
| | |
| == 功能特性 ==
| |
| | |
| === 项目加载与序列化 ===
| |
| | |
| 虚拟机支持解析.sb2与.sb3格式的项目文件,加载其中包含的图片、声音等资源,并将角色渲染到舞台上<ref name=":3" />。同时,虚拟机可将当前项目状态反向序列化,打包为.sb3文件以供导出或分享。
| |
| | |
| === 扩展系统 ===
| |
| | |
| Scratch虚拟机通过<code>src/extensions/</code>目录支持功能扩展。开发者可基于官方API创建自定义扩展,为Scratch添加新的积木分类与硬件支持<ref name=":8">[https://blog.csdn.net/gitblog_00661/article/details/151668571 scratch-vm扩展开发指南 - CSDN]</ref>。内置扩展包括画笔(Pen)、音乐(Music)、视频感知(Video Sensing)以及Micro:bit、LEGO Mindstorms等硬件连接扩展<ref name=":9">[https://github.com/mbot-project/mbot_scratch_gui mbot_scratch_gui - GitHub]</ref>。
| |
| | |
| === 运行时监控 ===
| |
| | |
| 虚拟机内置Playground调试环境,开发者启动开发服务器后可访问本地端口查看项目性能实时监控、积木执行次数统计、内存使用跟踪及帧速率分析等数据<ref name=":10">[https://blog.gitcode.com/eaa4c536e6e930ebc1a8e946d787fb3b.html Scratch核心引擎 - GitCode]</ref>。
| |
| | |
| == 开发与部署 ==
| |
| | |
| === 仓库迁移 ===
| |
| | |
| 2024年至2025年间,Scratch团队将scratch-vm模块迁移至新的单体仓库(mono-repo)<code>scratch-editor</code>中,原独立仓库被归档并设为只读<ref name=":1" />。新版本以<code>@scratch/scratch-vm</code>名称发布于NPM registry。迁移旨在统一管理编辑器各包的依赖关系,降低跨模块变更的维护成本,计划于2025年内分四批完成全部迁移工作<ref name=":11">[https://github.com/scratchfoundation/scratch-editor/ scratch-editor - GitHub]</ref>。
| |
| | |
| === 使用方式 ===
| |
| | |
| 开发者可通过NPM直接安装:<code>npm install scratch-vm</code>,或在浏览器中通过独立构建版本引入。在Node.js环境中,通过<code>require('scratch-vm')</code>获取<code>VirtualMachine</code>类,实例化后监听积木变更事件并调用<code>vm.start()</code>启动运行时<ref name=":0" />。
| |
| | |
| == 参考文献 ==
| |
| | |
| <references /> | |