From ed362ece4d108eac98f252a4d1fcc3cac67070a7 Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Mon, 24 Nov 2025 12:24:34 +0900 Subject: [PATCH 1/3] =?UTF-8?q?enhance:=20null=E5=80=A4=E3=81=AE=E3=82=A2?= =?UTF-8?q?=E3=82=B5=E3=83=BC=E3=82=B7=E3=83=A7=E3=83=B3=E9=96=A2=E6=95=B0?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interpreter/util.ts | 71 ++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/src/interpreter/util.ts b/src/interpreter/util.ts index ccc5a9ae..33d7488d 100644 --- a/src/interpreter/util.ts +++ b/src/interpreter/util.ts @@ -1,6 +1,6 @@ import { AiScriptRuntimeError } from '../error.js'; import { STR, NUM, ARR, OBJ, NULL, BOOL } from './value.js'; -import type { Value, VStr, VNum, VBool, VFn, VObj, VArr } from './value.js'; +import type { Value, VStr, VNum, VBool, VFn, VObj, VArr, VNull } from './value.js'; export function expectAny(val: Value | null | undefined): asserts val is Value { if (val == null) { @@ -8,11 +8,39 @@ export function expectAny(val: Value | null | undefined): asserts val is Value { } } +export function isBoolean(val: Value): val is VBool { + return val.type === 'bool'; +} + +export function isFunction(val: Value): val is VFn { + return val.type === 'fn'; +} + +export function isString(val: Value): val is VStr { + return val.type === 'str'; +} + +export function isNumber(val: Value): val is VNum { + return val.type === 'num'; +} + +export function isObject(val: Value): val is VObj { + return val.type === 'obj'; +} + +export function isArray(val: Value): val is VArr { + return val.type === 'arr'; +} + +export function isNull(val: Value): val is VNull { + return val.type === 'null'; +} + export function assertBoolean(val: Value | null | undefined): asserts val is VBool { if (val == null) { throw new AiScriptRuntimeError('Expect boolean, but got nothing.'); } - if (val.type !== 'bool') { + if (!isBoolean(val)) { throw new AiScriptRuntimeError(`Expect boolean, but got ${val.type}.`); } } @@ -21,7 +49,7 @@ export function assertFunction(val: Value | null | undefined): asserts val is VF if (val == null) { throw new AiScriptRuntimeError('Expect function, but got nothing.'); } - if (val.type !== 'fn') { + if (!isFunction(val)) { throw new AiScriptRuntimeError(`Expect function, but got ${val.type}.`); } } @@ -30,7 +58,7 @@ export function assertString(val: Value | null | undefined): asserts val is VStr if (val == null) { throw new AiScriptRuntimeError('Expect string, but got nothing.'); } - if (val.type !== 'str') { + if (!isString(val)) { throw new AiScriptRuntimeError(`Expect string, but got ${val.type}.`); } } @@ -39,7 +67,7 @@ export function assertNumber(val: Value | null | undefined): asserts val is VNum if (val == null) { throw new AiScriptRuntimeError('Expect number, but got nothing.'); } - if (val.type !== 'num') { + if (!isNumber(val)) { throw new AiScriptRuntimeError(`Expect number, but got ${val.type}.`); } } @@ -48,7 +76,7 @@ export function assertObject(val: Value | null | undefined): asserts val is VObj if (val == null) { throw new AiScriptRuntimeError('Expect object, but got nothing.'); } - if (val.type !== 'obj') { + if (!isObject(val)) { throw new AiScriptRuntimeError(`Expect object, but got ${val.type}.`); } } @@ -57,33 +85,18 @@ export function assertArray(val: Value | null | undefined): asserts val is VArr if (val == null) { throw new AiScriptRuntimeError('Expect array, but got nothing.'); } - if (val.type !== 'arr') { + if (!isArray(val)) { throw new AiScriptRuntimeError(`Expect array, but got ${val.type}.`); } } -export function isBoolean(val: Value): val is VBool { - return val.type === 'bool'; -} - -export function isFunction(val: Value): val is VFn { - return val.type === 'fn'; -} - -export function isString(val: Value): val is VStr { - return val.type === 'str'; -} - -export function isNumber(val: Value): val is VNum { - return val.type === 'num'; -} - -export function isObject(val: Value): val is VObj { - return val.type === 'obj'; -} - -export function isArray(val: Value): val is VArr { - return val.type === 'arr'; +export function assertNull(val: Value | null | undefined): asserts val is VNull { + if (val == null) { + throw new AiScriptRuntimeError('Expect null, but got nothing.'); + } + if (!isNull(val)) { + throw new AiScriptRuntimeError(`Expect null, but got ${val.type}.`); + } } export function eq(a: Value, b: Value): boolean { From 7b0ff4268f3333e641f47fcdcfdf721cd17ef5dd Mon Sep 17 00:00:00 2001 From: kakkokari-gtyih <67428053+kakkokari-gtyih@users.noreply.github.com> Date: Mon, 24 Nov 2025 12:25:30 +0900 Subject: [PATCH 2/3] Update Changelog --- unreleased/add-null-assertion.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 unreleased/add-null-assertion.md diff --git a/unreleased/add-null-assertion.md b/unreleased/add-null-assertion.md new file mode 100644 index 00000000..4c8d922a --- /dev/null +++ b/unreleased/add-null-assertion.md @@ -0,0 +1 @@ +- null値のアサーション関数(`isNull`, `assertNull`)を追加しました。 From 8fc83c6f853016e3199509a032187e2f718d3a38 Mon Sep 17 00:00:00 2001 From: takejohn Date: Mon, 24 Nov 2025 13:12:55 +0900 Subject: [PATCH 3/3] api extractor for null assertion --- etc/aiscript.api.md | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/etc/aiscript.api.md b/etc/aiscript.api.md index 7ece385f..ac275635 100644 --- a/etc/aiscript.api.md +++ b/etc/aiscript.api.md @@ -123,6 +123,9 @@ function assertBoolean(val: Value | null | undefined): asserts val is VBool; // @public (undocumented) function assertFunction(val: Value | null | undefined): asserts val is VFn; +// @public (undocumented) +function assertNull(val: Value | null | undefined): asserts val is VNull; + // @public (undocumented) function assertNumber(val: Value | null | undefined): asserts val is VNum; @@ -466,6 +469,9 @@ function isExpression(x: Node_2): x is Expression; // @public (undocumented) function isFunction(val: Value): val is VFn; +// @public (undocumented) +function isNull(val: Value): val is VNull; + // @public (undocumented) function isNumber(val: Value): val is VNum; @@ -757,18 +763,20 @@ type UnionTypeSource = NodeBase & { declare namespace utils { export { expectAny, - assertBoolean, - assertFunction, - assertString, - assertNumber, - assertObject, - assertArray, isBoolean, isFunction, isString, isNumber, isObject, isArray, + isNull, + assertBoolean, + assertFunction, + assertString, + assertNumber, + assertObject, + assertArray, + assertNull, eq, valToString, valToJs,