Skip to content

0.3.0

Compare
Choose a tag to compare
@Hywan Hywan released this 02 Mar 12:18
· 371 commits to master since this release

Added

  • Memory can be imported, thus making the distinction between owned and borrowed memory (#119 by @koponen-styra).

    The main new methods are Imports.AppendMemory, and NewMemory to create a new memory with bounds (in pages).

    The Imports.Append method is now deprecated in favor of Imports.AppendFunction.

    // Compile a WebAssembly module
    module, _ := wasm.Compile(wasmBytes)
    
    // Create a new memory (that's new!)
    memory, _ := wasm.NewMemory(/* min page */ 1, /* max page */ 2)
    
    // Create an import object
    imports := wasm.NewImports().Namespace("env").AppendMemory("memory", memory)
    importObject := wasm.NewImportObject()
    _ = importObject.Extend(*imports)
    
    instance, _ := module.InstantiateWithImportObject(importObject)
    defer instance.Close()
    
    // Now we can read or write the memory with `memory.Data()` as usual
  • Add Bazel files to build the project with Bazel (#108 by @joesonw)

  • Support multiple WASI version with WasiGetVersion, NewDefaultWasiImportObjectForVersion, NewWasiImportObjectForVersion and WasiVersion (#92 by @Hywan).

    Supported version are:

    • Latest,
    • Snapshot0,
    • Snapshot1,
    • Unknown (in case of error).
    // Compile a WebAssembly module
    module, _ = wasm.Compile(wasmBytes)
    
    // Read the WASI version required for this module
    wasiVersion = wasm.WasiGetVersion(module)
    
    // Create an import object
    importObject := wasm.NewDefaultWasiImportObjectForVersion(wasiVersion)
    
    // Extend the import object with the imports
    imports, _ := wasm.NewImports().Namespace("env").AppendFunction("sum", sum, C.sum)
    _ = importObject.Extend(*imports)
    
    // Instantiate the module with the import object
    instante, _ := module.InstantiateWithImportObject(importObject)
  • InstanceContext supports user data with any reference types or types that include any reference types or other Go pointers (#85 and #94 by @AdamSLevy)

    type logMessageContext struct {
        message string
        slice []string // that wasn't possible before
        ptr *string    // that wasn't possible before
    }
    
    str := "test"
    contextData = logMessageContext {
        message: "first",
        slice:   []string{str, str},
        ptr:     &str,
    }
    
    instance.SetContextData(&contextData)
  • WASI is now supported (#72 by @MarkMcCaskey)

    // Compile a WebAssembly module
    module, _ = wasm.Compile(wasmBytes)
    
    // Declare imports as usual
    imports, _ := wasm.NewImports().Namespace("env").AppendFunction("sum", sum, C.sum)
    
    // Create an import object
    importObject := wasm.NewDefaultWasiImportObject()
    
    // Extend the import object with the imports
    _ = importObject.Extend(*imports)
    
    // Instantiate the module with the import object
    instance, _ = wasm.InstantiateWithImportObject(importObject)
    defer instance.Close()
    
    // Run the module
    instance.Exports["_start"]()
  • Instance supports optional memory, i.e. a WebAssembly module that does not have an exported memory, and provides a new HasMemory method (#63 by @Hywan)

Changed

Fixed