Code Monkey home page Code Monkey logo

go2js's People

Contributors

tredoe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

go2js's Issues

Generate CommonJS / AMD compatible .js files

Not a high priority for me, but probably something to consider. It looks like it would be pretty easy to support as it just impacts the wrapper code around the generated .js file.

mustache.js has a good example:

https://github.com/janl/mustache.js/blob/master/mustache.js

(function (root, factory) {
  if (typeof exports === "object" && exports) {
    module.exports = factory; // CommonJS
  } else if (typeof define === "function" && define.amd) {
    define(factory); // AMD
  } else {
    root.Mustache = factory; // <script>
  }
}(this, (function () {

   // body code here

  var exports = {};
  // bind all exported identifiers -- similar to current output

  return exports;

}())));

This would work with require.js, CommonJS (e.g. node.js), and plain <script> tags.

var has different scope in javascript

Example:

func test() {
    a := 5
    fmt.Println(a)
    for i := 0; i < 1; i += 1 {
        a := 6
        fmt.Println(a)
    }
    fmt.Println(a)
}

Go and js have different results here.

Are you aware of GopherJS?

From README,

Status

Inactive | Incomplete

Sorry to not be able to continue with this project for some time, but I have to
work very hard in third projects.

Although I follow being interested in be able to use Go in the browser.

Since you say you're interested in being able to use Go in the browser, I wanted to ask...

I wanted to ask, are you aware of GopherJS project? It has gotten pretty far, including support for goroutines, channels, and select recently.

https://github.com/gopherjs/gopherjs
https://groups.google.com/forum/#!forum/gopherjs

Emit raw JS code from .go (for wrapping existing JS libs)

Thanks for this project. Quick question: is there a way to tell go2js to emit some raw JS code from within a .go file?

If not, how do you recommend writing wrappers for existing JS libraries? For example, I'd like to wrap a library like jQuery and encapsulate the Go->JS bindings in a single place. Then I can call the wrapped Go version in a type safe manner.

Thoughts?

Channels and gorountines in js

I also thought about go to js translator, pretty awesome idea.

What about this kind of channels/gorountines implementation:

var Go = (function () {
  var result = {package: {}};

    /* chan */

    var chan = function () {
        this.getCallbackStack = [];
        this.putCallbackStack = [];
    };

    chan.prototype.put = function (value, callback) {
        if (this.getCallbackStack.length == 0) {
            this.putCallbackStack.push({value: value, callback: callback});
        } else {
            var getCallback = this.getCallbackStack.pop().callback;
            if (getCallback) {
                getCallback(value);
            }
            if (callback) {
                callback();
            }
        }
    };

    chan.prototype.get = function (callback) {
        if (this.putCallbackStack.length == 0) {
            this.getCallbackStack.push({callback: callback});
        } else {
            var elt = this.putCallbackStack.pop();
            var putCallback = elt.callback, value = elt.value;
            if (callback) {
                callback(value);
            }
            if (putCallback) {
                putCallback();
            }
        }
    };

    result.chan = chan;

    /* goroutine */

    result.go = function (callback, arguments) {
        arguments = arguments || [];
        setTimeout(function () {
            callback.apply(null, arguments);
        }, 0);
    };

    result.println = function (value) {
        console.log(value);
    };

    return result;
})();

Go.package["time"] = {

    Second: 1000,

    Sleep: function (duration, callback) {
        setTimeout(callback, duration)
    }
};

And example of use:

// package main
//
// import "time"
//
// func main() {
//   ch := make(chan bool)
//  go func() {
//      println("Locked")
//      <-ch
//      println("Unlocked")
//      ch <- true
//  }()
//  time.Sleep(time.Second*2)
//  ch <- true
//  <- ch
// }

(function () {
    var time = Go.package["time"];
    var ch = new Go.chan();
    Go.go(function () {
        Go.println("Locked");
        ch.get(function (value) {
            Go.println("Unlocked");
            ch.put(true);
        });
    });
    time.Sleep(time.Second*2, function () {
        ch.put(true, function () {
            ch.get();
        });
    });
})();

This in this code channels have capacity equal to 1, but can be easily tuned to support buffered channels.

Suggestion: add local aliasing for this

This test

type test struct {
    value string
}

func (t *test) get() string {
    return t.value
}

Currently translates to:

function test(value) {
        this.value=value
}

test.prototype.get = function() {
        return this.value;
}

It would be probably better to translate into:

function test(value) {
        this.value=value
}

test.prototype.get = function() {
        var t = this;
        return t.value;
}

If you are at some point doing anon. functions then handling of this can get quite complicated otherwise.

Crashes at translating a type which has not been yet defined

This code crashes the translator:

type let struct {
    this *this
}

type this struct {
    this []*this
    let
}

func (this *this) apply(function this, call let) {
    this.this = append(this.this, &function)
    this.this = append(this.this, call.this)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.