Skip to content

Commit bc7010c

Browse files
enhance: null値のアサーション関数を追加 (#992)
* enhance: null値のアサーション関数を追加 * Update Changelog * api extractor for null assertion --------- Co-authored-by: Take-John <takejohn@takejohn.jp>
1 parent d8de854 commit bc7010c

File tree

3 files changed

+57
-35
lines changed

3 files changed

+57
-35
lines changed

etc/aiscript.api.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ function assertBoolean(val: Value | null | undefined): asserts val is VBool;
123123
// @public (undocumented)
124124
function assertFunction(val: Value | null | undefined): asserts val is VFn;
125125

126+
// @public (undocumented)
127+
function assertNull(val: Value | null | undefined): asserts val is VNull;
128+
126129
// @public (undocumented)
127130
function assertNumber(val: Value | null | undefined): asserts val is VNum;
128131

@@ -466,6 +469,9 @@ function isExpression(x: Node_2): x is Expression;
466469
// @public (undocumented)
467470
function isFunction(val: Value): val is VFn;
468471

472+
// @public (undocumented)
473+
function isNull(val: Value): val is VNull;
474+
469475
// @public (undocumented)
470476
function isNumber(val: Value): val is VNum;
471477

@@ -757,18 +763,20 @@ type UnionTypeSource = NodeBase & {
757763
declare namespace utils {
758764
export {
759765
expectAny,
760-
assertBoolean,
761-
assertFunction,
762-
assertString,
763-
assertNumber,
764-
assertObject,
765-
assertArray,
766766
isBoolean,
767767
isFunction,
768768
isString,
769769
isNumber,
770770
isObject,
771771
isArray,
772+
isNull,
773+
assertBoolean,
774+
assertFunction,
775+
assertString,
776+
assertNumber,
777+
assertObject,
778+
assertArray,
779+
assertNull,
772780
eq,
773781
valToString,
774782
valToJs,

src/interpreter/util.ts

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,46 @@
11
import { AiScriptRuntimeError } from '../error.js';
22
import { STR, NUM, ARR, OBJ, NULL, BOOL } from './value.js';
3-
import type { Value, VStr, VNum, VBool, VFn, VObj, VArr } from './value.js';
3+
import type { Value, VStr, VNum, VBool, VFn, VObj, VArr, VNull } from './value.js';
44

55
export function expectAny(val: Value | null | undefined): asserts val is Value {
66
if (val == null) {
77
throw new AiScriptRuntimeError('Expect anything, but got nothing.');
88
}
99
}
1010

11+
export function isBoolean(val: Value): val is VBool {
12+
return val.type === 'bool';
13+
}
14+
15+
export function isFunction(val: Value): val is VFn {
16+
return val.type === 'fn';
17+
}
18+
19+
export function isString(val: Value): val is VStr {
20+
return val.type === 'str';
21+
}
22+
23+
export function isNumber(val: Value): val is VNum {
24+
return val.type === 'num';
25+
}
26+
27+
export function isObject(val: Value): val is VObj {
28+
return val.type === 'obj';
29+
}
30+
31+
export function isArray(val: Value): val is VArr {
32+
return val.type === 'arr';
33+
}
34+
35+
export function isNull(val: Value): val is VNull {
36+
return val.type === 'null';
37+
}
38+
1139
export function assertBoolean(val: Value | null | undefined): asserts val is VBool {
1240
if (val == null) {
1341
throw new AiScriptRuntimeError('Expect boolean, but got nothing.');
1442
}
15-
if (val.type !== 'bool') {
43+
if (!isBoolean(val)) {
1644
throw new AiScriptRuntimeError(`Expect boolean, but got ${val.type}.`);
1745
}
1846
}
@@ -21,7 +49,7 @@ export function assertFunction(val: Value | null | undefined): asserts val is VF
2149
if (val == null) {
2250
throw new AiScriptRuntimeError('Expect function, but got nothing.');
2351
}
24-
if (val.type !== 'fn') {
52+
if (!isFunction(val)) {
2553
throw new AiScriptRuntimeError(`Expect function, but got ${val.type}.`);
2654
}
2755
}
@@ -30,7 +58,7 @@ export function assertString(val: Value | null | undefined): asserts val is VStr
3058
if (val == null) {
3159
throw new AiScriptRuntimeError('Expect string, but got nothing.');
3260
}
33-
if (val.type !== 'str') {
61+
if (!isString(val)) {
3462
throw new AiScriptRuntimeError(`Expect string, but got ${val.type}.`);
3563
}
3664
}
@@ -39,7 +67,7 @@ export function assertNumber(val: Value | null | undefined): asserts val is VNum
3967
if (val == null) {
4068
throw new AiScriptRuntimeError('Expect number, but got nothing.');
4169
}
42-
if (val.type !== 'num') {
70+
if (!isNumber(val)) {
4371
throw new AiScriptRuntimeError(`Expect number, but got ${val.type}.`);
4472
}
4573
}
@@ -48,7 +76,7 @@ export function assertObject(val: Value | null | undefined): asserts val is VObj
4876
if (val == null) {
4977
throw new AiScriptRuntimeError('Expect object, but got nothing.');
5078
}
51-
if (val.type !== 'obj') {
79+
if (!isObject(val)) {
5280
throw new AiScriptRuntimeError(`Expect object, but got ${val.type}.`);
5381
}
5482
}
@@ -57,33 +85,18 @@ export function assertArray(val: Value | null | undefined): asserts val is VArr
5785
if (val == null) {
5886
throw new AiScriptRuntimeError('Expect array, but got nothing.');
5987
}
60-
if (val.type !== 'arr') {
88+
if (!isArray(val)) {
6189
throw new AiScriptRuntimeError(`Expect array, but got ${val.type}.`);
6290
}
6391
}
6492

65-
export function isBoolean(val: Value): val is VBool {
66-
return val.type === 'bool';
67-
}
68-
69-
export function isFunction(val: Value): val is VFn {
70-
return val.type === 'fn';
71-
}
72-
73-
export function isString(val: Value): val is VStr {
74-
return val.type === 'str';
75-
}
76-
77-
export function isNumber(val: Value): val is VNum {
78-
return val.type === 'num';
79-
}
80-
81-
export function isObject(val: Value): val is VObj {
82-
return val.type === 'obj';
83-
}
84-
85-
export function isArray(val: Value): val is VArr {
86-
return val.type === 'arr';
93+
export function assertNull(val: Value | null | undefined): asserts val is VNull {
94+
if (val == null) {
95+
throw new AiScriptRuntimeError('Expect null, but got nothing.');
96+
}
97+
if (!isNull(val)) {
98+
throw new AiScriptRuntimeError(`Expect null, but got ${val.type}.`);
99+
}
87100
}
88101

89102
export function eq(a: Value, b: Value): boolean {

unreleased/add-null-assertion.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- null値のアサーション関数(`isNull`, `assertNull`)を追加しました。

0 commit comments

Comments
 (0)