question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use closures instead of genfun?

See original GitHub issue

Your compile logic currently uses generate-function to build some code, e.g. https://github.com/mafintosh/protocol-buffers/blob/012e982de02db16693ac777816677cbf6f38697e/compile.js#L105.

This is neat (it looks like in some cases you are dynamically including/excluding entire sections of code!) — however it does mean that it won’t work on the current Tessel runtime, where JavaScript code is actually compiled “offline” before it is pushed, and later eval/Function is not currently possible.

I know this is just one platform, and it’s not really your fault that its “JavaScript” implementation is incomplete, but this was kind of a bummer. Protocol buffers seem like a great fit there. (FWIW, you may also run into this restriction in-browser with default Content Security Policy settings in affect.)

Anyway, just thought I’d mention this for future consideration. I know performance is a big focus here, but seems like simply returning closures instead of “generated functions” would bring compatibility (and readability) benefits, and [speculatively] might not be appreciably slower on modern optimizing JS engines.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mafintoshcommented, Dec 12, 2014

We could bundle a command line tool that would allows you to compile the protobuf schema into a javascript file. I.e.

// schema.proto
message Test {
  required string hello = 1
}

Then run

protocol-buffers schema.proto > messages.js

Then in your js code you’ll be able to

var messages = require('./messages')
var buf = messages.Test.encode({hello:'world'})
console.log(buf)
0reactions
vweeverscommented, Apr 28, 2016

A fun thought (just the fact that it’s possible): one could also generate the Javascript code in a browserified Service Worker. Something like:

const protobuf = require('protocol-buffers')
const schema = require('fs').readFileSync(__dirname+'/schema.proto', 'utf8')

self.addEventListener('fetch', function (event) {
  const req = event.request

  if (/\/schema\.js$/.test(req.url)) {
    const js = protobuf.generate(schema)

    event.respondWith(new Response(js, {
      status: 200,
      statusText: 'OK',
      headers: {
        'Content-Type': 'application/javascript',
        'Content-Length': js.length.toString()
      }
    }))
  }
})

With some more code to cache the result and maybe wrap it. Then to use it somewhere in your app, you’d include it like <script src="schema.js"> or with some async module loader. Apart from the missing protobuf.generate() function, this works in Chrome today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Closures vs. Functions | Apple Developer Forums
A closure is similar to a C-style callback, except that instead of needing a separate "context" parameter, it has its own context built...
Read more >
5 Useful Ways to Use Closures in Go - Calhoun.io
With a closure you can instead just require that a function is passed in as your argument, since you really only care about...
Read more >
Closures - JavaScript - MDN Web Docs
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment).
Read more >
Why would a program use a closure?
First of all, there is nothing that is impossible without using closures. You can always replace a closure by an object implementing a...
Read more >
Function VS Closure in Swift - Medium
In swift, we declare function by using “func” keyword. ... You can use the value passed inside the statements of the closure by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found