Very experimental WASM code generation by MST in contexts of Int and Real #158

Closed
CommanderTvis wants to merge 16 commits from feature/binaryen into dev
17 changed files with 4999 additions and 1 deletions

View File

@ -2,6 +2,20 @@ plugins {
id("ru.mipt.npm.mpp") id("ru.mipt.npm.mpp")
} }
kotlin.js {
nodejs { // or `browser`
testTask {
useMocha().timeout = "0"
}
}
browser {
testTask {
useMocha().timeout = "0"
}
}
}
kotlin.sourceSets { kotlin.sourceSets {
commonMain { commonMain {
dependencies { dependencies {
@ -16,4 +30,12 @@ kotlin.sourceSets {
implementation("org.ow2.asm:asm-commons:8.0.1") implementation("org.ow2.asm:asm-commons:8.0.1")
} }
} }
jsMain {
dependencies {
implementation(npm("binaryen", "98.0.0-nightly.20201113"))
implementation(npm("js-base64", "3.6.0"))
implementation(npm("webassembly", "0.11.0"))
}
}
} }

135
kmath-ast/src/jsMain/kotlin/base64.d.ts vendored Normal file
View File

@ -0,0 +1,135 @@
/**
* base64.ts
*
* Licensed under the BSD 3-Clause License.
* http://opensource.org/licenses/BSD-3-Clause
*
* References:
* http://en.wikipedia.org/wiki/Base64
*
* @author Dan Kogai (https://github.com/dankogai)
*/
declare const version = "3.6.0";
/**
* @deprecated use lowercase `version`.
*/
declare const VERSION = "3.6.0";
/**
* polyfill version of `btoa`
*/
declare const btoaPolyfill: (bin: string) => string;
/**
* does what `window.btoa` of web browsers do.
* @param {String} bin binary string
* @returns {string} Base64-encoded string
*/
declare const _btoa: (bin: string) => string;
/**
* converts a Uint8Array to a Base64 string.
* @param {boolean} [urlsafe] URL-and-filename-safe a la RFC4648 §5
* @returns {string} Base64 string
*/
declare const fromUint8Array: (u8a: Uint8Array, urlsafe?: boolean) => string;
/**
* @deprecated should have been internal use only.
* @param {string} src UTF-8 string
* @returns {string} UTF-16 string
*/
declare const utob: (u: string) => string;
/**
* converts a UTF-8-encoded string to a Base64 string.
* @param {boolean} [urlsafe] if `true` make the result URL-safe
* @returns {string} Base64 string
*/
declare const encode: (src: string, urlsafe?: boolean) => string;
/**
* converts a UTF-8-encoded string to URL-safe Base64 RFC4648 §5.
* @returns {string} Base64 string
*/
declare const encodeURI: (src: string) => string;
/**
* @deprecated should have been internal use only.
* @param {string} src UTF-16 string
* @returns {string} UTF-8 string
*/
declare const btou: (b: string) => string;
/**
* polyfill version of `atob`
*/
declare const atobPolyfill: (asc: string) => string;
/**
* does what `window.atob` of web browsers do.
* @param {String} asc Base64-encoded string
* @returns {string} binary string
*/
declare const _atob: (asc: string) => string;
/**
* converts a Base64 string to a Uint8Array.
*/
declare const toUint8Array: (a: string) => Uint8Array;
/**
* converts a Base64 string to a UTF-8 string.
* @param {String} src Base64 string. Both normal and URL-safe are supported
* @returns {string} UTF-8 string
*/
declare const decode: (src: string) => string;
/**
* check if a value is a valid Base64 string
* @param {String} src a value to check
*/
declare const isValid: (src: any) => boolean;
/**
* extend String.prototype with relevant methods
*/
declare const extendString: () => void;
/**
* extend Uint8Array.prototype with relevant methods
*/
declare const extendUint8Array: () => void;
/**
* extend Builtin prototypes with relevant methods
*/
declare const extendBuiltins: () => void;
declare const gBase64: {
version: string;
VERSION: string;
atob: (asc: string) => string;
atobPolyfill: (asc: string) => string;
btoa: (bin: string) => string;
btoaPolyfill: (bin: string) => string;
fromBase64: (src: string) => string;
toBase64: (src: string, urlsafe?: boolean) => string;
encode: (src: string, urlsafe?: boolean) => string;
encodeURI: (src: string) => string;
encodeURL: (src: string) => string;
utob: (u: string) => string;
btou: (b: string) => string;
decode: (src: string) => string;
isValid: (src: any) => boolean;
fromUint8Array: (u8a: Uint8Array, urlsafe?: boolean) => string;
toUint8Array: (a: string) => Uint8Array;
extendString: () => void;
extendUint8Array: () => void;
extendBuiltins: () => void;
};
export { version };
export { VERSION };
export { _atob as atob };
export { atobPolyfill };
export { _btoa as btoa };
export { btoaPolyfill };
export { decode as fromBase64 };
export { encode as toBase64 };
export { utob };
export { encode };
export { encodeURI };
export { encodeURI as encodeURL };
export { btou };
export { decode };
export { isValid };
export { fromUint8Array };
export { toUint8Array };
export { extendString };
export { extendUint8Array };
export { extendBuiltins };
export { gBase64 as Base64 };

View File

@ -0,0 +1,75 @@
@file:Suppress(
"INTERFACE_WITH_SUPERCLASS",
"OVERRIDING_FINAL_MEMBER",
"RETURN_TYPE_MISMATCH_ON_OVERRIDE",
"CONFLICTING_OVERLOADS",
"NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING",
"KDocMissingDocumentation",
"ObjectPropertyName",
"ClassName",
"PackageDirectoryMismatch",
"PackageName",
)
@file:JsNonModule
@file:JsModule("js-base64")
package Base64
import org.khronos.webgl.Uint8Array
external var version: Any
external var VERSION: Any
external var btoaPolyfill: (bin: String) -> String
external var _btoa: (bin: String) -> String
external var fromUint8Array: (u8a: Uint8Array, urlsafe: Boolean) -> String
external var utob: (u: String) -> String
external var encode: (src: String, urlsafe: Boolean) -> String
external var encodeURI: (src: String) -> String
external var btou: (b: String) -> String
external var atobPolyfill: (asc: String) -> String
external var _atob: (asc: String) -> String
external var toUint8Array: (a: String) -> Uint8Array
external var decode: (src: String) -> String
external var isValid: (src: Any) -> Boolean
external var extendString: () -> Unit
external var extendUint8Array: () -> Unit
external var extendBuiltins: () -> Unit
external object gBase64 {
var version: String
var VERSION: String
var atob: (asc: String) -> String
var atobPolyfill: (asc: String) -> String
var btoa: (bin: String) -> String
var btoaPolyfill: (bin: String) -> String
var fromBase64: (src: String) -> String
var toBase64: (src: String, urlsafe: Boolean) -> String
var encode: (src: String, urlsafe: Boolean) -> String
var encodeURI: (src: String) -> String
var encodeURL: (src: String) -> String
var utob: (u: String) -> String
var btou: (b: String) -> String
var decode: (src: String) -> String
var isValid: (src: Any) -> Boolean
var fromUint8Array: (u8a: Uint8Array, urlsafe: Boolean) -> String
var toUint8Array: (a: String) -> Uint8Array
var extendString: () -> Unit
var extendUint8Array: () -> Unit
var extendBuiltins: () -> Unit
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,11 @@
@file:Suppress("PackageDirectoryMismatch", "NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING", "KDocMissingDocumentation")
package binaryen
typealias Type = Number
typealias ExpressionRef = Number
typealias FunctionRef = Number
typealias GlobalRef = Number
typealias ExportRef = Number
typealias EventRef = Number
typealias RelooperBlockRef = Number

1787
kmath-ast/src/jsMain/kotlin/index.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,140 @@
package kscience.kmath.wasm.internal
import WebAssembly.Instance
import binaryen.*
import kscience.kmath.ast.MST
import kscience.kmath.expressions.Expression
import kscience.kmath.expressions.StringSymbol
import kscience.kmath.operations.*
import WebAssembly.Module as WasmModule
import binaryen.Module as BinaryenModule
private val spreader = eval("(obj, args) => obj(...args)")
@Suppress("UnsafeCastFromDynamic")
internal sealed class WasmBuilder<T>(
val binaryenType: Type,
val kmathAlgebra: Algebra<T>,
val target: MST
) where T : Number {
val keys: MutableList<String> = mutableListOf()
lateinit var ctx: BinaryenModule
open fun visitSymbolic(mst: MST.Symbolic): ExpressionRef {
try {
kmathAlgebra.symbol(mst.value)
} catch (ignored: Throwable) {
null
}?.let { return visitNumeric(MST.Numeric(it)) }
var idx = keys.indexOf(mst.value)
if (idx == -1) {
keys += mst.value
idx = keys.lastIndex
}
return ctx.local.get(idx, binaryenType)
}
abstract fun visitNumeric(mst: MST.Numeric): ExpressionRef
open fun visitUnary(mst: MST.Unary): ExpressionRef =
error("Unary operation ${mst.operation} not defined in $this")
open fun visitBinary(mst: MST.Binary): ExpressionRef =
error("Binary operation ${mst.operation} not defined in $this")
open fun createModule(): BinaryenModule = js("new \$module\$binaryen.Module()")
fun visit(mst: MST): ExpressionRef = when (mst) {
is MST.Symbolic -> visitSymbolic(mst)
is MST.Numeric -> visitNumeric(mst)
is MST.Unary -> visitUnary(mst)
is MST.Binary -> visitBinary(mst)
}
val instance by lazy {
val c = WasmModule(with(createModule()) {
ctx = this
val expr = visit(target)
addFunction(
"executable",
createType(Array(keys.size) { binaryenType }),
binaryenType,
arrayOf(),
expr
)
setOptimizeLevel(3)
optimizeFunction("executable")
addFunctionExport("executable", "executable")
val res = emitBinary()
dispose()
res
})
val i = Instance(c, js("{}") as Any)
val symbols = keys.map(::StringSymbol)
keys.clear()
Expression<T> { args ->
val params = symbols.map(args::getValue).toTypedArray()
spreader(i.exports.asDynamic().executable, params) as T
}
}
}
internal class RealWasmBuilder(target: MST) : WasmBuilder<Double>(f64, RealField, target) {
override fun createModule(): BinaryenModule = readBinary(f64StandardFunctions)
override fun visitNumeric(mst: MST.Numeric): ExpressionRef = ctx.f64.const(mst.value)
override fun visitUnary(mst: MST.Unary): ExpressionRef = when (mst.operation) {
SpaceOperations.MINUS_OPERATION -> ctx.f64.neg(visit(mst.value))
SpaceOperations.PLUS_OPERATION -> visit(mst.value)
PowerOperations.SQRT_OPERATION -> ctx.f64.sqrt(visit(mst.value))
TrigonometricOperations.SIN_OPERATION -> ctx.call("sin", arrayOf(visit(mst.value)), f64)
TrigonometricOperations.COS_OPERATION -> ctx.call("cos", arrayOf(visit(mst.value)), f64)
TrigonometricOperations.TAN_OPERATION -> ctx.call("tan", arrayOf(visit(mst.value)), f64)
TrigonometricOperations.ASIN_OPERATION -> ctx.call("asin", arrayOf(visit(mst.value)), f64)
TrigonometricOperations.ACOS_OPERATION -> ctx.call("acos", arrayOf(visit(mst.value)), f64)
TrigonometricOperations.ATAN_OPERATION -> ctx.call("atan", arrayOf(visit(mst.value)), f64)
HyperbolicOperations.SINH_OPERATION -> ctx.call("sinh", arrayOf(visit(mst.value)), f64)
HyperbolicOperations.COSH_OPERATION -> ctx.call("cosh", arrayOf(visit(mst.value)), f64)
HyperbolicOperations.TANH_OPERATION -> ctx.call("tanh", arrayOf(visit(mst.value)), f64)
HyperbolicOperations.ASINH_OPERATION -> ctx.call("asinh", arrayOf(visit(mst.value)), f64)
HyperbolicOperations.ACOSH_OPERATION -> ctx.call("acosh", arrayOf(visit(mst.value)), f64)
HyperbolicOperations.ATANH_OPERATION -> ctx.call("atanh", arrayOf(visit(mst.value)), f64)
ExponentialOperations.EXP_OPERATION -> ctx.call("exp", arrayOf(visit(mst.value)), f64)
ExponentialOperations.LN_OPERATION -> ctx.call("log", arrayOf(visit(mst.value)), f64)
else -> super.visitUnary(mst)
}
override fun visitBinary(mst: MST.Binary): ExpressionRef = when (mst.operation) {
SpaceOperations.PLUS_OPERATION -> ctx.f64.add(visit(mst.left), visit(mst.right))
SpaceOperations.MINUS_OPERATION -> ctx.f64.sub(visit(mst.left), visit(mst.right))
RingOperations.TIMES_OPERATION -> ctx.f64.mul(visit(mst.left), visit(mst.right))
FieldOperations.DIV_OPERATION -> ctx.f64.div(visit(mst.left), visit(mst.right))
PowerOperations.POW_OPERATION -> ctx.call("pow", arrayOf(visit(mst.left), visit(mst.right)), f64)
else -> super.visitBinary(mst)
}
}
internal class IntWasmBuilder(target: MST) : WasmBuilder<Int>(i32, IntRing, target) {
override fun visitNumeric(mst: MST.Numeric): ExpressionRef = ctx.i32.const(mst.value)
override fun visitUnary(mst: MST.Unary): ExpressionRef = when (mst.operation) {
SpaceOperations.MINUS_OPERATION -> ctx.i32.sub(ctx.i32.const(0), visit(mst.value))
SpaceOperations.PLUS_OPERATION -> visit(mst.value)
else -> super.visitUnary(mst)
}
override fun visitBinary(mst: MST.Binary): ExpressionRef = when (mst.operation) {
SpaceOperations.PLUS_OPERATION -> ctx.i32.add(visit(mst.left), visit(mst.right))
SpaceOperations.MINUS_OPERATION -> ctx.i32.sub(visit(mst.left), visit(mst.right))
RingOperations.TIMES_OPERATION -> ctx.i32.mul(visit(mst.left), visit(mst.right))
else -> super.visitBinary(mst)
}
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,41 @@
package kscience.kmath.wasm
import kscience.kmath.ast.MST
import kscience.kmath.ast.MstExpression
import kscience.kmath.expressions.Expression
import kscience.kmath.operations.IntRing
import kscience.kmath.operations.RealField
import kscience.kmath.wasm.internal.IntWasmBuilder
import kscience.kmath.wasm.internal.RealWasmBuilder
/**
* Compiles an [MST] to WASM in the context of reals.
*
* @author Iaroslav Postovalov.
*/
public fun RealField.expression(mst: MST): Expression<Double> =
RealWasmBuilder(mst).instance
/**
* Compiles an [MST] to WASM in the context of integers.
*
* @author Iaroslav Postovalov.
*/
public fun IntRing.expression(mst: MST): Expression<Int> =
IntWasmBuilder(mst).instance
/**
* Optimizes performance of an [MstExpression] using WASM codegen in the context of reals.
*
* @author Iaroslav Postovalov.
*/
public fun MstExpression<Double, RealField>.compile(): Expression<Double> =
RealWasmBuilder(mst).instance
/**
* Optimizes performance of an [MstExpression] using WASM codegen in the context of integers.
*
* @author Iaroslav Postovalov.
*/
public fun MstExpression<Int, IntRing>.compile(): Expression<Int> =
IntWasmBuilder(mst).instance

View File

@ -0,0 +1,228 @@
@file:JsQualifier("WebAssembly")
@file:Suppress(
"INTERFACE_WITH_SUPERCLASS",
"OVERRIDING_FINAL_MEMBER",
"RETURN_TYPE_MISMATCH_ON_OVERRIDE",
"NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING",
"KDocMissingDocumentation",
"PackageDirectoryMismatch",
"PackageName",
"ClassName",
)
package WebAssembly
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.ArrayBufferView
import org.khronos.webgl.Uint8Array
import org.w3c.fetch.Response
import tsstdlib.PromiseLike
import kotlin.js.Promise
@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
external interface CompileError {
companion object {
var prototype: CompileError
}
}
@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
external interface Global {
var value: Any
fun valueOf(): Any
companion object {
var prototype: Global
}
}
@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
@JsName("Instance")
external interface Instance1 {
var exports: Exports
companion object {
var prototype: Instance
}
}
@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
external interface LinkError {
companion object {
var prototype: LinkError
}
}
@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
external interface Memory {
var buffer: ArrayBuffer
fun grow(delta: Number): Number
companion object {
var prototype: Memory
}
}
@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
@JsName("Module")
external interface Module1 {
companion object {
var prototype: Module
fun customSections(moduleObject: Module, sectionName: String): Array<ArrayBuffer>
fun exports(moduleObject: Module): Array<ModuleExportDescriptor>
fun imports(moduleObject: Module): Array<ModuleImportDescriptor>
}
}
@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
external interface RuntimeError {
companion object {
var prototype: RuntimeError
}
}
@Suppress("NESTED_CLASS_IN_EXTERNAL_INTERFACE")
external interface Table {
var length: Number
fun get(index: Number): Function<*>?
fun grow(delta: Number): Number
fun set(index: Number, value: Function<*>?)
companion object {
var prototype: Table
}
}
external interface GlobalDescriptor {
var mutable: Boolean?
get() = definedExternally
set(value) = definedExternally
var value: String /* "f32" | "f64" | "i32" | "i64" */
}
external interface MemoryDescriptor {
var initial: Number
var maximum: Number?
get() = definedExternally
set(value) = definedExternally
}
external interface ModuleExportDescriptor {
var kind: String /* "function" | "global" | "memory" | "table" */
var name: String
}
external interface ModuleImportDescriptor {
var kind: String /* "function" | "global" | "memory" | "table" */
var module: String
var name: String
}
external interface TableDescriptor {
var element: String /* "anyfunc" */
var initial: Number
var maximum: Number?
get() = definedExternally
set(value) = definedExternally
}
external interface WebAssemblyInstantiatedSource {
var instance: Instance
var module: Module
}
external fun compile(bytes: ArrayBufferView): Promise<Module>
external fun compile(bytes: ArrayBuffer): Promise<Module>
external fun compileStreaming(source: Response): Promise<Module>
external fun compileStreaming(source: Promise<Response>): Promise<Module>
external fun instantiate(
bytes: ArrayBufferView,
importObject: Imports = definedExternally
): Promise<WebAssemblyInstantiatedSource>
external fun instantiate(bytes: ArrayBufferView): Promise<WebAssemblyInstantiatedSource>
external fun instantiate(bytes: ArrayBuffer, importObject: Imports = definedExternally): dynamic /* Promise | Promise */
external fun instantiate(bytes: ArrayBuffer): dynamic /* Promise | Promise */
external fun instantiate(moduleObject: Module, importObject: Imports = definedExternally): Promise<Instance>
external fun instantiate(moduleObject: Module): Promise<Instance>
external fun instantiateStreaming(
response: Response,
importObject: Imports = definedExternally
): Promise<WebAssemblyInstantiatedSource>
external fun instantiateStreaming(response: Response): Promise<WebAssemblyInstantiatedSource>
external fun instantiateStreaming(
response: PromiseLike<Response>,
importObject: Imports = definedExternally
): Promise<WebAssemblyInstantiatedSource>
external fun instantiateStreaming(response: PromiseLike<Response>): Promise<WebAssemblyInstantiatedSource>
external fun validate(bytes: ArrayBufferView): Boolean
external fun validate(bytes: ArrayBuffer): Boolean
external interface `T$0` {
var name: String
var kind: String
}
external interface `T$1` {
var module: String
var name: String
var kind: String
}
open external class Module {
constructor(bufferSource: ArrayBuffer)
constructor(bufferSource: Uint8Array)
companion object {
fun customSections(module: Module, sectionName: String): Array<ArrayBuffer>
fun exports(module: Module): Array<`T$0`>
fun imports(module: Module): Array<`T$1`>
}
}
@JsName("Instance")
open external class Instance(module: Module, importObject: Any = definedExternally) {
open var exports: Any
}
@JsName("Memory")
open external class Memory1(memoryDescriptor: MemoryDescriptor) {
open var buffer: ArrayBuffer
open fun grow(numPages: Number): Number
}
@JsName("Table")
open external class Table1(tableDescriptor: TableDescriptor) {
open var length: Number
open fun get(index: Number): Function<*>
open fun grow(numElements: Number): Number
open fun set(index: Number, value: Function<*>)
}
external fun compile(bufferSource: Uint8Array): Promise<Module>
external interface ResultObject {
var module: Module
var instance: Instance
}
external fun instantiate(bufferSource: Uint8Array, importObject: Any = definedExternally): Promise<ResultObject>
external fun instantiate(bufferSource: Uint8Array): Promise<ResultObject>
external fun validate(bufferSource: Uint8Array): Boolean

View File

@ -0,0 +1,44 @@
@file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS",
"NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING", "PackageDirectoryMismatch", "KDocMissingDocumentation"
)
package tsstdlib
import kotlin.js.Promise
external interface IteratorYieldResult<TYield> {
var done: Boolean?
get() = definedExternally
set(value) = definedExternally
var value: TYield
}
external interface IteratorReturnResult<TReturn> {
var done: Boolean
var value: TReturn
}
external interface Iterator<T, TReturn, TNext> {
fun next(vararg args: Any /* JsTuple<> | JsTuple<TNext> */): dynamic /* IteratorYieldResult<T> | IteratorReturnResult<TReturn> */
val `return`: ((value: TReturn) -> dynamic)?
val `throw`: ((e: Any) -> dynamic)?
}
typealias Iterator__1<T> = Iterator<T, Any, Nothing?>
external interface Iterable<T>
external interface IterableIterator<T> : Iterator__1<T>
external interface PromiseConstructor {
var prototype: Promise<Any>
fun all(values: Any /* JsTuple<Any?, Any?, Any?, Any?, Any?, Any?, Any?, Any?, Any?, Any?> | JsTuple<Any?, Any?, Any?, Any?, Any?, Any?, Any?, Any?, Any?> | JsTuple<Any?, Any?, Any?, Any?, Any?, Any?, Any?, Any?> | JsTuple<Any?, Any?, Any?, Any?, Any?, Any?, Any?> | JsTuple<Any?, Any?, Any?, Any?, Any?, Any?> | JsTuple<Any?, Any?, Any?, Any?, Any?> | JsTuple<Any?, Any?, Any?, Any?> | JsTuple<Any?, Any?, Any?> | JsTuple<Any?, Any?> */): Promise<dynamic /* JsTuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> | JsTuple<T1, T2, T3, T4, T5, T6, T7, T8, T9> | JsTuple<T1, T2, T3, T4, T5, T6, T7, T8> | JsTuple<T1, T2, T3, T4, T5, T6, T7> | JsTuple<T1, T2, T3, T4, T5, T6> | JsTuple<T1, T2, T3, T4, T5> | JsTuple<T1, T2, T3, T4> | JsTuple<T1, T2, T3> | JsTuple<T1, T2> */>
fun <T> all(values: Array<Any? /* T | PromiseLike<T> */>): Promise<Array<T>>
fun <T> race(values: Array<T>): Promise<Any>
fun <T> reject(reason: Any = definedExternally): Promise<T>
fun <T> resolve(value: T): Promise<T>
fun <T> resolve(value: PromiseLike<T>): Promise<T>
fun resolve(): Promise<Unit>
fun <T> all(values: Iterable<Any? /* T | PromiseLike<T> */>): Promise<Array<T>>
fun <T> race(values: Iterable<T>): Promise<Any>
fun <T> race(values: Iterable<Any? /* T | PromiseLike<T> */>): Promise<T>
}

View File

@ -0,0 +1,73 @@
@file:Suppress(
"INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS",
"PackageDirectoryMismatch", "DEPRECATION", "NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING", "KDocMissingDocumentation",
"UNUSED_TYPEALIAS_PARAMETER", "PropertyName"
)
package tsstdlib
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Uint8Array
external interface FunctionConstructor {
@nativeInvoke
operator fun invoke(vararg args: String): Function<*>
var prototype: Function<*>
}
external interface ErrorConstructor {
@nativeInvoke
operator fun invoke(message: String = definedExternally): Error
var prototype: Error
}
external interface PromiseLike<T> {
fun then(
onfulfilled: ((value: T) -> Any?)? = definedExternally,
onrejected: ((reason: Any) -> Any?)? = definedExternally
): PromiseLike<dynamic /* TResult1 | TResult2 */>
}
external interface ArrayLike<T> {
var length: Number
@nativeGetter
operator fun get(n: Number): T?
@nativeSetter
operator fun set(n: Number, value: T)
}
typealias Record<K, T> = Any
external interface ArrayBufferTypes {
var ArrayBuffer: ArrayBuffer
}
external interface ArrayBufferConstructor {
var prototype: ArrayBuffer
fun isView(arg: Any): Boolean
}
external interface Uint8ArrayConstructor {
fun from(
arrayLike: Iterable<Number>,
mapfn: (v: Number, k: Number) -> Number = definedExternally,
thisArg: Any = definedExternally
): Uint8Array
fun from(arrayLike: Iterable<Number>): Uint8Array
fun from(arrayLike: Iterable<Number>, mapfn: (v: Number, k: Number) -> Number = definedExternally): Uint8Array
var prototype: Uint8Array
var BYTES_PER_ELEMENT: Number
fun of(vararg items: Number): Uint8Array
fun from(arrayLike: ArrayLike<Number>): Uint8Array
fun <T> from(
arrayLike: ArrayLike<T>,
mapfn: (v: T, k: Number) -> Number,
thisArg: Any = definedExternally
): Uint8Array
fun <T> from(arrayLike: ArrayLike<T>, mapfn: (v: T, k: Number) -> Number): Uint8Array
}

View File

@ -0,0 +1,32 @@
@file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS",
"UnusedImport", "PackageDirectoryMismatch", "PackageName", "NO_EXPLICIT_VISIBILITY_IN_API_MODE_WARNING",
"KDocMissingDocumentation"
)
package WebAssembly
import kotlin.js.*
import org.khronos.webgl.*
import org.w3c.dom.*
import org.w3c.dom.events.*
import org.w3c.dom.parsing.*
import org.w3c.dom.svg.*
import org.w3c.dom.url.*
import org.w3c.fetch.*
import org.w3c.files.*
import org.w3c.notifications.*
import org.w3c.performance.*
import org.w3c.workers.*
import org.w3c.xhr.*
import tsstdlib.Record
typealias Exports = Record<String, dynamic /* Function<*> | Global | Memory | Table */>
typealias ModuleImports = Record<String, dynamic /* Function<*> | Global | Memory | Table | Number */>
typealias Imports = Record<String, ModuleImports>
typealias CompileError1 = Error
typealias LinkError1 = Error
typealias RuntimeError1 = Error

2
kmath-ast/src/jsMain/kotlin/wasm.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
import * as binaryen from "./index";
export = binaryen;

View File

@ -0,0 +1,102 @@
/**
* WebAssembly v1 (MVP) declaration file for TypeScript
* Definitions by: 01alchemist (https://twitter.com/01alchemist)
*/
declare namespace WebAssembly {
/**
* WebAssembly.Module
**/
class Module {
constructor (bufferSource: ArrayBuffer | Uint8Array);
static customSections(module: Module, sectionName: string): ArrayBuffer[];
static exports(module: Module): {
name: string;
kind: string;
}[];
static imports(module: Module): {
module: string;
name: string;
kind: string;
}[];
}
/**
* WebAssembly.Instance
**/
class Instance {
readonly exports: any;
constructor (module: Module, importObject?: any);
}
/**
* WebAssembly.Memory
* Note: A WebAssembly page has a constant size of 65,536 bytes, i.e., 64KiB.
**/
interface MemoryDescriptor {
initial: number;
maximum?: number;
}
class Memory {
readonly buffer: ArrayBuffer;
constructor (memoryDescriptor: MemoryDescriptor);
grow(numPages: number): number;
}
/**
* WebAssembly.Table
**/
interface TableDescriptor {
element: "anyfunc",
initial: number;
maximum?: number;
}
class Table {
readonly length: number;
constructor (tableDescriptor: TableDescriptor);
get(index: number): Function;
grow(numElements: number): number;
set(index: number, value: Function): void;
}
/**
* Errors
*/
class CompileError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor (message?:string, fileName?:string, lineNumber?:number);
toString(): string;
}
class LinkError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor (message?:string, fileName?:string, lineNumber?:number);
toString(): string;
}
class RuntimeError extends Error {
readonly fileName: string;
readonly lineNumber: string;
readonly columnNumber: string;
constructor (message?:string, fileName?:string, lineNumber?:number);
toString(): string;
}
function compile(bufferSource: ArrayBuffer | Uint8Array): Promise<Module>;
interface ResultObject {
module: Module;
instance: Instance;
}
function instantiate(bufferSource: ArrayBuffer | Uint8Array, importObject?: any): Promise<ResultObject>;
function instantiate(module: Module, importObject?: any): Promise<Instance>;
function validate(bufferSource: ArrayBuffer | Uint8Array): boolean;
}

View File

@ -0,0 +1,64 @@
package kscience.kmath.ast
import kscience.kmath.expressions.Expression
import kscience.kmath.expressions.StringSymbol
import kscience.kmath.expressions.invoke
import kscience.kmath.operations.IntRing
import kscience.kmath.operations.RealField
import kscience.kmath.wasm.compile
import kotlin.math.pow
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.time.measureTime
internal class Test {
@Test
fun int() {
val res = IntRing.mstInRing { number(100000000) + number(10000000) }.compile()()
assertEquals(110000000, res)
}
@Test
fun real() {
val res = RealField.mstInExtendedField { number(100000000) + number(2).pow(10) }.compile()()
assertEquals(100001024.0, res)
}
@Test
fun argsPassing() {
val res = RealField
.mstInExtendedField { symbol("y") + symbol("x").pow(10) }
.compile()("x" to 2.0, "y" to 100000000.0)
assertEquals(100001024.0, res)
}
@Test
fun powFunction() {
val expr = RealField.mstInExtendedField { symbol("x").pow(1.0 / 6.0) }.compile()
assertEquals(0.9730585187140817, expr("x" to 0.8488554755054833))
}
@Test
fun manyRuns() {
println("Compiled")
val times = 1_000_000
var rng = Random(0)
var sum1 = 0.0
var sum2 = 0.0
var sum3 = 0.0
val e2 = RealField.mstInExtendedField { symbol("x").pow(1.0 / 6.0) }
val e1 = e2.compile()
measureTime { repeat(times) { sum1 += e1("x" to rng.nextDouble()) } }.also(::println)
println("MST")
rng = Random(0)
measureTime { repeat(times) { sum2 += e2("x" to rng.nextDouble()) } }.also(::println)
val e3 = Expression<Double> { args -> args.getValue(StringSymbol("x")).pow(1.0 / 6.0) }
println("JS Math")
rng = Random(0)
measureTime { repeat(times) { sum3 += e3("x" to rng.nextDouble()) } }.also(::println)
assertEquals(sum1, sum2)
assertEquals(sum1, sum3)
}
}

View File

@ -49,7 +49,7 @@ public interface NumericAlgebra<T> : Algebra<T> {
* Dynamic call of binary operation [operation] on [left] and [right] where right element is [Number]. * Dynamic call of binary operation [operation] on [left] and [right] where right element is [Number].
*/ */
public fun rightSideNumberOperation(operation: String, left: T, right: Number): T = public fun rightSideNumberOperation(operation: String, left: T, right: Number): T =
leftSideNumberOperation(operation, right, left) binaryOperation(operation, left, number(right))
} }
/** /**