Bicameral Engine
GPU-resident engine for a magic-research open-world game
Loading...
Searching...
No Matches
script_value.h
Go to the documentation of this file.
1// script_value.h — Luau の値を、アドレスに依らない C++ の木にしたもの(T-0138、13 §2・ADR-0031)。
2//
3// パッケージ(luau_package.*)が Luau から受け取る表(マニフェスト・反応則などの定義)は、Luau の外ではこの形で持つ。
4// 形: nil・真偽・数(倍精度)・文字列・表(鍵 → 値。鍵は数か文字列で、並べた順に持つ)。関数・userdata・スレッドは持たない。
5// 鍵の順番: 数(小さい順)→ 文字列(バイト順)。Luau の pairs の順番(アドレス・挿入の履歴で変わる)に依らない。
6// データの流れ: LuauSandbox::Run(captureValues)が Luau の戻り値をこれに直す → パッケージの読み込みが合わせる → ベイク(T-0021)へ。
7// AppendCanonicalBytes は同じ木から必ず同じバイト列を作る(決定性の検査と、表の版のハッシュに使う)。
8#pragma once
9
10#include <cstdint>
11#include <string>
12#include <string_view>
13#include <vector>
14
15namespace bicameral::script {
16
17 struct ScriptField;
18
19 struct ScriptValue {
20 enum class Kind : uint8_t {
26 };
27
29 bool boolean = false;
30 double number = 0.0;
31 std::string text;
32 std::vector<ScriptField> fields; // 表の中身。鍵は数(小さい順)→ 文字列(バイト順)に並んでいる
33
34 // --- 作る ---
35 [[nodiscard]] static ScriptValue MakeBoolean(bool value);
36 [[nodiscard]] static ScriptValue MakeNumber(double value);
37 [[nodiscard]] static ScriptValue MakeString(std::string value);
38 [[nodiscard]] static ScriptValue MakeTable(std::vector<ScriptField> fields); // 鍵を並べ直す
39
40 // --- 読む ---
41 [[nodiscard]] bool IsTable() const { return kind == Kind::Table; }
42 [[nodiscard]] bool IsString() const { return kind == Kind::String; }
43 [[nodiscard]] bool IsNumber() const { return kind == Kind::Number; }
44
45 // 文字列の鍵 key の値(無ければ nullptr)
46 [[nodiscard]] const ScriptValue* Find(std::string_view key) const;
47 };
48
53
54 // 鍵の順番(数 → 文字列。数は小さい順、文字列はバイト順)
55 [[nodiscard]] bool KeyLess(const ScriptValue& left, const ScriptValue& right);
56
57 // 同じ木なら同じバイト列(種類の印 + 中身。数はビット列のまま、長さは 4 バイトのリトルエンディアン)
58 void AppendCanonicalBytes(const ScriptValue& value, std::string& out);
59 void AppendCanonicalString(std::string_view text, std::string& out);
60
61 // バイト列の FNV-1a(64 ビット)。表の版に使う
62 [[nodiscard]] uint64_t HashBytes(std::string_view bytes);
63
64 // 人が読む形(テスト・ログ用。鍵の順番は fields のまま)
65 [[nodiscard]] std::string ToDebugText(const ScriptValue& value);
66
67} // namespace bicameral::script
std::string ToDebugText(const ScriptValue &value)
void AppendCanonicalBytes(const ScriptValue &value, std::string &out)
uint64_t HashBytes(std::string_view bytes)
bool KeyLess(const ScriptValue &left, const ScriptValue &right)
void AppendCanonicalString(std::string_view text, std::string &out)
std::vector< ScriptField > fields
static ScriptValue MakeString(std::string value)
static ScriptValue MakeTable(std::vector< ScriptField > fields)
static ScriptValue MakeNumber(double value)
const ScriptValue * Find(std::string_view key) const
static ScriptValue MakeBoolean(bool value)