initial commit (#13)

This commit is contained in:
vms 2019-08-10 14:52:25 +03:00 committed by Dima
parent 728b78d713
commit 4c65740d03
3 changed files with 24 additions and 34 deletions

View File

@ -21,7 +21,7 @@ buildscript {
allprojects { allprojects {
apply plugin: 'java' apply plugin: 'java'
group 'com.github.cretz.asmble' group 'com.github.cretz.asmble'
version '0.4.10-fl' version '0.4.11-fl'
// skips building and running for the specified examples // skips building and running for the specified examples
ext.skipExamples = ['c-simple', 'go-simple', 'rust-regex'] ext.skipExamples = ['c-simple', 'go-simple', 'rust-regex']

View File

@ -86,23 +86,12 @@ abstract class ScriptCommand<T> : Command<T>() {
} }
} }
val memBuilder = args.memoryBuilder
// throws ArithmeticException if the result overflows an int
val capacity = Math.multiplyExact(args.defaultMaxMemPages, Mem.PAGE_SIZE)
// Do registrations // Do registrations
context = args.registrations.fold(context) { ctx, (moduleName, className) -> context = args.registrations.fold(context) { ctx, (moduleName, className) ->
if (memBuilder != null) { ctx.withModuleRegistered(moduleName,
ctx.withModuleRegistered(moduleName, Module.Native(Class.forName(className, true, ctx.classLoader).newInstance()))
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()))
}
} }
if (args.specTestRegister) context = context.withHarnessRegistered() if (args.specTestRegister) context = context.withHarnessRegistered()
if (args.enableLogger) { if (args.enableLogger) {

View File

@ -1,33 +1,39 @@
package asmble.run.jvm 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. * Module used for gas and EIC metering.
*/ */
open class EnvModule(private val gasLimit: Long) { open class EnvModule(private val gasLimit: Long) {
private var overallSpentGas: Long = 0 private var state = EnvState();
// executed instruction counter
private var EIC: Long = 0
/** /**
* [Wasm function] * [Wasm function]
* Adds spent gas to overall spent gas and checks limit exceeding. * Adds spent gas to overall spent gas and checks limit exceeding.
*/ */
fun gas(spentGas: Int) { fun gas(spentGas: Int) {
if(overallSpentGas + spentGas > gasLimit) { if(state.spentGas + spentGas > gasLimit) {
// TODO : check for overflow, throw an exception // TODO : check for overflow, throw an exception
} }
overallSpentGas += spentGas; state.spentGas += spentGas;
} }
/** /**
* [Wasm function] * [Wasm function]
* Adds EIC to overall executed instruction counter. * Adds EIC to overall executed instruction counter.
*/ */
fun eic(currentEIC: Int) { fun eic(EIC: Int) {
EIC += currentEIC; 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. * It should be impossible to call this function from a Wasm module.
*/ */
fun clearState() { fun clearState() {
overallSpentGas = 0; state.spentGas = 0;
EIC = 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 { fun getState(): EnvState {
return overallSpentGas; return state;
} }
/**
* Returns current EIC. Used from WasmVm to determine count of executed instruction after each invocation.
*/
fun getEIC(): Long {
return EIC;
}
} }