dispatch.mx raw

   1  //go:build wasm
   2  //:build wasm
   3  
   4  package callback
   5  
   6  import "unsafe"
   7  
   8  //:wasmexport __cb0
   9  func Dispatch0(id int32) {
  10  	fn, ok := lookup(id)
  11  	if !ok {
  12  		return
  13  	}
  14  	if f, ok := fn.(func()); ok {
  15  		f()
  16  	}
  17  }
  18  
  19  //:wasmexport __cbs
  20  func DispatchS(id int32, ptr *byte, length int32) {
  21  	fn, ok := lookup(id)
  22  	if !ok {
  23  		return
  24  	}
  25  	s := ptrToString(ptr, length)
  26  	if f, ok := fn.(func(string)); ok {
  27  		f(s)
  28  	}
  29  }
  30  
  31  //:wasmexport __cbb
  32  func DispatchB(id int32, val int32) {
  33  	fn, ok := lookup(id)
  34  	if !ok {
  35  		return
  36  	}
  37  	if f, ok := fn.(func(bool)); ok {
  38  		f(val != 0)
  39  	}
  40  }
  41  
  42  //:wasmexport __cbi
  43  func DispatchI(id int32, val int32) {
  44  	fn, ok := lookup(id)
  45  	if !ok {
  46  		return
  47  	}
  48  	if f, ok := fn.(func(int32)); ok {
  49  		f(val)
  50  	}
  51  }
  52  
  53  //:wasmexport __cbis
  54  func DispatchIS(id int32, ival int32, ptr *byte, length int32) {
  55  	fn, ok := lookup(id)
  56  	if !ok {
  57  		return
  58  	}
  59  	s := ptrToString(ptr, length)
  60  	if f, ok := fn.(func(int32, string)); ok {
  61  		f(ival, s)
  62  	}
  63  }
  64  
  65  //:wasmexport __cbiis
  66  func DispatchIIS(id int32, a int32, b int32, ptr *byte, length int32) {
  67  	fn, ok := lookup(id)
  68  	if !ok {
  69  		return
  70  	}
  71  	s := ptrToString(ptr, length)
  72  	if f, ok := fn.(func(int32, int32, string)); ok {
  73  		f(a, b, s)
  74  	}
  75  }
  76  
  77  //:wasmexport __cbss
  78  func DispatchSS(id int32, ptr1 *byte, len1 int32, ptr2 *byte, len2 int32) {
  79  	fn, ok := lookup(id)
  80  	if !ok {
  81  		return
  82  	}
  83  	s1 := ptrToString(ptr1, len1)
  84  	s2 := ptrToString(ptr2, len2)
  85  	if f, ok := fn.(func(string, string)); ok {
  86  		f(s1, s2)
  87  	}
  88  }
  89  
  90  //:wasmexport __cb6i
  91  func Dispatch6I(id int32, a int32, b int32, c int32, d int32, e int32, f int32) {
  92  	fn, ok := lookup(id)
  93  	if !ok {
  94  		return
  95  	}
  96  	if cb, ok := fn.(func(int32, int32, int32, int32, int32, int32)); ok {
  97  		cb(a, b, c, d, e, f)
  98  	}
  99  }
 100  
 101  //:wasmexport __cbii
 102  func DispatchII(id int32, a int32, b int32) {
 103  	fn, ok := lookup(id)
 104  	if !ok {
 105  		return
 106  	}
 107  	if f, ok := fn.(func(int32, int32)); ok {
 108  		f(a, b)
 109  	}
 110  }
 111  
 112  //:wasmexport __cbiii
 113  func DispatchIII(id int32, a int32, b int32, c int32) {
 114  	fn, ok := lookup(id)
 115  	if !ok {
 116  		return
 117  	}
 118  	if f, ok := fn.(func(int32, int32, int32)); ok {
 119  		f(a, b, c)
 120  	}
 121  }
 122  
 123  //:wasmexport __cbdata
 124  func DispatchBytes(id int32, ptr *byte, length int32) {
 125  	fn, ok := lookup(id)
 126  	if !ok {
 127  		return
 128  	}
 129  	if length <= 0 {
 130  		if f, ok := fn.(func([]byte)); ok {
 131  			f(nil)
 132  		}
 133  		return
 134  	}
 135  	b := ptrToBytes(ptr, length)
 136  	if f, ok := fn.(func([]byte)); ok {
 137  		f(b)
 138  	}
 139  }
 140  
 141  //:wasmexport __cb_release
 142  func DispatchRelease(id int32) {
 143  	delete(table, id)
 144  }
 145  
 146  func ptrToBytes(ptr *byte, length int32) (buf []byte) {
 147  	if ptr == nil || length <= 0 {
 148  		return nil
 149  	}
 150  	return unsafe.Slice(ptr, length)
 151  }
 152  
 153  func ptrToString(ptr *byte, length int32) (s string) {
 154  	if ptr == nil || length <= 0 {
 155  		return ""
 156  	}
 157  	return unsafe.String(ptr, length)
 158  }
 159