扩展结构
编译扩展遵循特定的结构,与 Bilup 的编译器架构集成。理解此结构对于创建有效的编译扩展至关重要。
基本扩展模板
每个编译扩展都从这个基本结构开始:
扩展声明
扩展首先检查非沙箱环境,这是访问编译器内部所必需的:
(function(Scratch) {
'use strict';
if (!Scratch.extensions.unsandboxed) {
throw new Error("Extension needs to be run unsandboxed.");
}
访问编译器内部
接下来,扩展获取 Bilup 的编译器组件引用:
const { vm, BlockType, ArgumentType } = Scratch;
const { runtime } = vm;
// 访问编译器内部
const iwnafhwtb = vm.exports.i_will_not_ask_for_help_when_these_break();
const { JSGenerator, IRGenerator, ScriptTreeGenerator } = iwnafhwtb;
i_will_not_ask_for_help_when_these_break() 函数提供对内部 API 的访问,这些 API 可能在版本之间更改。名称作为警告,表示这些 API 不稳定。
编译器类型系统
编译扩展使用 Bilup 的类型系统进行优化:
const {
TYPE_NUMBER,
TYPE_STRING,
TYPE_BOOLEAN,
TYPE_UNKNOWN,
TYPE_NUMBER_NAN,
TypedInput,
ConstantInput,
VariableInput,
Frame,
sanitize
} = JSGenerator.unstable_exports;
修补系统
编译扩展使用修补系统来修改编译器行为。修补函数确保多个扩展可以共存: