module.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. package wamr
  6. // #include <wasm_export.h>
  7. import "C"
  8. import (
  9. "unsafe"
  10. "runtime"
  11. "fmt"
  12. )
  13. type Module struct {
  14. module C.wasm_module_t
  15. }
  16. /* Create WASM/AOT module from the memory buffer */
  17. func NewModule(wasmBytes []byte) (*Module, error) {
  18. if (wasmBytes == nil || len(wasmBytes) == 0) {
  19. return nil, fmt.Errorf("NewModule error: invalid input")
  20. }
  21. wasmPtr := (*C.uint8_t)(unsafe.Pointer(&wasmBytes[0]))
  22. wasmLen := C.uint(len(wasmBytes))
  23. errorBytes := make([]byte, 128)
  24. errorPtr := (*C.char)(unsafe.Pointer(&errorBytes[0]))
  25. errorLen := C.uint(len(errorBytes))
  26. m := C.wasm_runtime_load(wasmPtr, wasmLen, errorPtr, errorLen)
  27. if (m == nil) {
  28. return nil, fmt.Errorf("NewModule error: %s", string(errorBytes))
  29. }
  30. self := &Module{
  31. module: m,
  32. }
  33. runtime.SetFinalizer(self, func(self *Module) {
  34. self.Destroy()
  35. })
  36. return self, nil
  37. }
  38. /* Destroy the module */
  39. func (self *Module) Destroy() {
  40. runtime.SetFinalizer(self, nil)
  41. if (self.module != nil) {
  42. C.wasm_runtime_unload(self.module)
  43. }
  44. }
  45. /* Set module's wasi arguments */
  46. func (self *Module) SetWasiArgs(dirList [][]byte, mapDirList [][]byte,
  47. env [][]byte, argv[][]byte) {
  48. var dirPtr, mapDirPtr, envPtr, argvPtr **C.char
  49. var dirCount, mapDirCount, envCount C.uint
  50. var argc C.int
  51. if (dirList != nil) {
  52. dirPtr = (**C.char)(unsafe.Pointer(&dirList[0]))
  53. dirCount = C.uint(len(dirList))
  54. }
  55. if (mapDirList != nil) {
  56. mapDirPtr = (**C.char)(unsafe.Pointer(&mapDirList[0]))
  57. mapDirCount = C.uint(len(mapDirList))
  58. }
  59. if (env != nil) {
  60. envPtr = (**C.char)(unsafe.Pointer(&env[0]))
  61. envCount = C.uint(len(env))
  62. }
  63. if (argv != nil) {
  64. argvPtr = (**C.char)(unsafe.Pointer(&argv[0]))
  65. argc = C.int(len(argv))
  66. }
  67. C.wasm_runtime_set_wasi_args(self.module, dirPtr, dirCount,
  68. mapDirPtr, mapDirCount,
  69. envPtr, envCount, argvPtr, argc)
  70. }
  71. /* Set module's wasi arguments */
  72. func (self *Module) SetWasiArgsEx(dirList [][]byte, mapDirList [][]byte,
  73. env [][]byte, argv[][]byte,
  74. stdinfd int, stdoutfd int, stderrfd int) {
  75. var dirPtr, mapDirPtr, envPtr, argvPtr **C.char
  76. var dirCount, mapDirCount, envCount C.uint
  77. var argc C.int
  78. if (dirList != nil) {
  79. dirPtr = (**C.char)(unsafe.Pointer(&dirList[0]))
  80. dirCount = C.uint(len(dirList))
  81. }
  82. if (mapDirList != nil) {
  83. mapDirPtr = (**C.char)(unsafe.Pointer(&mapDirList[0]))
  84. mapDirCount = C.uint(len(mapDirList))
  85. }
  86. if (env != nil) {
  87. envPtr = (**C.char)(unsafe.Pointer(&env[0]))
  88. envCount = C.uint(len(env))
  89. }
  90. if (argv != nil) {
  91. argvPtr = (**C.char)(unsafe.Pointer(&argv[0]))
  92. argc = C.int(len(argv))
  93. }
  94. C.wasm_runtime_set_wasi_args_ex(self.module, dirPtr, dirCount,
  95. mapDirPtr, mapDirCount,
  96. envPtr, envCount, argvPtr, argc,
  97. C.int(stdinfd), C.int(stdoutfd),
  98. C.int(stderrfd))
  99. }
  100. /* Set module's wasi network address pool */
  101. func (self *Module) SetWasiAddrPool(addrPool [][]byte) {
  102. var addrPoolPtr **C.char
  103. var addrPoolSize C.uint
  104. if (addrPool != nil) {
  105. addrPoolPtr = (**C.char)(unsafe.Pointer(&addrPool[0]))
  106. addrPoolSize = C.uint(len(addrPool))
  107. }
  108. C.wasm_runtime_set_wasi_addr_pool(self.module, addrPoolPtr, addrPoolSize)
  109. }
  110. /* Set module's wasi domain lookup pool */
  111. func(self *Module) SetWasiNsLookupPool(nsLookupPool [][]byte) {
  112. var nsLookupPoolPtr **C.char
  113. var nsLookupPoolSize C.uint
  114. if (nsLookupPool != nil) {
  115. nsLookupPoolPtr = (**C.char)(unsafe.Pointer(&nsLookupPool[0]))
  116. nsLookupPoolSize = C.uint(len(nsLookupPool))
  117. }
  118. C.wasm_runtime_set_wasi_ns_lookup_pool(self.module, nsLookupPoolPtr, nsLookupPoolSize)
  119. }