wa-lang.org/wazero@v1.0.2/imports/emscripten/emscripten_example_test.go (about) 1 package emscripten_test 2 3 import ( 4 "context" 5 _ "embed" 6 7 "wa-lang.org/wazero" 8 "wa-lang.org/wazero/imports/emscripten" 9 "wa-lang.org/wazero/imports/wasi_snapshot_preview1" 10 ) 11 12 // This shows how to instantiate Emscripten function imports. 13 func Example_instantiate() { 14 ctx := context.Background() 15 16 r := wazero.NewRuntime(ctx) 17 defer r.Close(ctx) // This closes everything this Runtime created. 18 19 // Add WASI which is typically required when using Emscripten. 20 wasi_snapshot_preview1.MustInstantiate(ctx, r) 21 22 // Now, add the "env" module to the runtime, Emscripten default imports. 23 emscripten.MustInstantiate(ctx, r) 24 25 // Output: 26 } 27 28 // This shows how to instantiate Emscripten function imports when you also need 29 // other functions in the "env" module. 30 func Example_functionExporter() { 31 ctx := context.Background() 32 33 r := wazero.NewRuntime(ctx) 34 defer r.Close(ctx) // This closes everything this Runtime created. 35 36 // Add WASI which is typically required when using Emscripten. 37 wasi_snapshot_preview1.MustInstantiate(ctx, r) 38 39 // Next, construct your own module builder for "env" with any functions 40 // you need. 41 envBuilder := r.NewHostModuleBuilder("env"). 42 NewFunctionBuilder(). 43 WithFunc(func() uint32 { return 1 }). 44 Export("get_int") 45 46 // Now, add Emscripten special function imports into it. 47 emscripten.NewFunctionExporter().ExportFunctions(envBuilder) 48 49 // Output: 50 }