runtime.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. /*
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <wasm_export.h>
  10. void
  11. bh_log_set_verbose_level(uint32_t level);
  12. bool
  13. init_wamr_runtime(bool alloc_with_pool, uint8_t *heap_buf,
  14. uint32_t heap_size, uint32_t max_thread_num)
  15. {
  16. RuntimeInitArgs init_args;
  17. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  18. if (alloc_with_pool) {
  19. init_args.mem_alloc_type = Alloc_With_Pool;
  20. init_args.mem_alloc_option.pool.heap_buf = heap_buf;
  21. init_args.mem_alloc_option.pool.heap_size = heap_size;
  22. }
  23. else {
  24. init_args.mem_alloc_type = Alloc_With_System_Allocator;
  25. }
  26. return wasm_runtime_full_init(&init_args);
  27. }
  28. */
  29. import "C"
  30. import (
  31. "fmt"
  32. "unsafe"
  33. )
  34. type LogLevel uint32
  35. const (
  36. LOG_LEVEL_FATAL LogLevel = 0
  37. LOG_LEVEL_ERROR LogLevel = 1
  38. LOG_LEVEL_WARNING LogLevel = 2
  39. LOG_LEVEL_DEBUG LogLevel = 3
  40. LOG_LEVEL_VERBOSE LogLevel = 4
  41. )
  42. /*
  43. type NativeSymbol struct {
  44. symbol string
  45. func_ptr *uint8
  46. signature string
  47. }
  48. */
  49. type _Runtime struct {
  50. initialized bool
  51. }
  52. var _runtime_singleton *_Runtime
  53. /* Return the runtime singleton */
  54. func Runtime() *_Runtime {
  55. if (_runtime_singleton == nil) {
  56. self := &_Runtime{}
  57. _runtime_singleton = self
  58. }
  59. return _runtime_singleton;
  60. }
  61. /* Initialize the WASM runtime environment */
  62. func (self *_Runtime) FullInit(alloc_with_pool bool, heap_buf []byte,
  63. max_thread_num uint) error {
  64. var heap_buf_C *C.uchar
  65. if (self.initialized) {
  66. return nil
  67. }
  68. if (alloc_with_pool) {
  69. if (heap_buf == nil) {
  70. return fmt.Errorf("Failed to init WAMR runtime")
  71. }
  72. heap_buf_C = (*C.uchar)(unsafe.Pointer(&heap_buf[0]))
  73. }
  74. if (!C.init_wamr_runtime((C.bool)(alloc_with_pool), heap_buf_C,
  75. (C.uint)(len(heap_buf)),
  76. (C.uint)(max_thread_num))) {
  77. return fmt.Errorf("Failed to init WAMR runtime")
  78. }
  79. self.initialized = true
  80. return nil
  81. }
  82. /* Initialize the WASM runtime environment */
  83. func (self *_Runtime) Init() error {
  84. return self.FullInit(false, nil, 1)
  85. }
  86. /* Destroy the WASM runtime environment */
  87. func (self *_Runtime) Destroy() {
  88. if (self.initialized) {
  89. C.wasm_runtime_destroy()
  90. self.initialized = false
  91. }
  92. }
  93. /* Set log verbose level (0 to 5, default is 2),
  94. larger level with more log */
  95. func (self *_Runtime) SetLogLevel(level LogLevel) {
  96. C.bh_log_set_verbose_level(C.uint32_t(level))
  97. }
  98. /*
  99. func (self *_Runtime) RegisterNatives(moduleName string,
  100. nativeSymbols []NativeSymbol) {
  101. }
  102. */ /* TODO */
  103. func (self *_Runtime) InitThreadEnv() bool {
  104. if (!C.wasm_runtime_init_thread_env()) {
  105. return false
  106. }
  107. return true
  108. }
  109. func (self *_Runtime) DestroyThreadEnv() {
  110. C.wasm_runtime_destroy_thread_env();
  111. }
  112. func (self *_Runtime) ThreadEnvInited() bool {
  113. if (!C.wasm_runtime_thread_env_inited()) {
  114. return false
  115. }
  116. return true
  117. }
  118. /* Allocate memory from runtime memory environment */
  119. func (self *_Runtime) Malloc(size uint32) *uint8 {
  120. ptr := C.wasm_runtime_malloc((C.uint32_t)(size))
  121. return (*uint8)(ptr)
  122. }
  123. /* Free memory to runtime memory environment */
  124. func (self *_Runtime) Free(ptr *uint8) {
  125. C.wasm_runtime_free((unsafe.Pointer)(ptr))
  126. }