github.com/bananabytelabs/wazero@v0.0.0-20240105073314-54b22a776da8/imports/assemblyscript/assemblyscript_example_test.go (about)

     1  package assemblyscript_test
     2  
     3  import (
     4  	"context"
     5  	_ "embed"
     6  
     7  	"github.com/bananabytelabs/wazero"
     8  	"github.com/bananabytelabs/wazero/imports/assemblyscript"
     9  )
    10  
    11  // This shows how to instantiate AssemblyScript's special imports.
    12  func Example_instantiate() {
    13  	ctx := context.Background()
    14  
    15  	r := wazero.NewRuntime(ctx)
    16  	defer r.Close(ctx) // This closes everything this Runtime created.
    17  
    18  	// This adds the "env" module to the runtime, with AssemblyScript's special
    19  	// function imports.
    20  	assemblyscript.MustInstantiate(ctx, r)
    21  
    22  	// Output:
    23  }
    24  
    25  // This shows how to instantiate AssemblyScript's special imports when you also
    26  // need other functions in the "env" module.
    27  func Example_functionExporter() {
    28  	ctx := context.Background()
    29  
    30  	r := wazero.NewRuntime(ctx)
    31  	defer r.Close(ctx) // This closes everything this Runtime created.
    32  
    33  	// First construct your own module builder for "env"
    34  	envBuilder := r.NewHostModuleBuilder("env").
    35  		NewFunctionBuilder().
    36  		WithFunc(func() uint32 { return 1 }).
    37  		Export("get_int")
    38  
    39  	// Now, add AssemblyScript special function imports into it.
    40  	assemblyscript.NewFunctionExporter().
    41  		WithAbortMessageDisabled().
    42  		ExportFunctions(envBuilder)
    43  
    44  	// Output:
    45  }