Skip to content

Instantly share code, notes, and snippets.

@zoren
Created May 7, 2022 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoren/42de94984e037611dc808f481ad50b9e to your computer and use it in GitHub Desktop.
Save zoren/42de94984e037611dc808f481ad50b9e to your computer and use it in GitHub Desktop.
An experiment with asmble.io
;; most of this code is copied from a source that used an older version of asmble, here we use 0.4.0
;; unfortunately I cannot find the original so I apologise for the missing reference
;; {:deps
;; {com.github.cretz.asmble/asmble-compiler {:mvn/version "0.4.0"}}}
(ns asmble-fun
(:require [clojure.reflect :as r])
(:import [asmble.cli Translate]
[asmble.run.jvm ScriptContext ExceptionTranslator
ModuleBuilder$Compiled
ModuleBuilder$Compiled$SimpleClassLoader]
[asmble.compile.jvm ClsContext]
[asmble.ast SExpr]
[asmble.util Logger]
[asmble.io StrToSExpr SExprToAst]
[kotlin.jvm.functions Function1]))
(defn fn->kotlinFn1 [one-arg-fn]
(proxy [Function1] []
(invoke [arg]
(one-arg-fn arg))))
(defn create-script-context
[{:keys [package-name modules registrations logger adjust-context class-loader exception-translator default-max-mem-pages]
:or {package-name "package"
modules '()
registrations {}
logger (new asmble.util.Logger$Print asmble.util.Logger$Level/DEBUG)
adjust-context (fn->kotlinFn1 identity)
class-loader (ModuleBuilder$Compiled$SimpleClassLoader.
(.getClassLoader ScriptContext)
logger
false)
exception-translator (ExceptionTranslator/Companion)
default-max-mem-pages 1}}]
(let [module-builder
(ModuleBuilder$Compiled.
package-name
logger
class-loader
adjust-context
false
default-max-mem-pages)]
(ScriptContext.
; package-name
modules
registrations
logger
exception-translator
; adjust-context
module-builder
;default-max-mem-pages
(fn->kotlinFn1 (fn [] false)))))
(defn wast->module [wast]
(let [ctx (create-script-context {:package-name "foovar"})
sexpr (-> (StrToSExpr/Companion)
(.parse (str wast))
(.getVals)
(asmble.ast.SExpr$Multi.))
module (->> sexpr
(.toScript (SExprToAst/Companion))
(.getCommands)
(reduce #(.runCommand %1 %2) ctx)
(.getModules)
first)]
(.getInst module)))
(comment
(let [wast-text "(module
(func (export \"doAdd20\") (param $i i32) (result i32)
(i32.add (get_local 0) (i32.const 20))))"
inst (wast->module wast-text)]
(.doAdd20 inst 22))
(let [wast-text
(str
'(module
(func
(export "inc")
(param $x i32)
(result i32)
(i32.add (get_local $x) (i32.const 1)))
(func (export "doAdd20") (param $i i32) (result i32)
(i32.add (get_local 0) (i32.const 20)))))
inst (wast->module wast-text)]
(.inc inst 22))
;
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment