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, 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 { 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`)を追加しました。