├── doc.go ├── Readme.md ├── console └── console.go ├── object └── object.go ├── weakmap └── weakmap.go └── regexp └── regexp.go /doc.go: -------------------------------------------------------------------------------- 1 | // Package js provides APIs for working with JavaScript from Go. 2 | package js 3 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | Go WASM packages. 2 | 3 | --- 4 | 5 | [![GoDoc](https://godoc.org/github.com/apex/js?status.svg)](https://godoc.org/github.com/apex/js) 6 | ![](https://img.shields.io/badge/license-MIT-blue.svg) 7 | ![](https://img.shields.io/badge/status-stable-green.svg) 8 | 9 | 10 | -------------------------------------------------------------------------------- /console/console.go: -------------------------------------------------------------------------------- 1 | //+build js,wasm 2 | 3 | // Package console provides console logging API access. 4 | package console 5 | 6 | import "syscall/js" 7 | 8 | // console reference. 9 | var console = js.Global().Get("console") 10 | 11 | // Log outputs a message to the browser console. 12 | func Log(args ...interface{}) { 13 | console.Call("log", args...) 14 | } 15 | 16 | // Dir outputs an interactive list of the properties to the browser console. 17 | func Dir(args ...interface{}) { 18 | console.Call("dir", args...) 19 | } 20 | -------------------------------------------------------------------------------- /object/object.go: -------------------------------------------------------------------------------- 1 | //+build js,wasm 2 | 3 | // Package object provides functions for working with JavaScript objects. 4 | package object 5 | 6 | import "syscall/js" 7 | 8 | // object reference. 9 | var object = js.Global().Get("Object") 10 | 11 | // Create returns a new object using the given object as its prototype. 12 | func Create(v js.Value) js.Value { 13 | return object.Call("create", v) 14 | } 15 | 16 | // Keys returns the keys of an object. 17 | func Keys(o js.Value) (keys []string) { 18 | k := object.Call("keys", o) 19 | l := k.Get("length").Int() 20 | for i := 0; i < l; i++ { 21 | keys = append(keys, k.Index(i).String()) 22 | } 23 | return 24 | } 25 | 26 | // Entry is a single key/value pair. 27 | type Entry struct { 28 | Key string 29 | Value js.Value 30 | } 31 | 32 | // Entries returns the object entries as key/value pairs. 33 | func Entries(o js.Value) (entries []Entry) { 34 | e := object.Call("entries", o) 35 | l := e.Get("length").Int() 36 | for i := 0; i < l; i++ { 37 | v := e.Index(i) 38 | entries = append(entries, Entry{ 39 | Key: v.Index(0).String(), 40 | Value: v.Index(1), 41 | }) 42 | } 43 | return 44 | } 45 | -------------------------------------------------------------------------------- /weakmap/weakmap.go: -------------------------------------------------------------------------------- 1 | //+build js,wasm 2 | 3 | // Package weakmap provides a map of weakly reference key/value pairs 4 | // which are released upon garbage collection. 5 | package weakmap 6 | 7 | import "syscall/js" 8 | 9 | // WeakMap collection. 10 | type WeakMap struct { 11 | v js.Value 12 | } 13 | 14 | // New returns a new WeakMap. 15 | func New() *WeakMap { 16 | return &WeakMap{ 17 | v: js.Global().Get("WeakMap").New(), 18 | } 19 | } 20 | 21 | // Set a key's value. 22 | func (m *WeakMap) Set(key, value interface{}) { 23 | m.v.Call("set", key, value) 24 | } 25 | 26 | // Get a key's value. False is returned if the key does not exist. 27 | func (m *WeakMap) Get(key interface{}) (v js.Value, ok bool) { 28 | v = m.v.Call("get", key) 29 | if v == js.Undefined() { 30 | return v, false 31 | } 32 | return v, true 33 | } 34 | 35 | // Delete a key's value. 36 | func (m *WeakMap) Delete(key interface{}) { 37 | m.v.Call("delete", key) 38 | } 39 | 40 | // Has returns true if the key is present. 41 | func (m *WeakMap) Has(key interface{}) bool { 42 | return m.v.Call("has", key).Bool() 43 | } 44 | 45 | // JSValue implementation. 46 | func (r *RegExp) JSValue() js.Value { 47 | return r.v 48 | } 49 | -------------------------------------------------------------------------------- /regexp/regexp.go: -------------------------------------------------------------------------------- 1 | //+build js,wasm 2 | 3 | // Package regexp provides RegExp bindings. 4 | package regexp 5 | 6 | import ( 7 | "syscall/js" 8 | ) 9 | 10 | // references. 11 | var regexp = js.Global().Get("RegExp") 12 | 13 | // Flag enum. 14 | type Flag int 15 | 16 | // Flags available. 17 | const ( 18 | // Global finds all matches rather than stopping after the first match. 19 | Global Flag = 1 << iota 20 | 21 | // IgnoreCase ignore casing; if u flag is also enabled, use Unicode case folding. 22 | IgnoreCase 23 | 24 | // Multiline; treat beginning and end characters (^ and $) as working over multiple 25 | // lines (i.e., match the beginning or end of each line (delimited by \n or \r), not 26 | // only the very beginning or end of the whole input string) 27 | Multiline 28 | 29 | // Unicode; treat pattern as a sequence of Unicode code points. 30 | Unicode 31 | 32 | // Sticky; matches only from the index indicated by the lastIndex property of this regular 33 | // expression in the target string (and does not attempt to match from any later indexes). 34 | Sticky 35 | ) 36 | 37 | // String returns the flag(s) specified in the single-letter format 38 | // expected by the RegExp constructor. 39 | func (f Flag) String() (s string) { 40 | if f&Global == Global { 41 | s += "g" 42 | } 43 | 44 | if f&IgnoreCase == IgnoreCase { 45 | s += "i" 46 | } 47 | 48 | if f&Multiline == Multiline { 49 | s += "m" 50 | } 51 | 52 | if f&Unicode == Unicode { 53 | s += "u" 54 | } 55 | 56 | if f&Sticky == Sticky { 57 | s += "y" 58 | } 59 | 60 | return 61 | } 62 | 63 | // Result is the match information. 64 | type Result struct { 65 | v js.Value 66 | } 67 | 68 | // Input returns the original input string. 69 | func (r *Result) Input() string { 70 | return r.v.Get("input").String() 71 | } 72 | 73 | // Index returns the index of the matched string. 74 | func (r *Result) Index() int { 75 | return r.v.Get("index").Int() 76 | } 77 | 78 | // String returns the full matched string. 79 | func (r *Result) String() string { 80 | return r.v.Index(0).String() 81 | } 82 | 83 | // Match returns the parenthesized substring match at index. 84 | func (r *Result) Match(i int) string { 85 | return r.v.Index(i + 1).String() 86 | } 87 | 88 | // RegExp is a regular expression. 89 | type RegExp struct { 90 | v js.Value 91 | } 92 | 93 | // New returns a regexp from the given pattern and flags. 94 | func New(pattern string, flags Flag) *RegExp { 95 | return &RegExp{ 96 | v: regexp.New(pattern, flags.String()), 97 | } 98 | } 99 | 100 | // Exec returns the result of a search execution match, or nil. 101 | func (r *RegExp) Exec(s string) *Result { 102 | v := r.v.Call("exec", s) 103 | 104 | if v == js.Null() { 105 | return nil 106 | } 107 | 108 | return &Result{v} 109 | } 110 | 111 | // String implementation. 112 | func (r *RegExp) String() string { 113 | return r.v.String() 114 | } 115 | 116 | // JSValue implementation. 117 | func (r *RegExp) JSValue() js.Value { 118 | return r.v 119 | } 120 | --------------------------------------------------------------------------------