[fidl][go] Part 1/3: Remove Marshal() and Unmarshal()

There have been a number of bugs from not providing a MarshalerContext
and it isn't much more verbose to add one explicitly. Because of this,
it makes sense to remove Marshal() and Unmarshal().

Part 1 does the following:
- Expose NewCtx() so MarshalWithContext() can be called with a default
context.
- Change the signature of UnmarshalWithContext so we can migrate to that
from UnmarshalWithContext2 (for a simpler name)

Change-Id: Ibd2afeb14b5afff9faeae64ae326a8774ba35e19
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/go/+/572981
Reviewed-by: Mitchell Kember <mkember@google.com>
Commit-Queue: Benjamin Prosnitz <bprosnitz@google.com>
diff --git a/api/fuchsia.txt b/api/fuchsia.txt
index 23d5eb0..2b9801e 100644
--- a/api/fuchsia.txt
+++ b/api/fuchsia.txt
@@ -1090,10 +1090,11 @@
 pkg syscall/zx/fidl, func MarshalHeaderThenMessage(*MessageHeader, Message, []uint8, []zx.HandleDisposition) (int, int, error)
 pkg syscall/zx/fidl, func MarshalWithContext(MarshalerContext, Message, []uint8, []zx.HandleDisposition) (int, int, error)
 pkg syscall/zx/fidl, func MustCreateMarshaler(interface{}) Marshaler
+pkg syscall/zx/fidl, func NewCtx() MarshalerContext
 pkg syscall/zx/fidl, func NewInterfaceRequest() (InterfaceRequest, *ChannelProxy, error)
 pkg syscall/zx/fidl, func Unmarshal([]uint8, []zx.HandleInfo, Message) (int, int, error)
 pkg syscall/zx/fidl, func UnmarshalHeaderThenMessage([]uint8, []zx.HandleInfo, *MessageHeader, Message) error
-pkg syscall/zx/fidl, func UnmarshalWithContext(MarshalerContext, []uint8, []zx.Handle, Message) (int, int, error)
+pkg syscall/zx/fidl, func UnmarshalWithContext(MarshalerContext, []uint8, []zx.HandleInfo, Message) (int, int, error)
 pkg syscall/zx/fidl, func UnmarshalWithContext2(MarshalerContext, []uint8, []zx.HandleInfo, Message) (int, int, error)
 pkg syscall/zx/fidl, func WithMarshalerContext(context.Context, MarshalerContext) context.Context
 pkg syscall/zx/fidl, method (*ChannelProxy) Call(uint64, Message, Message) error
diff --git a/src/syscall/zx/fidl/ctx_and_header.go b/src/syscall/zx/fidl/ctx_and_header.go
index bac8a2f..8539c72 100644
--- a/src/syscall/zx/fidl/ctx_and_header.go
+++ b/src/syscall/zx/fidl/ctx_and_header.go
@@ -20,9 +20,9 @@
 	return ctx.UseV2WireFormat
 }
 
-// newCtx returns the default context.
+// NewCtx returns the default context.
 // During migrations, this controls the default write path.
-func newCtx() MarshalerContext {
+func NewCtx() MarshalerContext {
 	return MarshalerContext{UseV2WireFormat: false}
 }
 
diff --git a/src/syscall/zx/fidl/encoding_new.go b/src/syscall/zx/fidl/encoding_new.go
index c6440aa..1684960 100644
--- a/src/syscall/zx/fidl/encoding_new.go
+++ b/src/syscall/zx/fidl/encoding_new.go
@@ -100,7 +100,7 @@
 
 // Marshal marshals (or encodes) a message into the data and handles slices.
 func Marshal(message Message, data []byte, handleDispositions []zx.HandleDisposition) (int, int, error) {
-	return MarshalWithContext(newCtx(), message, data, handleDispositions)
+	return MarshalWithContext(NewCtx(), message, data, handleDispositions)
 }
 
 // Marshal marshals (or encodes) a message into the data and handles slices.
@@ -132,10 +132,9 @@
 // Unmarshal unmarshals (or decodes) into message using the data and handles
 // slices.
 func Unmarshal(data []byte, handleInfos []zx.HandleInfo, message Message) (int, int, error) {
-	return UnmarshalWithContext2(newCtx(), data, handleInfos, message)
+	return UnmarshalWithContext2(NewCtx(), data, handleInfos, message)
 }
 
-// UnmarshalWithContext2 behaves identically to UnmarshalWithContext but takes a HandleInfo.
 func UnmarshalWithContext2(ctx MarshalerContext, data []byte, handleInfos []zx.HandleInfo, message Message) (int, int, error) {
 	// By construction, we know that message is a pointer to a struct since
 	// we only generate pointer receiver methods for top-level messages.
@@ -164,19 +163,7 @@
 	return in.nextObject, len(handleInfos) - len(in.handleInfos), nil
 }
 
-func UnmarshalWithContext(ctx MarshalerContext, data []byte, handles []zx.Handle, message Message) (int, int, error) {
-	resphi := messageHandleInfosPool.Get().([]zx.HandleInfo)
-	defer messageHandleInfosPool.Put(resphi)
-
-	handleInfos := resphi[:len(handles)]
-	for i, handle := range handles {
-		handleInfos[i] = zx.HandleInfo{
-			Handle: handle,
-			Type:   zx.ObjectTypeNone,
-			Rights: zx.RightSameRights,
-		}
-	}
-
+func UnmarshalWithContext(ctx MarshalerContext, data []byte, handleInfos []zx.HandleInfo, message Message) (int, int, error) {
 	return UnmarshalWithContext2(ctx, data, handleInfos, message)
 }
 
diff --git a/src/syscall/zx/fidl/interface.go b/src/syscall/zx/fidl/interface.go
index a8792e4..9093724 100644
--- a/src/syscall/zx/fidl/interface.go
+++ b/src/syscall/zx/fidl/interface.go
@@ -126,7 +126,7 @@
 }
 
 func (p *ChannelProxy) send(txid uint32, ordinal uint64, req Message) error {
-	reqHeader := newCtx().NewHeader()
+	reqHeader := NewCtx().NewHeader()
 
 	reqHeader.Txid = txid
 	reqHeader.Ordinal = ordinal