@kubohiroya/turbowarp-aframe 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.ja.md CHANGED
@@ -26,6 +26,26 @@ Scratch のスプライトや背景を 3D オブジェクトとして扱うの
26
26
 
27
27
  unsandboxed 拡張はページ DOM を操作できます。信頼できる生成済み bundle だけを読み込んでください。
28
28
 
29
+ ## runtime scene capability
30
+
31
+ 複合的なunsandboxed機能拡張は、version付きruntime capabilityを通じてブロックと同じscene操作を利用できます。`Scratch.vm.runtime.turbowarpAFrameCapability`を取得し、最初に`requireVersion(1)`を呼んでから、次の同期メソッドを利用します。
32
+
33
+ ```ts
34
+ interface AFrameRuntimeCapabilityV1 {
35
+ readonly version: 1;
36
+ requireVersion(version: number): AFrameRuntimeCapabilityV1;
37
+ loadTemplate(id: string, source: string): void;
38
+ createFromTemplate(template: string, instance: string, parent: string): void;
39
+ setPosition(selector: string, x: number, y: number, z: number): void;
40
+ setRotation(selector: string, x: number, y: number, z: number): void;
41
+ emitEvent(type: string, selector: string, data: string): void;
42
+ deleteSelector(selector: string): void;
43
+ countSelector(selector: string): number;
44
+ }
45
+ ```
46
+
47
+ このcapabilityが公開するのは宣言的templateとselectorだけです。private DOM node、A-Frame object、Three.js object、glTF内部実装は公開しません。未対応versionはfail closedし、機能拡張のdispose後に保持されたcapabilityを呼び出した場合も、すべて明示的に拒否します。
48
+
29
49
  ## インストール
30
50
 
31
51
  ```bash
@@ -44,7 +64,7 @@ TurboWarp では `dist/turbowarp-aframe.js` を unsandboxed custom extension と
44
64
  package として参照する場合は version を固定します。
45
65
 
46
66
  ```bash
47
- pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.2.0
67
+ pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.3.0
48
68
  ```
49
69
 
50
70
  ## ブロック概要
package/README.md CHANGED
@@ -26,6 +26,26 @@ It treats TurboWarp sprites and backdrops as places to run 3D scene logic, not a
26
26
 
27
27
  Unsandboxed extensions can manipulate the containing page. Load only generated extension bundles that you trust.
28
28
 
29
+ ## Runtime scene capability
30
+
31
+ Composite unsandboxed extensions can use the same scene operations as the blocks through the versioned runtime capability. Read `Scratch.vm.runtime.turbowarpAFrameCapability`, call `requireVersion(1)`, and then use these synchronous methods:
32
+
33
+ ```ts
34
+ interface AFrameRuntimeCapabilityV1 {
35
+ readonly version: 1;
36
+ requireVersion(version: number): AFrameRuntimeCapabilityV1;
37
+ loadTemplate(id: string, source: string): void;
38
+ createFromTemplate(template: string, instance: string, parent: string): void;
39
+ setPosition(selector: string, x: number, y: number, z: number): void;
40
+ setRotation(selector: string, x: number, y: number, z: number): void;
41
+ emitEvent(type: string, selector: string, data: string): void;
42
+ deleteSelector(selector: string): void;
43
+ countSelector(selector: string): number;
44
+ }
45
+ ```
46
+
47
+ The capability deliberately exposes declarative templates and selectors, not private DOM nodes, A-Frame objects, Three.js objects, or glTF internals. Unsupported versions fail closed. A retained capability reference also rejects every operation after the extension is disposed.
48
+
29
49
  ## Install
30
50
 
31
51
  ```bash
@@ -44,7 +64,7 @@ Load `dist/turbowarp-aframe.js` as an unsandboxed custom extension in TurboWarp.
44
64
  For package-based reuse, pin the version:
45
65
 
46
66
  ```bash
47
- pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.2.0
67
+ pnpm add --save-exact @kubohiroya/turbowarp-aframe@0.3.0
48
68
  ```
49
69
 
50
70
  ## Block reference
@@ -588,6 +588,46 @@
588
588
  }
589
589
  ]
590
590
  };
591
+ var runtimeCapabilityKey = "turbowarpAFrameCapability";
592
+ function createRuntimeCapability(scene, assertActive) {
593
+ const capability = {
594
+ version: 1,
595
+ requireVersion(version) {
596
+ assertActive();
597
+ if (version !== 1) throw new Error(`Unsupported A-Frame runtime capability version: ${version}; supported version is 1.`);
598
+ return capability;
599
+ },
600
+ loadTemplate(id, source) {
601
+ assertActive();
602
+ scene.loadTemplate(id, source);
603
+ },
604
+ createFromTemplate(template, instance, parent) {
605
+ assertActive();
606
+ scene.createFromTemplate(template, instance, parent);
607
+ },
608
+ setPosition(selector, x, y, z) {
609
+ assertActive();
610
+ scene.setPosition(selector, x, y, z);
611
+ },
612
+ setRotation(selector, x, y, z) {
613
+ assertActive();
614
+ scene.setRotation(selector, x, y, z);
615
+ },
616
+ emitEvent(type, selector, data) {
617
+ assertActive();
618
+ scene.emitEvent(type, selector, data);
619
+ },
620
+ deleteSelector(selector) {
621
+ assertActive();
622
+ scene.deleteSelector(selector);
623
+ },
624
+ countSelector(selector) {
625
+ assertActive();
626
+ return scene.countSelector(selector);
627
+ }
628
+ };
629
+ return Object.freeze(capability);
630
+ }
591
631
  //#endregion
592
632
  //#region src/extension.ts
593
633
  var blockDefinitions = block_definitions_default.blocks;
@@ -623,7 +663,40 @@
623
663
  this.rootElement = null;
624
664
  this.sceneReadyPending = false;
625
665
  this.lastEvent = null;
666
+ this.disposed = false;
626
667
  this.resetGraph();
668
+ this.runtimeCapability = createRuntimeCapability({
669
+ loadTemplate: (id, source) => this.loadTemplate({
670
+ ID: id,
671
+ SOURCE: source
672
+ }),
673
+ createFromTemplate: (template, instance, parent) => this.createFromTemplate({
674
+ TEMPLATE: template,
675
+ INSTANCE: instance,
676
+ PARENT: parent
677
+ }),
678
+ setPosition: (selector, x, y, z) => this.setPosition({
679
+ SELECTOR: selector,
680
+ X: x,
681
+ Y: y,
682
+ Z: z
683
+ }),
684
+ setRotation: (selector, x, y, z) => this.setRotation({
685
+ SELECTOR: selector,
686
+ X: x,
687
+ Y: y,
688
+ Z: z
689
+ }),
690
+ emitEvent: (type, selector, data) => this.emitEvent({
691
+ TYPE: type,
692
+ SELECTOR: selector,
693
+ DATA: data
694
+ }),
695
+ deleteSelector: (selector) => this.deleteSelector({ SELECTOR: selector }),
696
+ countSelector: (selector) => this.countSelector({ SELECTOR: selector })
697
+ }, () => this.assertActive());
698
+ const runtime = Scratch.vm?.runtime;
699
+ if (runtime) runtime[runtimeCapabilityKey] = this.runtimeCapability;
627
700
  }
628
701
  getInfo() {
629
702
  return {
@@ -911,6 +984,18 @@
911
984
  }))
912
985
  };
913
986
  }
987
+ dispose() {
988
+ if (this.disposed) return;
989
+ this.disposed = true;
990
+ for (const key of [...this.animationPlaybacks.keys()]) this.stopPlayback(key, true);
991
+ this.rootElement?.parentElement?.remove();
992
+ this.rootElement = null;
993
+ const runtime = Scratch.vm?.runtime;
994
+ if (runtime?.["turbowarpAFrameCapability"] === this.runtimeCapability) delete runtime[runtimeCapabilityKey];
995
+ }
996
+ assertActive() {
997
+ if (this.disposed) throw new Error("A-Frame runtime capability is disposed.");
998
+ }
914
999
  resetGraph() {
915
1000
  for (const key of [...this.animationPlaybacks.keys()]) this.stopPlayback(key, true);
916
1001
  this.nodes.clear();
@@ -22,6 +22,14 @@ src/config.ts + src/block-definitions.json
22
22
 
23
23
  生成される JavaScript は unsandboxed TurboWarp 拡張です。manifest は block API contract として互換性確認に使います。
24
24
 
25
+ ## version付きruntime capability
26
+
27
+ 機能拡張は`Scratch.vm.runtime.turbowarpAFrameCapability`を公開します。version 1は、`loadTemplate`、`createFromTemplate`、`setPosition`、`setRotation`、`emitEvent`、`deleteSelector`、`countSelector`だけを持つ狭い型付きportです。
28
+
29
+ 各port methodは対応するblock handlerへ委譲します。そのため、block呼び出しと複合機能拡張からの呼び出しは、cast、validation、selector matching、event queue、機能拡張所有のscene stateを共有します。portはDOM element、A-Frame/Three.js object、glTF内部実装を公開しません。
30
+
31
+ consumerは利用前に`requireVersion(1)`を呼ぶ必要があります。未対応versionは互換fallbackを試さず例外になります。`dispose()`はruntimeからcapabilityを削除し、実行中のanimation playbackを停止し、scene hostを除去して、dispose前にconsumerが保持した参照も恒久的に無効化します。disposeの反復呼び出しは安全です。
32
+
25
33
  ## シーン初期化
26
34
 
27
35
  `createScene` は root の `#scene` ノードを作り、ブラウザ DOM がある場合は `#tw-aframe-root > a-scene` を作成します。host は最初に検出できた TurboWarp stage wrapper または canvas の親へ mount し、stage らしい要素が見つからない場合だけ `document.body` に fallback します。
@@ -22,6 +22,14 @@ src/config.ts + src/block-definitions.json
22
22
 
23
23
  The generated JavaScript bundle is an unsandboxed TurboWarp extension. The manifest records the block API contract for compatibility checks.
24
24
 
25
+ ## Versioned Runtime Capability
26
+
27
+ The extension publishes `Scratch.vm.runtime.turbowarpAFrameCapability`. Version 1 is a narrow typed port containing `loadTemplate`, `createFromTemplate`, `setPosition`, `setRotation`, `emitEvent`, `deleteSelector`, and `countSelector`.
28
+
29
+ Each port method delegates to the corresponding block handler. Consequently, block calls and composite-extension calls share casting, validation, selector matching, event queuing, and the extension-owned scene state. The port never exposes DOM elements, A-Frame/Three.js objects, or glTF internals.
30
+
31
+ Consumers must call `requireVersion(1)` before use. An unsupported version throws instead of attempting compatibility fallback. `dispose()` removes the capability from the runtime, stops active animation playbacks, removes the scene host, and permanently invalidates references that a consumer retained before disposal. Repeated disposal is safe.
32
+
25
33
  ## Scene initialization
26
34
 
27
35
  `createScene` creates a root `#scene` node and, when a browser DOM exists, a `#tw-aframe-root > a-scene` host. The host is mounted inside the first detected TurboWarp stage wrapper or canvas parent, falling back to `document.body` only when no stage-like element is found.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubohiroya/turbowarp-aframe",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A TurboWarp extension for declarative A-Frame scene graphs.",
5
5
  "author": "Hiroya Kubo <hiroya@cuc.ac.jp>",
6
6
  "license": "MPL-2.0",
@@ -37,24 +37,16 @@
37
37
  "access": "public",
38
38
  "registry": "https://registry.npmjs.org/"
39
39
  },
40
+ "packageManager": "pnpm@11.11.0",
40
41
  "engines": {
41
42
  "node": ">=22.18.0"
42
43
  },
43
- "devDependencies": {
44
- "@eslint/js": "^9.39.5",
45
- "@kubohiroya/vite-plugin-turbowarp-extension": "0.4.0",
46
- "@types/node": "^24.13.3",
47
- "eslint": "^9.39.5",
48
- "typescript": "^5.9.3",
49
- "typescript-eslint": "^8.66.0",
50
- "vite": "^8.2.2",
51
- "vitest": "^4.1.10"
52
- },
53
44
  "scripts": {
54
45
  "dev": "vite build --watch",
55
46
  "typecheck": "tsc --noEmit",
56
47
  "build": "vite build",
57
48
  "lint": "eslint src tests vite.config.ts",
49
+ "format": "eslint src tests vite.config.ts --fix",
58
50
  "test": "vitest run",
59
51
  "docs": "node scripts/generate-readme.ts",
60
52
  "docs:check": "pnpm run docs && git diff --exit-code -- README.md",
@@ -62,5 +54,15 @@
62
54
  "pack:check": "npm pack --dry-run --ignore-scripts",
63
55
  "check": "pnpm run typecheck && pnpm run lint && pnpm run test && pnpm run docs:check && pnpm run check:dist && pnpm run repo:check && pnpm run pack:check",
64
56
  "check:dist": "pnpm run build && node scripts/check-dist.ts"
57
+ },
58
+ "devDependencies": {
59
+ "@eslint/js": "^9.39.5",
60
+ "@kubohiroya/vite-plugin-turbowarp-extension": "0.4.0",
61
+ "@types/node": "^24.13.3",
62
+ "eslint": "^9.39.5",
63
+ "typescript": "^5.9.3",
64
+ "typescript-eslint": "^8.66.0",
65
+ "vite": "^8.2.2",
66
+ "vitest": "^4.1.10"
65
67
  }
66
- }
68
+ }
package/src/extension.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  import {extensionConfig} from './config';
2
2
  import definitions from './block-definitions.json';
3
+ import {
4
+ createRuntimeCapability,
5
+ runtimeCapabilityKey,
6
+ type AFrameRuntimeCapabilityV1
7
+ } from './runtime-capability.js';
3
8
 
4
9
  type BlockTypeName = 'COMMAND' | 'REPORTER' | 'HAT' | 'BOOLEAN';
5
10
  type ArgumentTypeName = 'STRING' | 'NUMBER' | 'BOOLEAN';
@@ -130,9 +135,29 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
130
135
  private rootElement: Element | null = null;
131
136
  private sceneReadyPending = false;
132
137
  private lastEvent: SceneEvent | null = null;
138
+ private readonly runtimeCapability: AFrameRuntimeCapabilityV1;
139
+ private disposed = false;
133
140
 
134
141
  public constructor() {
135
142
  this.resetGraph();
143
+ this.runtimeCapability = createRuntimeCapability(
144
+ {
145
+ loadTemplate: (id, source) => this.loadTemplate({ID: id, SOURCE: source}),
146
+ createFromTemplate: (template, instance, parent) =>
147
+ this.createFromTemplate({TEMPLATE: template, INSTANCE: instance, PARENT: parent}),
148
+ setPosition: (selector, x, y, z) =>
149
+ this.setPosition({SELECTOR: selector, X: x, Y: y, Z: z}),
150
+ setRotation: (selector, x, y, z) =>
151
+ this.setRotation({SELECTOR: selector, X: x, Y: y, Z: z}),
152
+ emitEvent: (type, selector, data) =>
153
+ this.emitEvent({TYPE: type, SELECTOR: selector, DATA: data}),
154
+ deleteSelector: (selector) => this.deleteSelector({SELECTOR: selector}),
155
+ countSelector: (selector) => this.countSelector({SELECTOR: selector})
156
+ },
157
+ () => this.assertActive()
158
+ );
159
+ const runtime = Scratch.vm?.runtime;
160
+ if (runtime) runtime[runtimeCapabilityKey] = this.runtimeCapability;
136
161
  }
137
162
 
138
163
  public getInfo(): Record<string, unknown> {
@@ -514,6 +539,26 @@ export class TurboWarpAFrameExtension implements TurboWarpExtension {
514
539
  };
515
540
  }
516
541
 
542
+ public dispose(): void {
543
+ if (this.disposed) return;
544
+ this.disposed = true;
545
+ for (const key of [...this.animationPlaybacks.keys()]) {
546
+ this.stopPlayback(key, true);
547
+ }
548
+ this.rootElement?.parentElement?.remove();
549
+ this.rootElement = null;
550
+ const runtime = Scratch.vm?.runtime;
551
+ if (runtime?.[runtimeCapabilityKey] === this.runtimeCapability) {
552
+ delete runtime[runtimeCapabilityKey];
553
+ }
554
+ }
555
+
556
+ private assertActive(): void {
557
+ if (this.disposed) {
558
+ throw new Error('A-Frame runtime capability is disposed.');
559
+ }
560
+ }
561
+
517
562
  private resetGraph(): void {
518
563
  for (const key of [...this.animationPlaybacks.keys()]) {
519
564
  this.stopPlayback(key, true);
package/src/globals.d.ts CHANGED
@@ -20,6 +20,9 @@ interface ScratchApi {
20
20
  toBoolean(value: unknown): boolean;
21
21
  };
22
22
  translate: ScratchTranslate;
23
+ vm?: {
24
+ runtime?: Record<string, unknown>;
25
+ };
23
26
  }
24
27
 
25
28
  declare const Scratch: ScratchApi;
@@ -0,0 +1,64 @@
1
+ export const runtimeCapabilityVersion = 1 as const;
2
+ export const runtimeCapabilityKey = 'turbowarpAFrameCapability';
3
+
4
+ export interface AFrameScenePort {
5
+ loadTemplate(id: string, source: string): void;
6
+ createFromTemplate(template: string, instance: string, parent: string): void;
7
+ setPosition(selector: string, x: number, y: number, z: number): void;
8
+ setRotation(selector: string, x: number, y: number, z: number): void;
9
+ emitEvent(type: string, selector: string, data: string): void;
10
+ deleteSelector(selector: string): void;
11
+ countSelector(selector: string): number;
12
+ }
13
+
14
+ export interface AFrameRuntimeCapabilityV1 extends AFrameScenePort {
15
+ readonly version: typeof runtimeCapabilityVersion;
16
+ requireVersion(version: number): AFrameRuntimeCapabilityV1;
17
+ }
18
+
19
+ export function createRuntimeCapability(
20
+ scene: AFrameScenePort,
21
+ assertActive: () => void
22
+ ): AFrameRuntimeCapabilityV1 {
23
+ const capability: AFrameRuntimeCapabilityV1 = {
24
+ version: runtimeCapabilityVersion,
25
+ requireVersion(version) {
26
+ assertActive();
27
+ if (version !== runtimeCapabilityVersion) {
28
+ throw new Error(
29
+ `Unsupported A-Frame runtime capability version: ${version}; supported version is ${runtimeCapabilityVersion}.`
30
+ );
31
+ }
32
+ return capability;
33
+ },
34
+ loadTemplate(id, source) {
35
+ assertActive();
36
+ scene.loadTemplate(id, source);
37
+ },
38
+ createFromTemplate(template, instance, parent) {
39
+ assertActive();
40
+ scene.createFromTemplate(template, instance, parent);
41
+ },
42
+ setPosition(selector, x, y, z) {
43
+ assertActive();
44
+ scene.setPosition(selector, x, y, z);
45
+ },
46
+ setRotation(selector, x, y, z) {
47
+ assertActive();
48
+ scene.setRotation(selector, x, y, z);
49
+ },
50
+ emitEvent(type, selector, data) {
51
+ assertActive();
52
+ scene.emitEvent(type, selector, data);
53
+ },
54
+ deleteSelector(selector) {
55
+ assertActive();
56
+ scene.deleteSelector(selector);
57
+ },
58
+ countSelector(selector) {
59
+ assertActive();
60
+ return scene.countSelector(selector);
61
+ }
62
+ };
63
+ return Object.freeze(capability);
64
+ }