@@ -6,6 +6,48 @@ var https = require('https');
6
6
var MemoryStream = require ( 'memorystream' ) ;
7
7
8
8
function setupMethods ( soljson ) {
9
+ var copyString = function ( str , ptr ) {
10
+ var length = soljson . lengthBytesUTF8 ( str ) ;
11
+ var buffer = soljson . _malloc ( length + 1 ) ;
12
+ soljson . stringToUTF8 ( str , buffer , length + 1 ) ;
13
+ soljson . setValue ( ptr , buffer , '*' ) ;
14
+ } ;
15
+
16
+ var wrapCallback = function ( callback ) {
17
+ assert ( typeof callback === 'function' , 'Invalid callback specified.' ) ;
18
+ return function ( path , contents , error ) {
19
+ var result = callback ( soljson . Pointer_stringify ( path ) ) ;
20
+ if ( typeof result . contents === 'string' ) {
21
+ copyString ( result . contents , contents ) ;
22
+ }
23
+ if ( typeof result . error === 'string' ) {
24
+ copyString ( result . error , error ) ;
25
+ }
26
+ } ;
27
+ } ;
28
+
29
+ // This calls compile() with args || cb
30
+ var runWithReadCallback = function ( readCallback , compile , args ) {
31
+ if ( readCallback === undefined ) {
32
+ readCallback = function ( path ) {
33
+ return {
34
+ error : 'File import callback not supported'
35
+ } ;
36
+ } ;
37
+ }
38
+ var cb = soljson . Runtime . addFunction ( wrapCallback ( readCallback ) ) ;
39
+ var output ;
40
+ try {
41
+ args . push ( cb ) ;
42
+ output = compile . apply ( undefined , args ) ;
43
+ } catch ( e ) {
44
+ soljson . Runtime . removeFunction ( cb ) ;
45
+ throw e ;
46
+ }
47
+ soljson . Runtime . removeFunction ( cb ) ;
48
+ return output ;
49
+ } ;
50
+
9
51
var compileJSON = soljson . cwrap ( 'compileJSON' , 'string' , [ 'string' , 'number' ] ) ;
10
52
var compileJSONMulti = null ;
11
53
if ( '_compileJSONMulti' in soljson ) {
@@ -14,47 +56,6 @@ function setupMethods (soljson) {
14
56
var compileJSONCallback = null ;
15
57
var compileStandard = null ;
16
58
if ( ( '_compileJSONCallback' in soljson ) || ( '_compileStandard' in soljson ) || ( '_solidity_compile' in soljson ) ) {
17
- var copyString = function ( str , ptr ) {
18
- var length = soljson . lengthBytesUTF8 ( str ) ;
19
- var buffer = soljson . _malloc ( length + 1 ) ;
20
- soljson . stringToUTF8 ( str , buffer , length + 1 ) ;
21
- soljson . setValue ( ptr , buffer , '*' ) ;
22
- } ;
23
- var wrapCallback = function ( callback ) {
24
- assert ( typeof callback === 'function' , 'Invalid callback specified.' ) ;
25
- return function ( path , contents , error ) {
26
- var result = callback ( soljson . Pointer_stringify ( path ) ) ;
27
- if ( typeof result . contents === 'string' ) {
28
- copyString ( result . contents , contents ) ;
29
- }
30
- if ( typeof result . error === 'string' ) {
31
- copyString ( result . error , error ) ;
32
- }
33
- } ;
34
- } ;
35
-
36
- // This calls compile() with args || cb
37
- var runWithReadCallback = function ( readCallback , compile , args ) {
38
- if ( readCallback === undefined ) {
39
- readCallback = function ( path ) {
40
- return {
41
- error : 'File import callback not supported'
42
- } ;
43
- } ;
44
- }
45
- var cb = soljson . Runtime . addFunction ( wrapCallback ( readCallback ) ) ;
46
- var output ;
47
- try {
48
- args . push ( cb ) ;
49
- output = compile . apply ( undefined , args ) ;
50
- } catch ( e ) {
51
- soljson . Runtime . removeFunction ( cb ) ;
52
- throw e ;
53
- }
54
- soljson . Runtime . removeFunction ( cb ) ;
55
- return output ;
56
- } ;
57
-
58
59
var compileInternal = soljson . cwrap ( 'compileJSONCallback' , 'string' , [ 'string' , 'number' , 'number' ] ) ;
59
60
compileJSONCallback = function ( input , optimize , readCallback ) {
60
61
return runWithReadCallback ( readCallback , compileInternal , [ input , optimize ] ) ;
0 commit comments