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

Introduce concept of elements as distinct from components #73

Merged
merged 1 commit into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vendor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
./_vendor/src/golang.org/x/crypto 3cb07270c9455e8ad27956a70891c962d121a228 https://go.googlesource.com/crypto
./_vendor/src/golang.org/x/tools 620ecdb8d7943e20dc030b61bfe898d1b000bdea https://go.googlesource.com/tools
./_vendor/src/github.com/gopherjs/gopherjs 9659c814f1d54d63f9c623449a7111d3864c1361 git@github.com:gopherjs/gopherjs
./_vendor/src/github.com/gopherjs/jsbuiltin 67703bfb044e3192fbcab025c3aeaeedafad1f2f git@github.com:gopherjs/jsbuiltin
./_vendor/src/github.com/kisielk/gotool 0de1eaf82fa3f583ce21fde859f1e7e0c5e9b220 git@github.com:kisielk/gotool
./_vendor/src/github.com/fsnotify/fsnotify 7d7316ed6e1ed2de075aab8dfc76de5d158d66e1 git@github.com:fsnotify/fsnotify
./_vendor/src/github.com/spf13/cobra 16c014f1a19d865b765b420e74508f80eb831ada git@github.com:spf13/cobra
Expand Down
12 changes: 12 additions & 0 deletions _vendor/src/github.com/gopherjs/jsbuiltin/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: go
go:
- 1.8.x
addons:
apt:
packages:
- nodejs
install:
- go get -u github.com/gopherjs/gopherjs
script:
- diff -u <(echo -n) <(gofmt -d ./)
- gopherjs test
24 changes: 24 additions & 0 deletions _vendor/src/github.com/gopherjs/jsbuiltin/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Copyright (c) 2015 Richard Musiol. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103 changes: 103 additions & 0 deletions _vendor/src/github.com/gopherjs/jsbuiltin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
[![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)

jsbuiltin - Built-in JavaScript functions for GopherJS
------------------------------------------------------

JavaScript has a small number of [built-in
functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects)
to handle some common day-to-day tasks. This package providers wrappers around
some of these functions for use in GopherJS.

It is worth noting that in many cases, using Go's equivalent functionality
(such as that found in the [net/url](https://golang.org/pkg/net/url/) package)
may be preferable to using this package, and will be a necessity any time you
wish to share functionality between front-end and back-end code.

### What is supported?
Not all JavaScript built-in functions make sense or are useful in a Go
environment. The table below shows each of the JavaScript built-in functions,
and its current state in this package.

| Name | Supported | Comment |
|----------------------|-----------|-----------------------------|
| eval() | -- | |
| uneval() | -- | |
| isFinite() | yes | |
| isNaN() | yes | |
| parseFloat() | TODO? | See note below |
| parseInt() | TODO? | See note below |
| decodeURI() | yes | |
| decodeURIComponent() | yes | |
| encodeURI() | yes | |
| encodeURIComponent() | yes | |
| escape() | -- | deprecated circa 2000 |
| Number() | -- | See note below |
| String() | -- | Use js.Object.String() |
| unescape() | -- | deprecated circa 2000 |
| typeof operator | yes | |
| instanceof operator | yes | |

#### Notes on unmplemented functions

* **eval()**: Is there ever a need to eval JS code from within Go?
* **Number()**: This requires handling a bunch of corner cases which don't
normally exist in a strictly typed language such as Go. It seems that anyone
with a legitimate need for this function probably needs to write their own
wrapper to handle the cases that matter to them.
* **parseInt()** and **parseFloat()**: These could be added, but doing so
will require answering some questions about the interfce. JavaScript has
effectively two relevant data types (int and float) where Go has has 12.
Deciding how to map JS's `parseInt()` to Go's `(u?)int(8|16|32|64)` types,
and JS's `parseFloat()` Go's `float(32|64)` or `complex(64|128)` needs to
be considered, as well as how to handle error cases (Go doesn't have a `NaN`
type, so any `NaN` result probably needs to be converted to a proper Go
error). If this matters to you, comments and/or PRs are welcome.

### Installation and Usage
Get or update this package and dependencies with:

```
go get -u -d -tags=js github.com/gopherjs/jsbuiltin
```

### Basic usage example

This is a modified version of the Pet example in the main GopherJS documentation,
to accept and return URI-encoded pet names using the jsbuiltin package.

```go
package main

import (
"github.com/gopherjs/gopherjs/js"
"github.com/gopherjs/jsbuiltin"
)

func main() {
js.Global.Set("pet", map[string]interface{}{
"New": New,
})
}

type Pet struct {
name string
}

func New(name string) *js.Object {
return js.MakeWrapper(&Pet{name})
}

func (p *Pet) Name() string {
return jsbuiltin.EncodeURIComponent(p.name)
}

func (p *Pet) SetName(uriComponent string) error {
name, err := jsbuiltin.DecodeURIComponent(uriComponent)
if err != nil {
// Malformed UTF8 in uriComponent
return err
}
p.name = name
return nil
}
```
98 changes: 98 additions & 0 deletions _vendor/src/github.com/gopherjs/jsbuiltin/jsbuiltin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Package jsbuiltin provides minimal wrappers around some JavasScript
// built-in functions.
package jsbuiltin

import (
"errors"

"github.com/gopherjs/gopherjs/js"
)

// DecodeURI decodes a Uniform Resource Identifier (URI) previously created
// by EncodeURI() or by a similar routine. If the underlying JavaScript
// function throws an error, it is returned as an error.
func DecodeURI(uri string) (raw string, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(*js.Error)
}
}()
raw = js.Global.Call("decodeURI", uri).String()
return
}

// EncodeURI encodes a Uniform Resource Identifier (URI) by replacing each
// instance of certain characters by one, two, three, or four escape sequences
// representing the UTF-8 encoding of the character (will only be four escape
// sequences for characters composed of two "surrogate" characters).
func EncodeURI(uri string) string {
return js.Global.Call("encodeURI", uri).String()
}

// EncodeURIComponent encodes a Uniform Resource Identifier (URI) component
// by replacing each instance of certain characters by one, two, three, or
// four escape sequences representing the UTF-8 encoding of the character
// (will only be four escape sequences for characters composed of two
// "surrogate" characters).
func EncodeURIComponent(uri string) string {
return js.Global.Call("encodeURIComponent", uri).String()
}

// DecodeURIComponent decodes a Uniform Resource Identifier (URI) component
// previously created by EncodeURIComponent() or by a similar routine. If the
// underlying JavaScript function throws an error, it is returned as an error.
func DecodeURIComponent(uri string) (raw string, err error) {
defer func() {
if r := recover(); r != nil {
err = r.(*js.Error)
}
}()
raw = js.Global.Call("decodeURIComponent", uri).String()
return
}

// IsFinite determines whether the passed value is a finite number, and returns
// true if it is. If needed, the parameter is first converted to a number.
func IsFinite(value interface{}) bool {
return js.Global.Call("isFinite", value).Bool()
}

// IsNaN determines whether a value is NaN (Not-a-Number) or not. A return
// value of true indicates the input value is considered NaN by JavaScript.
func IsNaN(value interface{}) bool {
return js.Global.Call("isNaN", value).Bool()
}

// Type constants represent the JavaScript builtin types, which may be returned
// by TypeOf().
const (
TypeUndefined = "undefined"
TypeNull = "null"
TypeObject = "object"
TypeBoolean = "boolean"
TypeNumber = "number"
TypeString = "string"
TypeFunction = "function"
TypeSymbol = "symbol"
)

// TypeOf returns the JavaScript type of the passed value
func TypeOf(value interface{}) string {
return js.Global.Get("$jsbuiltin$").Call("typeoffunc", value).String()
}

// InstanceOf returns true if value is an instance of object according to the
// built-in 'instanceof' operator. `object` must be a *js.Object representing
// a javascript constructor function.
func InstanceOf(value interface{}, object *js.Object) bool {
return js.Global.Get("$jsbuiltin$").Call("instanceoffunc", value, object).Bool()
}

// In returns true if key is a member of obj. An error is returned if obj is not
// a JavaScript object.
func In(key string, obj *js.Object) (ok bool, err error) {
if obj == nil || obj == js.Undefined || TypeOf(obj) != TypeObject {
return false, errors.New("obj not a JavaScript object")
}
return js.Global.Get("$jsbuiltin$").Call("infunc", key, obj).Bool(), nil
}
5 changes: 5 additions & 0 deletions _vendor/src/github.com/gopherjs/jsbuiltin/jsbuiltin.inc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$global.$jsbuiltin$ = {
typeoffunc: function(x) { return typeof x },
instanceoffunc: function(x,y) { return x instanceof y },
infunc: function(x,y) { return x in y }
}
Loading