blob: 89276ff3ed844e5eb6a690c76a9359d09c1090d3 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build java android
package util
import (
"unsafe"
"v.io/v23/vdl"
"v.io/v23/vom"
)
// #include "jni_wrapper.h"
import "C"
// VomDecodeToValue VOM-decodes the provided value into *vdl.Value using a new
// instance of a VOM decoder.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func VomDecodeToValue(data []byte) (*vdl.Value, error) {
var value *vdl.Value
if err := vom.Decode(data, &value); err != nil {
return nil, err
}
return value, nil
}
// VomCopy copies the provided Go value by encoding/decoding it from VOM.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func VomCopy(src interface{}, dstptr interface{}) error {
data, err := vom.Encode(src)
if err != nil {
return err
}
return vom.Decode(data, dstptr)
}
// JVomEncode VOM-encodes the provided Java object of the given type.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func JVomEncode(jEnv, jObj, jType interface{}) ([]byte, error) {
return CallStaticByteArrayMethod(jEnv, jVomUtilClass, "encode", []Sign{ObjectSign, TypeSign}, jObj, jType)
}
// JVomEncode VOM-encodes the provided Java VdlValue object.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func JVomEncodeValue(jEnv, jVdlValue interface{}) ([]byte, error) {
return CallStaticByteArrayMethod(jEnv, jVomUtilClass, "encode", []Sign{VdlValueSign}, jVdlValue)
}
// JVomDecode VOM-decodes the provided data into a Java object.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func JVomDecode(jEnv interface{}, data []byte, jTypeObj interface{}) (unsafe.Pointer, error) {
jType := getObject(jTypeObj)
if jType == nil {
jType = C.jobject(jObjectClass)
}
return CallStaticObjectMethod(jEnv, jVomUtilClass, "decode", []Sign{ByteArraySign, TypeSign}, ObjectSign, data, jType)
}
// JVomCopy copies the provided Go value into a corresponding Java object by
// encoding/decoding it from VOM.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func JVomCopy(jEnv interface{}, val interface{}, jType interface{}) (unsafe.Pointer, error) {
data, err := vom.Encode(val)
if err != nil {
return nil, err
}
return JVomDecode(jEnv, data, jType)
}
// GoVomCopy copies the provided Java object into a provided Go value pointer by
// encoding/decoding it from VOM.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func GoVomCopy(jEnv, jObj, jClass, dstptr interface{}) error {
data, err := JVomEncode(jEnv, jObj, jClass)
if err != nil {
return err
}
return vom.Decode(data, dstptr)
}
// GoVomCopyValue copies the provided Java VDLValue object into a Go *vdl.Value
// by encoding/decoding it from VOM.
// NOTE: Because CGO creates package-local types and because this method may be
// invoked from a different package, Java types are passed in an empty interface
// and then cast into their package local types.
func GoVomCopyValue(jEnv, jVdlValue interface{}) (*vdl.Value, error) {
data, err := JVomEncodeValue(jEnv, jVdlValue)
if err != nil {
return nil, err
}
return VomDecodeToValue(data)
}