github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/syscall/js/js.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build js && wasm 6 7 // Package js gives access to the WebAssembly host environment when using the js/wasm architecture. 8 // Its API is based on JavaScript semantics. 9 // 10 // This package is EXPERIMENTAL. Its current scope is only to allow tests to run, but not yet to provide a 11 // comprehensive API for users. It is exempt from the Go compatibility promise. 12 package js 13 14 // Value represents a JavaScript value. The zero value is the JavaScript value "undefined". 15 // Values can be checked for equality with the Equal method. 16 type Value struct { 17 _ [0]func() 18 ref ref 19 gcPtr *ref 20 } 21 22 // Error wraps a JavaScript error. 23 type Error struct { 24 // Value is the underlying JavaScript error value. 25 Value 26 } 27 28 // Error implements the error interface. 29 func (e Error) Error() string 30 31 // Equal reports whether v and w are equal according to JavaScript's === operator. 32 func (v Value) Equal(w Value) bool 33 34 // Undefined returns the JavaScript value "undefined". 35 func Undefined() Value 36 37 // IsUndefined reports whether v is the JavaScript value "undefined". 38 func (v Value) IsUndefined() bool 39 40 // Null returns the JavaScript value "null". 41 func Null() Value 42 43 // IsNull reports whether v is the JavaScript value "null". 44 func (v Value) IsNull() bool 45 46 // IsNaN reports whether v is the JavaScript value "NaN". 47 func (v Value) IsNaN() bool 48 49 // Global returns the JavaScript global object, usually "window" or "global". 50 func Global() Value 51 52 // ValueOf returns x as a JavaScript value: 53 // 54 // | Go | JavaScript | 55 // | ---------------------- | ---------------------- | 56 // | js.Value | [its value] | 57 // | js.Func | function | 58 // | nil | null | 59 // | bool | boolean | 60 // | integers and floats | number | 61 // | string | string | 62 // | []interface{} | new array | 63 // | map[string]interface{} | new object | 64 // 65 // Panics if x is not one of the expected types. 66 func ValueOf(x any) Value 67 68 // Type represents the JavaScript type of a Value. 69 type Type int 70 71 const ( 72 TypeUndefined Type = iota 73 TypeNull 74 TypeBoolean 75 TypeNumber 76 TypeString 77 TypeSymbol 78 TypeObject 79 TypeFunction 80 ) 81 82 func (t Type) String() string 83 84 // Type returns the JavaScript type of the value v. It is similar to JavaScript's typeof operator, 85 // except that it returns TypeNull instead of TypeObject for null. 86 func (v Value) Type() Type 87 88 // Get returns the JavaScript property p of value v. 89 // It panics if v is not a JavaScript object. 90 func (v Value) Get(p string) Value 91 92 // Set sets the JavaScript property p of value v to ValueOf(x). 93 // It panics if v is not a JavaScript object. 94 func (v Value) Set(p string, x any) 95 96 // Delete deletes the JavaScript property p of value v. 97 // It panics if v is not a JavaScript object. 98 func (v Value) Delete(p string) 99 100 // Index returns JavaScript index i of value v. 101 // It panics if v is not a JavaScript object. 102 func (v Value) Index(i int) Value 103 104 // SetIndex sets the JavaScript index i of value v to ValueOf(x). 105 // It panics if v is not a JavaScript object. 106 func (v Value) SetIndex(i int, x any) 107 108 // Length returns the JavaScript property "length" of v. 109 // It panics if v is not a JavaScript object. 110 func (v Value) Length() int 111 112 // Call does a JavaScript call to the method m of value v with the given arguments. 113 // It panics if v has no method m. 114 // The arguments get mapped to JavaScript values according to the ValueOf function. 115 func (v Value) Call(m string, args ...any) Value 116 117 // Invoke does a JavaScript call of the value v with the given arguments. 118 // It panics if v is not a JavaScript function. 119 // The arguments get mapped to JavaScript values according to the ValueOf function. 120 func (v Value) Invoke(args ...any) Value 121 122 // New uses JavaScript's "new" operator with value v as constructor and the given arguments. 123 // It panics if v is not a JavaScript function. 124 // The arguments get mapped to JavaScript values according to the ValueOf function. 125 func (v Value) New(args ...any) Value 126 127 // Float returns the value v as a float64. 128 // It panics if v is not a JavaScript number. 129 func (v Value) Float() float64 130 131 // Int returns the value v truncated to an int. 132 // It panics if v is not a JavaScript number. 133 func (v Value) Int() int 134 135 // Bool returns the value v as a bool. 136 // It panics if v is not a JavaScript boolean. 137 func (v Value) Bool() bool 138 139 // Truthy returns the JavaScript "truthiness" of the value v. In JavaScript, 140 // false, 0, "", null, undefined, and NaN are "falsy", and everything else is 141 // "truthy". See https://developer.mozilla.org/en-US/docs/Glossary/Truthy. 142 func (v Value) Truthy() bool 143 144 // String returns the value v as a string. 145 // String is a special case because of Go's String method convention. Unlike the other getters, 146 // it does not panic if v's Type is not TypeString. Instead, it returns a string of the form "<T>" 147 // or "<T: V>" where T is v's type and V is a string representation of v's value. 148 func (v Value) String() string 149 150 // InstanceOf reports whether v is an instance of type t according to JavaScript's instanceof operator. 151 func (v Value) InstanceOf(t Value) bool 152 153 // A ValueError occurs when a Value method is invoked on 154 // a Value that does not support it. Such cases are documented 155 // in the description of each method. 156 type ValueError struct { 157 Method string 158 Type Type 159 } 160 161 func (e *ValueError) Error() string 162 163 // CopyBytesToGo copies bytes from src to dst. 164 // It panics if src is not a Uint8Array or Uint8ClampedArray. 165 // It returns the number of bytes copied, which will be the minimum of the lengths of src and dst. 166 func CopyBytesToGo(dst []byte, src Value) int 167 168 // CopyBytesToJS copies bytes from src to dst. 169 // It panics if dst is not a Uint8Array or Uint8ClampedArray. 170 // It returns the number of bytes copied, which will be the minimum of the lengths of src and dst. 171 func CopyBytesToJS(dst Value, src []byte) int