Skip to content
Ontology wasm is a VM for ontology block chain, it can also be used for other stand-alone environment not only for block chains.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
disasm refactor code Mar 28, 2018
doc add en doc Apr 25, 2018
exec update code Apr 18, 2018
memory update code Apr 18, 2018
wasm update code Apr 18, 2018
.gitignore refactor code Mar 28, 2018
LICENSE Update Mar 29, 2018
wagonLICENSE first commit code Mar 26, 2018

Ontology wasm


Ontology wasm is a VM for ontology block chain, it can also be used for other stand-alone environment not only for block chains.

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust.


Ontology-wasm disassemble and execute wasm binary codes based on Wagon project with extra memory management.

Currently, we support int, int64, float, double, string(byte array), int array and int64 array data types,since wasm only has 4 types (i32,i64,f32 and f64), that means only the 4 types data could be pushed into the stack, other complex data types must be stored in memory.

In wasm MVP version,every module can only has one linear memory.



  1. create a Engine to contain the VM, example like below:
type ExecutionEngine struct {
	service *InteropService
	vm      *VM
	version  string //for test different contracts
	backupVM *vmstack

func NewExecutionEngine(iservice IInteropService, ver string) *ExecutionEngine {

	engine := &ExecutionEngine{
		service: NewInteropService(),
		version: ver,
	if iservice != nil {

	engine.backupVM = newStack(VM_STACK_DEPTH)
	return engine

service contains the system apis which exists in the "import 'env' " section, that means you can create any api calls implemented by golang code.

We already put some apis in env_service.go

ver represents the version of engine, you can use this field to decide how to deserialize your parameters.

Then load the wasm module(from a file or other stream)

	code, err := ioutil.ReadFile("./test_data2/contract.wasm")
	if err != nil {
		fmt.Println("error in read file", err.Error())

Pass the Parameters (Json format for example)

	par := make([]Param, 2)
	par[0] = Param{Ptype: "int", Pval: "20"}
	par[1] = Param{Ptype: "int", Pval: "30"}

	p := Args{Params: par}
	jbytes, err := json.Marshal(p)
	if err != nil {
	bf := bytes.NewBufferString("add")

Execute the wasm vm

    	res, err := engine.Call(nil, code, bf.Bytes())
	if err != nil {
		fmt.Println("call error!", err.Error())

If you know the result is not a basic type(int,int64,float or double),you should get the data from memory

    	retbytes, err := engine.vm.GetPointerMemory(uint64(binary.LittleEndian.Uint32(res)))
	if err != nil {
		t.Fatal("errors:" + err.Error())

	fmt.Println("retbytes is " + string(retbytes))

	result := &Result{}
	json.Unmarshal(retbytes, result)

You can try the tests in exec/engine_test.go and smart contract tests in exec/contract_test.go

Ontology Smart contract

Please refer to smart-contract-tutorial

You can’t perform that action at this time.