gomacro icon indicating copy to clipboard operation
gomacro copied to clipboard

gomacro cannot be used with google app engine because it uses the unsafe package

Open cosmos72 opened this issue 6 years ago • 4 comments

as highlighted by @morangorin in #42. He also adds:

would it be possible to have a smaller and simpler version of gomacro ? (perhaps at the cost of performance and/or features ?)

cosmos72 avatar Nov 10 '18 22:11 cosmos72

The unsafe package is used in several places as an optimization - without it, the gomacro/fast interpreter would be either much slower or much more complex. So, while technically possible to remove them, it's a lot of work - more than I am currently willing to put for the goal of running it on google app engine.

There is also a workaround for some missing features of the reflect package that uses unsafe, and the only way to implement them without unsafe is to patch the reflect package from Go standard library (or live with the missing features).

A possible solution is to use the gomacro/classic interpreter: as I mentioned in #42, it's older and much slower. On the plus side, it's much smaller and does not use unsafe. Unluckly, it currently lacks a command-line REPL but, as again I wrote in #42, it's basically a matter of duplicating gomacro/cmd and replacing the calls to fast interpreter with calls to the classic one. If you are willing to work on this, I will happily accept it as a contribution.

cosmos72 avatar Nov 10 '18 23:11 cosmos72

For documentation, what would be the list of missing features if one was not to use the unsafe package in reflect (with the classic interpreter)?

morangorin avatar Nov 12 '18 12:11 morangorin

unsafe is used for the following features:

  1. in the fast interpreter to optimize and simplify access to interpreted variables
  2. in gomacro/base/output/output.go asUnsafeValue to pretty-print function pointers
  3. in gomacro/xreflect/named.go unsafeAddMethod to support redefining existing methods (a current limitation of the go/types package)
  4. in gomacro/xreflect/named.go unsafeRemoveMethods to support removing methods (currently not supported by the go/types package) - used to remove wrapper methods for embedded struct fields when importing packages, as gomacro scavenges by itself for embedded struct fields and their methods, implementing accurate lookup rules.
  5. in gomacro/base/signal.go Signals.IsEmpty to atomically check in a single step whether four uint8 fields of a struct are all zero (only used by fast interpreter)
  6. in gomacro/base/untyped/lit.go to convert from untyped literals to *big.Int, *big.Rat and *big.Float - a Go language extension specific to gomacro (only used by fast interpreter)
  7. in gomacro/fast/selector.go makeAccessible to support accessing exported fields of embedded structs that are not exported (a current limitation of the reflect package)

As you can guess, removing some of these usages is quite trivial (1 2 5 6 in particular if you stick to the classic interpreter), while removing others (3 4 7) has non-trivial side effects

cosmos72 avatar Nov 12 '18 13:11 cosmos72

Thank you very much for documenting this.

morangorin avatar Nov 17 '18 09:11 morangorin