@liminalia/runtime 0.0.0-stage → 0.8.4
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/LICENSE +106 -0
- package/dist/src/chat.js +60 -0
- package/dist/src/commands-play.js +300 -0
- package/dist/src/commands.js +534 -0
- package/dist/src/coverage.js +208 -0
- package/dist/src/headless.js +109 -0
- package/dist/src/host.js +1 -0
- package/dist/src/index.js +15 -0
- package/dist/src/prefetch.js +80 -0
- package/dist/src/program.js +131 -0
- package/dist/src/project-options.js +84 -0
- package/dist/src/retranslate.js +44 -0
- package/dist/src/route.js +242 -0
- package/dist/src/runtime.js +1929 -0
- package/dist/src/save.js +51 -0
- package/dist/src/scenario-test.js +200 -0
- package/dist/src/state.js +62 -0
- package/dist/src/value.js +464 -0
- package/dist/src/voice-observe.js +82 -0
- package/package.json +33 -4
- package/src/chat.ts +109 -0
- package/src/commands-play.ts +280 -0
- package/src/commands.ts +457 -0
- package/src/coverage.ts +257 -0
- package/src/headless.ts +139 -0
- package/src/host.ts +116 -0
- package/src/index.ts +16 -0
- package/src/prefetch.ts +78 -0
- package/src/program.ts +186 -0
- package/src/project-options.ts +128 -0
- package/src/retranslate.ts +67 -0
- package/src/route.ts +365 -0
- package/src/runtime.ts +2143 -0
- package/src/save.ts +60 -0
- package/src/scenario-test.ts +218 -0
- package/src/state.ts +353 -0
- package/src/value.ts +443 -0
- package/src/voice-observe.ts +85 -0
- package/types/src/chat.d.ts +63 -0
- package/types/src/commands-play.d.ts +2 -0
- package/types/src/commands.d.ts +2 -0
- package/types/src/coverage.d.ts +100 -0
- package/types/src/headless.d.ts +77 -0
- package/types/src/host.d.ts +140 -0
- package/types/src/index.d.ts +16 -0
- package/types/src/prefetch.d.ts +18 -0
- package/types/src/program.d.ts +107 -0
- package/types/src/project-options.d.ts +103 -0
- package/types/src/retranslate.d.ts +39 -0
- package/types/src/route.d.ts +181 -0
- package/types/src/runtime.d.ts +553 -0
- package/types/src/save.d.ts +21 -0
- package/types/src/scenario-test.d.ts +43 -0
- package/types/src/state.d.ts +357 -0
- package/types/src/value.d.ts +61 -0
- package/types/src/voice-observe.d.ts +26 -0
- package/README.md +0 -3
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
import { arityText, FORBIDDEN_PROPERTIES, FUNCTION_ARITY, functionArity } from "@liminalia/lang";
|
|
2
|
+
export class EvalError extends Error {
|
|
3
|
+
}
|
|
4
|
+
/** @function の呼び出しの深さの上限(自分を呼ぶ関数が終わらないとき)。 */
|
|
5
|
+
export const MAX_FUNCTION_DEPTH = 200;
|
|
6
|
+
/**
|
|
7
|
+
* 式を評価する。暗黙の型変換はしない。例外は + の文字列連結(片方が文字列なら文字列にする)だけ。
|
|
8
|
+
* 未設定の変数や存在しないメンバーは null になる。__proto__、constructor、prototype のメンバーと添字も null
|
|
9
|
+
* (オブジェクトのプロトタイプを読ませない。代入は実行器がエラーにする)。
|
|
10
|
+
*/
|
|
11
|
+
export function evaluate(expr, env) {
|
|
12
|
+
switch (expr.kind) {
|
|
13
|
+
case "literal":
|
|
14
|
+
return expr.value;
|
|
15
|
+
case "array":
|
|
16
|
+
return expr.items.map((e) => evaluate(e, env));
|
|
17
|
+
case "object": {
|
|
18
|
+
const o = {};
|
|
19
|
+
// __proto__ などのキーは検査がエラーにする。ここでも入れない(プロトタイプを書き換えさせない)
|
|
20
|
+
for (const e of expr.entries)
|
|
21
|
+
if (!FORBIDDEN_PROPERTIES.has(e.key))
|
|
22
|
+
o[e.key] = evaluate(e.value, env);
|
|
23
|
+
return o;
|
|
24
|
+
}
|
|
25
|
+
case "name":
|
|
26
|
+
return env.lookup(expr.name);
|
|
27
|
+
case "member": {
|
|
28
|
+
const o = evaluate(expr.object, env);
|
|
29
|
+
if (FORBIDDEN_PROPERTIES.has(expr.property))
|
|
30
|
+
return null;
|
|
31
|
+
if (o !== null && typeof o === "object" && !Array.isArray(o))
|
|
32
|
+
return own(o, expr.property);
|
|
33
|
+
if (Array.isArray(o) && expr.property === "length")
|
|
34
|
+
return o.length;
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
case "index": {
|
|
38
|
+
const o = evaluate(expr.object, env);
|
|
39
|
+
const k = evaluate(expr.index, env);
|
|
40
|
+
if (Array.isArray(o) && typeof k === "number")
|
|
41
|
+
return o[k] ?? null;
|
|
42
|
+
if (o !== null && typeof o === "object" && !Array.isArray(o) && typeof k === "string")
|
|
43
|
+
return FORBIDDEN_PROPERTIES.has(k) ? null : own(o, k);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
case "call":
|
|
47
|
+
return call(expr.callee, expr.args.map((a) => evaluate(a, env)), env);
|
|
48
|
+
case "unary": {
|
|
49
|
+
const v = evaluate(expr.operand, env);
|
|
50
|
+
if (expr.op === "!")
|
|
51
|
+
return !truthy(v);
|
|
52
|
+
if (typeof v !== "number")
|
|
53
|
+
throw new EvalError(`- は数値にだけ使えます(${describe(v)})`);
|
|
54
|
+
return -v;
|
|
55
|
+
}
|
|
56
|
+
case "binary":
|
|
57
|
+
return binary(expr.op, expr.left, expr.right, env);
|
|
58
|
+
case "conditional":
|
|
59
|
+
return truthy(evaluate(expr.test, env)) ? evaluate(expr.then, env) : evaluate(expr.otherwise, env);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** オブジェクトが自分で持つ値(継承したもの(toString など)は読まない)。無ければ null。 */
|
|
63
|
+
function own(o, key) {
|
|
64
|
+
return Object.hasOwn(o, key) ? (o[key] ?? null) : null;
|
|
65
|
+
}
|
|
66
|
+
/** 偽になるのは false・null・0・"" だけ。 */
|
|
67
|
+
export function truthy(v) {
|
|
68
|
+
return !(v === false || v === null || v === 0 || v === "");
|
|
69
|
+
}
|
|
70
|
+
export function equals(a, b) {
|
|
71
|
+
if (a === b)
|
|
72
|
+
return true;
|
|
73
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
74
|
+
if (a.length !== b.length)
|
|
75
|
+
return false;
|
|
76
|
+
for (let i = 0; i < a.length; i++) {
|
|
77
|
+
if (!equals(a[i] ?? null, b[i] ?? null))
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (a && b && typeof a === "object" && typeof b === "object" && !Array.isArray(a) && !Array.isArray(b)) {
|
|
83
|
+
const ka = Object.keys(a);
|
|
84
|
+
return ka.length === Object.keys(b).length && ka.every((k) => k in b && equals(a[k], b[k]));
|
|
85
|
+
}
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
export function display(v) {
|
|
89
|
+
if (v === null)
|
|
90
|
+
return "";
|
|
91
|
+
if (typeof v === "string")
|
|
92
|
+
return v;
|
|
93
|
+
if (typeof v === "number" || typeof v === "boolean")
|
|
94
|
+
return String(v);
|
|
95
|
+
return JSON.stringify(v);
|
|
96
|
+
}
|
|
97
|
+
export function checkedDivision(a, b, op) {
|
|
98
|
+
if (b === 0)
|
|
99
|
+
throw new EvalError(`0 で${op === "/" ? "割る" : "割った余りを求める"}ことはできません(${describe(a)} ${op} 0)`);
|
|
100
|
+
return op === "/" ? a / b : a % b;
|
|
101
|
+
}
|
|
102
|
+
function binary(op, l, r, env) {
|
|
103
|
+
if (op === "&&") {
|
|
104
|
+
const a = evaluate(l, env);
|
|
105
|
+
return truthy(a) ? evaluate(r, env) : a;
|
|
106
|
+
}
|
|
107
|
+
if (op === "||") {
|
|
108
|
+
const a = evaluate(l, env);
|
|
109
|
+
return truthy(a) ? a : evaluate(r, env);
|
|
110
|
+
}
|
|
111
|
+
const a = evaluate(l, env);
|
|
112
|
+
const b = evaluate(r, env);
|
|
113
|
+
switch (op) {
|
|
114
|
+
case "==":
|
|
115
|
+
return equals(a, b);
|
|
116
|
+
case "!=":
|
|
117
|
+
return !equals(a, b);
|
|
118
|
+
case "+":
|
|
119
|
+
if (typeof a === "number" && typeof b === "number")
|
|
120
|
+
return a + b;
|
|
121
|
+
if (typeof a === "string" || typeof b === "string")
|
|
122
|
+
return display(a) + display(b);
|
|
123
|
+
throw new EvalError(`+ は数値どうしか文字列にだけ使えます(${describe(a)} + ${describe(b)})`);
|
|
124
|
+
case "-":
|
|
125
|
+
case "*":
|
|
126
|
+
case "/":
|
|
127
|
+
case "%": {
|
|
128
|
+
if (typeof a !== "number" || typeof b !== "number")
|
|
129
|
+
throw new EvalError(`${op} は数値にだけ使えます(${describe(a)} ${op} ${describe(b)})`);
|
|
130
|
+
if (op === "-")
|
|
131
|
+
return a - b;
|
|
132
|
+
if (op === "*")
|
|
133
|
+
return a * b;
|
|
134
|
+
return checkedDivision(a, b, op);
|
|
135
|
+
}
|
|
136
|
+
case "<":
|
|
137
|
+
case "<=":
|
|
138
|
+
case ">":
|
|
139
|
+
case ">=": {
|
|
140
|
+
if (!((typeof a === "number" && typeof b === "number") || (typeof a === "string" && typeof b === "string"))) {
|
|
141
|
+
throw new EvalError(`${op} は同じ型の数値か文字列にだけ使えます(${describe(a)} ${op} ${describe(b)})`);
|
|
142
|
+
}
|
|
143
|
+
if (op === "<")
|
|
144
|
+
return a < b;
|
|
145
|
+
if (op === "<=")
|
|
146
|
+
return a <= b;
|
|
147
|
+
if (op === ">")
|
|
148
|
+
return a > b;
|
|
149
|
+
return a >= b;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** プラグインが足した式の関数の処理(プラグインの名前 → 関数の名前 → 処理)。@liminalia/sdk の combinePlugins が入れる。 */
|
|
154
|
+
const pluginFunctions = new Map();
|
|
155
|
+
/**
|
|
156
|
+
* そのプラグインが足す式の関数の処理を入れ替える(そのプラグインが前に入れたものは外す。ほかのプラグインのものは残す。
|
|
157
|
+
* 引数の数は @liminalia/lang の setPluginFunctions)。
|
|
158
|
+
*/
|
|
159
|
+
export function setPluginFunctionImpls(plugin, impls) {
|
|
160
|
+
pluginFunctions.delete(plugin);
|
|
161
|
+
if (Object.keys(impls).length > 0)
|
|
162
|
+
pluginFunctions.set(plugin, new Map(Object.entries(impls)));
|
|
163
|
+
}
|
|
164
|
+
/** プラグインの関数の処理(同じ名前は後に入れたプラグインのもの)。 */
|
|
165
|
+
function pluginFunction(name) {
|
|
166
|
+
let found;
|
|
167
|
+
for (const own of pluginFunctions.values())
|
|
168
|
+
found = own.get(name) ?? found;
|
|
169
|
+
return found;
|
|
170
|
+
}
|
|
171
|
+
/** プラグインの関数を呼ぶ。返した値が値の形(JSON にできるもの)でなければ誤りにする。 */
|
|
172
|
+
function callPlugin(name, run, args, env) {
|
|
173
|
+
let v;
|
|
174
|
+
try {
|
|
175
|
+
v = run(structuredClone(args), { random: () => env.random() });
|
|
176
|
+
}
|
|
177
|
+
catch (e) {
|
|
178
|
+
throw new EvalError(`${name}() が失敗しました: ${e instanceof Error ? e.message : String(e)}`);
|
|
179
|
+
}
|
|
180
|
+
if (v === undefined)
|
|
181
|
+
return null;
|
|
182
|
+
if (v === null || typeof v === "string" || typeof v === "boolean" || (typeof v === "number" && Number.isFinite(v)))
|
|
183
|
+
return v;
|
|
184
|
+
if (typeof v === "object") {
|
|
185
|
+
try {
|
|
186
|
+
return JSON.parse(JSON.stringify(v));
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
// 下で誤りにする
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
throw new EvalError(`${name}() が値(数、文字、真偽、配列、オブジェクト)でないものを返しました`);
|
|
193
|
+
}
|
|
194
|
+
function call(name, args, env) {
|
|
195
|
+
if (name === "@each") {
|
|
196
|
+
// @for が回すもの(program.ts の EACH_FUNCTION)。配列はそのまま、辞書はキーの並び
|
|
197
|
+
const v = args[0] ?? null;
|
|
198
|
+
if (Array.isArray(v))
|
|
199
|
+
return v;
|
|
200
|
+
if (v !== null && typeof v === "object")
|
|
201
|
+
return Object.keys(v);
|
|
202
|
+
throw new EvalError(`@for で回せるのは配列か辞書です(${describe(v)})`);
|
|
203
|
+
}
|
|
204
|
+
if (!Object.hasOwn(FUNCTION_ARITY, name)) {
|
|
205
|
+
const f = env.fn?.(name);
|
|
206
|
+
if (f)
|
|
207
|
+
return callUser(name, f, args, env);
|
|
208
|
+
}
|
|
209
|
+
const arity = functionArity(name);
|
|
210
|
+
if (arity && (args.length < arity[0] || args.length > arity[1]))
|
|
211
|
+
throw new EvalError(`${name}() の引数は ${arityText(arity)}です(${args.length} つ)`);
|
|
212
|
+
const num = (i) => {
|
|
213
|
+
const v = args[i];
|
|
214
|
+
if (typeof v !== "number")
|
|
215
|
+
throw new EvalError(`${name}() の ${i + 1} 番目には数値が必要です(${describe(v ?? null)})`);
|
|
216
|
+
return v;
|
|
217
|
+
};
|
|
218
|
+
const str = (i) => {
|
|
219
|
+
const v = args[i];
|
|
220
|
+
if (typeof v !== "string")
|
|
221
|
+
throw new EvalError(`${name}() の ${i + 1} 番目には文字列が必要です(${describe(v ?? null)})`);
|
|
222
|
+
return v;
|
|
223
|
+
};
|
|
224
|
+
const list = (i) => {
|
|
225
|
+
const v = args[i];
|
|
226
|
+
if (!Array.isArray(v))
|
|
227
|
+
throw new EvalError(`${name}() の ${i + 1} 番目には配列が必要です(${describe(v ?? null)})`);
|
|
228
|
+
return v;
|
|
229
|
+
};
|
|
230
|
+
const object = (i) => {
|
|
231
|
+
const v = args[i];
|
|
232
|
+
if (!v || typeof v !== "object" || Array.isArray(v))
|
|
233
|
+
throw new EvalError(`${name}() の ${i + 1} 番目にはオブジェクトが必要です(${describe(v ?? null)})`);
|
|
234
|
+
return v;
|
|
235
|
+
};
|
|
236
|
+
const date = new Date();
|
|
237
|
+
switch (name) {
|
|
238
|
+
case "random":
|
|
239
|
+
// random() は 0 以上 1 未満、random(n) は 0 以上 n 未満の整数。
|
|
240
|
+
return args.length === 0 ? env.random() : Math.floor(env.random() * num(0));
|
|
241
|
+
case "randint": {
|
|
242
|
+
const [a, b] = [Math.ceil(num(0)), Math.floor(num(1))];
|
|
243
|
+
return a + Math.floor(env.random() * (b - a + 1));
|
|
244
|
+
}
|
|
245
|
+
case "pick": {
|
|
246
|
+
const xs = list(0);
|
|
247
|
+
return xs.length === 0 ? null : xs[Math.floor(env.random() * xs.length)];
|
|
248
|
+
}
|
|
249
|
+
case "shuffle": {
|
|
250
|
+
const xs = [...list(0)];
|
|
251
|
+
for (let i = xs.length - 1; i > 0; i--) {
|
|
252
|
+
const j = Math.floor(env.random() * (i + 1));
|
|
253
|
+
[xs[i], xs[j]] = [xs[j], xs[i]];
|
|
254
|
+
}
|
|
255
|
+
return xs;
|
|
256
|
+
}
|
|
257
|
+
case "floor":
|
|
258
|
+
return Math.floor(num(0));
|
|
259
|
+
case "ceil":
|
|
260
|
+
return Math.ceil(num(0));
|
|
261
|
+
case "round": {
|
|
262
|
+
if (args.length === 1)
|
|
263
|
+
return Math.round(num(0));
|
|
264
|
+
const k = 10 ** Math.max(0, Math.floor(num(1)));
|
|
265
|
+
return Math.round(num(0) * k) / k;
|
|
266
|
+
}
|
|
267
|
+
case "abs":
|
|
268
|
+
return Math.abs(num(0));
|
|
269
|
+
case "min":
|
|
270
|
+
return Math.min(...args.map((_, i) => num(i)));
|
|
271
|
+
case "max":
|
|
272
|
+
return Math.max(...args.map((_, i) => num(i)));
|
|
273
|
+
case "clamp":
|
|
274
|
+
return Math.min(num(2), Math.max(num(1), num(0)));
|
|
275
|
+
case "sum":
|
|
276
|
+
return list(0).reduce((t, v) => {
|
|
277
|
+
if (typeof v !== "number")
|
|
278
|
+
throw new EvalError(`sum() の配列には数値だけを入れます(${describe(v)})`);
|
|
279
|
+
return t + v;
|
|
280
|
+
}, 0);
|
|
281
|
+
case "len": {
|
|
282
|
+
const v = args[0];
|
|
283
|
+
if (typeof v === "string" || Array.isArray(v))
|
|
284
|
+
return v.length;
|
|
285
|
+
throw new EvalError(`len() には文字列か配列が必要です(${describe(v ?? null)})`);
|
|
286
|
+
}
|
|
287
|
+
case "includes": {
|
|
288
|
+
const [c, x] = args;
|
|
289
|
+
if (typeof c === "string" && typeof x === "string")
|
|
290
|
+
return c.includes(x);
|
|
291
|
+
if (Array.isArray(c))
|
|
292
|
+
return c.some((e) => equals(e, x ?? null));
|
|
293
|
+
throw new EvalError(`includes() の 1 番目には文字列か配列が必要です(${describe(c ?? null)})`);
|
|
294
|
+
}
|
|
295
|
+
case "index": {
|
|
296
|
+
const [c, x] = args;
|
|
297
|
+
if (typeof c === "string" && typeof x === "string")
|
|
298
|
+
return c.indexOf(x);
|
|
299
|
+
if (Array.isArray(c))
|
|
300
|
+
return c.findIndex((e) => equals(e, x ?? null));
|
|
301
|
+
throw new EvalError(`index() の 1 番目には文字列か配列が必要です(${describe(c ?? null)})`);
|
|
302
|
+
}
|
|
303
|
+
case "slice": {
|
|
304
|
+
const c = args[0];
|
|
305
|
+
const end = args.length > 2 ? num(2) : undefined;
|
|
306
|
+
if (typeof c === "string")
|
|
307
|
+
return [...c].slice(num(1), end).join("");
|
|
308
|
+
if (Array.isArray(c))
|
|
309
|
+
return c.slice(num(1), end);
|
|
310
|
+
throw new EvalError(`slice() の 1 番目には文字列か配列が必要です(${describe(c ?? null)})`);
|
|
311
|
+
}
|
|
312
|
+
case "replace":
|
|
313
|
+
return str(0).split(str(1)).join(str(2));
|
|
314
|
+
case "upper":
|
|
315
|
+
return str(0).toUpperCase();
|
|
316
|
+
case "lower":
|
|
317
|
+
return str(0).toLowerCase();
|
|
318
|
+
case "trim":
|
|
319
|
+
return str(0).trim();
|
|
320
|
+
case "split":
|
|
321
|
+
return str(0).split(str(1));
|
|
322
|
+
case "join":
|
|
323
|
+
return list(0).map((v) => display(v)).join(args.length > 1 ? str(1) : "");
|
|
324
|
+
case "pad":
|
|
325
|
+
return display(args[0] ?? null).padStart(Math.max(0, Math.floor(num(1))), args.length > 2 ? str(2) : "0");
|
|
326
|
+
case "str":
|
|
327
|
+
return display(args[0] ?? null);
|
|
328
|
+
case "num": {
|
|
329
|
+
const v = args[0];
|
|
330
|
+
if (typeof v === "number")
|
|
331
|
+
return v;
|
|
332
|
+
if (typeof v !== "string" || v.trim() === "")
|
|
333
|
+
return null;
|
|
334
|
+
const n = Number(v);
|
|
335
|
+
return Number.isFinite(n) ? n : null;
|
|
336
|
+
}
|
|
337
|
+
case "push":
|
|
338
|
+
return [...list(0), args[1] ?? null];
|
|
339
|
+
case "remove":
|
|
340
|
+
return list(0).filter((e) => !equals(e, args[1] ?? null));
|
|
341
|
+
case "sort":
|
|
342
|
+
return [...list(0)].sort((a, b) => (typeof a === "number" && typeof b === "number" ? a - b : display(a) < display(b) ? -1 : display(a) > display(b) ? 1 : 0));
|
|
343
|
+
case "reverse":
|
|
344
|
+
return [...list(0)].reverse();
|
|
345
|
+
case "unique":
|
|
346
|
+
return list(0).filter((e, i, xs) => xs.findIndex((x) => equals(x, e)) === i);
|
|
347
|
+
case "keys":
|
|
348
|
+
return Object.keys(object(0));
|
|
349
|
+
case "has":
|
|
350
|
+
return Object.hasOwn(object(0), str(1));
|
|
351
|
+
case "version_before":
|
|
352
|
+
return compareVersions(versionText(args[0]), versionText(args[1])) < 0;
|
|
353
|
+
case "year":
|
|
354
|
+
return date.getFullYear();
|
|
355
|
+
case "month":
|
|
356
|
+
return date.getMonth() + 1;
|
|
357
|
+
case "day":
|
|
358
|
+
return date.getDate();
|
|
359
|
+
case "hour":
|
|
360
|
+
return date.getHours();
|
|
361
|
+
case "minute":
|
|
362
|
+
return date.getMinutes();
|
|
363
|
+
case "weekday":
|
|
364
|
+
return date.getDay();
|
|
365
|
+
case "now":
|
|
366
|
+
return date.getTime();
|
|
367
|
+
case "visits":
|
|
368
|
+
case "read":
|
|
369
|
+
case "chosen":
|
|
370
|
+
str(0);
|
|
371
|
+
return env.story ? env.story(name, args) : name === "visits" ? 0 : false;
|
|
372
|
+
case "lang":
|
|
373
|
+
case "platform":
|
|
374
|
+
case "app":
|
|
375
|
+
case "replaying":
|
|
376
|
+
case "chapter":
|
|
377
|
+
return env.story ? env.story(name, args) : name === "lang" || name === "chapter" ? "" : name === "platform" ? "pc" : false;
|
|
378
|
+
case "endings":
|
|
379
|
+
// 見たエンディングの数(全セーブ共通。実行器が答える)
|
|
380
|
+
return env.story ? env.story(name, args) : 0;
|
|
381
|
+
case "item": {
|
|
382
|
+
// 持ち物の数(ゲームの変数の、式からは書けない名前のところ。@liminalia/lang の ITEMS_KEY)
|
|
383
|
+
const items = env.lookup("game")?.["@items"];
|
|
384
|
+
const key = args[0];
|
|
385
|
+
if (typeof key !== "string")
|
|
386
|
+
throw new EvalError(`item() には持ち物の名前(文字列)が必要です(${describe(key ?? null)})`);
|
|
387
|
+
const count = items && typeof items === "object" && !Array.isArray(items) && Object.hasOwn(items, key) ? items[key] : 0;
|
|
388
|
+
return typeof count === "number" ? count : 0;
|
|
389
|
+
}
|
|
390
|
+
default: {
|
|
391
|
+
const run = pluginFunction(name);
|
|
392
|
+
if (run)
|
|
393
|
+
return callPlugin(name, run, args, env);
|
|
394
|
+
throw new EvalError(`関数 ${name}() はありません`);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/** @function の関数を呼ぶ。中で読めるのは引数と game・system・temp・save だけ(呼んだ側の @let は見えない)。 */
|
|
399
|
+
function callUser(name, f, args, env) {
|
|
400
|
+
if (args.length !== f.params.length)
|
|
401
|
+
throw new EvalError(`${name}() の引数は ${f.params.length} つです(${args.length} つ)`);
|
|
402
|
+
const depth = (env.depth ?? 0) + 1;
|
|
403
|
+
if (depth > MAX_FUNCTION_DEPTH)
|
|
404
|
+
throw new EvalError(`${name}() の呼び出しが ${MAX_FUNCTION_DEPTH} 段を超えました(自分を呼び続けていないか確かめます)`);
|
|
405
|
+
const local = new Map(f.params.map((p, i) => [p, args[i] ?? null]));
|
|
406
|
+
const inner = {
|
|
407
|
+
...env,
|
|
408
|
+
depth,
|
|
409
|
+
lookup: (n) => (local.has(n) ? local.get(n) : n === "game" || n === "system" || n === "temp" || n === "save" ? env.lookup(n) : null),
|
|
410
|
+
...(env.fn ? { fn: (n, file) => env.fn(n, file ?? f.file) } : {}),
|
|
411
|
+
};
|
|
412
|
+
return evaluate(f.body, inner);
|
|
413
|
+
}
|
|
414
|
+
function describe(v) {
|
|
415
|
+
if (v === null)
|
|
416
|
+
return "null";
|
|
417
|
+
if (Array.isArray(v))
|
|
418
|
+
return "配列";
|
|
419
|
+
if (typeof v === "object")
|
|
420
|
+
return "オブジェクト";
|
|
421
|
+
return `${typeof v === "string" ? "文字列" : typeof v === "number" ? "数値" : "真偽値"} ${JSON.stringify(v)}`;
|
|
422
|
+
}
|
|
423
|
+
/** シード付きの乱数(mulberry32)。状態は数値一つなのでセーブできる。 */
|
|
424
|
+
export function nextRandom(seed) {
|
|
425
|
+
let t = (seed + 0x6d2b79f5) | 0;
|
|
426
|
+
const next = t;
|
|
427
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
428
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
429
|
+
return { value: ((t ^ (t >>> 14)) >>> 0) / 4294967296, seed: next };
|
|
430
|
+
}
|
|
431
|
+
/** version_before の版(文字列か数。null と空は「版の無い」もの)。 */
|
|
432
|
+
function versionText(v) {
|
|
433
|
+
if (v === undefined || v === null)
|
|
434
|
+
return "";
|
|
435
|
+
if (typeof v === "string" || typeof v === "number")
|
|
436
|
+
return String(v).trim();
|
|
437
|
+
throw new EvalError(`version_before() にはバージョン文字列("1.2.0" など)が必要です(${describe(v)})`);
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* 版を比べる(1.2.10 は 1.2.9 より後。- の後ろの付いた 1.0.0-beta は 1.0.0 より前。頭の v は読まない。空の版はどの版よりも前)。
|
|
441
|
+
* a が前なら負、同じなら 0、後なら正。デスクトップ版の更新(packages/desktop/update.cjs の compareVersions)と同じ決まり。
|
|
442
|
+
*/
|
|
443
|
+
export function compareVersions(a, b) {
|
|
444
|
+
if (a === "" || b === "")
|
|
445
|
+
return a === b ? 0 : a === "" ? -1 : 1;
|
|
446
|
+
const split = (v) => {
|
|
447
|
+
const [main = "", pre] = v.replace(/^v/, "").split("-", 2);
|
|
448
|
+
return { nums: main.split(".").map((x) => Number.parseInt(x, 10) || 0), pre: pre ?? null };
|
|
449
|
+
};
|
|
450
|
+
const x = split(a);
|
|
451
|
+
const y = split(b);
|
|
452
|
+
for (let i = 0; i < Math.max(x.nums.length, y.nums.length); i++) {
|
|
453
|
+
const d = (x.nums[i] ?? 0) - (y.nums[i] ?? 0);
|
|
454
|
+
if (d !== 0)
|
|
455
|
+
return d > 0 ? 1 : -1;
|
|
456
|
+
}
|
|
457
|
+
if (x.pre === y.pre)
|
|
458
|
+
return 0;
|
|
459
|
+
if (x.pre === null)
|
|
460
|
+
return 1;
|
|
461
|
+
if (y.pre === null)
|
|
462
|
+
return -1;
|
|
463
|
+
return x.pre.localeCompare(y.pre, "en", { numeric: true }) > 0 ? 1 : -1;
|
|
464
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { autoVoicedLines, packCombinations, voiceLines } from "@liminalia/lang";
|
|
2
|
+
import { explore } from "./route.js";
|
|
3
|
+
/** 一回の流しで出すページの上限(終わらない作りでも止める)。 */
|
|
4
|
+
const MAX_PAGES = 200_000;
|
|
5
|
+
/**
|
|
6
|
+
* 「はじめから」を画面なしで何通りか流し、台詞の鍵(voiceLines)→ 鳴った連番を集める(lim voice-lock と制作画面の「番号を固定」)。
|
|
7
|
+
* 固定がまだ無いときに、今の数え方で鳴る番号をそのまま固定するために使う。選び方は、まだ試していないものへ広げていく
|
|
8
|
+
* (全ルートテストと同じ。route.ts の explore)。runs は流す回数の上限で、選び方を試し終えたら早く止める。
|
|
9
|
+
* 数え方の番号を見るので、渡した設定に番号の固定(voiceLock)があっても使わない(使うと、固定に無い台詞が鳴らない)。
|
|
10
|
+
*/
|
|
11
|
+
export async function observeVoiceNumbers(o) {
|
|
12
|
+
const seen = await observeOnce(o.scripts, o.entry, (file) => o.loadSource(file), o.runtime, o.runs);
|
|
13
|
+
const mainKeys = new Set([...o.scripts.values()].flatMap((script) => voiceLines(script).map((l) => l.key)));
|
|
14
|
+
const packs = (o.packs ?? []).filter((p) => p.scripts.size > 0);
|
|
15
|
+
const packKeys = packs.map((p) => new Set([...p.scripts.values()].flatMap((script) => voiceLines(script).map((l) => l.key)).filter((k) => !mainKeys.has(k))));
|
|
16
|
+
for (const combo of packCombinations(packs.length)) {
|
|
17
|
+
if (combo.length === 0)
|
|
18
|
+
continue;
|
|
19
|
+
const chosen = combo.map((i) => packs[i]);
|
|
20
|
+
const set = new Map([...o.scripts, ...chosen.flatMap((p) => [...p.scripts])]);
|
|
21
|
+
// 写す台詞: この組で連番が鳴りうる(autoVoicedLines。番号を振る updateVoiceLock と同じ)、この組のパックにしか無い、まだ写していない台詞
|
|
22
|
+
const voiced = autoVoicedLines(set, { entry: o.entry });
|
|
23
|
+
const wanted = new Set();
|
|
24
|
+
for (const [name, script] of set) {
|
|
25
|
+
if (!chosen.some((p) => p.scripts.get(name) === script))
|
|
26
|
+
continue;
|
|
27
|
+
for (const l of voiceLines(script))
|
|
28
|
+
if (voiced.has(`${l.file}:${l.line}`) && combo.some((i) => packKeys[i].has(l.key)) && !seen.has(l.key))
|
|
29
|
+
wanted.add(l.key);
|
|
30
|
+
}
|
|
31
|
+
if (wanted.size === 0)
|
|
32
|
+
continue;
|
|
33
|
+
const source = async (file) => {
|
|
34
|
+
// その組に無い台本(ほかのパックにだけある台本へ飛ぶパックなど)は、そこで終わる空の台本にする(組み合わせによっては無いのが正しい)
|
|
35
|
+
if (!set.has(file))
|
|
36
|
+
return "";
|
|
37
|
+
for (const p of [...chosen].reverse()) {
|
|
38
|
+
const text = p.source(file);
|
|
39
|
+
if (text !== undefined)
|
|
40
|
+
return text;
|
|
41
|
+
}
|
|
42
|
+
return o.loadSource(file);
|
|
43
|
+
};
|
|
44
|
+
const observed = await observeOnce(set, o.entry, source, o.runtime, o.runs);
|
|
45
|
+
for (const [key, numbers] of observed)
|
|
46
|
+
if (wanted.has(key))
|
|
47
|
+
for (const n of numbers)
|
|
48
|
+
(seen.get(key) ?? seen.set(key, new Set()).get(key)).add(n);
|
|
49
|
+
}
|
|
50
|
+
return seen;
|
|
51
|
+
}
|
|
52
|
+
/** scripts(一緒に遊ばれる台本)を「はじめから」何通りか流し、台詞の鍵 → 鳴った連番を集める。 */
|
|
53
|
+
async function observeOnce(scripts, entry, loadSource, options, runs) {
|
|
54
|
+
const o = { scripts, entry, loadSource, runtime: options, runs };
|
|
55
|
+
const keys = new Map();
|
|
56
|
+
for (const [file, script] of o.scripts)
|
|
57
|
+
keys.set(file, new Map(voiceLines(script).map((l) => [l.line, l.key])));
|
|
58
|
+
const seen = new Map();
|
|
59
|
+
const { onStep: _onStep, onAssign: _onAssign, ...runtime } = o.runtime;
|
|
60
|
+
let pages = 0;
|
|
61
|
+
await explore({
|
|
62
|
+
start: o.entry,
|
|
63
|
+
runs: o.runs,
|
|
64
|
+
loadSource: (file) => o.loadSource(file),
|
|
65
|
+
runtime: { onError: "continue", maxSteps: 5_000_000, ...runtime, voiceLock: null },
|
|
66
|
+
begin: () => {
|
|
67
|
+
pages = 0;
|
|
68
|
+
},
|
|
69
|
+
present: (e, w) => {
|
|
70
|
+
if (e.type !== "page")
|
|
71
|
+
return;
|
|
72
|
+
if (++pages >= MAX_PAGES)
|
|
73
|
+
w.runtime.stop();
|
|
74
|
+
if (e.voice?.n === undefined)
|
|
75
|
+
return;
|
|
76
|
+
const key = keys.get(e.at.file)?.get(e.at.line);
|
|
77
|
+
if (key)
|
|
78
|
+
(seen.get(key) ?? seen.set(key, new Set()).get(key)).add(e.voice.n);
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
return seen;
|
|
82
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liminalia/runtime",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.8.4",
|
|
4
|
+
"description": ".lim を実行する。状態は JSON にできる素のデータだけで持ち、描画・音・入力は外から差し込む",
|
|
5
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
6
|
+
"homepage": "https://liminalia.skyta.dev/",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/skyt-a/liminalia-sdk.git",
|
|
10
|
+
"directory": "packages/runtime"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./types/src/index.d.ts",
|
|
16
|
+
"node": "./dist/src/index.js",
|
|
17
|
+
"default": "./src/index.ts"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"types",
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": "^22.23.4 || ^24.22.1 || >=26.12.1"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@liminalia/lang": "0.8.4"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
}
|
|
35
|
+
}
|