JS・WasmにもあるRustのアレ - コンポーネント間でやりとりするオブジェクトを管理する

YAMAMOTO Yuji (山本悠滋)

2026-11-22 JSConf JP 2026

はじめまして! 👋😄

📝今日話すこと

📝アジェンダ

  1. 所有権に入る前に…
    • postMessage とは?
    • 普通に postMessage でオブジェクトをやり取りしよう
    • 今度は Wasm Component Model でもオブジェクトをやり取りしよう
  2. 所有権とは?
  3. 所有権を移動させるとどうなる?
  4. 所有権を移動させないとどうなる?
  5. 普通のやり取り(コピー)だとダメなのか?

所有権に入る前に…

postMessageとは:

普通に postMessage でオブジェクトをやり取りしよう

<html>
<script>
  // メインスレッド
  const worker = new Worker("worker.js");
  worker.postMessage({ x: 1, y: 2 });
</script>
....
// worker.js
onmessage = (event) => {
  const { x, y } = event.data;
  postMessage({ x: x + 1, y: y + 1 });
};

postMessage によるオブジェクトのやり取りの仕組み

今度は Wasm Component Model でもオブジェクトをやり取りしよう

Wasm Component Model って?:

例

詳細はリポジトリーを参照!

// example.wit

world example {
  export greet: func(caller: string) -> string;
}

例

# python.py
import sys

class WitWorld():
    def greet(self, caller: str) -> str:
        version = sys.version_info
        major = version.major
        minor = version.minor
        micro = version.micro
        return f"Hello, {caller}! I'm Python {major}.{minor}.{micro}!"

例

componentize-py で Wasm Component を作り、jco で ESM に変換する:

$ componentize-py -d example.wit -w example componentize python -o python.wasm
$ jco transpile python.wasm -o out-python

例

実行例:

import { greet } from "./out-python/python.js";

console.log(greet(`Node.js ${process.version}`));

結果:

$ node simple/index.js
Hello, Node.js v26.7.0! I'm Python 3.14.0!

所有権とは

*️⃣このスライドにおける定義

環境とは

*️⃣これもこのスライドにおける定義

プログラム(と、そのパーツ。スレッドやモジュール)がアクセスできるメモリー内のデータの集まり

// モジュール(がアクセスできる変数)の環境
import { imported } from "some-module.mjs";
const x = "hello";

function f() {
  // 関数(がアクセスできる変数)の環境
  const y = "world";
  console.log(imported, x, y);
}

postMessage における所有権の移動 (1)

<html>
<script>
  // メインスレッド
  const worker = new Worker("worker.js");
  const array = new Uint8Array([1, 2, 3]);
  // ここで `array` の所有権を `worker` に移動させる
  worker.postMessage(array, [array.buffer]);
  console.log(array); // ← ここでエラー!
</script>
....
// worker.js
onmessage = (event) => {
  const array = event.data;
  console.log(array);
};

https://developer.mozilla.org/ja/docs/Web/API/Web_Workers_API/Transferable_objects

どんなオブジェクトを「移動させないといけない」のか

https://share.google/aimode/vaK8RnKezhQ7bTETR

Wasm Component Model における所有権の移動

JSによるWasmコンポーネント作りの参考に: - ComponentizeJS/EXAMPLE.md at main · bytecodealliance/ComponentizeJS

シミュレーションしてみよう

所有権を侵害したやり取りのシミュレーションに - Proxy - JavaScript | MDN

📝まとめ

取り上げられなかった話題

参考(本文中に言及がなかったもののみ)

https://github.com/WebAssembly/component-model/blob/main/design/high-level/Choices.md