-
Notifications
You must be signed in to change notification settings - Fork 1
/
bindings.ts
273 lines (234 loc) Β· 7.11 KB
/
bindings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* Original source code: https://github.com/ethereum/solc-js/blob/master/bindings/core.ts
*/
import { isNil } from './common.ts'
import type { Alloc, License, Reset, SolJson, Version } from './deps.ts'
import type { Callbacks, CoreBindings } from './types.ts'
function bindSolcMethod(
solJson: SolJson,
method: string,
returnType: string | null,
args: string[],
defaultValue?: ((...args: unknown[]) => void) | null,
) {
if (isNil((solJson as SolJson & { [method: string]: unknown })[`_${method}`]) && defaultValue !== undefined) {
return defaultValue
}
return solJson.cwrap(method, returnType, args) as (...args: unknown[]) => void
}
function bindSolcMethodWithFallbackFunc(
solJson: SolJson,
method: string,
returnType: string | null,
args: string[],
fallbackMethod: string,
finalFallback: (() => void) | undefined = undefined,
) {
const methodFunc = bindSolcMethod(solJson, method, returnType, args, null)
if (!isNil(methodFunc)) {
return methodFunc
}
return bindSolcMethod(solJson, fallbackMethod, returnType, args, finalFallback)
}
function bindAlloc(solJson: SolJson) {
const allocBinding = bindSolcMethod(
solJson,
'solidity_alloc',
'number',
['number'],
null,
)
// the fallback malloc is not a cwrap function and should just be returned
// directly in-case the alloc binding could not happen.
if (isNil(allocBinding)) {
return solJson._malloc
}
return allocBinding
}
const bindVersion = (solJson: SolJson) =>
bindSolcMethodWithFallbackFunc(
solJson,
'solidity_version',
'string',
[],
'version',
)
function bindLicense(solJson: SolJson) {
return bindSolcMethodWithFallbackFunc(
solJson,
'solidity_license',
'string',
[],
'license',
() => {
},
)
}
function bindReset(solJson: SolJson) {
return bindSolcMethod(
solJson,
'solidity_reset',
null,
[],
null,
)
}
function unboundCopyToCString(solJson: SolJson, alloc: Alloc, str: string, ptr: number) {
const length = solJson.lengthBytesUTF8(str)
const buffer = alloc(length + 1)
solJson.stringToUTF8(str, buffer, length + 1)
solJson.setValue(ptr, buffer, '*')
}
function unboundCopyFromCString(solJson: SolJson, ptr: number) {
const copyFromCString = solJson.UTF8ToString
return copyFromCString(ptr)
}
function unboundAddFunction(solJson: SolJson, func: (...args: unknown[]) => unknown, signature?: string) {
return (solJson.addFunction || solJson.Runtime.addFunction)(func, signature)
}
function unboundRemoveFunction(solJson: SolJson, ptr: number) {
return (solJson.removeFunction || solJson.Runtime.removeFunction)(ptr)
}
const setupCore = (solJson: SolJson) => {
const core = {
alloc: bindAlloc(solJson) as Alloc,
license: bindLicense(solJson) as License,
version: bindVersion(solJson) as Version,
reset: bindReset(solJson) as Reset,
}
const helpers = {
addFunction: unboundAddFunction.bind(this, solJson),
removeFunction: unboundRemoveFunction.bind(this, solJson),
copyFromCString: unboundCopyFromCString.bind(this, solJson),
copyToCString: unboundCopyToCString.bind(this, solJson, core.alloc) as unknown as (
str: string,
ptr: number,
) => string,
}
return {
...core,
...helpers,
}
}
const bindCompileJson = (solJson: SolJson) =>
bindSolcMethod(
solJson,
'compileJSON',
'string',
['string', 'number'],
null,
)
function wrapCallbackWithKind(
coreBindings: CoreBindings,
callback: (arg1: string, arg2: string) => { contents: string; error: string },
) {
if (typeof callback !== 'function') throw new Error('Invalid callback specified.')
return function (context: number, kind: number, data: number, contents: number, error: number): void {
// Must be a null pointer.
if (context !== 0) throw new Error('Callback context must be null.')
const result = callback(coreBindings.copyFromCString(kind), coreBindings.copyFromCString(data))
if (typeof result.contents === 'string') {
coreBindings.copyToCString(result.contents, contents)
}
if (typeof result.error === 'string') {
coreBindings.copyToCString(result.error, error)
}
}
}
// calls compile() with args || cb
function runWithCallbacks(
coreBindings: CoreBindings,
callbacks: Callbacks = {},
compile: (...args: unknown[]) => void,
args: (number | null)[],
) {
let readCallback = callbacks.import
if (readCallback === undefined) {
readCallback = function () {
return {
error: 'File import callback not supported',
}
}
}
const singleCallback = wrapCallbackWithKind(coreBindings, (kind: string, data: string) => {
if (kind === 'source') {
return readCallback!(data)!
} else if (kind === 'smt-query') {
return smtSolverCallback!(data)!
} else {
throw new Error('Invalid callback kind specified.')
}
})
// After 0.6.x multiple kind of callbacks are supported.
let smtSolverCallback = callbacks.smtSolver
if (smtSolverCallback === undefined) {
smtSolverCallback = function () {
return {
error: 'SMT solver callback not supported',
}
}
}
const cb = coreBindings.addFunction(singleCallback, 'viiiii')
let output
try {
args.push(cb)
args.push(null)
output = compile(...args)
} finally {
coreBindings.removeFunction(cb)
}
if (coreBindings.reset) {
// Explicitly free memory.
//
// NOTE: cwrap() of "compile" will copy the returned pointer into a
// Javascript string and it is not possible to call free() on it.
// reset() however will clear up all allocations.
coreBindings.reset()
}
return output
}
function bindCompileStandard(solJson: SolJson, coreBindings: CoreBindings) {
let boundFunctionStandard: ((input: number, readCallback: Callbacks) => void) | null = null
let boundFunctionSolidity: ((...args: unknown[]) => void) | null = null
// input (jsontext), callback (ptr) -> output (jsontext)
const compileInternal = bindSolcMethod(
solJson,
'compileStandard',
'string',
['string', 'number'],
null,
)
// input (jsontext), callback (ptr), callback_context (ptr) -> output (jsontext)
boundFunctionSolidity = bindSolcMethod(
solJson,
'solidity_compile',
'string',
['string', 'number', 'number'],
null,
)
if (!isNil(compileInternal)) {
boundFunctionStandard = function (input: number, readCallback: Callbacks) {
return runWithCallbacks(coreBindings, readCallback, compileInternal!, [input])
}
}
if (!isNil(boundFunctionSolidity)) {
boundFunctionStandard = function (input: number, callbacks: Callbacks) {
return runWithCallbacks(coreBindings, callbacks, boundFunctionSolidity as ((...args: unknown[]) => void), [input])
}
}
return boundFunctionStandard
}
function setupCompile(
solJson: SolJson,
core: CoreBindings,
) {
return {
compileJson: bindCompileJson(solJson),
compileStandard: bindCompileStandard(solJson, core),
}
}
export const setupBindings = (soljson: SolJson) => {
const coreBindings = setupCore(soljson)
const compileBindings = setupCompile(soljson, coreBindings)
return { coreBindings, compileBindings }
}