@mirascript/mirascript 0.1.0

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 (191) hide show
  1. package/dist/chunk-5FQWUJIY.js +766 -0
  2. package/dist/chunk-5FQWUJIY.js.map +6 -0
  3. package/dist/chunk-BTDGMWFK.js +202 -0
  4. package/dist/chunk-BTDGMWFK.js.map +6 -0
  5. package/dist/chunk-DCXIWIW5.js +3419 -0
  6. package/dist/chunk-DCXIWIW5.js.map +6 -0
  7. package/dist/chunk-RAPJ3XLV.js +10 -0
  8. package/dist/chunk-RAPJ3XLV.js.map +6 -0
  9. package/dist/cli/execute.d.ts +4 -0
  10. package/dist/cli/execute.d.ts.map +1 -0
  11. package/dist/cli/index.d.ts +2 -0
  12. package/dist/cli/index.d.ts.map +1 -0
  13. package/dist/cli/index.js +191 -0
  14. package/dist/cli/index.js.map +6 -0
  15. package/dist/cli/print.d.ts +4 -0
  16. package/dist/cli/print.d.ts.map +1 -0
  17. package/dist/compiler/compile-bytecode.d.ts +12 -0
  18. package/dist/compiler/compile-bytecode.d.ts.map +1 -0
  19. package/dist/compiler/compile-fast.d.ts +7 -0
  20. package/dist/compiler/compile-fast.d.ts.map +1 -0
  21. package/dist/compiler/create-script.d.ts +7 -0
  22. package/dist/compiler/create-script.d.ts.map +1 -0
  23. package/dist/compiler/diagnostic.d.ts +56 -0
  24. package/dist/compiler/diagnostic.d.ts.map +1 -0
  25. package/dist/compiler/emit.d.ts +4 -0
  26. package/dist/compiler/emit.d.ts.map +1 -0
  27. package/dist/compiler/index.d.ts +13 -0
  28. package/dist/compiler/index.d.ts.map +1 -0
  29. package/dist/compiler/types.d.ts +16 -0
  30. package/dist/compiler/types.d.ts.map +1 -0
  31. package/dist/compiler/worker-manager.d.ts +6 -0
  32. package/dist/compiler/worker-manager.d.ts.map +1 -0
  33. package/dist/compiler/worker.d.ts +6 -0
  34. package/dist/compiler/worker.d.ts.map +1 -0
  35. package/dist/compiler/worker.js +34 -0
  36. package/dist/compiler/worker.js.map +6 -0
  37. package/dist/helpers/constants.d.ts +3 -0
  38. package/dist/helpers/constants.d.ts.map +1 -0
  39. package/dist/helpers/serialize.d.ts +45 -0
  40. package/dist/helpers/serialize.d.ts.map +1 -0
  41. package/dist/helpers/utils.d.ts +36 -0
  42. package/dist/helpers/utils.d.ts.map +1 -0
  43. package/dist/index.d.ts +4 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +61 -0
  46. package/dist/index.js.map +6 -0
  47. package/dist/subtle.d.ts +7 -0
  48. package/dist/subtle.d.ts.map +1 -0
  49. package/dist/subtle.js +30 -0
  50. package/dist/subtle.js.map +6 -0
  51. package/dist/vm/env.d.ts +3 -0
  52. package/dist/vm/env.d.ts.map +1 -0
  53. package/dist/vm/error.d.ts +11 -0
  54. package/dist/vm/error.d.ts.map +1 -0
  55. package/dist/vm/helpers.d.ts +21 -0
  56. package/dist/vm/helpers.d.ts.map +1 -0
  57. package/dist/vm/index.d.ts +5 -0
  58. package/dist/vm/index.d.ts.map +1 -0
  59. package/dist/vm/lib/_helpers.d.ts +35 -0
  60. package/dist/vm/lib/_helpers.d.ts.map +1 -0
  61. package/dist/vm/lib/_loader.d.ts +16 -0
  62. package/dist/vm/lib/_loader.d.ts.map +1 -0
  63. package/dist/vm/lib/global/bit.d.ts +9 -0
  64. package/dist/vm/lib/global/bit.d.ts.map +1 -0
  65. package/dist/vm/lib/global/debug.d.ts +5 -0
  66. package/dist/vm/lib/global/debug.d.ts.map +1 -0
  67. package/dist/vm/lib/global/index.d.ts +10 -0
  68. package/dist/vm/lib/global/index.d.ts.map +1 -0
  69. package/dist/vm/lib/global/json.d.ts +4 -0
  70. package/dist/vm/lib/global/json.d.ts.map +1 -0
  71. package/dist/vm/lib/global/math-additional.d.ts +3 -0
  72. package/dist/vm/lib/global/math-additional.d.ts.map +1 -0
  73. package/dist/vm/lib/global/math-arr.d.ts +8 -0
  74. package/dist/vm/lib/global/math-arr.d.ts.map +1 -0
  75. package/dist/vm/lib/global/math-const.d.ts +3 -0
  76. package/dist/vm/lib/global/math-const.d.ts.map +1 -0
  77. package/dist/vm/lib/global/math-unary.d.ts +29 -0
  78. package/dist/vm/lib/global/math-unary.d.ts.map +1 -0
  79. package/dist/vm/lib/global/math.d.ts +9 -0
  80. package/dist/vm/lib/global/math.d.ts.map +1 -0
  81. package/dist/vm/lib/global/mod/index.d.ts +3 -0
  82. package/dist/vm/lib/global/mod/index.d.ts.map +1 -0
  83. package/dist/vm/lib/global/mod/matrix.d.ts +16 -0
  84. package/dist/vm/lib/global/mod/matrix.d.ts.map +1 -0
  85. package/dist/vm/lib/global/sequence/all-any.d.ts +4 -0
  86. package/dist/vm/lib/global/sequence/all-any.d.ts.map +1 -0
  87. package/dist/vm/lib/global/sequence/entries.d.ts +12 -0
  88. package/dist/vm/lib/global/sequence/entries.d.ts.map +1 -0
  89. package/dist/vm/lib/global/sequence/find.d.ts +10 -0
  90. package/dist/vm/lib/global/sequence/find.d.ts.map +1 -0
  91. package/dist/vm/lib/global/sequence/flatten.d.ts +3 -0
  92. package/dist/vm/lib/global/sequence/flatten.d.ts.map +1 -0
  93. package/dist/vm/lib/global/sequence/index.d.ts +12 -0
  94. package/dist/vm/lib/global/sequence/index.d.ts.map +1 -0
  95. package/dist/vm/lib/global/sequence/len.d.ts +3 -0
  96. package/dist/vm/lib/global/sequence/len.d.ts.map +1 -0
  97. package/dist/vm/lib/global/sequence/map-filter.d.ts +9 -0
  98. package/dist/vm/lib/global/sequence/map-filter.d.ts.map +1 -0
  99. package/dist/vm/lib/global/sequence/repeat.d.ts +4 -0
  100. package/dist/vm/lib/global/sequence/repeat.d.ts.map +1 -0
  101. package/dist/vm/lib/global/sequence/reverse.d.ts +3 -0
  102. package/dist/vm/lib/global/sequence/reverse.d.ts.map +1 -0
  103. package/dist/vm/lib/global/sequence/sort.d.ts +5 -0
  104. package/dist/vm/lib/global/sequence/sort.d.ts.map +1 -0
  105. package/dist/vm/lib/global/sequence/with.d.ts +5 -0
  106. package/dist/vm/lib/global/sequence/with.d.ts.map +1 -0
  107. package/dist/vm/lib/global/sequence/zip.d.ts +4 -0
  108. package/dist/vm/lib/global/sequence/zip.d.ts.map +1 -0
  109. package/dist/vm/lib/global/string.d.ts +12 -0
  110. package/dist/vm/lib/global/string.d.ts.map +1 -0
  111. package/dist/vm/lib/global/time.d.ts +15 -0
  112. package/dist/vm/lib/global/time.d.ts.map +1 -0
  113. package/dist/vm/lib/global/to-primitive.d.ts +6 -0
  114. package/dist/vm/lib/global/to-primitive.d.ts.map +1 -0
  115. package/dist/vm/operations.d.ts +49 -0
  116. package/dist/vm/operations.d.ts.map +1 -0
  117. package/dist/vm/types/checker.d.ts +30 -0
  118. package/dist/vm/types/checker.d.ts.map +1 -0
  119. package/dist/vm/types/context.d.ts +43 -0
  120. package/dist/vm/types/context.d.ts.map +1 -0
  121. package/dist/vm/types/extern.d.ts +32 -0
  122. package/dist/vm/types/extern.d.ts.map +1 -0
  123. package/dist/vm/types/function.d.ts +49 -0
  124. package/dist/vm/types/function.d.ts.map +1 -0
  125. package/dist/vm/types/index.d.ts +75 -0
  126. package/dist/vm/types/index.d.ts.map +1 -0
  127. package/dist/vm/types/module.d.ts +25 -0
  128. package/dist/vm/types/module.d.ts.map +1 -0
  129. package/dist/vm/types/script.d.ts +14 -0
  130. package/dist/vm/types/script.d.ts.map +1 -0
  131. package/dist/vm/types/wrapper.d.ts +25 -0
  132. package/dist/vm/types/wrapper.d.ts.map +1 -0
  133. package/package.json +55 -0
  134. package/src/cli/execute.ts +32 -0
  135. package/src/cli/index.ts +73 -0
  136. package/src/cli/print.ts +41 -0
  137. package/src/compiler/compile-bytecode.ts +65 -0
  138. package/src/compiler/compile-fast.ts +81 -0
  139. package/src/compiler/create-script.ts +34 -0
  140. package/src/compiler/diagnostic.ts +175 -0
  141. package/src/compiler/emit.ts +764 -0
  142. package/src/compiler/index.ts +67 -0
  143. package/src/compiler/types.ts +16 -0
  144. package/src/compiler/worker-manager.ts +60 -0
  145. package/src/compiler/worker.ts +37 -0
  146. package/src/helpers/constants.ts +3 -0
  147. package/src/helpers/serialize.ts +280 -0
  148. package/src/helpers/utils.ts +16 -0
  149. package/src/index.ts +3 -0
  150. package/src/subtle.ts +6 -0
  151. package/src/vm/env.ts +16 -0
  152. package/src/vm/error.ts +22 -0
  153. package/src/vm/helpers.ts +121 -0
  154. package/src/vm/index.ts +5 -0
  155. package/src/vm/lib/_helpers.ts +215 -0
  156. package/src/vm/lib/_loader.ts +51 -0
  157. package/src/vm/lib/global/bit.ts +93 -0
  158. package/src/vm/lib/global/debug.ts +36 -0
  159. package/src/vm/lib/global/index.ts +9 -0
  160. package/src/vm/lib/global/json.ts +45 -0
  161. package/src/vm/lib/global/math-additional.ts +71 -0
  162. package/src/vm/lib/global/math-arr.ts +62 -0
  163. package/src/vm/lib/global/math-const.ts +2 -0
  164. package/src/vm/lib/global/math-unary.ts +171 -0
  165. package/src/vm/lib/global/math.ts +27 -0
  166. package/src/vm/lib/global/mod/index.ts +4 -0
  167. package/src/vm/lib/global/mod/matrix.ts +579 -0
  168. package/src/vm/lib/global/sequence/all-any.ts +73 -0
  169. package/src/vm/lib/global/sequence/entries.ts +67 -0
  170. package/src/vm/lib/global/sequence/find.ts +49 -0
  171. package/src/vm/lib/global/sequence/flatten.ts +16 -0
  172. package/src/vm/lib/global/sequence/index.ts +11 -0
  173. package/src/vm/lib/global/sequence/len.ts +15 -0
  174. package/src/vm/lib/global/sequence/map-filter.ts +82 -0
  175. package/src/vm/lib/global/sequence/repeat.ts +28 -0
  176. package/src/vm/lib/global/sequence/reverse.ts +17 -0
  177. package/src/vm/lib/global/sequence/sort.ts +88 -0
  178. package/src/vm/lib/global/sequence/with.ts +43 -0
  179. package/src/vm/lib/global/sequence/zip.ts +40 -0
  180. package/src/vm/lib/global/string.ts +149 -0
  181. package/src/vm/lib/global/time.ts +73 -0
  182. package/src/vm/lib/global/to-primitive.ts +58 -0
  183. package/src/vm/operations.ts +497 -0
  184. package/src/vm/types/checker.ts +164 -0
  185. package/src/vm/types/context.ts +161 -0
  186. package/src/vm/types/extern.ts +166 -0
  187. package/src/vm/types/function.ts +136 -0
  188. package/src/vm/types/index.ts +124 -0
  189. package/src/vm/types/module.ts +40 -0
  190. package/src/vm/types/script.ts +18 -0
  191. package/src/vm/types/wrapper.ts +28 -0
@@ -0,0 +1,124 @@
1
+ import { isArray } from '../../helpers/utils.js';
2
+ import { VmExtern } from './extern.js';
3
+ import type { VmFunction } from './function.js';
4
+ import { VmModule } from './module.js';
5
+ import { VmWrapper } from './wrapper.js';
6
+
7
+ /** Mirascript 原始值 */
8
+ export type VmPrimitive = null | string | number | boolean;
9
+ /**
10
+ * Mirascript 记录
11
+ * 仅拥有且可枚举的字符串键视作存在
12
+ * 字段值 `undefined` 和 `null` 均视作 `nil`
13
+ */
14
+ export type VmRecord = {
15
+ readonly [key: string]: VmConst | undefined;
16
+ };
17
+ /**
18
+ * Mirascript 数组
19
+ * 数组中的 `undefined`、`null` 及 <empty slot> 均视作 `nil`
20
+ */
21
+ export type VmArray = ReadonlyArray<VmConst | undefined>;
22
+
23
+ /** Mirascript 虚拟机内的值语义值 */
24
+ export type VmConst = VmPrimitive | VmRecord | VmArray;
25
+
26
+ /** Mirascript 虚拟机内的不可变值 */
27
+ export type VmImmutable = VmConst | VmFunction | VmModule;
28
+
29
+ /** Mirascript 虚拟机内的合法值 */
30
+ export type VmValue = VmImmutable | VmExtern;
31
+
32
+ /** Mirascript 虚拟机内的未初始化变量 */
33
+ export type VmUninitialized = undefined;
34
+
35
+ /** Mirascript 虚拟机内的值(包括未初始化变量) */
36
+ export type VmAny = VmValue | VmUninitialized;
37
+
38
+ /** 类型名称 */
39
+ export type TypeName = keyof VmValueMap;
40
+
41
+ /** 类型名称映射 */
42
+ export interface VmValueMap {
43
+ /** 空值 */
44
+ nil: null;
45
+ /** 字符串 */
46
+ string: string;
47
+ /** 数字 */
48
+ number: number;
49
+ /** 布尔值 */
50
+ boolean: boolean;
51
+ /** Mirascript 记录 */
52
+ record: VmRecord;
53
+ /** Mirascript 数组 */
54
+ array: VmArray;
55
+ /** Mirascript 函数 */
56
+ function: VmFunction;
57
+ /** 外部调用对象 */
58
+ extern: VmExtern;
59
+ /** Mirascript 模块 */
60
+ module: VmModule;
61
+ }
62
+
63
+ /**
64
+ * 检查值是否为 Mirascript 数组
65
+ */
66
+ export function isVmArray(value: VmAny): value is VmArray {
67
+ if (!isArray(value)) return false;
68
+ value as VmArray satisfies VmArray;
69
+ return true;
70
+ }
71
+
72
+ /**
73
+ * 检查值是否为 Mirascript 记录
74
+ */
75
+ export function isVmRecord(value: VmAny): value is VmRecord {
76
+ if (value == null || typeof value !== 'object') return false;
77
+ if (value instanceof VmWrapper) return false;
78
+ if (isVmArray(value)) return false;
79
+ value satisfies VmRecord;
80
+ return true;
81
+ }
82
+
83
+ /**
84
+ * 检查值是否为 Mirascript 原始值
85
+ */
86
+ export function isVmPrimitive(value: unknown): value is VmPrimitive {
87
+ if (value === null || typeof value == 'number' || typeof value == 'string' || typeof value == 'boolean') {
88
+ value as VmPrimitive satisfies typeof value;
89
+ value satisfies VmPrimitive;
90
+ return true;
91
+ }
92
+ return false;
93
+ }
94
+
95
+ export { VmExtern, wrapToVmValue, unwrapFromVmValue } from './extern.js';
96
+
97
+ /** 检查值是否为 Mirascript 外部值 */
98
+ export function isVmExtern(value: unknown): value is VmExtern {
99
+ return value instanceof VmExtern;
100
+ }
101
+
102
+ export {
103
+ VmFunction,
104
+ isVmFunction,
105
+ getVmFunctionInfo,
106
+ type VmFunctionInfo,
107
+ type VmFunctionLike,
108
+ type VmFunctionOption,
109
+ } from './function.js';
110
+
111
+ export { VmModule } from './module.js';
112
+
113
+ /** 检查值是否为 Mirascript 模块 */
114
+ export function isVmModule(value: unknown): value is VmModule {
115
+ return value instanceof VmModule;
116
+ }
117
+
118
+ export { type VmContext, type VmSharedContext, isVmContext, defineVmContextValue, createVmContext } from './context.js';
119
+
120
+ export { type VmScript, isVmScript } from './script.js';
121
+
122
+ export { isVmAny, isVmConst, isVmImmutable, isVmValue } from './checker.js';
123
+
124
+ export const VM_ARRAY_MAX_LENGTH = 2 ** 31 - 1;
@@ -0,0 +1,40 @@
1
+ import { hasOwnEnumerable, keys } from '../../helpers/utils.js';
2
+ import type { TypeName, VmAny, VmValue } from './index.js';
3
+ import { VmWrapper } from './wrapper.js';
4
+
5
+ /** Mirascript 模块 */
6
+ export class VmModule<const T extends Record<string, VmValue> = Record<string, VmValue>> extends VmWrapper<T> {
7
+ constructor(
8
+ /** 模块名称 */
9
+ readonly name: string,
10
+ /** 模块导出 */
11
+ value: T,
12
+ ) {
13
+ super(value);
14
+ }
15
+ /** @inheritdoc */
16
+ override has(key: string): boolean {
17
+ return hasOwnEnumerable(this.value, key);
18
+ }
19
+ /** @inheritdoc */
20
+ override get(key: string): VmAny {
21
+ if (!this.has(key)) return undefined;
22
+ return this.value[key as keyof T] ?? null;
23
+ }
24
+ /** @inheritdoc */
25
+ override keys(): string[] {
26
+ return keys(this.value);
27
+ }
28
+ /** @inheritdoc */
29
+ override same(other: VmAny): boolean {
30
+ return this === other;
31
+ }
32
+ /** @inheritdoc */
33
+ override get type(): TypeName {
34
+ return 'module';
35
+ }
36
+ /** @inheritdoc */
37
+ override get describe(): string {
38
+ return this.name;
39
+ }
40
+ }
@@ -0,0 +1,18 @@
1
+ import type { VmContext, VmValue } from './index.js';
2
+
3
+ const kVmScript = Symbol.for('mirascript.vm.script');
4
+
5
+ /** Mirascript 脚本 */
6
+ export type VmScriptLike = (global?: VmContext) => VmValue;
7
+
8
+ /** Mirascript 脚本 */
9
+ export type VmScript = VmScriptLike & {
10
+ readonly [kVmScript]: true;
11
+ /** 原始代码 */
12
+ readonly source: string;
13
+ };
14
+
15
+ /** 检查是否为 Mirascript 脚本 */
16
+ export function isVmScript(value: unknown): value is VmScript {
17
+ return typeof value === 'function' && (value as VmScript)[kVmScript] === true;
18
+ }
@@ -0,0 +1,28 @@
1
+ import type { TypeName, VmAny } from './index.js';
2
+
3
+ /**
4
+ * Mirascript 特殊值的包装器
5
+ */
6
+ export abstract class VmWrapper<T extends object> {
7
+ constructor(readonly value: T) {}
8
+ /** 检查是否包含键 */
9
+ abstract has(key: string): boolean;
10
+ /** 获取键对应的值 */
11
+ abstract get(key: string): VmAny;
12
+ /** 获取键名 */
13
+ abstract keys(): string[];
14
+ /** 与其他值比较 */
15
+ abstract same(other: VmAny): boolean;
16
+ /** 获取类型 */
17
+ abstract get type(): TypeName;
18
+ /** 获取描述 */
19
+ abstract get describe(): string;
20
+ /** Convert the object to JSON */
21
+ toJSON(): undefined {
22
+ return undefined;
23
+ }
24
+ /** 转为字符串 */
25
+ toString(): string {
26
+ return `<${this.type} ${this.describe}>`;
27
+ }
28
+ }