Skip to content

add feature to include raw JS code? #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
flimzy opened this issue Sep 14, 2015 · 3 comments
Closed

add feature to include raw JS code? #309

flimzy opened this issue Sep 14, 2015 · 3 comments

Comments

@flimzy
Copy link
Member

flimzy commented Sep 14, 2015

I'm still thinking about #306 and #274.

I wonder if it would be reasonable to include a feature (exposed as part of the js package I imagine) to include raw JavaScript. I can see why this would be considered evil and ugly and all sorts of bad juju, as it would make syntax and type checking impossible in that code--but that's already true in JavaScript included in the *.inc.js fashion, so I wonder if it's really any worse. It's also evil in that it does magical things with a function with an otherwise normal appearance.

What I am imagining is a new js package method that looks something like the existing MakeFunc, and works something like JS's new Function() constructor. By requiring that any raw JS is encapsulated in a function, at least we maintain a clear barrier between JS and Go, and we don't start to look like PHP.

As a contrived example, we can presently use new Function() like so:

jsAdd := js.Global.Get("Function").New("x","y","return x+y")

And it produces the following JS:

jsAdd = new ($global.Function)($externalize("x", $String), $externalize("y", $String), $externalize("return x+y", $String));

What I would like, is something that instead produces:

jsAdd = function(x,y) { return x+y }

or a functional equivalent using an IIFE or somesuch.

I have a hacked up proof of concept (flimzy@f048c21) which takes the following input:

jsAdd := js.LiteralFunc("function(x,y) { return x+y }")

and produces:

jsAdd = (function() { return function(x,y) { return x+y } })();

Am I smoking crack to go down this road?

@flimzy
Copy link
Member Author

flimzy commented Sep 14, 2015

Perhaps a better alternative would be to copy cgo's comment preamble concept?

// var foo = require('foo')
// function jsAdd(x,y) { return x+y }
import "github.com/gopherjs/gopherjs/js"

func main() {
    foo := js.Global.Get("foo")
    jsAdd := js.Global.Get("jsAdd")
}

@neelance
Copy link
Member

For me, this feels like too much magic. If we fix #306, then JS code can be put into a proper separate file and can be included similar to .s assembly files. Would that be okay for you?

@flimzy
Copy link
Member Author

flimzy commented Sep 14, 2015

If we can solve #306, that would satisfy me for sure. I'll try my hand at that in the next couple of days.

@flimzy flimzy closed this as completed Sep 15, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants