Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions etc/aiscript.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
71 changes: 42 additions & 29 deletions src/interpreter/util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
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) {
throw new AiScriptRuntimeError('Expect anything, but got nothing.');
}
}

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}.`);
}
}
Expand All @@ -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}.`);
}
}
Expand All @@ -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}.`);
}
}
Expand All @@ -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}.`);
}
}
Expand All @@ -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}.`);
}
}
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions unreleased/add-null-assertion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- null値のアサーション関数(`isNull`, `assertNull`)を追加しました。
Loading