peachsim 0.1.2536 → 0.1.2537
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-lib/agent-daemon-client.cjs +1 -1
- package/dist-lib/agent-events.cjs +1 -1
- package/dist-lib/attached-projects.cjs +1 -1
- package/dist-lib/auth/shared-session.cjs +1 -1
- package/dist-lib/backend-origin.cjs +1 -1
- package/dist-lib/beta.cjs +1 -1
- package/dist-lib/beta.mjs +2 -2
- package/dist-lib/bridge-constants.cjs +1 -1
- package/dist-lib/bridge-contract-input.cjs +1 -1
- package/dist-lib/bridge-contract-input.mjs +5 -5
- package/dist-lib/bridge-contract.cjs +1 -1
- package/dist-lib/bridge-contract.mjs +3 -3
- package/dist-lib/bundle-channel.cjs +1 -1
- package/dist-lib/capture-contract.cjs +1 -1
- package/dist-lib/capture-contract.mjs +3 -3
- package/dist-lib/cloud-contract.cjs +1 -1
- package/dist-lib/cloud-contract.mjs +3 -3
- package/dist-lib/cloud-test-args.cjs +1 -1
- package/dist-lib/cloud-test-args.mjs +2 -2
- package/dist-lib/cloud.cjs +1 -1
- package/dist-lib/cloud.mjs +7 -7
- package/dist-lib/config.cjs +1 -1
- package/dist-lib/config.mjs +5 -5
- package/dist-lib/detox/index.cjs +1 -1
- package/dist-lib/dev-bundle-resolution.cjs +1 -1
- package/dist-lib/esm-chunks/{acorn-64DCGEO5.mjs → acorn-5HHCSUPM.mjs} +3 -3
- package/dist-lib/esm-chunks/{chunk-XAWOMO5B.mjs → chunk-26RG655M.mjs} +3 -3
- package/dist-lib/esm-chunks/{chunk-5T2YXMLQ.mjs → chunk-2EAGDULI.mjs} +5 -5
- package/dist-lib/esm-chunks/{chunk-D2QGUHR5.mjs → chunk-35VRQFDT.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-WUOKJ6CB.mjs → chunk-3QBNFYCR.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-NTK7AJXK.mjs → chunk-4NCLJNSK.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-DJ2L6VEZ.mjs → chunk-5T75DPRM.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-RBQVYPVA.mjs → chunk-AAJW4QQK.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-GEIUCF2Z.mjs → chunk-BW4DU6JS.mjs} +12 -12
- package/dist-lib/esm-chunks/{chunk-CHAQFP7A.mjs → chunk-EFFCQGQ3.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-RT7WZSL5.mjs → chunk-IJJHWJIC.mjs} +4 -4
- package/dist-lib/esm-chunks/{chunk-NP7LPWJN.mjs → chunk-JLV6Z7VM.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-TCBGT4BF.mjs → chunk-KDWQVDVE.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-WG3Q2TBQ.mjs → chunk-MJYQOSJ3.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-T2IAJXJL.mjs → chunk-N6WIQ3FX.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-WRLI32WI.mjs → chunk-PR4ZNZYE.mjs} +5 -5
- package/dist-lib/esm-chunks/{chunk-2KJWJZVK.mjs → chunk-PSNWHI4Z.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-XAV4VTSB.mjs → chunk-SCMMGD3W.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-O5UIIK3M.mjs → chunk-SJCKMSWJ.mjs} +2 -2
- package/dist-lib/esm-chunks/{chunk-LJX6K63X.mjs → chunk-TTFUHKNJ.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-2QBPT7ZJ.mjs → chunk-VSGXG7TJ.mjs} +1 -1
- package/dist-lib/esm-chunks/{chunk-Q2MLJKTS.mjs → chunk-XZ6T5CMZ.mjs} +1 -1
- package/dist-lib/esm-chunks/{metro-layout-parser-K6YPVTNB.mjs → metro-layout-parser-SWDKGEBD.mjs} +3 -3
- package/dist-lib/esm-chunks/{swift-package-prebuilt-C2CRDF3M.mjs → swift-package-prebuilt-PLXBOWA7.mjs} +10 -10
- package/dist-lib/esm-chunks/{swift-sdk-app-module-R2MRWDCV.mjs → swift-sdk-app-module-6JMQHUVK.mjs} +13 -13
- package/dist-lib/esm-chunks/{swift-sdk-image-C27DZRDV.mjs → swift-sdk-image-FCZKRXBS.mjs} +13 -13
- package/dist-lib/esm-chunks/{swift-sdk-image-producer-AS4VIKW2.mjs → swift-sdk-image-producer-KKW3NIN6.mjs} +4 -4
- package/dist-lib/expo-swift-bundle.cjs +1 -1
- package/dist-lib/expo-swift-metadata.cjs +1 -1
- package/dist-lib/expo-swift-metadata.mjs +7 -7
- package/dist-lib/home-paths.cjs +1 -1
- package/dist-lib/host/bridge-host.cjs +1 -1
- package/dist-lib/host/fetch-proxy-handler.cjs +1 -1
- package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
- package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
- package/dist-lib/host/replacement-module-handler.cjs +1 -1
- package/dist-lib/host/websocket-proxy.cjs +1 -1
- package/dist-lib/index.cjs +1 -1
- package/dist-lib/kotlin-host.cjs +1 -1
- package/dist-lib/menu.cjs +1 -1
- package/dist-lib/menu.mjs +3 -3
- package/dist-lib/metro-fingerprint-registry.cjs +1 -1
- package/dist-lib/metro-fingerprint-registry.mjs +3 -3
- package/dist-lib/metro-production-bundle.cjs +1 -1
- package/dist-lib/metro-production-bundle.mjs +4 -4
- package/dist-lib/native-app-identity-project.cjs +1 -1
- package/dist-lib/native-app-identity-project.mjs +15 -15
- package/dist-lib/native-app-identity.cjs +1 -1
- package/dist-lib/native-app-identity.mjs +6 -6
- package/dist-lib/native-firebase-metadata.cjs +1 -1
- package/dist-lib/native-firebase-metadata.mjs +3 -3
- package/dist-lib/pdfium-raster.cjs +1 -1
- package/dist-lib/pdfium-raster.mjs +2 -2
- package/dist-lib/plist.cjs +1 -1
- package/dist-lib/plist.mjs +3 -3
- package/dist-lib/profiles.cjs +1 -1
- package/dist-lib/public-brand.cjs +1 -1
- package/dist-lib/public-origins-lib-esm.json +79 -79
- package/dist-lib/public-origins-lib.json +69 -69
- package/dist-lib/react-native-host-modules.cjs +1 -1
- package/dist-lib/react-native-host-modules.mjs +2 -2
- package/dist-lib/reap-browser.cjs +1 -1
- package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
- package/dist-lib/sdk.cjs +1 -1
- package/dist-lib/sdk.mjs +7 -7
- package/dist-lib/skills.cjs +1 -1
- package/dist-lib/swift-compile.cjs +1 -1
- package/dist-lib/swift-compile.mjs +10 -10
- package/dist-lib/swift-package.cjs +1 -1
- package/dist-lib/swift-package.mjs +3 -3
- package/dist-lib/swift-submit.cjs +1 -1
- package/dist-lib/swift-submit.mjs +5 -5
- package/dist-lib/swift-svg-raster.cjs +1 -1
- package/dist-lib/swift-svg-raster.mjs +2 -2
- package/dist-lib/swift-toolchain.cjs +1 -1
- package/dist-lib/swift.cjs +1 -1
- package/dist-lib/vendor/@peculiar/x509.cjs +1 -1
- package/dist-lib/vendor/@ruby/prism/src/nodes.js.cjs +1 -1
- package/dist-lib/vendor/@ruby/prism/src/parsePrism.js.cjs +1 -1
- package/dist-lib/vendor/@xmldom/xmldom.cjs +1 -1
- package/dist-lib/vendor/acorn.cjs +1 -1
- package/dist-lib/vendor/bplist-parser.cjs +1 -1
- package/dist-lib/vendor/ignore.cjs +1 -1
- package/dist-lib/vendor/magic-string.cjs +1 -1
- package/dist-lib/vendor/plist.cjs +1 -1
- package/dist-lib/vendor/smol-toml.cjs +1 -1
- package/dist-lib/vendor/stream-json.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/tokenizer/index.js.cjs +1 -1
- package/dist-lib/vendor/sucrase/dist/parser/tokenizer/types.js.cjs +1 -1
- package/dist-lib/vendor/sucrase.cjs +1 -1
- package/dist-lib/vendor/terser.cjs +1 -1
- package/dist-lib/vendor/valibot.cjs +1 -1
- package/dist-lib/vendor/ws.cjs +1 -1
- package/dist-lib/vendor/zod.cjs +1 -1
- package/dist-lib/worklet-executable.cjs +1 -1
- package/dist-lib/worklet-executable.mjs +3 -3
- package/kotlin/sdk.tar.gz +0 -0
- package/kotlin/toolchain.json +2 -2
- package/package.json +1 -1
- package/src/kotlin-wasm-proxy-source.ts +283 -0
- package/src/kotlin-wasm-runtime-module.ts +1 -0
- package/src/kotlin-wasm-runtime.ts +31 -0
- package/src/kotlinProxyWasmContract.ts +49 -0
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
export const kotlinWasmProxySource = String.raw`
|
|
2
|
+
const utf8 = new TextEncoder()
|
|
3
|
+
const wasmOptions = { builtins: ['js-string'], importedStringConstants: "'" }
|
|
4
|
+
|
|
5
|
+
function unsigned(value) {
|
|
6
|
+
const bytes = []
|
|
7
|
+
do {
|
|
8
|
+
const remaining = Math.floor(value / 128)
|
|
9
|
+
bytes.push((value % 128) | (remaining ? 128 : 0))
|
|
10
|
+
value = remaining
|
|
11
|
+
} while (value)
|
|
12
|
+
return bytes
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function signed(value) {
|
|
16
|
+
const bytes = []
|
|
17
|
+
for (let remaining = BigInt(value); ; ) {
|
|
18
|
+
const byte = Number(remaining & 127n)
|
|
19
|
+
remaining >>= 7n
|
|
20
|
+
if ((remaining === 0n && byte < 64) || (remaining === -1n && byte >= 64))
|
|
21
|
+
return [...bytes, byte]
|
|
22
|
+
bytes.push(byte | 128)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const vector = (values) => [unsigned(values.length), values].flat(2)
|
|
27
|
+
const name = (value) => vector([...utf8.encode(value)])
|
|
28
|
+
const ref = (type) => [0x64, ...signed(type)]
|
|
29
|
+
const nullable = (type) => [0x63, ...signed(type)]
|
|
30
|
+
const indexed = (op, index) => [op, ...unsigned(index)]
|
|
31
|
+
const gc = (op, ...indices) => [0xfb, op, ...indices.flatMap(unsigned)]
|
|
32
|
+
const local = (index) => indexed(0x20, index)
|
|
33
|
+
const global = (index) => indexed(0x23, index)
|
|
34
|
+
const func = (index) => indexed(0xd2, index)
|
|
35
|
+
const call = (index) => indexed(0x10, index)
|
|
36
|
+
const integer = (value) => [0x41, ...signed(value)]
|
|
37
|
+
const long = (value) => [0x42, ...signed(value)]
|
|
38
|
+
const end = 0x0b
|
|
39
|
+
|
|
40
|
+
function section(id, content) {
|
|
41
|
+
return [id, ...unsigned(content.length), ...content]
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function field(type, mutable = 0) {
|
|
45
|
+
return [...type, mutable]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function importGlobal(module, label, type) {
|
|
49
|
+
return [...name(module), ...name(label), 3, ...type, 0]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function importFunction(label, type) {
|
|
53
|
+
return [...name('core'), ...name(label), ...indexed(0, type)]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function defineGlobal(type, initializer) {
|
|
57
|
+
return [...ref(type), 0, ...initializer, end]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function body(locals, expression) {
|
|
61
|
+
const code = [...vector(locals), ...expression, end]
|
|
62
|
+
return [...unsigned(code.length), ...code]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function descriptorKind(type) {
|
|
66
|
+
switch (type) {
|
|
67
|
+
case 'J': return 'long'
|
|
68
|
+
case 'F': return 'float'
|
|
69
|
+
case 'D': return 'double'
|
|
70
|
+
case 'L':
|
|
71
|
+
case '[': return 'reference'
|
|
72
|
+
default: return 'int'
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function methodDescriptor(key) {
|
|
77
|
+
const begin = key.indexOf('(')
|
|
78
|
+
const close = key.indexOf(')', begin)
|
|
79
|
+
if (begin < 0 || close < 0 || close === key.length - 1)
|
|
80
|
+
throw new Error('invalid Kotlin proxy method descriptor ' + key)
|
|
81
|
+
const parameters = []
|
|
82
|
+
for (let index = begin + 1; index < close; ) {
|
|
83
|
+
const start = index
|
|
84
|
+
while (key[index] === '[') index++
|
|
85
|
+
index = key[index] === 'L' ? key.indexOf(';', index) + 1 : index + 1
|
|
86
|
+
if (index <= start || index > close)
|
|
87
|
+
throw new Error('invalid Kotlin proxy parameter descriptor ' + key)
|
|
88
|
+
parameters.push(key[start])
|
|
89
|
+
}
|
|
90
|
+
return { parameters, result: key[close + 1] }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function dispatchBody(slot, argumentsType, target, functions) {
|
|
94
|
+
const { parameters, result } = methodDescriptor(slot.key)
|
|
95
|
+
const hasArguments = parameters.length > 0
|
|
96
|
+
const argumentLocal = parameters.length + 1
|
|
97
|
+
const locals = hasArguments ? [[1, ...nullable(argumentsType)]] : []
|
|
98
|
+
const code = []
|
|
99
|
+
if (hasArguments) {
|
|
100
|
+
code.push(
|
|
101
|
+
...integer(parameters.length),
|
|
102
|
+
...call(functions['create-arguments']),
|
|
103
|
+
...indexed(0x21, argumentLocal),
|
|
104
|
+
)
|
|
105
|
+
for (const [index, descriptor] of parameters.entries()) {
|
|
106
|
+
const kind = descriptorKind(descriptor)
|
|
107
|
+
code.push(...local(argumentLocal), 0xd4, ...integer(index))
|
|
108
|
+
if (kind === 'int') code.push(...integer(descriptor.charCodeAt(0)))
|
|
109
|
+
code.push(...local(index + 1), ...call(functions['store-' + kind]))
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
code.push(
|
|
113
|
+
...local(0), ...gc(22), ...signed(target), ...global(2), ...integer(slot.methodIndex),
|
|
114
|
+
...(hasArguments ? local(argumentLocal) : [0xd0, ...signed(argumentsType)]),
|
|
115
|
+
...call(functions.invoke),
|
|
116
|
+
)
|
|
117
|
+
if (result === 'V') code.push(0x1a)
|
|
118
|
+
else {
|
|
119
|
+
const kind = descriptorKind(result)
|
|
120
|
+
if (kind === 'reference') {
|
|
121
|
+
if (!slot.result) throw new Error('missing Kotlin proxy return type for ' + slot.key)
|
|
122
|
+
code.push(...gc(slot.result.nullable ? 23 : 22), ...signed(slot.result.type))
|
|
123
|
+
} else {
|
|
124
|
+
if (kind === 'int') code.push(...integer(result.charCodeAt(0)))
|
|
125
|
+
code.push(...call(functions['return-' + kind]))
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return body(locals, code)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function createModule(layout, typeBytes, implemented, packageName, simpleName, typeId, keys) {
|
|
132
|
+
const { base, virtual, javaObject, functions } = layout
|
|
133
|
+
const entries = Object.entries(functions)
|
|
134
|
+
const functionIndices = Object.fromEntries(entries.map(([key], index) => [key, index]))
|
|
135
|
+
const loadName = entries.length
|
|
136
|
+
const getClass = loadName + 1
|
|
137
|
+
const target = layout.typeCount
|
|
138
|
+
const constructorType = target + 1
|
|
139
|
+
|
|
140
|
+
const methods = new Map()
|
|
141
|
+
function register(slot) {
|
|
142
|
+
const key = slot.signature + '/' + slot.key
|
|
143
|
+
const existing = methods.get(key)
|
|
144
|
+
if (existing) return existing
|
|
145
|
+
const methodIndex = keys.indexOf(slot.key)
|
|
146
|
+
if (methodIndex < 0) throw new Error('missing Kotlin proxy method ' + slot.key)
|
|
147
|
+
const method = { ...slot, methodIndex, functionIndex: getClass + 1 + methods.size }
|
|
148
|
+
methods.set(key, method)
|
|
149
|
+
return method
|
|
150
|
+
}
|
|
151
|
+
const virtualSlots = virtual.map((slot) => slot && register(slot))
|
|
152
|
+
const javaString = virtualSlots.at(-1)
|
|
153
|
+
const tables = [
|
|
154
|
+
{ ...javaObject, members: [func(getClass), func(functionIndices.copy), func(javaString.functionIndex)] },
|
|
155
|
+
...implemented.map((entry) => ({
|
|
156
|
+
...entry,
|
|
157
|
+
members: entry.slots.map((slot) => func(register(slot).functionIndex)),
|
|
158
|
+
})),
|
|
159
|
+
].map((entry) => ({ ...entry, id: BigInt(entry.id) }))
|
|
160
|
+
tables.sort((left, right) => left.id < right.id ? -1 : 1)
|
|
161
|
+
const dispatch = [...methods.values()]
|
|
162
|
+
const create = getClass + dispatch.length + 1
|
|
163
|
+
const qualifiedName = packageName ? packageName + '.' + simpleName : simpleName
|
|
164
|
+
|
|
165
|
+
const proxyType = [
|
|
166
|
+
...indexed(0x4f, 1), ...unsigned(base.proxy), 0x5f,
|
|
167
|
+
...vector([
|
|
168
|
+
field(ref(base.vtable)),
|
|
169
|
+
field(nullable(base.interfaceArray)),
|
|
170
|
+
field(ref(base.rtti)),
|
|
171
|
+
field([0x7f], 1),
|
|
172
|
+
field(nullable(base.any), 1),
|
|
173
|
+
]),
|
|
174
|
+
]
|
|
175
|
+
const createType = [0x60, ...vector([[0x6f]]), ...vector([ref(base.proxy)])]
|
|
176
|
+
const typeHeader = unsigned(layout.groupCount + 2)
|
|
177
|
+
const types = new Uint8Array(
|
|
178
|
+
typeHeader.length + typeBytes.length + proxyType.length + createType.length,
|
|
179
|
+
)
|
|
180
|
+
types.set(typeHeader)
|
|
181
|
+
types.set(typeBytes, typeHeader.length)
|
|
182
|
+
types.set(proxyType, typeHeader.length + typeBytes.length)
|
|
183
|
+
types.set(createType, types.length - createType.length)
|
|
184
|
+
const imports = vector([
|
|
185
|
+
importGlobal('core', 'base-vtable', ref(base.vtable)),
|
|
186
|
+
importGlobal('core', 'base-rtti', ref(base.rtti)),
|
|
187
|
+
importGlobal('proxy', 'table', [0x6f]),
|
|
188
|
+
importGlobal("'", packageName, ref(-17)),
|
|
189
|
+
importGlobal("'", simpleName, ref(-17)),
|
|
190
|
+
importGlobal("'", qualifiedName, ref(-17)),
|
|
191
|
+
...entries.map(([key, binding]) => importFunction(key, binding.type)),
|
|
192
|
+
])
|
|
193
|
+
const table = defineGlobal(base.interfaceArray, [
|
|
194
|
+
...tables.flatMap((entry) => [...entry.members.flat(), ...gc(0, entry.table)]),
|
|
195
|
+
...gc(8, base.interfaceArray, tables.length),
|
|
196
|
+
])
|
|
197
|
+
const rtti = defineGlobal(base.rtti, [
|
|
198
|
+
...tables.flatMap((entry) => long(entry.id)), ...gc(8, base.interfaceIds, tables.length),
|
|
199
|
+
...global(1), ...integer(0), ...integer(0), ...long(typeId), ...integer(0),
|
|
200
|
+
...func(loadName), ...func(loadName), ...global(3), ...global(4), ...gc(0, base.rtti),
|
|
201
|
+
])
|
|
202
|
+
const createBody = body([], [
|
|
203
|
+
...virtualSlots.flatMap((slot, index) =>
|
|
204
|
+
slot ? func(slot.functionIndex) : [...global(0), ...gc(2, base.vtable, index)]),
|
|
205
|
+
...gc(0, base.vtable), ...global(6), ...global(7),
|
|
206
|
+
...integer(0), 0xd0, ...signed(base.any), ...gc(0, target),
|
|
207
|
+
...local(0), ...gc(26), ...gc(22), ...signed(base.any),
|
|
208
|
+
...call(functionIndices['base-constructor']), 0xd4,
|
|
209
|
+
])
|
|
210
|
+
const sections = [
|
|
211
|
+
[0, 97, 115, 109, 1, 0, 0, 0],
|
|
212
|
+
[1, ...unsigned(types.length)],
|
|
213
|
+
types,
|
|
214
|
+
section(2, imports),
|
|
215
|
+
section(3, vector([
|
|
216
|
+
unsigned(base.loadName), unsigned(base.getClass),
|
|
217
|
+
...dispatch.map((slot) => unsigned(slot.signature)), unsigned(constructorType),
|
|
218
|
+
])),
|
|
219
|
+
section(6, vector([table, rtti])),
|
|
220
|
+
section(7, vector([[...name('create'), ...indexed(0, create)]])),
|
|
221
|
+
section(9, vector([[3, 0, ...vector([
|
|
222
|
+
getClass, functionIndices.copy, ...dispatch.map((slot) => slot.functionIndex),
|
|
223
|
+
].map(unsigned))]])),
|
|
224
|
+
section(10, vector([
|
|
225
|
+
body([], [...local(1), ...call(functionIndices['from-js-string'])]),
|
|
226
|
+
body([], [
|
|
227
|
+
...call(functionIndices['class-companion']), ...global(5),
|
|
228
|
+
...call(functionIndices['from-js-string']), ...call(functionIndices['canonical-class']),
|
|
229
|
+
]),
|
|
230
|
+
...dispatch.map((slot) => dispatchBody(slot, base.arguments, target, functionIndices)),
|
|
231
|
+
createBody,
|
|
232
|
+
])),
|
|
233
|
+
]
|
|
234
|
+
const output = new Uint8Array(sections.reduce((length, part) => length + part.length, 0))
|
|
235
|
+
let offset = 0
|
|
236
|
+
for (const part of sections) {
|
|
237
|
+
output.set(part, offset)
|
|
238
|
+
offset += part.length
|
|
239
|
+
}
|
|
240
|
+
return output
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function prepareKotlinProxyLinker(sdk, app) {
|
|
244
|
+
const layout = { ...sdk, groupCount: app.groupCount, typeCount: app.typeCount }
|
|
245
|
+
const interfaces = new Map([...sdk.interfaces, ...app.interfaces].map((entry) => [entry.name, entry]))
|
|
246
|
+
const decode = (base64) => Uint8Array.from(atob(base64), (character) => character.charCodeAt(0))
|
|
247
|
+
const symbolBindings = Object.entries({ ...layout.globals, ...layout.functions })
|
|
248
|
+
let typeBytes
|
|
249
|
+
return function createLinker(readExports) {
|
|
250
|
+
let core
|
|
251
|
+
let lastTypeId = 0n
|
|
252
|
+
return function generateProxy(name, interfaceNames, methodKeys, table) {
|
|
253
|
+
if (!typeBytes) {
|
|
254
|
+
const base = decode(sdk.bytes)
|
|
255
|
+
let skip = 0
|
|
256
|
+
while (base[skip++] & 128) {}
|
|
257
|
+
const appended = decode(app.bytes)
|
|
258
|
+
typeBytes = new Uint8Array(base.length - skip + appended.length)
|
|
259
|
+
typeBytes.set(base.subarray(skip))
|
|
260
|
+
typeBytes.set(appended, base.length - skip)
|
|
261
|
+
}
|
|
262
|
+
core ??= Object.fromEntries(symbolBindings.map(([label, { module, symbol }]) => {
|
|
263
|
+
const binding = readExports(module)?.[symbol]
|
|
264
|
+
if (!binding) throw new Error('missing Kotlin proxy export ' + label)
|
|
265
|
+
return [label, binding]
|
|
266
|
+
}))
|
|
267
|
+
const implemented = interfaceNames ? interfaceNames.split('\n').map((name) => {
|
|
268
|
+
const entry = interfaces.get(name)
|
|
269
|
+
if (!entry) throw new Error('Kotlin proxy interface layout is unavailable for ' + name)
|
|
270
|
+
return entry
|
|
271
|
+
}) : []
|
|
272
|
+
const dot = name.lastIndexOf('.')
|
|
273
|
+
const bytes = createModule(
|
|
274
|
+
layout, typeBytes, implemented,
|
|
275
|
+
dot < 0 ? '' : name.slice(0, dot), name.slice(dot + 1), --lastTypeId,
|
|
276
|
+
methodKeys.split('\n'),
|
|
277
|
+
)
|
|
278
|
+
const module = new WebAssembly.Module(bytes, wasmOptions)
|
|
279
|
+
return new WebAssembly.Instance(module, { core, proxy: { table } }).exports
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
`
|
|
@@ -19,6 +19,7 @@ export function prepareKotlinWasmModule(
|
|
|
19
19
|
.replaceAll(...kotlinNodeDetection)
|
|
20
20
|
.replaceAll(...kotlinCoroutineDispatcher)
|
|
21
21
|
.replaceAll(...kotlinJavaReferenceCallbacks)
|
|
22
|
+
.replaceAll('=> peachGenerateProxy(', '=> bindings.generateProxy(')
|
|
22
23
|
const { imports, wasm } = readKotlinWasmModule(
|
|
23
24
|
patched,
|
|
24
25
|
wasmUrl,
|
|
@@ -2,6 +2,7 @@ import {
|
|
|
2
2
|
kotlinSdkLoaderImport,
|
|
3
3
|
type KotlinWasmModule,
|
|
4
4
|
} from './kotlin-wasm-runtime-module'
|
|
5
|
+
import type { KotlinProxyWasmAppLayout } from './kotlinProxyWasmContract'
|
|
5
6
|
|
|
6
7
|
export function kotlinWasmRuntime(
|
|
7
8
|
entry: string,
|
|
@@ -13,12 +14,19 @@ export function kotlinWasmRuntime(
|
|
|
13
14
|
sdkLoaderUrl: string,
|
|
14
15
|
publishFactory: (module: KotlinWasmModule) => string,
|
|
15
16
|
readFunctionSlots?: (name: string) => ReadonlyMap<string, number> | undefined,
|
|
17
|
+
proxy?: {
|
|
18
|
+
url: string
|
|
19
|
+
app: KotlinProxyWasmAppLayout
|
|
20
|
+
modules: string[]
|
|
21
|
+
javaModule: string
|
|
22
|
+
},
|
|
16
23
|
): string {
|
|
17
24
|
const preparations: string[] = []
|
|
18
25
|
let entryFactory = ''
|
|
19
26
|
const loading = new Set<string>()
|
|
20
27
|
const factories: string[] = []
|
|
21
28
|
const files = new Map<string, number>()
|
|
29
|
+
const proxyImportObject = proxy?.javaModule.replace(/\.mjs$/, '.import-object.mjs')
|
|
22
30
|
|
|
23
31
|
function visit(name: string): number {
|
|
24
32
|
const existing = files.get(name)
|
|
@@ -43,6 +51,7 @@ export function kotlinWasmRuntime(
|
|
|
43
51
|
: `compileKotlinSdkModule(${resolved}, ${wasm.options})`
|
|
44
52
|
members.push(`module: modules[${index}][1]`)
|
|
45
53
|
}
|
|
54
|
+
if (name === proxyImportObject) members.push('generateProxy')
|
|
46
55
|
if (functionSlots)
|
|
47
56
|
members.push(`functionSlots: ${JSON.stringify([...functionSlots])}`)
|
|
48
57
|
let instantiate = 'instantiate'
|
|
@@ -60,9 +69,26 @@ export function kotlinWasmRuntime(
|
|
|
60
69
|
}
|
|
61
70
|
|
|
62
71
|
const root = visit(entry)
|
|
72
|
+
const usesProxy = proxy && proxyImportObject && files.has(proxyImportObject)
|
|
73
|
+
const proxyProviderIndices =
|
|
74
|
+
usesProxy &&
|
|
75
|
+
Object.fromEntries(
|
|
76
|
+
proxy.modules.map((module) => {
|
|
77
|
+
const index = files.get(module)
|
|
78
|
+
if (index === undefined) throw new Error(`missing Kotlin proxy module ${module}`)
|
|
79
|
+
return [module, index]
|
|
80
|
+
}),
|
|
81
|
+
)
|
|
63
82
|
return [
|
|
64
83
|
kotlinSdkLoaderImport(sdkLoaderUrl),
|
|
65
84
|
entryFactory,
|
|
85
|
+
...(usesProxy
|
|
86
|
+
? [
|
|
87
|
+
`import { prepareAppProxyLinker } from ${JSON.stringify(proxy.url)};`,
|
|
88
|
+
`const createProxyLinker = prepareAppProxyLinker(${JSON.stringify(proxy.app)});`,
|
|
89
|
+
`const proxyProviderIndices = ${JSON.stringify(proxyProviderIndices)};`,
|
|
90
|
+
]
|
|
91
|
+
: []),
|
|
66
92
|
'let pending;',
|
|
67
93
|
'export async function createKotlinInstance() {',
|
|
68
94
|
' pending ??= Promise.all([',
|
|
@@ -70,6 +96,11 @@ export function kotlinWasmRuntime(
|
|
|
70
96
|
' ]).catch(error => { pending = undefined; throw error; });',
|
|
71
97
|
' const modules = await pending;',
|
|
72
98
|
' const instances = [];',
|
|
99
|
+
...(usesProxy
|
|
100
|
+
? [
|
|
101
|
+
' const generateProxy = createProxyLinker(name => instances[proxyProviderIndices[name]]?.__ALL_EXPORTS);',
|
|
102
|
+
]
|
|
103
|
+
: []),
|
|
73
104
|
' const dispose = () => {',
|
|
74
105
|
' for (const instance of instances.splice(0).reverse()) instance.peachDisposeInstance();',
|
|
75
106
|
' };',
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export const KOTLIN_PROXY_SDK_LAYOUT = 'sdk.proxy-layout.json'
|
|
2
|
+
export const KOTLIN_PROXY_APP_LAYOUT = 'app.proxy-layout.json'
|
|
3
|
+
|
|
4
|
+
export type KotlinProxyMethodSlot = {
|
|
5
|
+
key: string
|
|
6
|
+
signature: number
|
|
7
|
+
result?: { type: number; nullable: boolean }
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export type KotlinProxyInterface = {
|
|
11
|
+
name: string
|
|
12
|
+
id: string
|
|
13
|
+
table: number
|
|
14
|
+
slots: KotlinProxyMethodSlot[]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type KotlinProxyWasmBinding = {
|
|
18
|
+
module: string
|
|
19
|
+
symbol: string
|
|
20
|
+
type: number
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type KotlinProxyTypeCatalog = {
|
|
24
|
+
bytes: string
|
|
25
|
+
interfaces: KotlinProxyInterface[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type KotlinProxyWasmSdkLayout = KotlinProxyTypeCatalog & {
|
|
29
|
+
base: {
|
|
30
|
+
any: number
|
|
31
|
+
proxy: number
|
|
32
|
+
vtable: number
|
|
33
|
+
interfaceArray: number
|
|
34
|
+
rtti: number
|
|
35
|
+
interfaceIds: number
|
|
36
|
+
loadName: number
|
|
37
|
+
getClass: number
|
|
38
|
+
arguments: number
|
|
39
|
+
}
|
|
40
|
+
virtual: (KotlinProxyMethodSlot | null)[]
|
|
41
|
+
javaObject: Pick<KotlinProxyInterface, 'id' | 'table'>
|
|
42
|
+
functions: Record<string, KotlinProxyWasmBinding>
|
|
43
|
+
globals: Record<string, Pick<KotlinProxyWasmBinding, 'module' | 'symbol'>>
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export type KotlinProxyWasmAppLayout = KotlinProxyTypeCatalog & {
|
|
47
|
+
groupCount: number
|
|
48
|
+
typeCount: number
|
|
49
|
+
}
|