• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

deno-web3 / solc / 13575745341

27 Feb 2025 08:59PM UTC coverage: 76.21% (+4.4%) from 71.786%
13575745341

Pull #3

github

web-flow
Merge ba97409cf into 3d76b77ea
Pull Request #3: Update with upstream

4 of 8 branches covered (50.0%)

Branch coverage included in aggregate %.

159 of 214 new or added lines in 7 files covered. (74.3%)

185 of 240 relevant lines covered (77.08%)

2.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.36
/bindings/compile.ts
1
import assert from 'node:assert'
1✔
2

3
import { bindSolcMethod } from './helpers.ts'
1✔
4
import type { Callbacks, CompileBindings, CompileJsonStandard, CoreBindings, ReadCallback, SolJson } from 'solc/types'
5

6
export function setupCompile(solJson: SolJson, core: CoreBindings): CompileBindings {
1✔
7
  return {
2✔
8
    compileStandard: bindCompileStandard(solJson, core),
2✔
9
  }
2✔
10
}
2✔
11

12
/**********************
13
 * COMPILE
14
 **********************/
15

16
/**
17
 * Returns a binding to the solidity solidity_compile method.
18
 * input (jsontext), callback (optional >= v6 only - ptr) -> output (jsontext)
19
 *
20
 * @param solJson The Emscripten compiled Solidity object.
21
 * @param coreBindings The core bound Solidity methods.
22
 */
23
function bindCompileStandard(solJson: SolJson, coreBindings: CoreBindings): CompileJsonStandard {
1✔
24
  // input (jsontext), callback (ptr), callback_context (ptr) -> output (jsontext)
25
  const boundFunctionSolidity: (jsontext: string, ptr?: number) => string = bindSolcMethod(
2✔
26
    solJson,
2✔
27
    'solidity_compile',
2✔
28
    'string',
2✔
29
    ['string', 'number', 'number'],
10✔
30
    null,
2✔
31
  )
32

33
  const boundFunctionStandard = function (input: string, callbacks: Callbacks) {
2✔
34
    return runWithCallbacks(solJson, coreBindings, callbacks, boundFunctionSolidity, [input])
9✔
35
  }
2✔
36

37
  return boundFunctionStandard as unknown as CompileJsonStandard
2✔
38
}
2✔
39

40
/**********************
41
 * CALL BACKS
42
 **********************/
43

44
function wrapCallbackWithKind<Arg extends string>(
1✔
45
  coreBindings: CoreBindings,
1✔
46
  callback: (...args: Arg[]) => Partial<{ contents: unknown; error: unknown }>,
1✔
47
) {
48
  assert(typeof callback === 'function', 'Invalid callback specified.')
2✔
49

NEW
50
  return function (context: 0, kind: number, data: number, contents: number, error: number) {
×
51
    // Must be a null pointer.
NEW
52
    assert(context === 0, 'Callback context must be null.')
×
NEW
53
    console.log({ kind, data })
×
NEW
54
    const result = callback(coreBindings.copyFromCString(kind) as Arg, coreBindings.copyFromCString(data) as Arg)
×
NEW
55
    if (typeof result.contents === 'string') {
×
NEW
56
      coreBindings.copyToCString(result.contents, contents)
×
NEW
57
    }
×
NEW
58
    if (typeof result.error === 'string') {
×
NEW
59
      coreBindings.copyToCString(result.error, error)
×
NEW
60
    }
×
NEW
61
  }
×
62
}
2✔
63

64
// calls compile() with args || cb
65
function runWithCallbacks<Args extends unknown[]>(
2✔
66
  _solJson: SolJson,
2✔
67
  coreBindings: CoreBindings,
2✔
68
  callbacks?: Callbacks | ReadCallback,
2✔
69
  compile?: (...args: Args) => void,
2✔
70
  args: Args = [] as unknown as Args,
2✔
71
) {
NEW
72
  if (callbacks) {
×
NEW
73
    assert(typeof callbacks === 'object', 'Invalid callback object specified.')
×
NEW
74
  } else {
×
75
    callbacks = {}
2✔
76
  }
2✔
77

78
  let readCallback = callbacks.import
2✔
79
  if (readCallback === undefined) {
2✔
NEW
80
    readCallback = function () {
×
NEW
81
      return {
×
NEW
82
        error: 'File import callback not supported',
×
NEW
83
      }
×
NEW
84
    }
×
85
  }
2✔
86

87
  let singleCallback
2✔
88
  // After 0.6.x multiple kind of callbacks are supported.
89
  let smtSolverCallback = callbacks.smtSolver
2✔
90
  if (smtSolverCallback === undefined) {
2✔
NEW
91
    smtSolverCallback = function () {
×
NEW
92
      return {
×
NEW
93
        error: 'SMT solver callback not supported',
×
NEW
94
      }
×
NEW
95
    }
×
96
  }
2✔
97

NEW
98
  singleCallback = function (kind: 'source' | 'smt-query', data: string) {
×
NEW
99
    if (kind === 'source') {
×
NEW
100
      return readCallback(data)
×
NEW
101
    } else if (kind === 'smt-query') {
×
NEW
102
      return smtSolverCallback(data)
×
NEW
103
    } else {
×
NEW
104
      assert(false, 'Invalid callback kind specified.')
×
NEW
105
    }
×
NEW
106
  }
×
107

108
  singleCallback = wrapCallbackWithKind<'source' | 'smt-query'>(coreBindings, singleCallback)
2✔
109

110
  const cb = coreBindings.addFunction(singleCallback, 'viiiii')
2✔
111
  let output
2✔
112
  try {
2✔
113
    args.push(cb)
2✔
114

115
    // Callback context.
116
    args.push(null)
2✔
117

118
    output = compile?.(...args)
2✔
119
  } finally {
2✔
120
    coreBindings.removeFunction(cb)
2✔
121
  }
2✔
122

123
  if (coreBindings.reset) {
2✔
124
    // Explicitly free memory.
125
    //
126
    // NOTE: cwrap() of "compile" will copy the returned pointer into a
127
    //       Javascript string and it is not possible to call free() on it.
128
    //       reset() however will clear up all allocations.
129
    coreBindings.reset()
2✔
130
  }
2✔
131
  return output
2✔
132
}
2✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc