1 入門
1.1 関数1.2 クラス1.3 Rust ファイル1.4 ライブラリ2 C++
2.1 数値2.2 vector と map2.3 構造体と列挙型2.4 オブジェクトのライフタイム2.5 継承2.6 コールバック2.7 optional な値と例外2.8 ポインターとメモリ2.9 バインドできないもの3 Rust
3.1 コレクションとバイト3.2 Option と Result4 ライブラリ
4.1 C++ で使うライブラリ4.2 JavaScript から C API を使う4.3 C API を包むクラス4.4 クレート4.5 Conan パッケージ5 ランタイム
5.1 ファイル5.2 ワーカーとページスレッド5.3 呼び出しのコスト構造体と列挙型 構造体の public フィールドはプロパティになり、列挙型のすべてのメンバーは名前で参照できます。
await new Point() は、両方のフィールドが 0 の Point を作ります。フィールドはプロパティで、 await at.x で読み、 at.x = 1 で書き込みます。 step は点を値で受け取るので、C++ はコピーを操作し、新しい点を返します。
enum class Direction のメンバーは Direction.North、 Direction.East などで、そのまま C++ に渡せます。数値、 bool、 std::string のフィールドはプロパティになります。vector 型やクラス型のフィールドには、アクセサーメソッドが必要です。
試してみる
正方形を逆回りに歩いてみましょう。 turnLeft はないので、 turnRight を 3 回呼び出します。 [■ ] native.h 1 #pragma once
#include <string>
// Public fields of a struct read and write like properties.
struct Point {
double x;
double y;
};
// Members arrive as Direction.North, .East and so on.
enum class Direction { North, East, South, West };
Point step(Point point, Direction direction, double distance);
Direction turnRight(Direction direction);
std ::string nameOf(Direction direction);
[■ ] main.js 2 import {
initNative, Direction, Point, nameOf, step, turnRight,
} from './native/native.h' ;
await initNative();
let at = await new Point();
let facing = await Direction.North;
for (let turn = 0 ; turn < 4 ; turn += 1 ) {
at = await step(at, facing, 2 );
console.log(await nameOf(facing), await at.x, await at.y);
facing = await turnRight(facing);
}