summaryrefslogtreecommitdiff
path: root/src/syscall/js/js.go
Commit message (Collapse)AuthorAgeFilesLines
* all: fix misuses of "a" vs "an"cui fliter2023-04-041-2/+2
| | | | | | | | | | | | | | Fixes the misuse of "a" vs "an", according to English grammatical expectations and using https://www.a-or-an.com/ Change-Id: I53ac724070e3ff3d33c304483fe72c023c7cda47 Reviewed-on: https://go-review.googlesource.com/c/go/+/480536 Run-TryBot: shuang cui <imcusg@gmail.com> Reviewed-by: Ian Lance Taylor <iant@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
* all: implement wasmimport directiveEvan Phoenix2023-03-021-0/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | Go programs can now use the //go:wasmimport module_name function_name directive to import functions from the WebAssembly runtime. For now, the directive is restricted to the runtime and syscall/js packages. * Derived from CL 350737 * Original work modified to work with changes to the IR conversion code. * Modification of CL 350737 changes to fully exist in Unified IR path (emp) * Original work modified to work with changes to the ABI configuration code. * Fixes #38248 Co-authored-by: Vedant Roy <vroy101@gmail.com> Co-authored-by: Richard Musiol <mail@richard-musiol.de> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Change-Id: I740719735d91c306ac718a435a78e1ee9686bc16 Reviewed-on: https://go-review.googlesource.com/c/go/+/463018 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
* all: gofmt main repoRuss Cox2022-04-111-10/+10
| | | | | | | | | | | | | | | [This CL is part of a sequence implementing the proposal #51082. The design doc is at https://go.dev/s/godocfmt-design.] Run the updated gofmt, which reformats doc comments, on the main repository. Vendored files are excluded. For #51082. Change-Id: I7332f099b60f716295fb34719c98c04eb1a85407 Reviewed-on: https://go-review.googlesource.com/c/go/+/384268 Reviewed-by: Jonathan Amsterdam <jba@google.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
* all: gofmt -w -r 'interface{} -> any' srcRuss Cox2021-12-131-9/+9
| | | | | | | | | | | | | | | | | | | | | | | | And then revert the bootstrap cmd directories and certain testdata. And adjust tests as needed. Not reverting the changes in std that are bootstrapped, because some of those changes would appear in API docs, and we want to use any consistently. Instead, rewrite 'any' to 'interface{}' in cmd/dist for those directories when preparing the bootstrap copy. A few files changed as a result of running gofmt -w not because of interface{} -> any but because they hadn't been updated for the new //go:build lines. Fixes #49884. Change-Id: Ie8045cba995f65bd79c694ec77a1b3d1fe01bb09 Reviewed-on: https://go-review.googlesource.com/c/go/+/368254 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
* all: manual fixups for //go:build vs // +buildRuss Cox2021-10-281-1/+0
| | | | | | | | | | | | | Update many generators, also handle files that were not part of the standard build during 'go fix' in CL 344955. Fixes #41184. Change-Id: I1edc684e8101882dcd11f75c6745c266fccfe9e7 Reviewed-on: https://go-review.googlesource.com/c/go/+/359476 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
* syscall/js: remove Wrapper interfaceRichard Musiol2021-10-181-14/+3
| | | | | | | | | | | | | | | | This change removes the js.Wrapper interface for performance reasons. See proposal #44006 for details. This is a breaking change, but syscall/js is exempt from Go's compatibility promise. Fixes #44006 Change-Id: I968cd14b1e61cc72ea9f84240b6bd29e8b8ae673 Reviewed-on: https://go-review.googlesource.com/c/go/+/356430 Trust: Richard Musiol <neelance@gmail.com> Reviewed-by: Cherry Mui <cherryyz@google.com> Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
* all: gofmt more (but vendor, testdata, and top-level test directories)Dmitri Shuralyov2021-08-131-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | CL 294430 made packages in std and cmd modules use Go 1.17 gofmt format, adding //go:build lines. This change applies the same formatting to some more packages that 'go fmt' missed (e.g., syscall/js, runtime/msan), and everything else that is easy and safe to modify in bulk. Consider the top-level test directory, testdata, and vendor directories out of scope, since there are many files that don't follow strict gofmt formatting, often for intentional and legitimate reasons (testing gofmt itself, invalid Go programs that shouldn't crash the compiler, etc.). That makes it easy and safe to gofmt -w the .go files that are found with gofmt -l with aforementioned directories filtered out: $ gofmt -l . 2>/dev/null | \ grep -v '^test/' | \ grep -v '/testdata/' | \ grep -v '/vendor/' | wc -l 51 None of the 51 files are generated. After this change, the same command prints 0. For #41184. Change-Id: Ia96ee2a0f998d6a167d4473bcad17ad09bc1d86e Reviewed-on: https://go-review.googlesource.com/c/go/+/341009 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Dmitri Shuralyov <dmitshur@golang.org>
* syscall/js: allow copyBytesTo(Go|JS) to use Uint8ClampedArrayAurélio A. Heckert2020-03-241-6/+6
| | | | | | | | | | | closes #38011 Change-Id: Ic50f2f27456dccdc3fca1bda076871af1eb81705 Reviewed-on: https://go-review.googlesource.com/c/go/+/224638 Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Richard Musiol <neelance@gmail.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* syscall/js: garbage collect references to JavaScript valuesRichard Musiol2019-11-041-37/+116
| | | | | | | | | | | | | | | | | | | | | The js.Value struct now contains a pointer, so a finalizer can determine if the value is not referenced by Go any more. Unfortunately this breaks Go's == operator with js.Value. This change adds a new Equal method to check for the equality of two Values. This is a breaking change. The == operator is now disallowed to not silently break code. Additionally the helper methods IsUndefined, IsNull and IsNaN got added. Fixes #35111 Change-Id: I58a50ca18f477bf51a259c668a8ba15bfa76c955 Reviewed-on: https://go-review.googlesource.com/c/go/+/203600 Run-TryBot: Richard Musiol <neelance@gmail.com> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* syscall/js: add Value.Delete for deleting JavaScript propertiesRichard Musiol2019-09-301-0/+11
| | | | | | | | | | | | | | This change adds the method Value.Delete, which implements JavaScript's "delete" operator for deleting properties. Fixes #33079. Change-Id: Ia5b190240bd59daca48094fcbc32f8d0a06f19d5 Reviewed-on: https://go-review.googlesource.com/c/go/+/197840 Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com>
* syscall/js: fix commentsHajime Hoshi2019-06-021-1/+0
| | | | | | | | As js.TypedArray no longer exists, the comment should be updated. Change-Id: Idd1087c8007afc90307fdd965f28d3be8d8cd73e Reviewed-on: https://go-review.googlesource.com/c/go/+/180097 Reviewed-by: Richard Musiol <neelance@gmail.com>
* syscall/js: replace TypedArrayOf with CopyBytesToGo/CopyBytesToJSRichard Musiol2019-05-241-2/+27
| | | | | | | | | | | | | | | | | | | | | The typed arrays returned by TypedArrayOf were backed by WebAssembly memory. They became invalid each time we grow the WebAssembly memory. This made them very error prone and hard to use correctly. This change removes TypedArrayOf completely and instead introduces CopyBytesToGo and CopyBytesToJS for copying bytes between a byte slice and an Uint8Array. This breaking change is still allowed for the syscall/js package. Fixes #31980. Fixes #31812. Change-Id: I14c76fdd60b48dd517c1593972a56d04965cb272 Reviewed-on: https://go-review.googlesource.com/c/go/+/177537 Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: improve Value.String() for non-string valuesRichard Musiol2019-03-281-2/+28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This change modifies Value.String() to use the following representations for non-string values: <undefined> <null> <boolean: true> <number: 42> <symbol> <object> <function> It avoids JavaScript conversion semantics in the Go API and lowers the risk of hidden bugs by unexpected conversions, e.g. the conversion of the number 42 to the string "42". See discussion in #29642. This is a breaking change, which are still allowed for syscall/js. The impact should be small since it only affects uses of Value.String() with non-string values, which should be uncommon. Updates #29642. Change-Id: I2c27be6e24befe8cb713031fbf66f7b6041e7148 Reviewed-on: https://go-review.googlesource.com/c/go/+/169757 Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: improve type checks of js.Value's methodsRichard Musiol2019-03-261-6/+36
| | | | | | | | | | | Add more explicit checks if the given js.Value is of the correct type instead of erroring on the JavaScript layer. Change-Id: I30b18a76820fb68f6ac279bb88a57456f5bab467 Reviewed-on: https://go-review.googlesource.com/c/go/+/168886 Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: rename js.Callback to js.FuncRichard Musiol2018-12-131-1/+1
| | | | | | | | | | | | | | | | | | The name "Callback" does not fit to all use cases of js.Callback. This commit changes its name to Func. Accordingly NewCallback gets renamed to FuncOf, which matches ValueOf and TypedArrayOf. The package syscall/js is currently exempt from Go's compatibility promise and js.Callback is already affected by a breaking change in this release cycle. See #28711 for details. Fixes #28711 Change-Id: I2c380970c3822bed6a3893909672c15d0cbe9da3 Reviewed-on: https://go-review.googlesource.com/c/153559 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: document ValueOf() panicMarkus2018-11-201-0/+2
| | | | | | | | | | ValueOf() panics if x is not one of the expected types. Change-Id: I1105e46bd09a5ab13c162b77c1c50cc45bce27a2 GitHub-Last-Rev: 34a88ce8206954d94f0a884ab7f6494116c54a2d GitHub-Pull-Request: golang/go#28846 Reviewed-on: https://go-review.googlesource.com/c/150138 Reviewed-by: Richard Musiol <neelance@gmail.com>
* syscall/js: add the Value.Truthy methodLarry Clapp2018-10-261-0/+20
| | | | | | | | | | | | | | | | | Truthy returns the JavaScript "truthiness" of the given value. In JavaScript, false, 0, "", null, undefined, and NaN are "falsy", and everything else is "truthy". Fixes #28264 Change-Id: I4586f98646c05a4147d06a7c4a5d9c61d956fc83 GitHub-Last-Rev: 649b353ebc23b09d840faf927a2eeca41ee164bf GitHub-Pull-Request: golang/go#28358 Reviewed-on: https://go-review.googlesource.com/c/144384 Reviewed-by: Richard Musiol <neelance@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
* syscall/js: add Wrapper interface to support external Value wrapper typesDenys Smirnov2018-10-241-5/+14
| | | | | | | | | | | | | | | | | The Callback and TypedArray are the only JavaScript types supported by the library, thus they are special-cased in a type switch of ValueOf. Instead, a Ref interface is defined to allow external wrapper types to be handled properly by ValueOf. Change-Id: I03240ba7ec46979336b88389a70b7bcac37fc715 GitHub-Last-Rev: c8cf08d8ccfaab2af98df9eec8bc7b60dbce2c64 GitHub-Pull-Request: golang/go#28181 Reviewed-on: https://go-review.googlesource.com/c/141644 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Richard Musiol <neelance@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: make zero js.Value represent "undefined"Richard Musiol2018-10-181-5/+16
| | | | | | | | | | | | | | | | | This commit changes the encoding of js.Value so that the zero js.Value represents the JavaScript value "undefined". This is what users intuitively expect. Specifically, the encodings of "undefined" and the number zero have been swapped. Fixes #27592. Change-Id: Icfc832c8cdf7a8a78bd69d20e00a04dbed0ccd10 Reviewed-on: https://go-review.googlesource.com/c/143137 Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: extend ValueOf to support arrays and objectsRichard Musiol2018-08-071-9/+26
| | | | | | | | | | | This commits adds []interface{} and map[string]interface{} as quick ways to create JavaScript arrays and objects. They correspond to the JavaScript notations [...] and {...}. A type alias can be used for a concise notation. Change-Id: I98bb08dbef1e0f3bd3d65c732d6b09e1520026ba Reviewed-on: https://go-review.googlesource.com/126855 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: improve documentation about mappings to JavaScript valuesRichard Musiol2018-07-311-2/+5
| | | | | | | | | | | This commit moves the documentation about how Go values are mapped to JavaScript values to the functions that apply the mapping, instead of mentioning them in the documentation of the types being mapped. This should be easier to read. Change-Id: I2465eb4a45f71b3b61624349e908a195010a09f1 Reviewed-on: https://go-review.googlesource.com/126856 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: show goroutine stack traces on deadlockRichard Musiol2018-07-191-8/+8
| | | | | | | | | | | | | | | | | | When using callbacks, it is not necessarily a deadlock if there is no runnable goroutine, since a callback might still be pending. If there is no callback pending, Node.js simply exits with exit code zero, which is not desired if the Go program is still considered running. This is why an explicit check on exit is used to trigger the "deadlock" error. This CL makes it so this is Go's normal "deadlock" error, which includes the stack traces of all goroutines. Updates #26382 Change-Id: If88486684d0517a64f570009a5ea0ad082679a54 Reviewed-on: https://go-review.googlesource.com/123936 Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: improve panic messagesRichard Musiol2018-07-091-5/+30
| | | | | | | | | | | | This commit adds the actual type to the panic message when calling a method of Value on a Value with a bad type. It also adds better panic messages to Value.Invoke and Value.Call. Change-Id: Ic4b3aa29d3bef8e357be40cd07664ad602ffab12 Reviewed-on: https://go-review.googlesource.com/122376 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
* syscall/js: add Value.TypeRichard Musiol2018-07-091-4/+68
| | | | | | | | | | | | This commits adds Value.Type(), which returns the JavaScript type of a Value. The implementation uses two previously unused bits of the NaN payload to encode type information. Change-Id: I568609569983791d50d35b8d80c44f3472203511 Reviewed-on: https://go-review.googlesource.com/122375 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: rename Callback.Close to Release and expose Callback.ValueRichard Musiol2018-06-281-1/+1
| | | | | | | | | | | This makes Callback more in line with TypedArray. The name "Release" is better than "Close" because the function does not implement io.Closer. Change-Id: I23829a14b1c969ceb04608afd9505fd5b4b0df2e Reviewed-on: https://go-review.googlesource.com/121216 Run-TryBot: Richard Musiol <neelance@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: add TypedArrayOfRichard Musiol2018-06-271-9/+14
| | | | | | | | | | | | | | | | The new function js.TypedArrayOf returns a JavaScript typed array for a given slice. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays This change also changes js.ValueOf to not accept a []byte any more. Fixes #25532. Change-Id: I8c7bc98ca4e21c3514d19eee7a1f92388d74ab2a Reviewed-on: https://go-review.googlesource.com/121215 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: use stable references to JavaScript valuesRichard Musiol2018-06-261-40/+66
| | | | | | | | | | | | | | | | | This commit changes how JavaScript values are referenced by Go code. After this change, a JavaScript value is always represented by the same ref, even if passed multiple times from JavaScript to Go. This allows Go's == operator to work as expected on js.Value (strict equality). Additionally, the performance of some operations of the syscall/js package got improved by saving additional roundtrips to JavaScript code. Fixes #25802. Change-Id: Ide6ffe66c6aa1caf5327a2d3ddbe48fe7c180461 Reviewed-on: https://go-review.googlesource.com/120561 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: turn constant package vars into functionsRichard Musiol2018-06-251-14/+20
| | | | | | | | | | This is so the values can not be changed and the type is easy to see. Requested on https://go-review.googlesource.com/c/go/+/120561. Change-Id: If2ed48ca3ba8874074687bfb2375d2f5592e8e0d Reviewed-on: https://go-review.googlesource.com/120564 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall.js: add Value.InstanceOfRichard Musiol2018-06-251-0/+7
| | | | | | | Change-Id: Icf56188fdb2b8ce6789830a35608203fdb9a3df6 Reviewed-on: https://go-review.googlesource.com/120560 Reviewed-by: Paul Jolly <paul@myitcv.org.uk> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: improve import functionsRichard Musiol2018-06-151-56/+89
| | | | | | | | | | | | | | | | 1. Make import functions not use the js.Value type directly, but only the ref field. This gives more flexibility on the Go side for the js.Value type, which is a preparation for adding garbage collection of js.Value. 2. Turn import functions which are methods of js.Value into package-level functions. This is necessary to make vet happy. Change-Id: I69959bf1fbea0a0b99a552a1112ffcd0c024e9b8 Reviewed-on: https://go-review.googlesource.com/118656 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* runtime, sycall/js: add support for callbacks from JavaScriptRichard Musiol2018-06-141-0/+6
| | | | | | | | | | | | | | | | | | | | | | | | This commit adds support for JavaScript callbacks back into WebAssembly. This is experimental API, just like the rest of the syscall/js package. The time package now also uses this mechanism to properly support timers without resorting to a busy loop. JavaScript code can call into the same entry point multiple times. The new RUN register is used to keep track of the program's run state. Possible values are: starting, running, paused and exited. If no goroutine is ready any more, the scheduler can put the program into the "paused" state and the WebAssembly code will stop running. When a callback occurs, the JavaScript code puts the callback data into a queue and then calls into WebAssembly to allow the Go code to continue running. Updates #18892 Updates #25506 Change-Id: Ib8701cfa0536d10d69bd541c85b0e2a754eb54fb Reviewed-on: https://go-review.googlesource.com/114197 Reviewed-by: Austin Clements <austin@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* syscall/js: add packageRichard Musiol2018-05-031-0/+190
This commit adds the syscall/js package, which is used by the wasm architecture to access the WebAssembly host environment (and the operating system through it). Currently, web browsers and Node.js are supported hosts, which is why the API is based on JavaScript APIs. There is no common API standardized in the WebAssembly ecosystem yet. This package is experimental. Its current scope is only to allow tests to run, but not yet to provide a comprehensive API for users. Updates #18892 Change-Id: I236ea10a70d95cdd50562212f2c18c3db5009230 Reviewed-on: https://go-review.googlesource.com/109195 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>