@mirascript/mirascript 0.1.63 → 0.1.65

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.
Files changed (84) hide show
  1. package/README.md +7 -8
  2. package/dist/{chunk-5BBTXV5A.js → chunk-MRA5WJ72.js} +386 -402
  3. package/dist/chunk-MRA5WJ72.js.map +6 -0
  4. package/dist/{chunk-HZYARHEQ.js → chunk-X4FUFXO6.js} +17 -1
  5. package/dist/chunk-X4FUFXO6.js.map +6 -0
  6. package/dist/compiler/worker.js +1 -1
  7. package/dist/helpers/constants.d.ts +1 -0
  8. package/dist/helpers/constants.d.ts.map +1 -1
  9. package/dist/helpers/utils.d.ts +2 -0
  10. package/dist/helpers/utils.d.ts.map +1 -1
  11. package/dist/index.js +2 -2
  12. package/dist/subtle.js +2 -2
  13. package/dist/vm/lib/global/bit.d.ts.map +1 -1
  14. package/dist/vm/lib/global/debug.d.ts.map +1 -1
  15. package/dist/vm/lib/global/json.d.ts.map +1 -1
  16. package/dist/vm/lib/global/math/arr.d.ts.map +1 -1
  17. package/dist/vm/lib/global/math/const.d.ts.map +1 -1
  18. package/dist/vm/lib/global/math/gamma.d.ts.map +1 -1
  19. package/dist/vm/lib/global/math/index.d.ts.map +1 -1
  20. package/dist/vm/lib/global/math/round.d.ts.map +1 -1
  21. package/dist/vm/lib/global/math/unary.d.ts.map +1 -1
  22. package/dist/vm/lib/global/sequence/all-any.d.ts.map +1 -1
  23. package/dist/vm/lib/global/sequence/entries.d.ts.map +1 -1
  24. package/dist/vm/lib/global/sequence/find.d.ts.map +1 -1
  25. package/dist/vm/lib/global/sequence/flatten.d.ts.map +1 -1
  26. package/dist/vm/lib/global/sequence/len.d.ts.map +1 -1
  27. package/dist/vm/lib/global/sequence/map-filter.d.ts.map +1 -1
  28. package/dist/vm/lib/global/sequence/new.d.ts.map +1 -1
  29. package/dist/vm/lib/global/sequence/repeat.d.ts.map +1 -1
  30. package/dist/vm/lib/global/sequence/reverse.d.ts.map +1 -1
  31. package/dist/vm/lib/global/sequence/sort.d.ts.map +1 -1
  32. package/dist/vm/lib/global/sequence/unique.d.ts.map +1 -1
  33. package/dist/vm/lib/global/sequence/with.d.ts.map +1 -1
  34. package/dist/vm/lib/global/sequence/zip.d.ts.map +1 -1
  35. package/dist/vm/lib/global/string/case.d.ts.map +1 -1
  36. package/dist/vm/lib/global/string/index.d.ts.map +1 -1
  37. package/dist/vm/lib/global/string/search.d.ts.map +1 -1
  38. package/dist/vm/lib/global/string/trim.d.ts.map +1 -1
  39. package/dist/vm/lib/global/time.d.ts.map +1 -1
  40. package/dist/vm/lib/global/to-primitive.d.ts.map +1 -1
  41. package/dist/vm/lib/helpers.d.ts +1 -1
  42. package/dist/vm/lib/helpers.d.ts.map +1 -1
  43. package/dist/vm/lib/mod/matrix.d.ts.map +1 -1
  44. package/dist/vm/types/context.d.ts.map +1 -1
  45. package/dist/vm/types/function.d.ts +8 -7
  46. package/dist/vm/types/function.d.ts.map +1 -1
  47. package/package.json +5 -5
  48. package/src/helpers/constants.ts +2 -0
  49. package/src/helpers/utils.ts +7 -0
  50. package/src/vm/lib/global/bit.ts +23 -21
  51. package/src/vm/lib/global/debug.ts +6 -6
  52. package/src/vm/lib/global/json.ts +7 -6
  53. package/src/vm/lib/global/math/arr.ts +10 -15
  54. package/src/vm/lib/global/math/const.ts +0 -10
  55. package/src/vm/lib/global/math/gamma.ts +4 -6
  56. package/src/vm/lib/global/math/index.ts +11 -8
  57. package/src/vm/lib/global/math/round.ts +20 -12
  58. package/src/vm/lib/global/math/unary.ts +44 -66
  59. package/src/vm/lib/global/sequence/all-any.ts +14 -10
  60. package/src/vm/lib/global/sequence/entries.ts +8 -9
  61. package/src/vm/lib/global/sequence/find.ts +6 -7
  62. package/src/vm/lib/global/sequence/flatten.ts +5 -3
  63. package/src/vm/lib/global/sequence/len.ts +2 -3
  64. package/src/vm/lib/global/sequence/map-filter.ts +18 -21
  65. package/src/vm/lib/global/sequence/new.ts +13 -6
  66. package/src/vm/lib/global/sequence/repeat.ts +3 -7
  67. package/src/vm/lib/global/sequence/reverse.ts +2 -3
  68. package/src/vm/lib/global/sequence/sort.ts +16 -16
  69. package/src/vm/lib/global/sequence/unique.ts +16 -16
  70. package/src/vm/lib/global/sequence/with.ts +6 -7
  71. package/src/vm/lib/global/sequence/zip.ts +2 -3
  72. package/src/vm/lib/global/string/case.ts +4 -6
  73. package/src/vm/lib/global/string/index.ts +18 -12
  74. package/src/vm/lib/global/string/search.ts +15 -9
  75. package/src/vm/lib/global/string/trim.ts +6 -9
  76. package/src/vm/lib/global/time.ts +10 -13
  77. package/src/vm/lib/global/to-primitive.ts +15 -15
  78. package/src/vm/lib/helpers.ts +1 -6
  79. package/src/vm/lib/mod/matrix.ts +48 -39
  80. package/src/vm/operations/compound.ts +2 -2
  81. package/src/vm/types/context.ts +17 -20
  82. package/src/vm/types/function.ts +9 -10
  83. package/dist/chunk-5BBTXV5A.js.map +0 -6
  84. package/dist/chunk-HZYARHEQ.js.map +0 -6
@@ -10,9 +10,8 @@ export const chars = VmLib(
10
10
  },
11
11
  {
12
12
  summary: '将字符串转换为字符数组',
13
- params: { str: '要转换的字符串' },
14
- paramsType: { str: 'string' },
15
- returnsType: 'string[]',
13
+ params: { str: { type: 'string', description: '要转换的字符串' } },
14
+ returns: { type: 'string[]' },
16
15
  examples: ['chars("Mira") // ["M", "i", "r", "a"]'],
17
16
  },
18
17
  );
@@ -26,9 +25,12 @@ export const replace = VmLib(
26
25
  },
27
26
  {
28
27
  summary: '替换字符串中的指定子串',
29
- params: { str: '要处理的字符串', search: '要替换的子串', replacement: '替换后的字符串' },
30
- paramsType: { str: 'string', search: 'string', replacement: 'string' },
31
- returnsType: 'string',
28
+ params: {
29
+ str: { type: 'string', description: '要处理的字符串' },
30
+ search: { type: 'string', description: '要替换的子串' },
31
+ replacement: { type: 'string', description: '替换后的字符串' },
32
+ },
33
+ returns: { type: 'string' },
32
34
  examples: ['replace("foo bar foo", "foo", "baz") // "baz bar baz"'],
33
35
  },
34
36
  );
@@ -42,9 +44,11 @@ export const split = VmLib(
42
44
  },
43
45
  {
44
46
  summary: '将字符串拆分为子串数组',
45
- params: { str: '要拆分的字符串', separator: '分隔符' },
46
- paramsType: { str: 'string', separator: 'string' },
47
- returnsType: 'string[]',
47
+ params: {
48
+ str: { type: 'string', description: '要拆分的字符串' },
49
+ separator: { type: 'string', description: '分隔符' },
50
+ },
51
+ returns: { type: 'string[]' },
48
52
  examples: ['split("a,b,c", ",") // ["a", "b", "c"]'],
49
53
  },
50
54
  );
@@ -57,9 +61,11 @@ export const join = VmLib(
57
61
  },
58
62
  {
59
63
  summary: '将字符串数组连接为单个字符串',
60
- params: { arr: '要连接的字符串数组', separator: '分隔符' },
61
- paramsType: { arr: 'string[]', separator: 'string' },
62
- returnsType: 'string',
64
+ params: {
65
+ arr: { type: 'string[]', description: '要连接的字符串数组' },
66
+ separator: { type: 'string', description: '分隔符' },
67
+ },
68
+ returns: { type: 'string' },
63
69
  examples: ['join(["a", "b", "c"], "-") // "a-b-c"'],
64
70
  },
65
71
  );
@@ -6,9 +6,11 @@ export const starts_with = VmLib(
6
6
  },
7
7
  {
8
8
  summary: '检查字符串是否以指定子串开头',
9
- params: { str: '要检查的字符串', search: '要匹配的子串' },
10
- paramsType: { str: 'string', search: 'string' },
11
- returnsType: 'boolean',
9
+ params: {
10
+ str: { type: 'string', description: '要检查的字符串' },
11
+ search: { type: 'string', description: '要匹配的子串' },
12
+ },
13
+ returns: { type: 'boolean' },
12
14
  examples: ['starts_with("mira", "mi") // true'],
13
15
  },
14
16
  );
@@ -18,9 +20,11 @@ export const ends_with = VmLib(
18
20
  },
19
21
  {
20
22
  summary: '检查字符串是否以指定子串结尾',
21
- params: { str: '要检查的字符串', search: '要匹配的子串' },
22
- paramsType: { str: 'string', search: 'string' },
23
- returnsType: 'boolean',
23
+ params: {
24
+ str: { type: 'string', description: '要检查的字符串' },
25
+ search: { type: 'string', description: '要匹配的子串' },
26
+ },
27
+ returns: { type: 'boolean' },
24
28
  examples: ['ends_with("mira", "ra") // true'],
25
29
  },
26
30
  );
@@ -31,9 +35,11 @@ export const contains = VmLib(
31
35
  },
32
36
  {
33
37
  summary: '检查字符串是否包含指定子串',
34
- params: { str: '要检查的字符串', search: '要匹配的子串' },
35
- paramsType: { str: 'string', search: 'string' },
36
- returnsType: 'boolean',
38
+ params: {
39
+ str: { type: 'string', description: '要检查的字符串' },
40
+ search: { type: 'string', description: '要匹配的子串' },
41
+ },
42
+ returns: { type: 'boolean' },
37
43
  examples: ['contains("hello", "ll") // true'],
38
44
  },
39
45
  );
@@ -6,9 +6,8 @@ export const trim_start = VmLib(
6
6
  },
7
7
  {
8
8
  summary: '去除字符串开头的空白字符',
9
- params: { str: '要处理的字符串' },
10
- paramsType: { str: 'string' },
11
- returnsType: 'string',
9
+ params: { str: { type: 'string', description: '要处理的字符串' } },
10
+ returns: { type: 'string' },
12
11
  examples: ['trim_start(" mira") // "mira"'],
13
12
  },
14
13
  );
@@ -19,9 +18,8 @@ export const trim_end = VmLib(
19
18
  },
20
19
  {
21
20
  summary: '去除字符串结尾的空白字符',
22
- params: { str: '要处理的字符串' },
23
- paramsType: { str: 'string' },
24
- returnsType: 'string',
21
+ params: { str: { type: 'string', description: '要处理的字符串' } },
22
+ returns: { type: 'string' },
25
23
  examples: ['trim_end("mira ") // "mira"'],
26
24
  },
27
25
  );
@@ -32,9 +30,8 @@ export const trim = VmLib(
32
30
  },
33
31
  {
34
32
  summary: '去除字符串两端的空白字符',
35
- params: { str: '要处理的字符串' },
36
- paramsType: { str: 'string' },
37
- returnsType: 'string',
33
+ params: { str: { type: 'string', description: '要处理的字符串' } },
34
+ returns: { type: 'string' },
38
35
  examples: ['trim(" mira ") // "mira"'],
39
36
  },
40
37
  );
@@ -41,11 +41,10 @@ export const to_timestamp = VmLib(
41
41
  {
42
42
  summary: '将数据转换为 Unix 毫秒时间戳',
43
43
  params: {
44
- datetime: '要转换的数据,默认为当前时间',
45
- fallback: '转换失败时的返回值',
44
+ datetime: { type: 'number | string | nil', description: '要转换的数据,默认为当前时间' },
45
+ fallback: { type: 'any', description: '转换失败时的返回值' },
46
46
  },
47
- paramsType: { datetime: 'number | string | nil', fallback: 'any' },
48
- returnsType: 'number | type(fallback)',
47
+ returns: { type: 'number | type(fallback)' },
49
48
  examples: ['to_timestamp("1970-01-01T00:00:00Z") // 0'],
50
49
  },
51
50
  );
@@ -71,12 +70,11 @@ export const to_datetime = VmLib(
71
70
  {
72
71
  summary: '将数据转换为 DateTime 记录',
73
72
  params: {
74
- datetime: '要转换的数据,默认为当前时间',
75
- offset: '时区偏移量(单位:小时),默认为 0',
76
- fallback: '转换失败时的返回值',
73
+ datetime: { type: 'number | string | nil', description: '要转换的数据,默认为当前时间' },
74
+ offset: { type: 'number | nil', description: '时区偏移量(单位:小时),默认为 0' },
75
+ fallback: { type: 'any', description: '转换失败时的返回值' },
77
76
  },
78
- paramsType: { datetime: 'number | string | nil', offset: 'number | nil', fallback: 'any' },
79
- returnsType: 'DateTime | type(fallback)',
77
+ returns: { type: 'DateTime | type(fallback)' },
80
78
  examples: [
81
79
  `
82
80
  to_datetime(0)
@@ -100,11 +98,10 @@ export const to_iso8601 = VmLib(
100
98
  {
101
99
  summary: '将数据转换为 ISO 8601 格式的字符串',
102
100
  params: {
103
- datetime: '要转换的数据,默认为当前时间',
104
- fallback: '转换失败时的返回值',
101
+ datetime: { type: 'number | string | nil', description: '要转换的数据,默认为当前时间' },
102
+ fallback: { type: 'any', description: '转换失败时的返回值' },
105
103
  },
106
- paramsType: { datetime: 'number | string | nil', fallback: 'any' },
107
- returnsType: 'string | type(fallback)',
104
+ returns: { type: 'string | type(fallback)' },
108
105
  examples: ['to_iso8601(0) // "1970-01-01T00:00:00.000Z"'],
109
106
  },
110
107
  );
@@ -10,11 +10,10 @@ export const to_string = VmLib(
10
10
  {
11
11
  summary: '将数据转换为字符串',
12
12
  params: {
13
- data: '要转换的数据',
14
- fallback: '转换失败时的返回值',
13
+ data: { type: 'any', description: '要转换的数据' },
14
+ fallback: { type: 'any', description: '转换失败时的返回值' },
15
15
  },
16
- paramsType: { data: 'any', fallback: 'any' },
17
- returnsType: 'string | type(fallback)',
16
+ returns: { type: 'string | type(fallback)' },
18
17
  examples: ['to_string([1, 2]) // "1, 2"'],
19
18
  },
20
19
  );
@@ -27,11 +26,10 @@ export const to_number = VmLib(
27
26
  {
28
27
  summary: '将数据转换为数字',
29
28
  params: {
30
- data: '要转换的数据',
31
- fallback: '转换失败时的返回值',
29
+ data: { type: 'string | number | boolean', description: '要转换的数据' },
30
+ fallback: { type: 'any', description: '转换失败时的返回值' },
32
31
  },
33
- paramsType: { data: 'string | number | boolean', fallback: 'any' },
34
- returnsType: 'number | type(fallback)',
32
+ returns: { type: 'number | type(fallback)' },
35
33
  examples: ['to_number("1.5") // 1.5'],
36
34
  },
37
35
  );
@@ -44,11 +42,11 @@ export const to_number = VmLib(
44
42
  // {
45
43
  // summary: '将布尔值标准化',
46
44
  // params: {
47
- // data: '要转换的数据,仅当为布尔类型时才会参与转换',
48
- // fallback: '当输入不是布尔值时返回的值',
45
+ // data: { type: 'boolean', description: '要转换的数据,仅当为布尔类型时才会参与转换' },
46
+ // fallback: { type: 'any', description: '当输入不是布尔值时返回的值' },
49
47
  // },
50
- // paramsType: { data: 'boolean', fallback: 'any' },
51
- // returnsType: 'boolean | type(fallback)',
48
+ //
49
+ // returns: { type: 'boolean | type(fallback)' },
52
50
  // examples: [
53
51
  // 'to_boolean(true, false) // true',
54
52
  // 'to_boolean(nil, "failed") // "failed"',
@@ -64,9 +62,11 @@ export const format = VmLib(
64
62
  },
65
63
  {
66
64
  summary: '将数据格式化为指定格式的字符串',
67
- params: { data: '要格式化的数据', format: '格式字符串' },
68
- paramsType: { data: 'any', format: 'string' },
69
- returnsType: 'string',
65
+ params: {
66
+ data: { type: 'any', description: '要格式化的数据' },
67
+ format: { type: 'string', description: '格式字符串' },
68
+ },
69
+ returns: { type: 'string' },
70
70
  examples: ['format(12, ".3") // "12.000"'],
71
71
  },
72
72
  );
@@ -271,10 +271,7 @@ export function map(
271
271
  }
272
272
 
273
273
  /** 库函数选项 */
274
- export type VmLibOption = Pick<
275
- VmFunctionOption,
276
- 'summary' | 'params' | 'paramsType' | 'returns' | 'returnsType' | 'examples' | 'deprecated'
277
- >;
274
+ export type VmLibOption = Pick<VmFunctionOption, 'summary' | 'params' | 'returns' | 'examples' | 'deprecated'>;
278
275
  /** 库函数 */
279
276
  export type VmLib<T extends VmFunctionLike | VmConst = VmFunctionLike> = (T extends VmFunctionLike ? T : { value: T }) &
280
277
  VmLibOption;
@@ -291,9 +288,7 @@ export function VmLib<
291
288
  Object.defineProperties(ret, {
292
289
  summary: { enumerable: true, value: option.summary },
293
290
  params: { enumerable: true, value: option.params },
294
- paramsType: { enumerable: true, value: option.paramsType },
295
291
  returns: { enumerable: true, value: option.returns },
296
- returnsType: { enumerable: true, value: option.returnsType },
297
292
  examples: { enumerable: true, value: option.examples },
298
293
  deprecated: { enumerable: true, value: option.deprecated ?? undefined },
299
294
  });
@@ -47,9 +47,8 @@ export const size = VmLib(
47
47
  },
48
48
  {
49
49
  summary: '获取矩阵尺寸',
50
- params: { matrix: '要获取尺寸的矩阵' },
51
- paramsType: { matrix: 'any[][]' },
52
- returnsType: '[number, number]',
50
+ params: { matrix: { type: 'any[][]', description: '要获取尺寸的矩阵' } },
51
+ returns: { type: '[number, number]' },
53
52
  examples: ['matrix.size([[1, 2], [3, 4]]) // [2, 2]'],
54
53
  },
55
54
  );
@@ -75,9 +74,8 @@ export const transpose = VmLib(
75
74
  },
76
75
  {
77
76
  summary: '转置矩阵',
78
- params: { matrix: '要转置的矩阵' },
79
- paramsType: { matrix: 'any[][]' },
80
- returnsType: 'any[][]',
77
+ params: { matrix: { type: 'any[][]', description: '要转置的矩阵' } },
78
+ returns: { type: 'any[][]' },
81
79
  examples: ['matrix.transpose([[1, 2], [3, 4]]) // [[1, 3], [2, 4]]'],
82
80
  },
83
81
  );
@@ -220,9 +218,12 @@ export const entrywise = VmLib(
220
218
  },
221
219
  {
222
220
  summary: '逐项操作',
223
- params: { a: '第一个操作数', b: '第二个操作数', f: '操作函数' },
224
- paramsType: { a: 'any | any[] | any[][]', b: 'any | any[] | any[][]', f: 'fn(a: any, b: any) -> any' },
225
- returnsType: 'any | any[] | any[][]',
221
+ params: {
222
+ a: { type: 'any | any[] | any[][]', description: '第一个操作数' },
223
+ b: { type: 'any | any[] | any[][]', description: '第二个操作数' },
224
+ f: { type: 'fn(a: any, b: any) -> any', description: '操作函数' },
225
+ },
226
+ returns: { type: 'any | any[] | any[][]' },
226
227
  examples: [`matrix.entrywise([1, 2], [3, 4], fn (x, y) { x + y }) // [4, 6]`],
227
228
  },
228
229
  );
@@ -235,9 +236,11 @@ export const add = VmLib(
235
236
  },
236
237
  {
237
238
  summary: '逐项相加',
238
- params: { a: '第一个操作数', b: '第二个操作数' },
239
- paramsType: { a: 'number | number[] | number[][]', b: 'number | number[] | number[][]' },
240
- returnsType: 'number | number[] | number[][]',
239
+ params: {
240
+ a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
241
+ b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
242
+ },
243
+ returns: { type: 'number | number[] | number[][]' },
241
244
  examples: ['matrix.add([1, 2], [3, 4]) // [4, 6]'],
242
245
  },
243
246
  );
@@ -250,9 +253,11 @@ export const subtract = VmLib(
250
253
  },
251
254
  {
252
255
  summary: '逐项相减',
253
- params: { a: '第一个操作数', b: '第二个操作数' },
254
- paramsType: { a: 'number | number[] | number[][]', b: 'number | number[] | number[][]' },
255
- returnsType: 'number | number[] | number[][]',
256
+ params: {
257
+ a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
258
+ b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
259
+ },
260
+ returns: { type: 'number | number[] | number[][]' },
256
261
  examples: ['matrix.subtract([3, 4], [1, 2]) // [2, 2]'],
257
262
  },
258
263
  );
@@ -265,9 +270,11 @@ export const entrywise_multiply = VmLib(
265
270
  },
266
271
  {
267
272
  summary: '逐项相乘',
268
- params: { a: '第一个操作数', b: '第二个操作数' },
269
- paramsType: { a: 'number | number[] | number[][]', b: 'number | number[] | number[][]' },
270
- returnsType: 'number | number[] | number[][]',
273
+ params: {
274
+ a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
275
+ b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
276
+ },
277
+ returns: { type: 'number | number[] | number[][]' },
271
278
  examples: ['matrix.entrywise_multiply([1, 2], [3, 4]) // [3, 8]'],
272
279
  },
273
280
  );
@@ -280,9 +287,11 @@ export const entrywise_divide = VmLib(
280
287
  },
281
288
  {
282
289
  summary: '逐项相除',
283
- params: { a: '第一个操作数', b: '第二个操作数' },
284
- paramsType: { a: 'number | number[] | number[][]', b: 'number | number[] | number[][]' },
285
- returnsType: 'number | number[] | number[][]',
290
+ params: {
291
+ a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
292
+ b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
293
+ },
294
+ returns: { type: 'number | number[] | number[][]' },
286
295
  examples: ['matrix.entrywise_divide([4, 6], [2, 3]) // [2, 2]'],
287
296
  },
288
297
  );
@@ -347,9 +356,11 @@ export const multiply = VmLib(
347
356
  },
348
357
  {
349
358
  summary: '矩阵相乘',
350
- params: { a: '第一个操作数', b: '第二个操作数' },
351
- paramsType: { a: 'number | number[] | number[][]', b: 'number | number[] | number[][]' },
352
- returnsType: 'number | number[] | number[][]',
359
+ params: {
360
+ a: { type: 'number | number[] | number[][]', description: '第一个操作数' },
361
+ b: { type: 'number | number[] | number[][]', description: '第二个操作数' },
362
+ },
363
+ returns: { type: 'number | number[] | number[][]' },
353
364
  examples: ['matrix.multiply([[1, 2], [3, 4]], [5, 6]) // [17, 39]'],
354
365
  },
355
366
  );
@@ -467,9 +478,8 @@ export const invert = VmLib(
467
478
  },
468
479
  {
469
480
  summary: '矩阵求逆',
470
- params: { a: '待求逆的矩阵' },
471
- paramsType: { a: 'number | number[][]' },
472
- returnsType: 'number | number[][]',
481
+ params: { a: { type: 'number | number[][]', description: '待求逆的矩阵' } },
482
+ returns: { type: 'number | number[][]' },
473
483
  examples: ['matrix.invert([[1, 2], [3, 4]]) // [[-2, 1], [1.5, -0.5]]'],
474
484
  },
475
485
  );
@@ -492,17 +502,15 @@ function filled(size: readonly VmAny[], value: VmConst): VmArray {
492
502
 
493
503
  export const zeros = VmLib((...size) => filled(size, 0), {
494
504
  summary: '创建一个全零的矩阵',
495
- params: { '..size': '矩阵的维度' },
496
- paramsType: { '..size': 'number[]' },
497
- returnsType: 'number[][]',
505
+ params: { '..size': { type: 'number[]', description: '矩阵的维度' } },
506
+ returns: { type: 'number[][]' },
498
507
  examples: ['matrix.zeros(2, 3) // [[0, 0, 0], [0, 0, 0]]'],
499
508
  });
500
509
 
501
510
  export const ones = VmLib((...size) => filled(size, 1), {
502
511
  summary: '创建一个全一的矩阵',
503
- params: { '..size': '矩阵的维度' },
504
- paramsType: { '..size': 'number[]' },
505
- returnsType: 'number[][]',
512
+ params: { '..size': { type: 'number[]', description: '矩阵的维度' } },
513
+ returns: { type: 'number[][]' },
506
514
  examples: ['matrix.ones(2, 2) // [[1, 1], [1, 1]]'],
507
515
  });
508
516
 
@@ -527,9 +535,8 @@ export const identity = VmLib(
527
535
  },
528
536
  {
529
537
  summary: '创建一个单位矩阵',
530
- params: { '..size': '矩阵的维度' },
531
- paramsType: { '..size': '[number] | [number, number]' },
532
- returnsType: 'number[][]',
538
+ params: { '..size': { type: '[number] | [number, number]', description: '矩阵的维度' } },
539
+ returns: { type: 'number[][]' },
533
540
  examples: ['matrix.identity(3) // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]'],
534
541
  },
535
542
  );
@@ -570,9 +577,11 @@ export const diagonal = VmLib(
570
577
  },
571
578
  {
572
579
  summary: '创建一个对角矩阵或获取矩阵的对角线',
573
- params: { x: '对角线元素或要获取对角线的矩阵', k: '对角线偏移量,默认为 0' },
574
- paramsType: { x: 'number[] | number[][]', k: 'number' },
575
- returnsType: 'number[][] | number[]',
580
+ params: {
581
+ x: { type: 'number[] | number[][]', description: '对角线元素或要获取对角线的矩阵' },
582
+ k: { type: 'number', description: '对角线偏移量,默认为 0' },
583
+ },
584
+ returns: { type: 'number[][] | number[]' },
576
585
  examples: [
577
586
  'matrix.diagonal([1, 2, 3]) // [[1, 0, 0], [0, 2, 0], [0, 0, 3]]',
578
587
  'matrix.diagonal([[1, 2], [3, 4]]) // [1, 4]',
@@ -1,5 +1,5 @@
1
1
  import { VmError } from '../../helpers/error.js';
2
- import { hasOwnEnumerable, NotNumber, keys, create, isFinite } from '../../helpers/utils.js';
2
+ import { hasOwnEnumerable, NotNumber, keys, create, isFinite, keysLength } from '../../helpers/utils.js';
3
3
  import { toNumber, toString } from '../../helpers/convert/index.js';
4
4
  import { display } from '../../helpers/serialize.js';
5
5
  import {
@@ -48,7 +48,7 @@ export const $In = (value: VmAny, iterable: VmAny): boolean => {
48
48
  export const $Length = (value: VmAny): number => {
49
49
  $AssertInit(value);
50
50
  if (isVmArray(value)) return value.length;
51
- if (isVmRecord(value)) return keys(value).length;
51
+ if (isVmRecord(value)) return keysLength(value);
52
52
  if (isVmWrapper(value)) return value.keys().length;
53
53
 
54
54
  throw new VmError(`Value has no length: ${display(value)}`, 0);
@@ -90,21 +90,19 @@ class ValueVmContext implements VmContext {
90
90
  }
91
91
  /** @inheritdoc */
92
92
  get(key: string): VmValue {
93
- if (hasOwn(this.env, key)) return this.env[key] ?? null;
94
- {
95
- const val = VM_SHARED_CONTEXT[key];
96
- if (val !== undefined) return val;
97
- return globalVarNotFound(key);
93
+ if (hasOwn(this.env, key)) {
94
+ return this.env[key] ?? null;
98
95
  }
96
+ return DefaultVmContext.get(key);
99
97
  }
100
98
  /** @inheritdoc */
101
99
  has(key: string): boolean {
102
- return hasOwn(this.env, key) || key in VM_SHARED_CONTEXT;
100
+ return hasOwn(this.env, key) || DefaultVmContext.has(key);
103
101
  }
104
102
  /** @inheritdoc */
105
103
  describe(key: string): string | undefined {
106
104
  if (hasOwn(this.env, key)) return this.describer?.(key);
107
- return VM_SHARED_CONTEXT_DESCRIPTIONS[key];
105
+ return DefaultVmContext.describe(key);
108
106
  }
109
107
  constructor(
110
108
  private readonly env: VmContextRecord,
@@ -123,7 +121,9 @@ class Value2VmContext implements VmContext {
123
121
  }
124
122
  /** @inheritdoc */
125
123
  get(key: string): VmValue {
126
- if (this.env != null && hasOwn(this.env, key)) return this.env[key] ?? null;
124
+ if (this.env != null && hasOwn(this.env, key)) {
125
+ return this.env[key] ?? null;
126
+ }
127
127
  if (hasOwn(this.extern, key)) {
128
128
  const val = this.extern[key];
129
129
  if (val == null) return null;
@@ -138,18 +138,18 @@ class Value2VmContext implements VmContext {
138
138
  return cached;
139
139
  }
140
140
 
141
- const val = VM_SHARED_CONTEXT[key];
142
- if (val !== undefined) return val;
143
- return globalVarNotFound(key);
141
+ return DefaultVmContext.get(key);
144
142
  }
145
143
  /** @inheritdoc */
146
144
  has(key: string): boolean {
147
- return (this.env != null && hasOwn(this.env, key)) || hasOwn(this.extern, key) || key in VM_SHARED_CONTEXT;
145
+ return (this.env != null && hasOwn(this.env, key)) || hasOwn(this.extern, key) || DefaultVmContext.has(key);
148
146
  }
149
147
  /** @inheritdoc */
150
148
  describe(key: string): string | undefined {
151
- if ((this.env != null && hasOwn(this.env, key)) || hasOwn(this.extern, key)) return this.describer?.(key);
152
- return VM_SHARED_CONTEXT_DESCRIPTIONS[key];
149
+ if ((this.env != null && hasOwn(this.env, key)) || hasOwn(this.extern, key)) {
150
+ return this.describer?.(key);
151
+ }
152
+ return DefaultVmContext.describe(key);
153
153
  }
154
154
  private readonly externCache = new WeakMap<object, VmValue>();
155
155
  constructor(
@@ -173,19 +173,16 @@ class FactoryVmContext implements VmContext {
173
173
  get(key: string): VmValue {
174
174
  const value = this.getter(key);
175
175
  if (value !== undefined) return value;
176
-
177
- const val = VM_SHARED_CONTEXT[key];
178
- if (val !== undefined) return val;
179
- return globalVarNotFound(key);
176
+ return DefaultVmContext.get(key);
180
177
  }
181
178
  /** @inheritdoc */
182
179
  has(key: string): boolean {
183
- return this.getter(key) !== undefined || key in VM_SHARED_CONTEXT;
180
+ return this.getter(key) !== undefined || DefaultVmContext.has(key);
184
181
  }
185
182
  /** @inheritdoc */
186
183
  describe(key: string): string | undefined {
187
184
  if (this.getter(key) !== undefined) return this.describer?.(key);
188
- return VM_SHARED_CONTEXT_DESCRIPTIONS[key];
185
+ return DefaultVmContext.describe(key);
189
186
  }
190
187
  constructor(
191
188
  private readonly getter: (key: string) => VmValue | undefined,
@@ -18,6 +18,11 @@ export type VmFunctionLike = (...args: ReadonlyArray<VmValue | undefined>) => Vm
18
18
  /** Mirascript 函数 */
19
19
  export type VmFunction<T extends VmFunctionLike = VmFunctionLike> = T & { readonly [kVmFunction]: VmFunctionInfo };
20
20
 
21
+ /** Mirascript 值信息 */
22
+ export type VmValueInfo = {
23
+ readonly type: string;
24
+ readonly description?: string;
25
+ };
21
26
  /** Mirascript 函数信息 */
22
27
  export type VmFunctionInfo = {
23
28
  /** 完整名称 */
@@ -27,13 +32,9 @@ export type VmFunctionInfo = {
27
32
  /** 文档字符串 */
28
33
  readonly summary?: string;
29
34
  /** 文档字符串 */
30
- readonly params?: Record<string, string>;
31
- /** 文档字符串 */
32
- readonly paramsType?: Record<string, string>;
33
- /** 文档字符串 */
34
- readonly returns?: string;
35
+ readonly params?: Record<string, VmValueInfo>;
35
36
  /** 文档字符串 */
36
- readonly returnsType?: string;
37
+ readonly returns?: VmValueInfo;
37
38
  /** 文档字符串 */
38
39
  readonly examples?: string[];
39
40
  /** 标记为弃用 */
@@ -41,7 +42,7 @@ export type VmFunctionInfo = {
41
42
  };
42
43
 
43
44
  /** Mirascript 函数创建选项 */
44
- export type VmFunctionOption = Partial<Omit<VmFunctionInfo, 'original'>> & {
45
+ export type VmFunctionOption = Partial<VmFunctionInfo> & {
45
46
  /** 函数名称 */
46
47
  readonly name?: string | null | undefined;
47
48
  };
@@ -83,9 +84,7 @@ export function VmFunction<T extends VmFunctionLike>(
83
84
  isLib: opt.isLib ?? false,
84
85
  summary: opt.summary || undefined,
85
86
  params: opt.params,
86
- paramsType: opt.paramsType,
87
- returns: opt.returns || undefined,
88
- returnsType: opt.returnsType || undefined,
87
+ returns: opt.returns ?? undefined,
89
88
  examples: opt.examples?.length ? opt.examples : undefined,
90
89
  deprecated: opt.deprecated ?? undefined,
91
90
  };