js.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package app
  2. import (
  3. "net/url"
  4. )
  5. const (
  6. // isClientSide = runtime.GOARCH == "wasm" && runtime.GOOS == "js"
  7. )
  8. // Type represents the JavaScript type of a Value.
  9. type Type int
  10. // Constants that enumerates the JavaScript types.
  11. const (
  12. TypeUndefined Type = iota
  13. TypeNull
  14. TypeBoolean
  15. TypeNumber
  16. TypeString
  17. TypeSymbol
  18. TypeObject
  19. TypeFunction
  20. )
  21. // Wrapper is implemented by types that are backed by a JavaScript value.
  22. type Wrapper interface {
  23. JSValue() Value
  24. }
  25. // Value is the interface that represents a JavaScript value. On wasm
  26. // architecture, it wraps the Value from https://golang.org/pkg/syscall/js/
  27. // package.
  28. type Value interface {
  29. // Bool returns the value v as a bool. It panics if v is not a JavaScript
  30. // boolean.
  31. Bool() bool
  32. // Call does a JavaScript call to the method m of value v with the given
  33. // arguments. It panics if v has no method m. The arguments get mapped to
  34. // JavaScript values according to the ValueOf function.
  35. Call(m string, args ...any) Value
  36. // Delete deletes the JavaScript property p of value v. It panics if v is
  37. // not a JavaScript object.
  38. Delete(p string)
  39. // Equal reports whether v and w are equal according to JavaScript's ===
  40. // operator.
  41. Equal(w Value) bool
  42. // Float returns the value v as a float64. It panics if v is not a
  43. // JavaScript number.
  44. Float() float64
  45. // Get returns the JavaScript property p of value v. It panics if v is not a
  46. // JavaScript object.
  47. Get(p string) Value
  48. // Index returns JavaScript index i of value v. It panics if v is not a
  49. // JavaScript object.
  50. Index(i int) Value
  51. // InstanceOf reports whether v is an instance of type t according to
  52. // JavaScript's instanceof operator.
  53. InstanceOf(t Value) bool
  54. // Int returns the value v truncated to an int. It panics if v is not a
  55. // JavaScript number.
  56. Int() int
  57. // Invoke does a JavaScript call of the value v with the given arguments. It
  58. // panics if v is not a JavaScript function. The arguments get mapped to
  59. // JavaScript values according to the ValueOf function.
  60. Invoke(args ...any) Value
  61. // IsNaN reports whether v is the JavaScript value "NaN".
  62. IsNaN() bool
  63. // IsNull reports whether v is the JavaScript value "null".
  64. IsNull() bool
  65. // IsUndefined reports whether v is the JavaScript value "undefined".
  66. IsUndefined() bool
  67. // JSValue implements Wrapper interface.
  68. JSValue() Value
  69. // Length returns the JavaScript property "length" of v. It panics if v is
  70. // not a JavaScript object.
  71. Length() int
  72. // New uses JavaScript's "new" operator with value v as constructor and the
  73. // given arguments. It panics if v is not a JavaScript function. The
  74. // arguments get mapped to JavaScript values according to the ValueOf
  75. // function.
  76. New(args ...any) Value
  77. // Set sets the JavaScript property p of value v to ValueOf(x). It panics if
  78. // v is not a JavaScript object.
  79. Set(p string, x any)
  80. // SetIndex sets the JavaScript index i of value v to ValueOf(x). It panics
  81. // if v is not a JavaScript object.
  82. SetIndex(i int, x any)
  83. // String returns the value v as a string. String is a special case because
  84. // of Go's String method convention. Unlike the other getters, it does not
  85. // panic if v's Type is not TypeString. Instead, it returns a string of the
  86. // form "<T>" or "<T: V>" where T is v's type and V is a string
  87. // representation of v's value.
  88. String() string
  89. // Truthy returns the JavaScript "truthiness" of the value v. In JavaScript,
  90. // false, 0, "", null, undefined, and NaN are "falsy", and everything else
  91. // is "truthy". See
  92. // https://developer.mozilla.org/en-US/docs/Glossary/Truthy.
  93. Truthy() bool
  94. // Type returns the JavaScript type of the value v. It is similar to
  95. // JavaScript's typeof operator, except that it returns TypeNull instead of
  96. // TypeObject for null.
  97. Type() Type
  98. // Then calls the given function when the promise resolves. The current
  99. // value must be a promise.
  100. Then(f func(Value))
  101. getAttr(k string) string
  102. setAttr(k, v string)
  103. delAttr(k string)
  104. firstChild() Value
  105. appendChild(c Wrapper)
  106. replaceChild(new, old Wrapper)
  107. removeChild(c Wrapper)
  108. firstElementChild() Value
  109. addEventListener(event string, fn Func)
  110. removeEventListener(event string, fn Func)
  111. setNodeValue(v string)
  112. setInnerHTML(v string)
  113. setInnerText(v string)
  114. }
  115. // Null returns the JavaScript value "null".
  116. func Null() Value {
  117. return null()
  118. }
  119. // Undefined returns the JavaScript value "undefined".
  120. func Undefined() Value {
  121. return undefined()
  122. }
  123. // ValueOf returns x as a JavaScript value:
  124. //
  125. // | Go | JavaScript |
  126. // | ---------------------- | ---------------------- |
  127. // | js.Value | [its value] |
  128. // | js.Func | function |
  129. // | nil | null |
  130. // | bool | boolean |
  131. // | integers and floats | number |
  132. // | string | string |
  133. // | []any | new array |
  134. // | map[string]any | new object |
  135. //
  136. // Panics if x is not one of the expected types.
  137. func ValueOf(x any) Value {
  138. return valueOf(x)
  139. }
  140. // Func is the interface that describes a wrapped Go function to be called by
  141. // JavaScript.
  142. type Func interface {
  143. Value
  144. // Release frees up resources allocated for the function. The function must
  145. // not be invoked after calling Release.
  146. Release()
  147. }
  148. // FuncOf returns a wrapped function.
  149. //
  150. // Invoking the JavaScript function will synchronously call the Go function fn
  151. // with the value of JavaScript's "this" keyword and the arguments of the
  152. // invocation. The return value of the invocation is the result of the Go
  153. // function mapped back to JavaScript according to ValueOf.
  154. //
  155. // A wrapped function triggered during a call from Go to JavaScript gets
  156. // executed on the same goroutine. A wrapped function triggered by JavaScript's
  157. // event loop gets executed on an extra goroutine. Blocking operations in the
  158. // wrapped function will block the event loop. As a consequence, if one wrapped
  159. // function blocks, other wrapped funcs will not be processed. A blocking
  160. // function should therefore explicitly start a new goroutine.
  161. //
  162. // Func.Release must be called to free up resources when the function will not
  163. // be used any more.
  164. func FuncOf(fn func(this Value, args []Value) any) Func {
  165. return funcOf(fn)
  166. }
  167. // BrowserWindow is the interface that describes the browser window.
  168. type BrowserWindow interface {
  169. Value
  170. // The window current url (window.location.href).
  171. URL() *url.URL
  172. // The window size.
  173. Size() (w, h int)
  174. // The position of the cursor (mouse or touch).
  175. CursorPosition() (x, y int)
  176. setCursorPosition(x, y int)
  177. // Returns the HTML element with the id property that matches the given id.
  178. GetElementByID(id string) Value
  179. // Scrolls to the HTML element with the given id.
  180. ScrollToID(id string)
  181. // AddEventListener subscribes a given handler to the specified event. It
  182. // returns a function that must be called to unsubscribe the handler and
  183. // release allocated resources.
  184. AddEventListener(event string, h EventHandler) func()
  185. setBody(body UI)
  186. createElement(tag, xmlns string) (Value, error)
  187. createTextNode(v string) Value
  188. addHistory(u *url.URL)
  189. replaceHistory(u *url.URL)
  190. }
  191. // CopyBytesToGo copies bytes from the Uint8Array src to dst. It returns the
  192. // number of bytes copied, which will be the minimum of the lengths of src and
  193. // dst.
  194. //
  195. // CopyBytesToGo panics if src is not an Uint8Array.
  196. func CopyBytesToGo(dst []byte, src Value) int {
  197. return copyBytesToGo(dst, src)
  198. }
  199. // CopyBytesToJS copies bytes from src to the Uint8Array dst. It returns the
  200. // number of bytes copied, which will be the minimum of the lengths of src and
  201. // dst.
  202. //
  203. // CopyBytesToJS panics if dst is not an Uint8Array.
  204. func CopyBytesToJS(dst Value, src []byte) int {
  205. return copyBytesToJS(dst, src)
  206. }