kotlin-format 0.0.0-stage → 0.0.1
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.
- package/dist/LICENSE +202 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +738 -0
- package/dist/config.d.mts +21 -0
- package/dist/config.mjs +105 -0
- package/dist/kotlin-format.d.mts +18 -0
- package/dist/kotlin-format.import-object.mjs +136 -0
- package/dist/kotlin-format.js-builtins.mjs +52 -0
- package/dist/kotlin-format.mjs +98 -0
- package/dist/kotlin-format.wasm +0 -0
- package/dist/kotlin-format.wasm.map +1 -0
- package/package.json +43 -3
- package/README.md +0 -3
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export interface FormatConfig {
|
|
2
|
+
style?: "META" | "GOOGLE" | "KOTLINLANG";
|
|
3
|
+
maxWidth?: number;
|
|
4
|
+
removeUnusedImports?: boolean;
|
|
5
|
+
preserveLambdaBreaks?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function exists(path: string): Promise<boolean>;
|
|
8
|
+
export declare function validateConfig(value: unknown, label?: string): FormatConfig;
|
|
9
|
+
export declare function projectRoot(file: string): Promise<string>;
|
|
10
|
+
/** Nearest config wins; stop at the supplied workspace root or nearest package/Git root. */
|
|
11
|
+
export declare function loadConfig(file: string, options?: {
|
|
12
|
+
configPath?: string;
|
|
13
|
+
root?: string;
|
|
14
|
+
allowCode?: boolean;
|
|
15
|
+
}): Promise<FormatConfig>;
|
|
16
|
+
export interface ResolvedOptions extends Required<FormatConfig> {
|
|
17
|
+
blockIndent: number;
|
|
18
|
+
continuationIndent: number;
|
|
19
|
+
trailingCommas: string;
|
|
20
|
+
}
|
|
21
|
+
export declare function resolveOptions(config?: FormatConfig): ResolvedOptions;
|
package/dist/config.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { readFile, stat } from "node:fs/promises";
|
|
2
|
+
import { dirname, extname, join, resolve } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
5
|
+
// js/config.ts
|
|
6
|
+
async function exists(path) {
|
|
7
|
+
try {
|
|
8
|
+
await stat(path);
|
|
9
|
+
return true;
|
|
10
|
+
} catch (error) {
|
|
11
|
+
if (error.code === "ENOENT") return false;
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function validateConfig(value, label = "configuration") {
|
|
16
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error(`${label}: expected an object`);
|
|
17
|
+
for (const [key, item] of Object.entries(value)) {
|
|
18
|
+
if (key === "$schema" && typeof item === "string") continue;
|
|
19
|
+
if (key === "style" && [
|
|
20
|
+
"META",
|
|
21
|
+
"GOOGLE",
|
|
22
|
+
"KOTLINLANG"
|
|
23
|
+
].includes(item)) continue;
|
|
24
|
+
if (key === "maxWidth" && Number.isInteger(item) && item >= 1 && item <= 999) continue;
|
|
25
|
+
if (["removeUnusedImports", "preserveLambdaBreaks"].includes(key) && typeof item === "boolean") continue;
|
|
26
|
+
throw new Error(`${label}: invalid option ${key}=${JSON.stringify(item)}`);
|
|
27
|
+
}
|
|
28
|
+
const { style, maxWidth, removeUnusedImports, preserveLambdaBreaks } = value;
|
|
29
|
+
return Object.fromEntries(Object.entries({
|
|
30
|
+
style,
|
|
31
|
+
maxWidth,
|
|
32
|
+
removeUnusedImports,
|
|
33
|
+
preserveLambdaBreaks
|
|
34
|
+
}).filter(([, v]) => v !== void 0));
|
|
35
|
+
}
|
|
36
|
+
async function projectRoot(file) {
|
|
37
|
+
let directory = dirname(resolve(file));
|
|
38
|
+
while (true) {
|
|
39
|
+
if (await exists(join(directory, ".git")) || await exists(join(directory, "package.json"))) return directory;
|
|
40
|
+
const parent = dirname(directory);
|
|
41
|
+
if (parent === directory) return directory;
|
|
42
|
+
directory = parent;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Nearest config wins; stop at the supplied workspace root or nearest package/Git root. */
|
|
46
|
+
async function loadConfig(file, options = {}) {
|
|
47
|
+
let path = options.configPath && resolve(options.configPath);
|
|
48
|
+
if (!path) {
|
|
49
|
+
let directory = dirname(resolve(file));
|
|
50
|
+
const root = resolve(options.root ?? await projectRoot(file));
|
|
51
|
+
while (true) {
|
|
52
|
+
const candidates = [];
|
|
53
|
+
for (const name of [
|
|
54
|
+
"kotlin-format.json",
|
|
55
|
+
"kotlin-format.config.js",
|
|
56
|
+
"kotlin-format.config.mjs",
|
|
57
|
+
"kotlin-format.config.ts"
|
|
58
|
+
]) {
|
|
59
|
+
const candidate = join(directory, name);
|
|
60
|
+
if (await exists(candidate)) candidates.push(candidate);
|
|
61
|
+
}
|
|
62
|
+
if (candidates.length > 1) throw new Error(`${directory}: multiple kotlin-format configurations found`);
|
|
63
|
+
if (candidates.length) {
|
|
64
|
+
path = candidates[0];
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
const parent = dirname(directory);
|
|
68
|
+
if (directory === root || parent === directory) break;
|
|
69
|
+
directory = parent;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!path) return {};
|
|
73
|
+
try {
|
|
74
|
+
const extension = extname(path);
|
|
75
|
+
if (![
|
|
76
|
+
".json",
|
|
77
|
+
".js",
|
|
78
|
+
".mjs",
|
|
79
|
+
".ts"
|
|
80
|
+
].includes(extension)) throw new Error("expected a .json, .js, .mjs or .ts config");
|
|
81
|
+
if (extension !== ".json" && options.allowCode === false) throw new Error("executable configuration requires a trusted workspace");
|
|
82
|
+
const source = await readFile(path, "utf8");
|
|
83
|
+
if (extension === ".json") return validateConfig(JSON.parse(source));
|
|
84
|
+
const url = pathToFileURL(path);
|
|
85
|
+
url.searchParams.set("version", createHash("sha256").update(source).digest("hex"));
|
|
86
|
+
const module = await import(url.href);
|
|
87
|
+
if ("module.exports" in module) throw new Error("CommonJS configuration is not supported; use ESM export default");
|
|
88
|
+
return validateConfig(module.default);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
throw new Error(`${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function resolveOptions(config = {}) {
|
|
94
|
+
const style = config.style ?? "KOTLINLANG";
|
|
95
|
+
return {
|
|
96
|
+
style,
|
|
97
|
+
maxWidth: config.maxWidth ?? 100,
|
|
98
|
+
blockIndent: style === "KOTLINLANG" ? 4 : 2,
|
|
99
|
+
continuationIndent: style === "GOOGLE" ? 2 : 4,
|
|
100
|
+
trailingCommas: style === "META" ? "ONLY_ADD" : "COMPLETE",
|
|
101
|
+
removeUnusedImports: config.removeUnusedImports ?? true,
|
|
102
|
+
preserveLambdaBreaks: config.preserveLambdaBreaks ?? true
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
export { exists, loadConfig, projectRoot, resolveOptions, validateConfig };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
type Nullable<T> = T | null | undefined
|
|
2
|
+
declare function KtSingleton<T>(): T & (abstract new() => any);
|
|
3
|
+
export declare function tokenize(source: string): string;
|
|
4
|
+
export declare function parseFile(source: string): string;
|
|
5
|
+
export declare function parseScript(source: string): string;
|
|
6
|
+
export declare function openDocument(source: string, script: boolean): number;
|
|
7
|
+
export declare function editDocument(id: number, start: number, end: number, text: string): string;
|
|
8
|
+
export declare function documentTree(id: number): string;
|
|
9
|
+
export declare function documentSymbols(id: number): string;
|
|
10
|
+
export declare function documentNavigationQuery(id: number, offset: number): string;
|
|
11
|
+
export declare function documentExternalReference(id: number, offset: number): string;
|
|
12
|
+
export declare function closeDocument(id: number): void;
|
|
13
|
+
export declare function applyDocumentEdit(id: number, start: number, end: number, text: string): void;
|
|
14
|
+
export declare function documentClassifications(id: number): NonNullable<unknown>;
|
|
15
|
+
export declare function tokenLegend(): string;
|
|
16
|
+
export declare function documentDefinitions(id: number, offset: number): NonNullable<unknown>;
|
|
17
|
+
export declare function formatCode(source: string, style: string, maxWidth: number, script: boolean): string;
|
|
18
|
+
export declare function formatWithOptions(source: string, maxWidth: number, blockIndent: number, continuationIndent: number, trailingCommas: string, removeUnusedImports: boolean, preserveLambdaBreaks: boolean, script: boolean): string;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
|
|
2
|
+
import * as d2FzbTpqcy1zdHJpbmc from './kotlin-format.js-builtins.mjs';
|
|
3
|
+
|
|
4
|
+
const wasmJsTag = WebAssembly.JSTag;
|
|
5
|
+
const wasmTag = wasmJsTag ?? new WebAssembly.Tag({ parameters: ['externref'] });
|
|
6
|
+
|
|
7
|
+
// Placed here to give access to it from externals (js_code)
|
|
8
|
+
let wasmExports;
|
|
9
|
+
|
|
10
|
+
if (typeof process !== 'undefined' && process.release.name === 'node') {
|
|
11
|
+
function doNotUseRequire() {
|
|
12
|
+
throw new Error("Do not use top-level require. Prefer to use JS import or define your own require instead. Read more: https://kotl.in/r9txlt")
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
var require = new Proxy((function() {}), {
|
|
16
|
+
apply(target, thisArg, argumentsList) {
|
|
17
|
+
if (globalThis.require != null) {
|
|
18
|
+
return globalThis.require.apply(thisArg, argumentsList);
|
|
19
|
+
} else {
|
|
20
|
+
doNotUseRequire();
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
get(target, prop, receiver) {
|
|
24
|
+
if (globalThis.require != null) {
|
|
25
|
+
return Reflect.get(globalThis.require, prop);
|
|
26
|
+
} else {
|
|
27
|
+
doNotUseRequire();
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function setWasmExports(exports) {
|
|
34
|
+
wasmExports = exports;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const cachedJsObjects = new WeakMap();
|
|
38
|
+
function getCachedJsObject(ref, ifNotCached) {
|
|
39
|
+
if (typeof ref !== 'object' && typeof ref !== 'function') return ifNotCached;
|
|
40
|
+
const cached = cachedJsObjects.get(ref);
|
|
41
|
+
if (cached !== void 0) return cached;
|
|
42
|
+
cachedJsObjects.set(ref, ifNotCached);
|
|
43
|
+
return ifNotCached;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const js_code = {
|
|
47
|
+
'kotlin.createJsError' : (message, cause) => new Error(message, { cause }),
|
|
48
|
+
'kotlin.wasm.internal.jsThrow' : wasmTag === wasmJsTag ? (e) => { throw e; } : () => {},
|
|
49
|
+
'kotlin.wasm.internal.getJsEmptyString' : () => '',
|
|
50
|
+
'kotlin.wasm.internal.externrefToString' : (ref) => String(ref),
|
|
51
|
+
'kotlin.wasm.internal.externrefEquals' : (lhs, rhs) => lhs === rhs,
|
|
52
|
+
'kotlin.wasm.internal.externrefHashCode' :
|
|
53
|
+
(() => {
|
|
54
|
+
const dataView = new DataView(new ArrayBuffer(8));
|
|
55
|
+
function numberHashCode(obj) {
|
|
56
|
+
if ((obj | 0) === obj) {
|
|
57
|
+
return obj | 0;
|
|
58
|
+
} else {
|
|
59
|
+
dataView.setFloat64(0, obj, true);
|
|
60
|
+
return (dataView.getInt32(0, true) * 31 | 0) + dataView.getInt32(4, true) | 0;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const hashCodes = new WeakMap();
|
|
65
|
+
function getObjectHashCode(obj) {
|
|
66
|
+
const res = hashCodes.get(obj);
|
|
67
|
+
if (res === undefined) {
|
|
68
|
+
const POW_2_32 = 4294967296;
|
|
69
|
+
const hash = (Math.random() * POW_2_32) | 0;
|
|
70
|
+
hashCodes.set(obj, hash);
|
|
71
|
+
return hash;
|
|
72
|
+
}
|
|
73
|
+
return res;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function getStringHashCode(str) {
|
|
77
|
+
var hash = 0;
|
|
78
|
+
for (var i = 0; i < str.length; i++) {
|
|
79
|
+
var code = str.charCodeAt(i);
|
|
80
|
+
hash = (hash * 31 + code) | 0;
|
|
81
|
+
}
|
|
82
|
+
return hash;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return (obj) => {
|
|
86
|
+
if (obj == null) {
|
|
87
|
+
return 0;
|
|
88
|
+
}
|
|
89
|
+
switch (typeof obj) {
|
|
90
|
+
case "object":
|
|
91
|
+
case "function":
|
|
92
|
+
return getObjectHashCode(obj);
|
|
93
|
+
case "number":
|
|
94
|
+
return numberHashCode(obj);
|
|
95
|
+
case "boolean":
|
|
96
|
+
return obj ? 1231 : 1237;
|
|
97
|
+
default:
|
|
98
|
+
return getStringHashCode(String(obj));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
})(),
|
|
102
|
+
'kotlin.wasm.internal.isNullish' : (ref) => ref == null,
|
|
103
|
+
'kotlin.wasm.internal.kotlinUIntToJsNumberUnsafe' : (x) => x >>> 0,
|
|
104
|
+
'kotlin.wasm.internal.getCachedJsObject_$external_fun' : (p0, p1) => getCachedJsObject(p0, p1),
|
|
105
|
+
'kotlin.wasm.internal.utoa32_$external_fun' : (p0) => String(p0),
|
|
106
|
+
'kotlin.wasm.internal.itoa32_$external_fun' : (p0) => String(p0),
|
|
107
|
+
'kotlin.js.stackPlaceHolder_js_code' : () => (''),
|
|
108
|
+
'kotlin.js.message_$external_prop_getter' : (_this) => _this.message,
|
|
109
|
+
'kotlin.js.name_$external_prop_setter' : (_this, v) => _this.name = v,
|
|
110
|
+
'kotlin.js.stack_$external_prop_getter' : (_this) => _this.stack,
|
|
111
|
+
'kotlin.js.kotlinException_$external_prop_getter' : (_this) => _this.kotlinException,
|
|
112
|
+
'kotlin.js.kotlinException_$external_prop_setter' : (_this, v) => _this.kotlinException = v,
|
|
113
|
+
'kotlin.js.JsError_$external_class_instanceof' : (x) => x instanceof Error,
|
|
114
|
+
'kotlin.random.initialSeed' : () => ((Math.random() * Math.pow(2, 32)) | 0),
|
|
115
|
+
'kotlin.wasm.internal.getJsClassName' : (jsKlass) => jsKlass.name,
|
|
116
|
+
'kotlin.wasm.internal.getConstructor' : (obj) => obj.constructor,
|
|
117
|
+
'li.songe.kotlin.writeSpan' : (data, offset, start, end, type, modifiers) => { data[offset] = start; data[offset + 1] = end; data[offset + 2] = type; data[offset + 3] = modifiers; },
|
|
118
|
+
'li.songe.kotlin.Int32Array_$external_fun' : (p0) => new Int32Array(p0)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const StringConstantsProxy = new Proxy({}, {
|
|
122
|
+
get(_, prop) { return prop; }
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
export { wasmTag as __TAG };
|
|
126
|
+
|
|
127
|
+
export const importObject = {
|
|
128
|
+
js_code,
|
|
129
|
+
intrinsics: {
|
|
130
|
+
memory: new WebAssembly.Memory({ initial: 0 }),
|
|
131
|
+
tag: wasmTag
|
|
132
|
+
},
|
|
133
|
+
"'": StringConstantsProxy,
|
|
134
|
+
'wasm:js-string': d2FzbTpqcy1zdHJpbmc,
|
|
135
|
+
};
|
|
136
|
+
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
export const length = (s) => s.length
|
|
2
|
+
export const concat = (a, b) => a + b
|
|
3
|
+
export const charCodeAt = (s, i) => s.charCodeAt(i >>> 0)
|
|
4
|
+
export const substring = (s, start, end) => {
|
|
5
|
+
start >>>= 0;
|
|
6
|
+
end >>>= 0;
|
|
7
|
+
return s.substring(start, end);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const compare = (a, b) => {
|
|
11
|
+
return a === b ? 0 : a < b ? -1 : 1;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const equals = (a, b) => a === b ? 1 : 0
|
|
15
|
+
const moduleFromCharCodeArray = new WebAssembly.Module(new Uint8Array([
|
|
16
|
+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0b, 0x02, 0x5e,
|
|
17
|
+
0x77, 0x01, 0x60, 0x02, 0x64, 0x00, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01,
|
|
18
|
+
0x01, 0x07, 0x0b, 0x01, 0x07, 0x61, 0x31, 0x36, 0x5f, 0x67, 0x65, 0x74,
|
|
19
|
+
0x00, 0x00, 0x0a, 0x0b, 0x01, 0x09, 0x00, 0x20, 0x00, 0x20, 0x01, 0xfb,
|
|
20
|
+
0x0d, 0x00, 0x0b, 0x00, 0x13, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x04, 0x0c,
|
|
21
|
+
0x01, 0x00, 0x09, 0x61, 0x72, 0x72, 0x61, 0x79, 0x5f, 0x69, 0x31, 0x36
|
|
22
|
+
]));
|
|
23
|
+
const helpersFromCharCodeArray = new WebAssembly.Instance(moduleFromCharCodeArray).exports;
|
|
24
|
+
|
|
25
|
+
export const fromCharCodeArray = (array, start, end) => {
|
|
26
|
+
start >>>= 0;
|
|
27
|
+
end >>>= 0;
|
|
28
|
+
let result = [];
|
|
29
|
+
for (let i = start; i < end; i++) {
|
|
30
|
+
result.push(String.fromCharCode(helpersFromCharCodeArray.a16_get(array, i)));
|
|
31
|
+
}
|
|
32
|
+
return result.join("");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const moduleIntoCharCode = new WebAssembly.Module(new Uint8Array([
|
|
36
|
+
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0b, 0x02, 0x5e,
|
|
37
|
+
0x77, 0x01, 0x60, 0x03, 0x64, 0x00, 0x7f, 0x7f, 0x00, 0x03, 0x02, 0x01,
|
|
38
|
+
0x01, 0x07, 0x0b, 0x01, 0x07, 0x61, 0x31, 0x36, 0x5f, 0x73, 0x65, 0x74,
|
|
39
|
+
0x00, 0x00, 0x0a, 0x0d, 0x01, 0x0b, 0x00, 0x20, 0x00, 0x20, 0x01, 0x20,
|
|
40
|
+
0x02, 0xfb, 0x0e, 0x00, 0x0b, 0x00, 0x13, 0x04, 0x6e, 0x61, 0x6d, 0x65,
|
|
41
|
+
0x04, 0x0c, 0x01, 0x00, 0x09, 0x61, 0x72, 0x72, 0x61, 0x79, 0x5f, 0x69,
|
|
42
|
+
0x31, 0x36
|
|
43
|
+
]));
|
|
44
|
+
const helpersIntoCharCode = new WebAssembly.Instance(moduleIntoCharCode).exports;
|
|
45
|
+
|
|
46
|
+
export const intoCharCodeArray = (s, array, start) => {
|
|
47
|
+
start >>>= 0;
|
|
48
|
+
for (let i = 0; i < s.length; i++) {
|
|
49
|
+
helpersIntoCharCode.a16_set(array, start + i, s.charCodeAt(i));
|
|
50
|
+
}
|
|
51
|
+
return s.length;
|
|
52
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { importObject, setWasmExports } from './kotlin-format.import-object.mjs'
|
|
4
|
+
|
|
5
|
+
let wasmInstance;
|
|
6
|
+
|
|
7
|
+
const wasmOptions = { builtins: ['js-string'], importedStringConstants: "'" }
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
if ((typeof process !== 'undefined') && (process.release.name === 'node')) {
|
|
11
|
+
const fs = await import(/* webpackIgnore: true */'node:fs');
|
|
12
|
+
const url = await import(/* webpackIgnore: true */'node:url');
|
|
13
|
+
const filepath = import.meta.resolve('./kotlin-format.wasm');
|
|
14
|
+
const wasmBuffer = fs.readFileSync(url.fileURLToPath(filepath));
|
|
15
|
+
const wasmModule = new WebAssembly.Module(wasmBuffer, wasmOptions);
|
|
16
|
+
wasmInstance = new WebAssembly.Instance(wasmModule, importObject);
|
|
17
|
+
} else if (typeof Deno !== 'undefined') {
|
|
18
|
+
const path = await import(/* webpackIgnore: true */'https://deno.land/std/path/mod.ts');
|
|
19
|
+
const binary = Deno.readFileSync(path.fromFileUrl(import.meta.resolve('./kotlin-format.wasm')));
|
|
20
|
+
const module = await WebAssembly.compile(binary, wasmOptions);
|
|
21
|
+
wasmInstance = await WebAssembly.instantiate(module, importObject);
|
|
22
|
+
} else if (
|
|
23
|
+
(typeof d8 !== 'undefined' // V8
|
|
24
|
+
|| typeof inIon !== 'undefined' // SpiderMonkey
|
|
25
|
+
|| typeof jscOptions !== 'undefined' // JavaScriptCore
|
|
26
|
+
)
|
|
27
|
+
) {
|
|
28
|
+
const filepath = import.meta.url.replace(/\.mjs$/, '.wasm');
|
|
29
|
+
const wasmBuffer = read(filepath, 'binary');
|
|
30
|
+
const wasmModule = new WebAssembly.Module(wasmBuffer, wasmOptions);
|
|
31
|
+
wasmInstance = new WebAssembly.Instance(wasmModule, importObject);
|
|
32
|
+
} else {
|
|
33
|
+
wasmInstance = (await WebAssembly.instantiateStreaming(fetch(new URL('./kotlin-format.wasm',import.meta.url).href), importObject, wasmOptions)).instance;
|
|
34
|
+
}
|
|
35
|
+
} catch (e) {
|
|
36
|
+
if (e instanceof WebAssembly.CompileError) {
|
|
37
|
+
let text = `Please make sure that your runtime environment supports the latest version of Wasm GC and Exception-Handling proposals.
|
|
38
|
+
For more information, see https://kotl.in/wasm-help
|
|
39
|
+
`;
|
|
40
|
+
if (typeof console !== "undefined" && console.error !== void 0) {
|
|
41
|
+
console.error(text);
|
|
42
|
+
} else {
|
|
43
|
+
const t = "\n" + text;
|
|
44
|
+
if (typeof console !== "undefined" && console.log !== void 0)
|
|
45
|
+
console.log(t);
|
|
46
|
+
else
|
|
47
|
+
print(t);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
throw e;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const exports = wasmInstance.exports
|
|
54
|
+
|
|
55
|
+
let memoryFirstTimeAccess = true;
|
|
56
|
+
const memoryProxy = new Proxy(importObject.intrinsics.memory, {
|
|
57
|
+
get(target, prop, receiver) {
|
|
58
|
+
if (memoryFirstTimeAccess) {
|
|
59
|
+
memoryFirstTimeAccess = false;
|
|
60
|
+
console.error('Accessing `memory` via `wasmExports` is deprecated. Use `kotlin.wasm.unsafe.wasmMemory` or update dependencies. Read more: https://kotl.in/vr3szr');
|
|
61
|
+
}
|
|
62
|
+
return Reflect.get(target, prop);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
const wasmExports = new Proxy(memoryProxy, {
|
|
66
|
+
get(target, prop, receiver) {
|
|
67
|
+
if (prop == 'memory') {
|
|
68
|
+
return target;
|
|
69
|
+
} else {
|
|
70
|
+
throw new Error('Accessing exports via `wasmExports` is no longer supported. Remove usages or update dependencies. Read more: https://kotl.in/vr3szr');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const wasmMemory = wasmExports.memory;
|
|
76
|
+
export { wasmMemory as memory }
|
|
77
|
+
|
|
78
|
+
export const {
|
|
79
|
+
tokenize,
|
|
80
|
+
parseFile,
|
|
81
|
+
parseScript,
|
|
82
|
+
openDocument,
|
|
83
|
+
editDocument,
|
|
84
|
+
documentTree,
|
|
85
|
+
documentSymbols,
|
|
86
|
+
documentNavigationQuery,
|
|
87
|
+
documentExternalReference,
|
|
88
|
+
closeDocument,
|
|
89
|
+
applyDocumentEdit,
|
|
90
|
+
documentClassifications,
|
|
91
|
+
tokenLegend,
|
|
92
|
+
documentDefinitions,
|
|
93
|
+
formatCode,
|
|
94
|
+
formatWithOptions
|
|
95
|
+
} = exports
|
|
96
|
+
|
|
97
|
+
setWasmExports(wasmExports);
|
|
98
|
+
|
|
Binary file
|