Add new modules to standard library

This commit is contained in:
2020-03-13 17:28:02 +01:00
parent 264100eef1
commit 5680ed4e42
17 changed files with 321 additions and 4 deletions

View File

@@ -0,0 +1,10 @@
package io.smnp.ext
import io.smnp.ext.function.ExitFunction
import io.smnp.ext.function.SleepFunction
import org.pf4j.Extension
@Extension
class SystemModule : NativeModuleProvider("smnp.system") {
override fun functions() = listOf(ExitFunction(), SleepFunction())
}

View File

@@ -0,0 +1,19 @@
package io.smnp.ext.function
import io.smnp.callable.function.Function
import io.smnp.callable.function.FunctionDefinitionTool
import io.smnp.callable.signature.Signature.Companion.simple
import io.smnp.type.enumeration.DataType.INT
import io.smnp.type.matcher.Matcher.Companion.ofType
import io.smnp.type.matcher.Matcher.Companion.optional
import io.smnp.type.model.Value
import kotlin.system.exitProcess
class ExitFunction : Function("exit") {
override fun define(new: FunctionDefinitionTool) {
new function simple(optional(ofType(INT))) body { _, arguments ->
val exitCode = arguments.getOrNull(0) ?: Value.int(0)
exitProcess(exitCode.value!! as Int)
}
}
}

View File

@@ -0,0 +1,17 @@
package io.smnp.ext.function
import io.smnp.callable.function.Function
import io.smnp.callable.function.FunctionDefinitionTool
import io.smnp.callable.signature.Signature.Companion.simple
import io.smnp.type.enumeration.DataType.INT
import io.smnp.type.matcher.Matcher.Companion.ofType
import io.smnp.type.model.Value
class SleepFunction : Function("sleep") {
override fun define(new: FunctionDefinitionTool) {
new function simple(ofType(INT)) body { _, (milli) ->
Thread.sleep((milli.value!! as Int).toLong())
Value.void()
}
}
}