Skip to content
This repository was archived by the owner on Aug 27, 2018. It is now read-only.

Commit 14548f4

Browse files
authored
Merge pull request #73 from myitcv/fix_comp_vs_elem
Introduce concept of elements as distinct from components
2 parents d9fac98 + 4f00450 commit 14548f4

File tree

98 files changed

+1072
-579
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1072
-579
lines changed

.vendor.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
./_vendor/src/golang.org/x/crypto 3cb07270c9455e8ad27956a70891c962d121a228 https://go.googlesource.com/crypto
44
./_vendor/src/golang.org/x/tools 620ecdb8d7943e20dc030b61bfe898d1b000bdea https://go.googlesource.com/tools
55
./_vendor/src/github.com/gopherjs/gopherjs 9659c814f1d54d63f9c623449a7111d3864c1361 git@github.com:gopherjs/gopherjs
6+
./_vendor/src/github.com/gopherjs/jsbuiltin 67703bfb044e3192fbcab025c3aeaeedafad1f2f git@github.com:gopherjs/jsbuiltin
67
./_vendor/src/github.com/kisielk/gotool 0de1eaf82fa3f583ce21fde859f1e7e0c5e9b220 git@github.com:kisielk/gotool
78
./_vendor/src/github.com/fsnotify/fsnotify 7d7316ed6e1ed2de075aab8dfc76de5d158d66e1 git@github.com:fsnotify/fsnotify
89
./_vendor/src/github.com/spf13/cobra 16c014f1a19d865b765b420e74508f80eb831ada git@github.com:spf13/cobra
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: go
2+
go:
3+
- 1.8.x
4+
addons:
5+
apt:
6+
packages:
7+
- nodejs
8+
install:
9+
- go get -u github.com/gopherjs/gopherjs
10+
script:
11+
- diff -u <(echo -n) <(gofmt -d ./)
12+
- gopherjs test
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Copyright (c) 2015 Richard Musiol. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without
4+
modification, are permitted provided that the following conditions are
5+
met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
* Redistributions in binary form must reproduce the above
10+
copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the
12+
distribution.
13+
14+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
[![Build Status](https://api.travis-ci.org/gopherjs/jsbuiltin.svg?branch=master)](https://travis-ci.org/gopherjs/jsbuiltin) [![GoDoc](https://godoc.org/github.com/gopherjs/jsbuiltin?status.png)](http://godoc.org/github.com/gopherjs/jsbuiltin)
2+
3+
jsbuiltin - Built-in JavaScript functions for GopherJS
4+
------------------------------------------------------
5+
6+
JavaScript has a small number of [built-in
7+
functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects)
8+
to handle some common day-to-day tasks. This package providers wrappers around
9+
some of these functions for use in GopherJS.
10+
11+
It is worth noting that in many cases, using Go's equivalent functionality
12+
(such as that found in the [net/url](https://golang.org/pkg/net/url/) package)
13+
may be preferable to using this package, and will be a necessity any time you
14+
wish to share functionality between front-end and back-end code.
15+
16+
### What is supported?
17+
Not all JavaScript built-in functions make sense or are useful in a Go
18+
environment. The table below shows each of the JavaScript built-in functions,
19+
and its current state in this package.
20+
21+
| Name | Supported | Comment |
22+
|----------------------|-----------|-----------------------------|
23+
| eval() | -- | |
24+
| uneval() | -- | |
25+
| isFinite() | yes | |
26+
| isNaN() | yes | |
27+
| parseFloat() | TODO? | See note below |
28+
| parseInt() | TODO? | See note below |
29+
| decodeURI() | yes | |
30+
| decodeURIComponent() | yes | |
31+
| encodeURI() | yes | |
32+
| encodeURIComponent() | yes | |
33+
| escape() | -- | deprecated circa 2000 |
34+
| Number() | -- | See note below |
35+
| String() | -- | Use js.Object.String() |
36+
| unescape() | -- | deprecated circa 2000 |
37+
| typeof operator | yes | |
38+
| instanceof operator | yes | |
39+
40+
#### Notes on unmplemented functions
41+
42+
* **eval()**: Is there ever a need to eval JS code from within Go?
43+
* **Number()**: This requires handling a bunch of corner cases which don't
44+
normally exist in a strictly typed language such as Go. It seems that anyone
45+
with a legitimate need for this function probably needs to write their own
46+
wrapper to handle the cases that matter to them.
47+
* **parseInt()** and **parseFloat()**: These could be added, but doing so
48+
will require answering some questions about the interfce. JavaScript has
49+
effectively two relevant data types (int and float) where Go has has 12.
50+
Deciding how to map JS's `parseInt()` to Go's `(u?)int(8|16|32|64)` types,
51+
and JS's `parseFloat()` Go's `float(32|64)` or `complex(64|128)` needs to
52+
be considered, as well as how to handle error cases (Go doesn't have a `NaN`
53+
type, so any `NaN` result probably needs to be converted to a proper Go
54+
error). If this matters to you, comments and/or PRs are welcome.
55+
56+
### Installation and Usage
57+
Get or update this package and dependencies with:
58+
59+
```
60+
go get -u -d -tags=js github.com/gopherjs/jsbuiltin
61+
```
62+
63+
### Basic usage example
64+
65+
This is a modified version of the Pet example in the main GopherJS documentation,
66+
to accept and return URI-encoded pet names using the jsbuiltin package.
67+
68+
```go
69+
package main
70+
71+
import (
72+
"github.com/gopherjs/gopherjs/js"
73+
"github.com/gopherjs/jsbuiltin"
74+
)
75+
76+
func main() {
77+
js.Global.Set("pet", map[string]interface{}{
78+
"New": New,
79+
})
80+
}
81+
82+
type Pet struct {
83+
name string
84+
}
85+
86+
func New(name string) *js.Object {
87+
return js.MakeWrapper(&Pet{name})
88+
}
89+
90+
func (p *Pet) Name() string {
91+
return jsbuiltin.EncodeURIComponent(p.name)
92+
}
93+
94+
func (p *Pet) SetName(uriComponent string) error {
95+
name, err := jsbuiltin.DecodeURIComponent(uriComponent)
96+
if err != nil {
97+
// Malformed UTF8 in uriComponent
98+
return err
99+
}
100+
p.name = name
101+
return nil
102+
}
103+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Package jsbuiltin provides minimal wrappers around some JavasScript
2+
// built-in functions.
3+
package jsbuiltin
4+
5+
import (
6+
"errors"
7+
8+
"github.com/gopherjs/gopherjs/js"
9+
)
10+
11+
// DecodeURI decodes a Uniform Resource Identifier (URI) previously created
12+
// by EncodeURI() or by a similar routine. If the underlying JavaScript
13+
// function throws an error, it is returned as an error.
14+
func DecodeURI(uri string) (raw string, err error) {
15+
defer func() {
16+
if r := recover(); r != nil {
17+
err = r.(*js.Error)
18+
}
19+
}()
20+
raw = js.Global.Call("decodeURI", uri).String()
21+
return
22+
}
23+
24+
// EncodeURI encodes a Uniform Resource Identifier (URI) by replacing each
25+
// instance of certain characters by one, two, three, or four escape sequences
26+
// representing the UTF-8 encoding of the character (will only be four escape
27+
// sequences for characters composed of two "surrogate" characters).
28+
func EncodeURI(uri string) string {
29+
return js.Global.Call("encodeURI", uri).String()
30+
}
31+
32+
// EncodeURIComponent encodes a Uniform Resource Identifier (URI) component
33+
// by replacing each instance of certain characters by one, two, three, or
34+
// four escape sequences representing the UTF-8 encoding of the character
35+
// (will only be four escape sequences for characters composed of two
36+
// "surrogate" characters).
37+
func EncodeURIComponent(uri string) string {
38+
return js.Global.Call("encodeURIComponent", uri).String()
39+
}
40+
41+
// DecodeURIComponent decodes a Uniform Resource Identifier (URI) component
42+
// previously created by EncodeURIComponent() or by a similar routine. If the
43+
// underlying JavaScript function throws an error, it is returned as an error.
44+
func DecodeURIComponent(uri string) (raw string, err error) {
45+
defer func() {
46+
if r := recover(); r != nil {
47+
err = r.(*js.Error)
48+
}
49+
}()
50+
raw = js.Global.Call("decodeURIComponent", uri).String()
51+
return
52+
}
53+
54+
// IsFinite determines whether the passed value is a finite number, and returns
55+
// true if it is. If needed, the parameter is first converted to a number.
56+
func IsFinite(value interface{}) bool {
57+
return js.Global.Call("isFinite", value).Bool()
58+
}
59+
60+
// IsNaN determines whether a value is NaN (Not-a-Number) or not. A return
61+
// value of true indicates the input value is considered NaN by JavaScript.
62+
func IsNaN(value interface{}) bool {
63+
return js.Global.Call("isNaN", value).Bool()
64+
}
65+
66+
// Type constants represent the JavaScript builtin types, which may be returned
67+
// by TypeOf().
68+
const (
69+
TypeUndefined = "undefined"
70+
TypeNull = "null"
71+
TypeObject = "object"
72+
TypeBoolean = "boolean"
73+
TypeNumber = "number"
74+
TypeString = "string"
75+
TypeFunction = "function"
76+
TypeSymbol = "symbol"
77+
)
78+
79+
// TypeOf returns the JavaScript type of the passed value
80+
func TypeOf(value interface{}) string {
81+
return js.Global.Get("$jsbuiltin$").Call("typeoffunc", value).String()
82+
}
83+
84+
// InstanceOf returns true if value is an instance of object according to the
85+
// built-in 'instanceof' operator. `object` must be a *js.Object representing
86+
// a javascript constructor function.
87+
func InstanceOf(value interface{}, object *js.Object) bool {
88+
return js.Global.Get("$jsbuiltin$").Call("instanceoffunc", value, object).Bool()
89+
}
90+
91+
// In returns true if key is a member of obj. An error is returned if obj is not
92+
// a JavaScript object.
93+
func In(key string, obj *js.Object) (ok bool, err error) {
94+
if obj == nil || obj == js.Undefined || TypeOf(obj) != TypeObject {
95+
return false, errors.New("obj not a JavaScript object")
96+
}
97+
return js.Global.Get("$jsbuiltin$").Call("infunc", key, obj).Bool(), nil
98+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$global.$jsbuiltin$ = {
2+
typeoffunc: function(x) { return typeof x },
3+
instanceoffunc: function(x,y) { return x instanceof y },
4+
infunc: function(x,y) { return x in y }
5+
}

0 commit comments

Comments
 (0)