Skip to content
This repository was archived by the owner on Jan 5, 2019. It is now read-only.

Commit 4a75216

Browse files
committed
Clean up logic to make code more readable
1 parent 4052d20 commit 4a75216

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

wasm/generateInterface.js

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function generateManifest (interfaceManifest, opts) {
248248
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8})))
249249
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 2})))
250250
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3}))))`
251-
} else if (input === 'i64' && opcode !== 'CALL') {
251+
} else if (input === 'i64') {
252252
call += `(call $check_overflow_i64
253253
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32})))
254254
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8})))
@@ -263,11 +263,7 @@ function generateManifest (interfaceManifest, opts) {
263263
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 2})))
264264
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3})))))`
265265
call += `(get_local $offset${numOfLocals})`
266-
} else if (input === 'length' && (opcode === 'CALL' || opcode === 'CALLCODE')) {
267-
// CALLs in EVM have 7 arguments
268-
// but in ewasm CALLs only have 5 arguments
269-
// so delete the bottom two stack elements, after processing the 5th argument
270-
266+
} else if (input === 'length') {
271267
locals += `(local $length${numOfLocals} i32)`
272268
body += `(set_local $length${numOfLocals}
273269
(call $check_overflow
@@ -282,25 +278,13 @@ function generateManifest (interfaceManifest, opts) {
282278
call += `(get_local $length${numOfLocals})`
283279
numOfLocals++
284280

285-
// delete 6th stack element
286-
spOffset--
287-
288-
// delete 7th stack element
289-
spOffset--
290-
} else if (input === 'length' && (opcode !== 'CALL' && opcode !== 'CALLCODE')) {
291-
locals += `(local $length${numOfLocals} i32)`
292-
body += `(set_local $length${numOfLocals}
293-
(call $check_overflow
294-
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32})))
295-
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8})))
296-
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 2})))
297-
(i64.load (i32.add (get_global $sp) (i32.const ${spOffset * 32 + 8 * 3})))))
298-
299-
(call $memusegas (get_local $offset${numOfLocals}) (get_local $length${numOfLocals}))
300-
(set_local $offset${numOfLocals} (i32.add (get_global $memstart) (get_local $offset${numOfLocals})))`
301-
302-
call += `(get_local $length${numOfLocals})`
303-
numOfLocals++
281+
// CALLs in EVM have 7 arguments
282+
// but in ewasm CALLs only have 5 arguments
283+
// so delete the bottom two stack elements, after processing the 5th argument
284+
if (opcode === 'CALL' || opcode === 'CALLCODE') {
285+
spOffset--
286+
spOffset--
287+
}
304288
}
305289
spOffset--
306290
})
@@ -350,20 +334,16 @@ function generateManifest (interfaceManifest, opts) {
350334
call += '(get_local $callback)'
351335
}
352336

337+
let wrapper
353338
if (opcode === 'CALL' || opcode === 'CALLCODE' || opcode === 'DELEGATECALL') {
354-
call =
355-
`(i64.store
356-
(i32.add (get_global $sp) (i32.const ${spOffset * 32}))
357-
(i64.extend_u/i32
358-
(i32.eqz ${call}) ;; flip CALL result from EEI to EVM convention (0 -> 1, 1,2,.. -> 1)
359-
)))`
339+
// flip CALL result from EEI to EVM convention (0 -> 1, 1,2,.. -> 0)
340+
wrapper = `(i32.eqz ${call})`
360341
} else {
361-
call =
362-
`(i64.store
363-
(i32.add (get_global $sp) (i32.const ${spOffset * 32}))
364-
(i64.extend_u/i32
365-
${call})))`
342+
wrapper = call
366343
}
344+
call = `(i64.store
345+
(i32.add (get_global $sp) (i32.const ${spOffset * 32}))
346+
(i64.extend_u/i32 ${wrapper})))`
367347

368348
call += `
369349
;; zero out mem

0 commit comments

Comments
 (0)