From 4c65740d03b7a0723d0d4286bf2fa46c461171a0 Mon Sep 17 00:00:00 2001 From: vms Date: Sat, 10 Aug 2019 14:52:25 +0300 Subject: [PATCH] initial commit (#13) --- build.gradle | 2 +- .../main/kotlin/asmble/cli/ScriptCommand.kt | 17 ++------ .../main/kotlin/asmble/run/jvm/EnvModule.kt | 39 ++++++++++--------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/build.gradle b/build.gradle index 56b8ccc..afcf03f 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ buildscript { allprojects { apply plugin: 'java' group 'com.github.cretz.asmble' - version '0.4.10-fl' + version '0.4.11-fl' // skips building and running for the specified examples ext.skipExamples = ['c-simple', 'go-simple', 'rust-regex'] diff --git a/compiler/src/main/kotlin/asmble/cli/ScriptCommand.kt b/compiler/src/main/kotlin/asmble/cli/ScriptCommand.kt index e3b41cb..64cad63 100644 --- a/compiler/src/main/kotlin/asmble/cli/ScriptCommand.kt +++ b/compiler/src/main/kotlin/asmble/cli/ScriptCommand.kt @@ -86,23 +86,12 @@ abstract class ScriptCommand : Command() { } } - val memBuilder = args.memoryBuilder - - // throws ArithmeticException if the result overflows an int - val capacity = Math.multiplyExact(args.defaultMaxMemPages, Mem.PAGE_SIZE) - // Do registrations context = args.registrations.fold(context) { ctx, (moduleName, className) -> - if (memBuilder != null) { - ctx.withModuleRegistered(moduleName, - Module.Native(Class.forName(className, true, ctx.classLoader) - .getConstructor(MemoryBuffer::class.java) - .newInstance(memBuilder.build(capacity)))) - } else { - ctx.withModuleRegistered(moduleName, - Module.Native(Class.forName(className, true, ctx.classLoader).newInstance())) - } + ctx.withModuleRegistered(moduleName, + Module.Native(Class.forName(className, true, ctx.classLoader).newInstance())) } + if (args.specTestRegister) context = context.withHarnessRegistered() if (args.enableLogger) { diff --git a/compiler/src/main/kotlin/asmble/run/jvm/EnvModule.kt b/compiler/src/main/kotlin/asmble/run/jvm/EnvModule.kt index 2d10a13..8987f9c 100644 --- a/compiler/src/main/kotlin/asmble/run/jvm/EnvModule.kt +++ b/compiler/src/main/kotlin/asmble/run/jvm/EnvModule.kt @@ -1,33 +1,39 @@ package asmble.run.jvm +/** + * Used to tack the state of the environment module. + */ +data class EnvState( + var spentGas: Long = 0, + // executed instruction counter + var EIC: Long = 0 +) + /** * Module used for gas and EIC metering. */ open class EnvModule(private val gasLimit: Long) { - private var overallSpentGas: Long = 0 - - // executed instruction counter - private var EIC: Long = 0 + private var state = EnvState(); /** * [Wasm function] * Adds spent gas to overall spent gas and checks limit exceeding. */ fun gas(spentGas: Int) { - if(overallSpentGas + spentGas > gasLimit) { + if(state.spentGas + spentGas > gasLimit) { // TODO : check for overflow, throw an exception } - overallSpentGas += spentGas; + state.spentGas += spentGas; } /** * [Wasm function] * Adds EIC to overall executed instruction counter. */ - fun eic(currentEIC: Int) { - EIC += currentEIC; + fun eic(EIC: Int) { + state.EIC += EIC; } /** @@ -35,21 +41,16 @@ open class EnvModule(private val gasLimit: Long) { * It should be impossible to call this function from a Wasm module. */ fun clearState() { - overallSpentGas = 0; - EIC = 0; + state.spentGas = 0; + state.EIC = 0; } /** - * Returns spent gas. Used from WasmVm to determine spent gas after each invocation. + * Returns environment module state. + * Used from WasmVm to determine spent gas and executed instruction counter after each invocation. */ - fun getSpentGas(): Long { - return overallSpentGas; + fun getState(): EnvState { + return state; } - /** - * Returns current EIC. Used from WasmVm to determine count of executed instruction after each invocation. - */ - fun getEIC(): Long { - return EIC; - } }