@mirascript/mirascript 0.1.18 → 0.1.20

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 (86) hide show
  1. package/dist/{chunk-5AEWM4W6.js → chunk-4T5YE7LC.js} +241 -177
  2. package/dist/chunk-4T5YE7LC.js.map +6 -0
  3. package/dist/{chunk-RLWIIOH5.js → chunk-BV5YXQYA.js} +1 -1
  4. package/dist/{chunk-RLWIIOH5.js.map → chunk-BV5YXQYA.js.map} +1 -1
  5. package/dist/chunk-OVTSNACH.js +152 -0
  6. package/dist/chunk-OVTSNACH.js.map +6 -0
  7. package/dist/{chunk-3BAOZ2P2.js → chunk-ZKL4MRMP.js} +185 -212
  8. package/dist/chunk-ZKL4MRMP.js.map +6 -0
  9. package/dist/cli/index.js +6 -5
  10. package/dist/cli/index.js.map +1 -1
  11. package/dist/compiler/emit/constants.d.ts +1 -2
  12. package/dist/compiler/emit/constants.d.ts.map +1 -1
  13. package/dist/compiler/emit/consts.d.ts +2 -2
  14. package/dist/compiler/emit/consts.d.ts.map +1 -1
  15. package/dist/compiler/emit/index.d.ts +2 -3
  16. package/dist/compiler/emit/index.d.ts.map +1 -1
  17. package/dist/compiler/emit/sourcemap.d.ts +1 -2
  18. package/dist/compiler/emit/sourcemap.d.ts.map +1 -1
  19. package/dist/compiler/emit-script.d.ts +10 -0
  20. package/dist/compiler/emit-script.d.ts.map +1 -0
  21. package/dist/compiler/generate-bytecode.d.ts +4 -2
  22. package/dist/compiler/generate-bytecode.d.ts.map +1 -1
  23. package/dist/compiler/index.d.ts +1 -6
  24. package/dist/compiler/index.d.ts.map +1 -1
  25. package/dist/compiler/load-module.d.ts +2 -0
  26. package/dist/compiler/load-module.d.ts.map +1 -0
  27. package/dist/compiler/worker.js +1 -1
  28. package/dist/helpers/constants.d.ts +2 -0
  29. package/dist/helpers/constants.d.ts.map +1 -1
  30. package/dist/helpers/serialize.d.ts.map +1 -1
  31. package/dist/helpers/utils.d.ts.map +1 -1
  32. package/dist/index.js +6 -11
  33. package/dist/subtle.d.ts +3 -1
  34. package/dist/subtle.d.ts.map +1 -1
  35. package/dist/subtle.js +3 -3
  36. package/dist/vm/checkpoint.d.ts +1 -1
  37. package/dist/vm/checkpoint.d.ts.map +1 -1
  38. package/dist/vm/helpers.d.ts +6 -1
  39. package/dist/vm/helpers.d.ts.map +1 -1
  40. package/dist/vm/lib/global/json.d.ts.map +1 -1
  41. package/dist/vm/lib/global/math-const.d.ts +24 -2
  42. package/dist/vm/lib/global/math-const.d.ts.map +1 -1
  43. package/dist/vm/lib/helpers.d.ts +5 -3
  44. package/dist/vm/lib/helpers.d.ts.map +1 -1
  45. package/dist/vm/lib/index.d.ts.map +1 -1
  46. package/dist/vm/lib/loader.d.ts +12 -5
  47. package/dist/vm/lib/loader.d.ts.map +1 -1
  48. package/dist/vm/types/context.d.ts +10 -18
  49. package/dist/vm/types/context.d.ts.map +1 -1
  50. package/dist/vm/types/extern.d.ts +1 -1
  51. package/dist/vm/types/extern.d.ts.map +1 -1
  52. package/dist/vm/types/module.d.ts +1 -1
  53. package/dist/vm/types/module.d.ts.map +1 -1
  54. package/dist/vm/types/wrapper.d.ts +4 -2
  55. package/dist/vm/types/wrapper.d.ts.map +1 -1
  56. package/package.json +4 -4
  57. package/src/compiler/emit/constants.ts +1 -2
  58. package/src/compiler/emit/consts.ts +166 -29
  59. package/src/compiler/emit/index.ts +40 -32
  60. package/src/compiler/emit/sourcemap.ts +21 -76
  61. package/src/compiler/emit-script.ts +30 -0
  62. package/src/compiler/generate-bytecode.ts +5 -8
  63. package/src/compiler/index.ts +2 -30
  64. package/src/compiler/load-module.ts +3 -0
  65. package/src/helpers/constants.ts +2 -0
  66. package/src/helpers/serialize.ts +1 -4
  67. package/src/helpers/utils.ts +2 -7
  68. package/src/subtle.ts +3 -1
  69. package/src/vm/checkpoint.ts +42 -17
  70. package/src/vm/helpers.ts +12 -5
  71. package/src/vm/lib/global/json.ts +2 -0
  72. package/src/vm/lib/global/math-const.ts +34 -2
  73. package/src/vm/lib/helpers.ts +10 -11
  74. package/src/vm/lib/index.ts +7 -3
  75. package/src/vm/lib/loader.ts +56 -19
  76. package/src/vm/types/context.ts +58 -30
  77. package/src/vm/types/extern.ts +1 -1
  78. package/src/vm/types/module.ts +1 -1
  79. package/src/vm/types/wrapper.ts +9 -5
  80. package/dist/chunk-3BAOZ2P2.js.map +0 -6
  81. package/dist/chunk-5AEWM4W6.js.map +0 -6
  82. package/dist/chunk-RIT53WVY.js +0 -1
  83. package/dist/chunk-RIT53WVY.js.map +0 -6
  84. package/dist/compiler/emit/globals.d.ts +0 -17
  85. package/dist/compiler/emit/globals.d.ts.map +0 -1
  86. package/src/compiler/emit/globals.ts +0 -39
@@ -13,8 +13,6 @@ import {
13
13
  entries,
14
14
  formatDiagnostics,
15
15
  fromEntries,
16
- generateBytecode,
17
- generateBytecodeSync,
18
16
  getPrototypeOf,
19
17
  hasOwn,
20
18
  hasOwnEnumerable,
@@ -45,7 +43,7 @@ import {
45
43
  parseDiagnostics,
46
44
  toString,
47
45
  values
48
- } from "./chunk-5AEWM4W6.js";
46
+ } from "./chunk-4T5YE7LC.js";
49
47
 
50
48
  // src/helpers/convert/index.ts
51
49
  var convert_exports = {};
@@ -555,15 +553,19 @@ var VmWrapper = class {
555
553
  constructor(value) {
556
554
  this.value = value;
557
555
  }
556
+ /** 描述键对应的值 */
557
+ describe(key) {
558
+ return void 0;
559
+ }
558
560
  /** Convert the object to JSON */
559
561
  toJSON() {
560
562
  return void 0;
561
563
  }
562
564
  /** 转为字符串 */
563
565
  toString(useBraces) {
564
- const { type, describe } = this;
565
- if (!describe) return `<${type}>`;
566
- return `<${type} ${describe}>`;
566
+ const { type, tag } = this;
567
+ if (!tag) return `<${type}>`;
568
+ return `<${type} ${tag}>`;
567
569
  }
568
570
  };
569
571
  Object.defineProperty(VmWrapper.prototype, kVmWrapper, { value: true });
@@ -736,7 +738,7 @@ var VmExtern = class extends VmWrapper {
736
738
  return "extern";
737
739
  }
738
740
  /** @inheritdoc */
739
- get describe() {
741
+ get tag() {
740
742
  const tag = ObjectToString.call(this.value).slice(8, -1);
741
743
  if (isArray(this.value)) {
742
744
  return `${tag}(${this.value.length})`;
@@ -774,46 +776,65 @@ Object.defineProperty(VmExtern.prototype, kVmExtern, { value: true });
774
776
 
775
777
  // src/vm/checkpoint.ts
776
778
  var { now } = Date;
777
- var TIME_ORIGIN = now() - 1e3 * 3600 * 24;
778
- var timestamp = () => now() - TIME_ORIGIN;
779
+ var TIME_ORIGIN = now() - 1e3 * 3600 * 12;
780
+ var timestamp = () => now() - TIME_ORIGIN | 0;
781
+ var CP_DEFAULT_INTERVAL = 100;
779
782
  var MAX_DEPTH = 128;
780
783
  var CP_UNSET = -1;
781
784
  var CP_DEFAULT_TIMEOUT = 100;
782
785
  var cpDepth = 0;
783
- var cp = CP_UNSET;
784
- var cpTimeout = CP_DEFAULT_TIMEOUT;
786
+ var cp = CP_UNSET | 0;
787
+ var cpTimeout = CP_DEFAULT_TIMEOUT | 0;
788
+ var cpInterval = CP_DEFAULT_INTERVAL | 0;
789
+ var cpCounter = 0;
785
790
  function Cp() {
786
- if (cp === CP_UNSET) {
787
- cp = timestamp();
788
- } else if (timestamp() - cp > cpTimeout) {
789
- throw new RangeError("Execution timeout");
791
+ if ((cp | 0) === (CP_UNSET | 0)) {
792
+ cpCounter = 0;
793
+ cp = timestamp() | 0;
794
+ return;
795
+ }
796
+ if ((cpCounter = cpCounter + 1 | 0) % (cpInterval | 0) !== 0) {
797
+ return;
798
+ }
799
+ cpCounter = 0;
800
+ if ((timestamp() | 0) - (cp | 0) >= (cpTimeout | 0)) {
801
+ throw new RangeError("Execution timed out");
790
802
  }
791
803
  }
792
804
  function CpEnter() {
793
- cpDepth++;
794
- if (cpDepth <= 1) {
795
- cp = timestamp();
805
+ cpDepth = (cpDepth | 0) + 1;
806
+ if ((cpDepth | 0) <= 1) {
807
+ cp = timestamp() | 0;
796
808
  cpDepth = 1;
797
- } else if (cpDepth > MAX_DEPTH) {
809
+ } else if ((cpDepth | 0) > MAX_DEPTH) {
798
810
  throw new RangeError("Maximum call depth exceeded");
799
811
  } else {
800
812
  Cp();
801
813
  }
802
814
  }
803
815
  function CpExit() {
804
- cpDepth--;
805
- if (cpDepth < 1) {
806
- cp = CP_UNSET;
816
+ cpDepth = (cpDepth | 0) - 1;
817
+ if ((cpDepth | 0) < 1) {
818
+ cp = CP_UNSET | 0;
807
819
  cpDepth = 0;
808
820
  } else {
809
821
  Cp();
810
822
  }
811
823
  }
812
- function configCheckpoint(timeout = CP_DEFAULT_TIMEOUT) {
824
+ var INT_MAX = 2147483647;
825
+ function configCheckpoint(timeout = CP_DEFAULT_TIMEOUT, checkInterval = CP_DEFAULT_INTERVAL) {
813
826
  if (typeof timeout !== "number" || timeout <= 0 || isNaN(timeout)) {
814
827
  throw new RangeError("Invalid timeout value");
828
+ } else if (timeout > INT_MAX) {
829
+ timeout = INT_MAX;
830
+ }
831
+ if (typeof checkInterval !== "number" || checkInterval <= 0 || isNaN(checkInterval)) {
832
+ throw new RangeError("Invalid check interval value");
833
+ } else if (checkInterval > INT_MAX) {
834
+ checkInterval = INT_MAX;
815
835
  }
816
- cpTimeout = Math.ceil(timeout);
836
+ cpTimeout = timeout | 0;
837
+ cpInterval = checkInterval | 0;
817
838
  }
818
839
 
819
840
  // src/vm/types/function.ts
@@ -856,11 +877,12 @@ function VmFunction(fn, option = {}) {
856
877
  // src/vm/types/context.ts
857
878
  var { freeze } = Object;
858
879
  var VM_SHARED_CONTEXT = create(null);
880
+ var VM_SHARED_CONTEXT_DESCRIPTIONS = create(null);
859
881
  var VM_SHARED_CONTEXT_KEYS = null;
860
882
  function globalVarNotFound(name) {
861
883
  throw new VmError(`Global variable '${name}' is not defined.`, null);
862
884
  }
863
- function defineVmContextValue(name, value, override = false) {
885
+ function defineVmContextValue(name, value, override = false, description = void 0) {
864
886
  if (!override && name in VM_SHARED_CONTEXT) throw new Error(`Global variable '${name}' is already defined.`);
865
887
  let v;
866
888
  if (typeof value == "function") {
@@ -872,9 +894,11 @@ function defineVmContextValue(name, value, override = false) {
872
894
  v = value;
873
895
  }
874
896
  VM_SHARED_CONTEXT[name] = v ?? null;
897
+ if (description) VM_SHARED_CONTEXT_DESCRIPTIONS[name] = description;
875
898
  VM_SHARED_CONTEXT_KEYS = null;
876
899
  }
877
900
  var DefaultVmContext = freeze({
901
+ __proto__: null,
878
902
  [kVmContext]: true,
879
903
  /** @inheritdoc */
880
904
  keys() {
@@ -890,15 +914,16 @@ var DefaultVmContext = freeze({
890
914
  /** @inheritdoc */
891
915
  has(key) {
892
916
  return key in VM_SHARED_CONTEXT;
917
+ },
918
+ /** @inheritdoc */
919
+ describe(key) {
920
+ return VM_SHARED_CONTEXT_DESCRIPTIONS[key];
893
921
  }
894
922
  });
895
- var _a;
896
- _a = kVmContext;
897
923
  var ValueVmContext = class {
898
- constructor(env, describe) {
924
+ constructor(env, describer) {
899
925
  this.env = env;
900
- this.describe = describe;
901
- this[_a] = true;
926
+ this.describer = describer;
902
927
  this.cachedKeys = null;
903
928
  }
904
929
  /** @inheritdoc */
@@ -920,15 +945,19 @@ var ValueVmContext = class {
920
945
  has(key) {
921
946
  return key in this.env;
922
947
  }
948
+ /** @inheritdoc */
949
+ describe(key) {
950
+ if (hasOwn(this.env, key)) return this.describer?.(key);
951
+ return VM_SHARED_CONTEXT_DESCRIPTIONS[key];
952
+ }
923
953
  };
924
- var _a2;
925
- _a2 = kVmContext;
954
+ defineProperty(ValueVmContext.prototype, kVmContext, { value: true });
955
+ freeze(ValueVmContext.prototype);
926
956
  var FactoryVmContext = class {
927
- constructor(getter, enumerator, describe) {
957
+ constructor(getter, enumerator, describer) {
928
958
  this.getter = getter;
929
959
  this.enumerator = enumerator;
930
- this.describe = describe;
931
- this[_a2] = true;
960
+ this.describer = describer;
932
961
  }
933
962
  /** @inheritdoc */
934
963
  keys() {
@@ -945,16 +974,25 @@ var FactoryVmContext = class {
945
974
  has(key) {
946
975
  return this.getter(key) !== void 0 || DefaultVmContext.has(key);
947
976
  }
977
+ /** @inheritdoc */
978
+ describe(key) {
979
+ if (this.getter(key) !== void 0) return this.describer?.(key);
980
+ return VM_SHARED_CONTEXT_DESCRIPTIONS[key];
981
+ }
948
982
  };
949
- function createVmContext(...args) {
950
- if (args[0] == null && args[1] == null) {
951
- return { ...DefaultVmContext };
983
+ defineProperty(FactoryVmContext.prototype, kVmContext, { value: true });
984
+ freeze(FactoryVmContext.prototype);
985
+ function createVmContext(arg0 = null, arg1 = null, describer = null) {
986
+ if (arg0 == null && arg1 == null) {
987
+ return DefaultVmContext;
952
988
  }
953
- if (typeof args[0] == "function") {
954
- const [getter, enumerator, describer2] = args;
955
- return new FactoryVmContext(getter, enumerator ?? void 0, describer2 ?? void 0);
989
+ if (typeof arg0 == "function") {
990
+ const getter = arg0;
991
+ const enumerator = arg1;
992
+ return new FactoryVmContext(getter, enumerator, describer);
956
993
  }
957
- const [vmValues, externValues, describer] = args;
994
+ const vmValues = arg0;
995
+ const externValues = arg1;
958
996
  const env = create(VM_SHARED_CONTEXT);
959
997
  if (vmValues) {
960
998
  for (const [key, value] of entries(vmValues)) {
@@ -967,12 +1005,9 @@ function createVmContext(...args) {
967
1005
  env[key] = value == null ? null : wrapToVmValue(value, null);
968
1006
  }
969
1007
  }
970
- return new ValueVmContext(env, describer ?? void 0);
1008
+ return new ValueVmContext(env, describer);
971
1009
  }
972
1010
 
973
- // src/compiler/index.ts
974
- import { loadModule } from "@mirascript/bindings";
975
-
976
1011
  // src/vm/helpers.ts
977
1012
  var helpers_exports = {};
978
1013
  __export(helpers_exports, {
@@ -1002,13 +1037,14 @@ var Element = (value) => {
1002
1037
  if (!isVmConst(value)) return null;
1003
1038
  return value;
1004
1039
  };
1040
+ var EMPTY = create(null);
1005
1041
  var ElementOpt = (key, value) => {
1006
1042
  $AssertInit(value);
1007
- if (value == null || !isVmConst(value)) return {};
1008
- return { [key]: value };
1043
+ if (value == null || !isVmConst(value)) return EMPTY;
1044
+ return { __proto__: null, [key]: value };
1009
1045
  };
1010
- var Function2 = (fn, name = VM_FUNCTION_ANONYMOUS_NAME) => {
1011
- defineProperty(fn, "name", { value: name });
1046
+ var Function2 = (name, fn) => {
1047
+ defineProperty(fn, "name", { value: name || VM_FUNCTION_ANONYMOUS_NAME });
1012
1048
  return VmFunction(fn, { isLib: false, injectCp: false });
1013
1049
  };
1014
1050
  var Upvalue = (value) => {
@@ -1085,113 +1121,7 @@ function createScript(source, code) {
1085
1121
  return wrapScript(source, script);
1086
1122
  }
1087
1123
 
1088
- // src/compiler/compile-fast.ts
1089
- var REG_NUMBER_FULL = /^\d+(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
1090
- var REG_IDENTIFIER_FAST = /^(?:\$+|@+)[a-zA-Z0-9_]*$/;
1091
- var FAST_SCRIPT_MAX_LEN = 32;
1092
- function compileScriptFast(code, options) {
1093
- if (code.length > FAST_SCRIPT_MAX_LEN) return void 0;
1094
- const trimmedCode = code.trim();
1095
- if (!trimmedCode) {
1096
- return wrapScript(code, () => null);
1097
- }
1098
- switch (trimmedCode) {
1099
- case "nil":
1100
- return wrapScript(code, () => null);
1101
- case "true":
1102
- return wrapScript(code, () => true);
1103
- case "false":
1104
- return wrapScript(code, () => false);
1105
- case "nan":
1106
- return wrapScript(code, () => 0 / 0);
1107
- case "inf":
1108
- case "+inf":
1109
- return wrapScript(code, () => 1 / 0);
1110
- case "-inf":
1111
- return wrapScript(code, () => -1 / 0);
1112
- }
1113
- if (REG_IDENTIFIER_FAST.test(trimmedCode)) {
1114
- const id = trimmedCode;
1115
- return wrapScript(code, (global = GlobalFallback()) => global.get(id));
1116
- }
1117
- if (REG_NUMBER_FULL.test(trimmedCode)) {
1118
- const num2 = Number(trimmedCode);
1119
- if (!isFinite(num2)) return void 0;
1120
- return wrapScript(code, () => num2);
1121
- }
1122
- return void 0;
1123
- }
1124
- var FAST_TEMPLATE_MAX_LEN = 1024;
1125
- function compileTemplateFast(code, options) {
1126
- if (code.length > FAST_TEMPLATE_MAX_LEN) return void 0;
1127
- if (!code.includes("$")) {
1128
- return wrapScript(code, () => code);
1129
- }
1130
- return void 0;
1131
- }
1132
- function compileFast(code, options) {
1133
- if (options.sourceMap) return void 0;
1134
- return (options.input_mode === "Template" ? compileTemplateFast : compileScriptFast)(code, options);
1135
- }
1136
-
1137
- // src/compiler/worker-manager.ts
1138
- var worker;
1139
- async function getWorker() {
1140
- if (worker) return worker;
1141
- worker = new Promise((resolve, reject) => {
1142
- const w = new Worker(new URL("#compiler/worker", import.meta.url), {
1143
- type: "module",
1144
- name: "@mirascript/compiler"
1145
- });
1146
- const onError = (e) => {
1147
- cleanUp();
1148
- reject(new Error(`Worker failed to start: ${e.message}`));
1149
- };
1150
- const onMessage = (e) => {
1151
- if (e.data === "ready") {
1152
- cleanUp();
1153
- resolve(w);
1154
- } else if (e.data instanceof Error) {
1155
- cleanUp();
1156
- reject(e.data);
1157
- }
1158
- };
1159
- w.addEventListener("error", onError);
1160
- w.addEventListener("message", onMessage);
1161
- const cleanUp = () => {
1162
- w.removeEventListener("error", onError);
1163
- w.removeEventListener("message", onMessage);
1164
- };
1165
- setTimeout(() => {
1166
- onError(new ErrorEvent("error", { message: "Worker did not respond in time" }));
1167
- }, 3e4);
1168
- });
1169
- return worker;
1170
- }
1171
- async function compileWorker(...args) {
1172
- const worker2 = await getWorker();
1173
- const seq = Math.random();
1174
- worker2.postMessage([seq, ...args]);
1175
- return await new Promise((resolve, reject) => {
1176
- const callback = (ev) => {
1177
- const data = ev.data;
1178
- if (!Array.isArray(data)) return;
1179
- const [retSeq, ...rest] = data;
1180
- if (seq !== retSeq) return;
1181
- worker2.removeEventListener("message", callback);
1182
- if (rest.length === 2) {
1183
- resolve(rest);
1184
- } else {
1185
- reject(rest[0]);
1186
- }
1187
- };
1188
- worker2.addEventListener("message", callback);
1189
- });
1190
- }
1191
-
1192
- // src/compiler/index.ts
1193
- await loadModule();
1194
- var WORKER_MIN_LEN = typeof Worker != "function" ? Number.MAX_VALUE : 1024;
1124
+ // src/compiler/emit-script.ts
1195
1125
  function reportDiagnostic(source, diagnostics, fileName) {
1196
1126
  const parsed = parseDiagnostics(source, diagnostics);
1197
1127
  const messages = formatDiagnostics(parsed.errors, source, fileName);
@@ -1206,33 +1136,10 @@ function emitScript(source, [code, diagnostics], options) {
1206
1136
  const target = emit(source, code, sourcemaps, options);
1207
1137
  return createScript(source, target);
1208
1138
  }
1209
- async function compile(source, options = {}) {
1210
- if (options.sourceMap) {
1211
- options.diagnostic_sourcemap = true;
1212
- options.diagnostic_position_encoding ??= "Utf16";
1213
- }
1214
- if (typeof source == "string") {
1215
- const result = compileFast(source, options);
1216
- if (result) return result;
1217
- }
1218
- if (source.length < WORKER_MIN_LEN) {
1219
- const bc = await generateBytecode(source, options);
1220
- return emitScript(source, bc, options);
1221
- }
1222
- const [target, diagnostics] = await compileWorker(source, options);
1223
- if (target == null) {
1224
- reportDiagnostic(source, diagnostics, options.fileName);
1225
- }
1226
- return createScript(source, target);
1227
- }
1228
- function compileSync(source, options = {}) {
1229
- if (typeof source == "string") {
1230
- const result = compileFast(source, options);
1231
- if (result) return result;
1232
- }
1233
- const bc = generateBytecodeSync(source, options);
1234
- return emitScript(source, bc, options);
1235
- }
1139
+
1140
+ // src/compiler/load-module.ts
1141
+ import { loadModule } from "@mirascript/bindings";
1142
+ await loadModule();
1236
1143
 
1237
1144
  // src/vm/types/module.ts
1238
1145
  var VmModule = class extends VmWrapper {
@@ -1262,7 +1169,7 @@ var VmModule = class extends VmWrapper {
1262
1169
  return "module";
1263
1170
  }
1264
1171
  /** @inheritdoc */
1265
- get describe() {
1172
+ get tag() {
1266
1173
  return this.name;
1267
1174
  }
1268
1175
  };
@@ -1271,8 +1178,14 @@ Object.defineProperty(VmModule.prototype, kVmModule, { value: true });
1271
1178
  // src/vm/lib/global/index.ts
1272
1179
  var global_exports = {};
1273
1180
  __export(global_exports, {
1274
- "@e": () => E,
1275
- "@pi": () => PI,
1181
+ E: () => E,
1182
+ LN10: () => LN10,
1183
+ LN2: () => LN2,
1184
+ LOG10E: () => LOG10E,
1185
+ LOG2E: () => LOG2E,
1186
+ PI: () => PI,
1187
+ SQRT1_2: () => SQRT1_2,
1188
+ SQRT2: () => SQRT2,
1276
1189
  abs: () => abs2,
1277
1190
  acos: () => acos,
1278
1191
  acosh: () => acosh,
@@ -1503,10 +1416,9 @@ function map(data, mapper) {
1503
1416
  return fromEntries(e);
1504
1417
  }
1505
1418
  }
1506
- function VmLib(fn, option, properties) {
1507
- if (typeof fn != "function") throw new TypeError("Invalid function");
1508
- if (isVmFunction(fn)) throw new TypeError("Cannot create VmLib from a VmFunction");
1509
- const ret = fn;
1419
+ function VmLib(value, option, properties) {
1420
+ if (isVmFunction(value)) throw new TypeError("Cannot create VmLib from a VmFunction");
1421
+ const ret = typeof value == "function" ? value : { value };
1510
1422
  Object.assign(ret, properties);
1511
1423
  ret.params = option.params;
1512
1424
  ret.paramsType = option.paramsType;
@@ -1514,6 +1426,7 @@ function VmLib(fn, option, properties) {
1514
1426
  ret.returnsType = option.returnsType;
1515
1427
  ret.summary = option.summary;
1516
1428
  ret.examples = option.examples;
1429
+ ret.injectCp = option.injectCp ?? false;
1517
1430
  return ret;
1518
1431
  }
1519
1432
 
@@ -1573,7 +1486,38 @@ var product = VmLib(
1573
1486
  );
1574
1487
 
1575
1488
  // src/vm/lib/global/math-const.ts
1576
- var { PI, E } = Math;
1489
+ var PI = VmLib(Math.PI, {
1490
+ summary: "圆周率",
1491
+ returnsType: "number"
1492
+ });
1493
+ var E = VmLib(Math.E, {
1494
+ summary: "自然对数的底数",
1495
+ returnsType: "number"
1496
+ });
1497
+ var SQRT1_2 = VmLib(Math.SQRT1_2, {
1498
+ summary: "1 的平方根除以 2",
1499
+ returnsType: "number"
1500
+ });
1501
+ var SQRT2 = VmLib(Math.SQRT2, {
1502
+ summary: "2 的平方根",
1503
+ returnsType: "number"
1504
+ });
1505
+ var LN2 = VmLib(Math.LN2, {
1506
+ summary: "2 的自然对数",
1507
+ returnsType: "number"
1508
+ });
1509
+ var LN10 = VmLib(Math.LN10, {
1510
+ summary: "10 的自然对数",
1511
+ returnsType: "number"
1512
+ });
1513
+ var LOG2E = VmLib(Math.LOG2E, {
1514
+ summary: "e 以 2 为底的对数",
1515
+ returnsType: "number"
1516
+ });
1517
+ var LOG10E = VmLib(Math.LOG10E, {
1518
+ summary: "e 以 10 为底的对数",
1519
+ returnsType: "number"
1520
+ });
1577
1521
 
1578
1522
  // src/vm/lib/global/math-unary.ts
1579
1523
  function build2(f) {
@@ -2599,7 +2543,8 @@ var to_json = VmLib(
2599
2543
  params: { data: "要转换为 JSON 的数据" },
2600
2544
  paramsType: { data: "any" },
2601
2545
  returnsType: "string",
2602
- examples: ['to_json([1, 2, 3]) // "[1,2,3]"']
2546
+ examples: ['to_json([1, 2, 3]) // "[1,2,3]"'],
2547
+ injectCp: true
2603
2548
  }
2604
2549
  );
2605
2550
  var from_json = VmLib(
@@ -2617,7 +2562,8 @@ var from_json = VmLib(
2617
2562
  params: { json: "要转换的 JSON 字符串", fallback: "如果转换失败,返回的默认值" },
2618
2563
  paramsType: { json: "string", fallback: "any" },
2619
2564
  returnsType: "any",
2620
- examples: [`from_json('{"a":1}') // (a: 1)`]
2565
+ examples: [`from_json('{"a":1}') // (a: 1)`],
2566
+ injectCp: true
2621
2567
  }
2622
2568
  );
2623
2569
 
@@ -3420,39 +3366,64 @@ var diagonal = VmLib(
3420
3366
  );
3421
3367
 
3422
3368
  // src/vm/lib/loader.ts
3369
+ var VmBuiltinModule = class extends VmModule {
3370
+ constructor(name, value, descriptions) {
3371
+ super(name, value);
3372
+ this.descriptions = descriptions;
3373
+ }
3374
+ /** @inheritdoc */
3375
+ describe(key) {
3376
+ return this.descriptions[key];
3377
+ }
3378
+ };
3423
3379
  function wrapEntry(name, value, module) {
3424
- if (typeof value == "function") {
3425
- if (value.name !== name) {
3426
- defineProperty(value, "name", {
3427
- value: name,
3428
- configurable: true
3429
- });
3380
+ if (isVmModule(value)) {
3381
+ return [value, void 0];
3382
+ }
3383
+ if (typeof value != "function") {
3384
+ if (value == null || typeof value != "object" || !("value" in value)) {
3385
+ throw new TypeError(`Cannot wrap non-function, non-const value: ${name} in module ${module}`);
3430
3386
  }
3431
- return VmFunction(value, {
3387
+ return [value.value, value.summary || void 0];
3388
+ }
3389
+ if (value.name !== name) {
3390
+ defineProperty(value, "name", {
3391
+ value: name,
3392
+ configurable: true
3393
+ });
3394
+ }
3395
+ return [
3396
+ VmFunction(value, {
3432
3397
  isLib: true,
3433
3398
  injectCp: true,
3434
3399
  fullName: `${module}.${name}`,
3435
3400
  ...value
3436
- });
3437
- } else {
3438
- return value;
3439
- }
3401
+ }),
3402
+ value.summary || void 0
3403
+ ];
3440
3404
  }
3441
3405
  function createModule(name, lib2) {
3442
3406
  const mod = create(null);
3407
+ const descriptions = create(null);
3443
3408
  for (const [key, value] of entries(lib2)) {
3444
- mod[key] = wrapEntry(key, value, name);
3409
+ const [wrappedValue, description] = wrapEntry(key, value, name);
3410
+ mod[key] = wrappedValue;
3411
+ descriptions[key] = description;
3445
3412
  }
3446
- return new VmModule(name, mod);
3413
+ return new VmBuiltinModule(name, mod, descriptions);
3447
3414
  }
3448
3415
 
3449
3416
  // src/vm/lib/index.ts
3450
3417
  for (const [name, value] of entries(global_exports)) {
3451
- VM_SHARED_CONTEXT[name] = wrapEntry(name, value, "global");
3418
+ const [wrappedValue, description] = wrapEntry(name, value, "global");
3419
+ VM_SHARED_CONTEXT[name] = wrappedValue;
3420
+ VM_SHARED_CONTEXT_DESCRIPTIONS[name] = description;
3452
3421
  }
3453
3422
  for (const [name, value] of entries(mod_exports)) {
3454
3423
  const mod = createModule(name, value);
3455
- VM_SHARED_CONTEXT[name] = wrapEntry(name, mod, "global");
3424
+ const [wrappedValue, description] = wrapEntry(name, mod, "global");
3425
+ VM_SHARED_CONTEXT[name] = wrappedValue;
3426
+ VM_SHARED_CONTEXT_DESCRIPTIONS[name] = description;
3456
3427
  }
3457
3428
  var lib = Object.freeze(Object.assign(create(null), global_exports, mod_exports));
3458
3429
 
@@ -3467,12 +3438,14 @@ export {
3467
3438
  defineVmContextValue,
3468
3439
  DefaultVmContext,
3469
3440
  createVmContext,
3441
+ GlobalFallback,
3442
+ wrapScript,
3443
+ createScript,
3444
+ reportDiagnostic,
3470
3445
  emitScript,
3471
- compile,
3472
- compileSync,
3473
3446
  VmModule,
3474
3447
  debug_print,
3475
3448
  panic,
3476
3449
  lib
3477
3450
  };
3478
- //# sourceMappingURL=chunk-3BAOZ2P2.js.map
3451
+ //# sourceMappingURL=chunk-ZKL4MRMP.js.map