gc_stack_wasm.mx raw

   1  //go:build (gc.conservative || gc.custom || gc.precise || gc.boehm) && moxie.wasm
   2  
   3  package task
   4  
   5  import "unsafe"
   6  
   7  // gcData holds GC-relevant state for WASM tasks.
   8  type gcData struct {
   9  	stackTop uintptr
  10  }
  11  
  12  func (gcd *gcData) swap() {
  13  	// On WASM, the stack is managed by the engine. Swap the stack top
  14  	// pointer so the GC can scan the correct stack.
  15  	gcd.stackTop, stackTop = stackTop, gcd.stackTop
  16  }
  17  
  18  // stackTop is the current stack top for GC scanning.
  19  var stackTop uintptr
  20  
  21  //go:export tinygo_scanCurrentStack
  22  func scanCurrentStack() {
  23  	// Mark from the current stack pointer to the stack top.
  24  	scanstack(stackTop)
  25  }
  26  
  27  //go:linkname scanstack runtime.scanstack
  28  func scanstack(uintptr)
  29  
  30  //go:export tinygo_getStackTop
  31  func getStackTop() uintptr {
  32  	return stackTop
  33  }
  34  
  35  // SetStackTop is called by the runtime to set the top of the stack for GC.
  36  //
  37  //go:linkname SetStackTop runtime.setStackTop
  38  func SetStackTop(top unsafe.Pointer) {
  39  	stackTop = uintptr(top)
  40  }
  41