github.com/tetratelabs/wazero@v1.2.1/api/wasm.go (about) 1 // Package api includes constants and interfaces used by both end-users and internal implementations. 2 package api 3 4 import ( 5 "context" 6 "fmt" 7 "math" 8 9 "github.com/tetratelabs/wazero/internal/internalapi" 10 ) 11 12 // ExternType classifies imports and exports with their respective types. 13 // 14 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#external-types%E2%91%A0 15 type ExternType = byte 16 17 const ( 18 ExternTypeFunc ExternType = 0x00 19 ExternTypeTable ExternType = 0x01 20 ExternTypeMemory ExternType = 0x02 21 ExternTypeGlobal ExternType = 0x03 22 ) 23 24 // The below are exported to consolidate parsing behavior for external types. 25 const ( 26 // ExternTypeFuncName is the name of the WebAssembly 1.0 (20191205) Text Format field for ExternTypeFunc. 27 ExternTypeFuncName = "func" 28 // ExternTypeTableName is the name of the WebAssembly 1.0 (20191205) Text Format field for ExternTypeTable. 29 ExternTypeTableName = "table" 30 // ExternTypeMemoryName is the name of the WebAssembly 1.0 (20191205) Text Format field for ExternTypeMemory. 31 ExternTypeMemoryName = "memory" 32 // ExternTypeGlobalName is the name of the WebAssembly 1.0 (20191205) Text Format field for ExternTypeGlobal. 33 ExternTypeGlobalName = "global" 34 ) 35 36 // ExternTypeName returns the name of the WebAssembly 1.0 (20191205) Text Format field of the given type. 37 // 38 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#exports%E2%91%A4 39 func ExternTypeName(et ExternType) string { 40 switch et { 41 case ExternTypeFunc: 42 return ExternTypeFuncName 43 case ExternTypeTable: 44 return ExternTypeTableName 45 case ExternTypeMemory: 46 return ExternTypeMemoryName 47 case ExternTypeGlobal: 48 return ExternTypeGlobalName 49 } 50 return fmt.Sprintf("%#x", et) 51 } 52 53 // ValueType describes a parameter or result type mapped to a WebAssembly 54 // function signature. 55 // 56 // The following describes how to convert between Wasm and Golang types: 57 // 58 // - ValueTypeI32 - EncodeU32 DecodeU32 for uint32 / EncodeI32 DecodeI32 for int32 59 // - ValueTypeI64 - uint64(int64) 60 // - ValueTypeF32 - EncodeF32 DecodeF32 from float32 61 // - ValueTypeF64 - EncodeF64 DecodeF64 from float64 62 // - ValueTypeExternref - unintptr(unsafe.Pointer(p)) where p is any pointer 63 // type in Go (e.g. *string) 64 // 65 // e.g. Given a Text Format type use (param i64) (result i64), no conversion is 66 // necessary. 67 // 68 // results, _ := fn(ctx, input) 69 // result := result[0] 70 // 71 // e.g. Given a Text Format type use (param f64) (result f64), conversion is 72 // necessary. 73 // 74 // results, _ := fn(ctx, api.EncodeF64(input)) 75 // result := api.DecodeF64(result[0]) 76 // 77 // Note: This is a type alias as it is easier to encode and decode in the 78 // binary format. 79 // 80 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-valtype 81 type ValueType = byte 82 83 const ( 84 // ValueTypeI32 is a 32-bit integer. 85 ValueTypeI32 ValueType = 0x7f 86 // ValueTypeI64 is a 64-bit integer. 87 ValueTypeI64 ValueType = 0x7e 88 // ValueTypeF32 is a 32-bit floating point number. 89 ValueTypeF32 ValueType = 0x7d 90 // ValueTypeF64 is a 64-bit floating point number. 91 ValueTypeF64 ValueType = 0x7c 92 93 // ValueTypeExternref is a externref type. 94 // 95 // Note: in wazero, externref type value are opaque raw 64-bit pointers, 96 // and the ValueTypeExternref type in the signature will be translated as 97 // uintptr in wazero's API level. 98 // 99 // For example, given the import function: 100 // (func (import "env" "f") (param externref) (result externref)) 101 // 102 // This can be defined in Go as: 103 // r.NewHostModuleBuilder("env"). 104 // NewFunctionBuilder(). 105 // WithFunc(func(context.Context, _ uintptr) (_ uintptr) { return }). 106 // Export("f") 107 // 108 // Note: The usage of this type is toggled with api.CoreFeatureBulkMemoryOperations. 109 ValueTypeExternref ValueType = 0x6f 110 ) 111 112 // ValueTypeName returns the type name of the given ValueType as a string. 113 // These type names match the names used in the WebAssembly text format. 114 // 115 // Note: This returns "unknown", if an undefined ValueType value is passed. 116 func ValueTypeName(t ValueType) string { 117 switch t { 118 case ValueTypeI32: 119 return "i32" 120 case ValueTypeI64: 121 return "i64" 122 case ValueTypeF32: 123 return "f32" 124 case ValueTypeF64: 125 return "f64" 126 case ValueTypeExternref: 127 return "externref" 128 } 129 return "unknown" 130 } 131 132 // Module is a sandboxed, ready to execute Wasm module. This can be used to get exported functions, etc. 133 // 134 // In WebAssembly terminology, this corresponds to a "Module Instance", but wazero calls pre-instantiation module as 135 // "Compiled Module" as in wazero.CompiledModule, therefore we call this post-instantiation module simply "Module". 136 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#module-instances%E2%91%A0 137 // 138 // # Notes 139 // 140 // - This is an interface for decoupling, not third-party implementations. 141 // All implementations are in wazero. 142 // - Closing the wazero.Runtime closes any Module it instantiated. 143 type Module interface { 144 fmt.Stringer 145 146 // Name is the name this module was instantiated with. Exported functions can be imported with this name. 147 Name() string 148 149 // Memory returns a memory defined in this module or nil if there are none wasn't. 150 Memory() Memory 151 152 // ExportedFunction returns a function exported from this module or nil if it wasn't. 153 ExportedFunction(name string) Function 154 155 // ExportedFunctionDefinitions returns all the exported function 156 // definitions in this module, keyed on export name. 157 ExportedFunctionDefinitions() map[string]FunctionDefinition 158 159 // TODO: Table 160 161 // ExportedMemory returns a memory exported from this module or nil if it wasn't. 162 // 163 // WASI modules require exporting a Memory named "memory". This means that a module successfully initialized 164 // as a WASI Command or Reactor will never return nil for this name. 165 // 166 // See https://github.com/WebAssembly/WASI/blob/snapshot-01/design/application-abi.md#current-unstable-abi 167 ExportedMemory(name string) Memory 168 169 // ExportedMemoryDefinitions returns all the exported memory definitions 170 // in this module, keyed on export name. 171 // 172 // Note: As of WebAssembly Core Specification 2.0, there can be at most one 173 // memory. 174 ExportedMemoryDefinitions() map[string]MemoryDefinition 175 176 // ExportedGlobal a global exported from this module or nil if it wasn't. 177 ExportedGlobal(name string) Global 178 179 // CloseWithExitCode releases resources allocated for this Module. Use a non-zero exitCode parameter to indicate a 180 // failure to ExportedFunction callers. 181 // 182 // The error returned here, if present, is about resource de-allocation (such as I/O errors). Only the last error is 183 // returned, so a non-nil return means at least one error happened. Regardless of error, this Module will 184 // be removed, making its name available again. 185 // 186 // Calling this inside a host function is safe, and may cause ExportedFunction callers to receive a sys.ExitError 187 // with the exitCode. 188 CloseWithExitCode(ctx context.Context, exitCode uint32) error 189 190 // Closer closes this module by delegating to CloseWithExitCode with an exit code of zero. 191 Closer 192 193 internalapi.WazeroOnly 194 } 195 196 // Closer closes a resource. 197 // 198 // # Notes 199 // 200 // - This is an interface for decoupling, not third-party implementations. 201 // All implementations are in wazero. 202 type Closer interface { 203 // Close closes the resource. 204 // 205 // Note: The context parameter is used for value lookup, such as for 206 // logging. A canceled or otherwise done context will not prevent Close 207 // from succeeding. 208 Close(context.Context) error 209 } 210 211 // ExportDefinition is a WebAssembly type exported in a module 212 // (wazero.CompiledModule). 213 // 214 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#exports%E2%91%A0 215 // 216 // # Notes 217 // 218 // - This is an interface for decoupling, not third-party implementations. 219 // All implementations are in wazero. 220 type ExportDefinition interface { 221 // ModuleName is the possibly empty name of the module defining this 222 // export. 223 // 224 // Note: This may be different from Module.Name, because a compiled module 225 // can be instantiated multiple times as different names. 226 ModuleName() string 227 228 // Index is the position in the module's index, imports first. 229 Index() uint32 230 231 // Import returns true with the module and name when this was imported. 232 // Otherwise, it returns false. 233 // 234 // Note: Empty string is valid for both names in the WebAssembly Core 235 // Specification, so "" "" is possible. 236 Import() (moduleName, name string, isImport bool) 237 238 // ExportNames include all exported names. 239 // 240 // Note: The empty name is allowed in the WebAssembly Core Specification, 241 // so "" is possible. 242 ExportNames() []string 243 244 internalapi.WazeroOnly 245 } 246 247 // MemoryDefinition is a WebAssembly memory exported in a module 248 // (wazero.CompiledModule). Units are in pages (64KB). 249 // 250 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#exports%E2%91%A0 251 // 252 // # Notes 253 // 254 // - This is an interface for decoupling, not third-party implementations. 255 // All implementations are in wazero. 256 type MemoryDefinition interface { 257 ExportDefinition 258 259 // Min returns the possibly zero initial count of 64KB pages. 260 Min() uint32 261 262 // Max returns the possibly zero max count of 64KB pages, or false if 263 // unbounded. 264 Max() (uint32, bool) 265 266 internalapi.WazeroOnly 267 } 268 269 // FunctionDefinition is a WebAssembly function exported in a module 270 // (wazero.CompiledModule). 271 // 272 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#exports%E2%91%A0 273 // 274 // # Notes 275 // 276 // - This is an interface for decoupling, not third-party implementations. 277 // All implementations are in wazero. 278 type FunctionDefinition interface { 279 ExportDefinition 280 281 // Name is the module-defined name of the function, which is not necessarily 282 // the same as its export name. 283 Name() string 284 285 // DebugName identifies this function based on its Index or Name in the 286 // module. This is used for errors and stack traces. e.g. "env.abort". 287 // 288 // When the function name is empty, a substitute name is generated by 289 // prefixing '$' to its position in the index. Ex ".$0" is the 290 // first function (possibly imported) in an unnamed module. 291 // 292 // The format is dot-delimited module and function name, but there are no 293 // restrictions on the module and function name. This means either can be 294 // empty or include dots. e.g. "x.x.x" could mean module "x" and name "x.x", 295 // or it could mean module "x.x" and name "x". 296 // 297 // Note: This name is stable regardless of import or export. For example, 298 // if Import returns true, the value is still based on the Name or Index 299 // and not the imported function name. 300 DebugName() string 301 302 // GoFunction is non-nil when implemented by the embedder instead of a wasm 303 // binary, e.g. via wazero.HostModuleBuilder 304 // 305 // The expected results are nil, GoFunction or GoModuleFunction. 306 GoFunction() interface{} 307 308 // ParamTypes are the possibly empty sequence of value types accepted by a 309 // function with this signature. 310 // 311 // See ValueType documentation for encoding rules. 312 ParamTypes() []ValueType 313 314 // ParamNames are index-correlated with ParamTypes or nil if not available 315 // for one or more parameters. 316 ParamNames() []string 317 318 // ResultTypes are the results of the function. 319 // 320 // When WebAssembly 1.0 (20191205), there can be at most one result. 321 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#result-types%E2%91%A0 322 // 323 // See ValueType documentation for encoding rules. 324 ResultTypes() []ValueType 325 326 // ResultNames are index-correlated with ResultTypes or nil if not 327 // available for one or more results. 328 ResultNames() []string 329 330 internalapi.WazeroOnly 331 } 332 333 // Function is a WebAssembly function exported from an instantiated module 334 // (wazero.Runtime InstantiateModule). 335 // 336 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#syntax-func 337 // 338 // # Notes 339 // 340 // - This is an interface for decoupling, not third-party implementations. 341 // All implementations are in wazero. 342 type Function interface { 343 // Definition is metadata about this function from its defining module. 344 Definition() FunctionDefinition 345 346 // Call invokes the function with the given parameters and returns any 347 // results or an error for any failure looking up or invoking the function. 348 // 349 // Encoding is described in Definition, and supplying an incorrect count of 350 // parameters vs FunctionDefinition.ParamTypes is an error. 351 // 352 // If the exporting Module was closed during this call, the error returned 353 // may be a sys.ExitError. See Module.CloseWithExitCode for details. 354 // 355 // Call is not goroutine-safe, therefore it is recommended to create 356 // another Function if you want to invoke the same function concurrently. 357 // On the other hand, sequential invocations of Call is allowed. 358 // 359 // To safely encode/decode params/results expressed as uint64, users are encouraged to 360 // use api.EncodeXXX or DecodeXXX functions. See the docs on api.ValueType. 361 // 362 // When RuntimeConfig.WithCloseOnContextDone is toggled, the invocation of this Call method is ensured to be closed 363 // whenever one of the three conditions is met. In the event of close, sys.ExitError will be returned and 364 // the api.Module from which this api.Function is derived will be made closed. See the documentation of 365 // WithCloseOnContextDone on wazero.RuntimeConfig for detail. See examples in context_done_example_test.go for 366 // the end-to-end demonstrations of how these terminations can be performed. 367 Call(ctx context.Context, params ...uint64) ([]uint64, error) 368 369 // CallWithStack is an optimized variation of Call that saves memory 370 // allocations when the stack slice is reused across calls. 371 // 372 // Stack length must be at least the max of parameter or result length. 373 // The caller adds parameters in order to the stack, and reads any results 374 // in order from the stack, except in the error case. 375 // 376 // For example, the following reuses the same stack slice to call searchFn 377 // repeatedly saving one allocation per iteration: 378 // 379 // stack := make([]uint64, 4) 380 // for i, search := range searchParams { 381 // // copy the next params to the stack 382 // copy(stack, search) 383 // if err := searchFn.CallWithStack(ctx, stack); err != nil { 384 // return err 385 // } else if stack[0] == 1 { // found 386 // return i // searchParams[i] matched! 387 // } 388 // } 389 // 390 // # Notes 391 // 392 // - This is similar to GoModuleFunction, except for using calling functions 393 // instead of implementing them. Moreover, this is used regardless of 394 // whether the callee is a host or wasm defined function. 395 CallWithStack(ctx context.Context, stack []uint64) error 396 397 internalapi.WazeroOnly 398 } 399 400 // GoModuleFunction is a Function implemented in Go instead of a wasm binary. 401 // The Module parameter is the calling module, used to access memory or 402 // exported functions. See GoModuleFunc for an example. 403 // 404 // The stack is includes any parameters encoded according to their ValueType. 405 // Its length is the max of parameter or result length. When there are results, 406 // write them in order beginning at index zero. Do not use the stack after the 407 // function returns. 408 // 409 // Here's a typical way to read three parameters and write back one. 410 // 411 // // read parameters off the stack in index order 412 // argv, argvBuf := api.DecodeU32(stack[0]), api.DecodeU32(stack[1]) 413 // 414 // // write results back to the stack in index order 415 // stack[0] = api.EncodeU32(ErrnoSuccess) 416 // 417 // This function can be non-deterministic or cause side effects. It also 418 // has special properties not defined in the WebAssembly Core specification. 419 // Notably, this uses the caller's memory (via Module.Memory). See 420 // https://www.w3.org/TR/wasm-core-1/#host-functions%E2%91%A0 421 // 422 // Most end users will not define functions directly with this, as they will 423 // use reflection or code generators instead. These approaches are more 424 // idiomatic as they can map go types to ValueType. This type is exposed for 425 // those willing to trade usability and safety for performance. 426 // 427 // To safely decode/encode values from/to the uint64 stack, users are encouraged to use 428 // api.EncodeXXX or api.DecodeXXX functions. See the docs on api.ValueType. 429 type GoModuleFunction interface { 430 Call(ctx context.Context, mod Module, stack []uint64) 431 } 432 433 // GoModuleFunc is a convenience for defining an inlined function. 434 // 435 // For example, the following returns an uint32 value read from parameter zero: 436 // 437 // api.GoModuleFunc(func(ctx context.Context, mod api.Module, stack []uint64) { 438 // offset := api.DecodeU32(stack[0]) // read the parameter from the stack 439 // 440 // ret, ok := mod.Memory().ReadUint32Le(offset) 441 // if !ok { 442 // panic("out of memory") 443 // } 444 // 445 // stack[0] = api.EncodeU32(ret) // add the result back to the stack. 446 // }) 447 type GoModuleFunc func(ctx context.Context, mod Module, stack []uint64) 448 449 // Call implements GoModuleFunction.Call. 450 func (f GoModuleFunc) Call(ctx context.Context, mod Module, stack []uint64) { 451 f(ctx, mod, stack) 452 } 453 454 // GoFunction is an optimized form of GoModuleFunction which doesn't require 455 // the Module parameter. See GoFunc for an example. 456 // 457 // For example, this function does not need to use the importing module's 458 // memory or exported functions. 459 type GoFunction interface { 460 Call(ctx context.Context, stack []uint64) 461 } 462 463 // GoFunc is a convenience for defining an inlined function. 464 // 465 // For example, the following returns the sum of two uint32 parameters: 466 // 467 // api.GoFunc(func(ctx context.Context, stack []uint64) { 468 // x, y := api.DecodeU32(stack[0]), api.DecodeU32(stack[1]) 469 // stack[0] = api.EncodeU32(x + y) 470 // }) 471 type GoFunc func(ctx context.Context, stack []uint64) 472 473 // Call implements GoFunction.Call. 474 func (f GoFunc) Call(ctx context.Context, stack []uint64) { 475 f(ctx, stack) 476 } 477 478 // Global is a WebAssembly 1.0 (20191205) global exported from an instantiated module (wazero.Runtime InstantiateModule). 479 // 480 // For example, if the value is not mutable, you can read it once: 481 // 482 // offset := module.ExportedGlobal("memory.offset").Get() 483 // 484 // Globals are allowed by specification to be mutable. However, this can be disabled by configuration. When in doubt, 485 // safe cast to find out if the value can change. Here's an example: 486 // 487 // offset := module.ExportedGlobal("memory.offset") 488 // if _, ok := offset.(api.MutableGlobal); ok { 489 // // value can change 490 // } else { 491 // // value is constant 492 // } 493 // 494 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#globals%E2%91%A0 495 // 496 // # Notes 497 // 498 // - This is an interface for decoupling, not third-party implementations. 499 // All implementations are in wazero. 500 type Global interface { 501 fmt.Stringer 502 503 // Type describes the numeric type of the global. 504 Type() ValueType 505 506 // Get returns the last known value of this global. 507 // 508 // See Type for how to decode this value to a Go type. 509 Get() uint64 510 } 511 512 // MutableGlobal is a Global whose value can be updated at runtime (variable). 513 // 514 // # Notes 515 // 516 // - This is an interface for decoupling, not third-party implementations. 517 // All implementations are in wazero. 518 type MutableGlobal interface { 519 Global 520 521 // Set updates the value of this global. 522 // 523 // See Global.Type for how to encode this value from a Go type. 524 Set(v uint64) 525 526 internalapi.WazeroOnly 527 } 528 529 // Memory allows restricted access to a module's memory. Notably, this does not allow growing. 530 // 531 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#storage%E2%91%A0 532 // 533 // # Notes 534 // 535 // - This is an interface for decoupling, not third-party implementations. 536 // All implementations are in wazero. 537 // - This includes all value types available in WebAssembly 1.0 (20191205) and all are encoded little-endian. 538 type Memory interface { 539 // Definition is metadata about this memory from its defining module. 540 Definition() MemoryDefinition 541 542 // Size returns the size in bytes available. e.g. If the underlying memory 543 // has 1 page: 65536 544 // 545 // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#-hrefsyntax-instr-memorymathsfmemorysize%E2%91%A0 546 Size() uint32 547 548 // Grow increases memory by the delta in pages (65536 bytes per page). 549 // The return val is the previous memory size in pages, or false if the 550 // delta was ignored as it exceeds MemoryDefinition.Max. 551 // 552 // # Notes 553 // 554 // - This is the same as the "memory.grow" instruction defined in the 555 // WebAssembly Core Specification, except returns false instead of -1. 556 // - When this returns true, any shared views via Read must be refreshed. 557 // 558 // See MemorySizer Read and https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#grow-mem 559 Grow(deltaPages uint32) (previousPages uint32, ok bool) 560 561 // ReadByte reads a single byte from the underlying buffer at the offset or returns false if out of range. 562 ReadByte(offset uint32) (byte, bool) 563 564 // ReadUint16Le reads a uint16 in little-endian encoding from the underlying buffer at the offset in or returns 565 // false if out of range. 566 ReadUint16Le(offset uint32) (uint16, bool) 567 568 // ReadUint32Le reads a uint32 in little-endian encoding from the underlying buffer at the offset in or returns 569 // false if out of range. 570 ReadUint32Le(offset uint32) (uint32, bool) 571 572 // ReadFloat32Le reads a float32 from 32 IEEE 754 little-endian encoded bits in the underlying buffer at the offset 573 // or returns false if out of range. 574 // See math.Float32bits 575 ReadFloat32Le(offset uint32) (float32, bool) 576 577 // ReadUint64Le reads a uint64 in little-endian encoding from the underlying buffer at the offset or returns false 578 // if out of range. 579 ReadUint64Le(offset uint32) (uint64, bool) 580 581 // ReadFloat64Le reads a float64 from 64 IEEE 754 little-endian encoded bits in the underlying buffer at the offset 582 // or returns false if out of range. 583 // 584 // See math.Float64bits 585 ReadFloat64Le(offset uint32) (float64, bool) 586 587 // Read reads byteCount bytes from the underlying buffer at the offset or 588 // returns false if out of range. 589 // 590 // For example, to search for a NUL-terminated string: 591 // buf, _ = memory.Read(offset, byteCount) 592 // n := bytes.IndexByte(buf, 0) 593 // if n < 0 { 594 // // Not found! 595 // } 596 // 597 // Write-through 598 // 599 // This returns a view of the underlying memory, not a copy. This means any 600 // writes to the slice returned are visible to Wasm, and any updates from 601 // Wasm are visible reading the returned slice. 602 // 603 // For example: 604 // buf, _ = memory.Read(offset, byteCount) 605 // buf[1] = 'a' // writes through to memory, meaning Wasm code see 'a'. 606 // 607 // If you don't intend-write through, make a copy of the returned slice. 608 // 609 // When to refresh Read 610 // 611 // The returned slice disconnects on any capacity change. For example, 612 // `buf = append(buf, 'a')` might result in a slice that is no longer 613 // shared. The same exists Wasm side. For example, if Wasm changes its 614 // memory capacity, ex via "memory.grow"), the host slice is no longer 615 // shared. Those who need a stable view must set Wasm memory min=max, or 616 // use wazero.RuntimeConfig WithMemoryCapacityPages to ensure max is always 617 // allocated. 618 Read(offset, byteCount uint32) ([]byte, bool) 619 620 // WriteByte writes a single byte to the underlying buffer at the offset in or returns false if out of range. 621 WriteByte(offset uint32, v byte) bool 622 623 // WriteUint16Le writes the value in little-endian encoding to the underlying buffer at the offset in or returns 624 // false if out of range. 625 WriteUint16Le(offset uint32, v uint16) bool 626 627 // WriteUint32Le writes the value in little-endian encoding to the underlying buffer at the offset in or returns 628 // false if out of range. 629 WriteUint32Le(offset, v uint32) bool 630 631 // WriteFloat32Le writes the value in 32 IEEE 754 little-endian encoded bits to the underlying buffer at the offset 632 // or returns false if out of range. 633 // 634 // See math.Float32bits 635 WriteFloat32Le(offset uint32, v float32) bool 636 637 // WriteUint64Le writes the value in little-endian encoding to the underlying buffer at the offset in or returns 638 // false if out of range. 639 WriteUint64Le(offset uint32, v uint64) bool 640 641 // WriteFloat64Le writes the value in 64 IEEE 754 little-endian encoded bits to the underlying buffer at the offset 642 // or returns false if out of range. 643 // 644 // See math.Float64bits 645 WriteFloat64Le(offset uint32, v float64) bool 646 647 // Write writes the slice to the underlying buffer at the offset or returns false if out of range. 648 Write(offset uint32, v []byte) bool 649 650 // WriteString writes the string to the underlying buffer at the offset or returns false if out of range. 651 WriteString(offset uint32, v string) bool 652 653 internalapi.WazeroOnly 654 } 655 656 // CustomSection contains the name and raw data of a custom section. 657 // 658 // # Notes 659 // 660 // - This is an interface for decoupling, not third-party implementations. 661 // All implementations are in wazero. 662 type CustomSection interface { 663 // Name is the name of the custom section 664 Name() string 665 // Data is the raw data of the custom section 666 Data() []byte 667 668 internalapi.WazeroOnly 669 } 670 671 // EncodeExternref encodes the input as a ValueTypeExternref. 672 // 673 // See DecodeExternref 674 func EncodeExternref(input uintptr) uint64 { 675 return uint64(input) 676 } 677 678 // DecodeExternref decodes the input as a ValueTypeExternref. 679 // 680 // See EncodeExternref 681 func DecodeExternref(input uint64) uintptr { 682 return uintptr(input) 683 } 684 685 // EncodeI32 encodes the input as a ValueTypeI32. 686 func EncodeI32(input int32) uint64 { 687 return uint64(uint32(input)) 688 } 689 690 // DecodeI32 decodes the input as a ValueTypeI32. 691 func DecodeI32(input uint64) int32 { 692 return int32(input) 693 } 694 695 // EncodeU32 encodes the input as a ValueTypeI32. 696 func EncodeU32(input uint32) uint64 { 697 return uint64(input) 698 } 699 700 // DecodeU32 decodes the input as a ValueTypeI32. 701 func DecodeU32(input uint64) uint32 { 702 return uint32(input) 703 } 704 705 // EncodeI64 encodes the input as a ValueTypeI64. 706 func EncodeI64(input int64) uint64 { 707 return uint64(input) 708 } 709 710 // EncodeF32 encodes the input as a ValueTypeF32. 711 // 712 // See DecodeF32 713 func EncodeF32(input float32) uint64 { 714 return uint64(math.Float32bits(input)) 715 } 716 717 // DecodeF32 decodes the input as a ValueTypeF32. 718 // 719 // See EncodeF32 720 func DecodeF32(input uint64) float32 { 721 return math.Float32frombits(uint32(input)) 722 } 723 724 // EncodeF64 encodes the input as a ValueTypeF64. 725 // 726 // See EncodeF32 727 func EncodeF64(input float64) uint64 { 728 return math.Float64bits(input) 729 } 730 731 // DecodeF64 decodes the input as a ValueTypeF64. 732 // 733 // See EncodeF64 734 func DecodeF64(input uint64) float64 { 735 return math.Float64frombits(input) 736 }